|
1 | 1 | package com.algorithmia.algo; |
2 | 2 |
|
| 3 | +import com.algorithmia.client.HttpClient; |
3 | 4 | import com.google.gson.Gson; |
4 | 5 | import com.google.gson.JsonElement; |
5 | 6 |
|
6 | 7 | import org.apache.commons.codec.binary.Base64; |
7 | 8 | import org.apache.http.HttpResponse; |
| 9 | +import org.apache.http.ProtocolVersion; |
| 10 | +import org.apache.http.entity.BasicHttpEntity; |
| 11 | +import org.apache.http.entity.StringEntity; |
| 12 | +import org.apache.http.message.BasicHttpResponse; |
| 13 | +import org.apache.http.message.BasicStatusLine; |
8 | 14 | import org.junit.Before; |
9 | 15 | import org.junit.Test; |
10 | 16 | import org.junit.Assume; |
11 | 17 | import org.junit.Assert; |
| 18 | +import org.mockito.ArgumentCaptor; |
| 19 | +import org.mockito.ArgumentMatchers; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.mockito.MockitoAnnotations; |
12 | 23 |
|
| 24 | +import java.io.ByteArrayInputStream; |
13 | 25 | import java.util.HashMap; |
14 | 26 | import java.util.Map; |
15 | 27 | import java.util.concurrent.*; |
16 | 28 |
|
| 29 | +import static org.mockito.ArgumentMatchers.any; |
| 30 | +import static org.mockito.ArgumentMatchers.anyString; |
| 31 | +import static org.mockito.ArgumentMatchers.eq; |
| 32 | + |
17 | 33 | public class AlgorithmTest { |
18 | 34 |
|
19 | 35 | private String defaultKey; |
20 | | - private String adminKey; |
21 | | - private String testAddress; |
| 36 | + @Mock |
| 37 | + private HttpClient httpClient; |
22 | 38 |
|
23 | 39 | @Before |
24 | 40 | public void setup() { |
25 | 41 | defaultKey = System.getenv("ALGORITHMIA_DEFAULT_API_KEY"); |
26 | | - adminKey = System.getenv("ALGORITHMIA_ADMIN_API_KEY"); |
27 | | - testAddress = System.getenv("ALGORITHMIA_API_TEST_ADDRESS"); |
28 | 42 | Assume.assumeNotNull(defaultKey); |
29 | | - Assume.assumeNotNull(adminKey); |
30 | | - Assume.assumeNotNull(testAddress); |
| 43 | + MockitoAnnotations.openMocks(this); |
31 | 44 | } |
32 | 45 |
|
33 | 46 | @Test |
@@ -247,6 +260,30 @@ public void algoDeleteAlgo() throws Exception { |
247 | 260 | Assert.assertEquals(204, response.getStatusLine().getStatusCode()); |
248 | 261 | } |
249 | 262 |
|
| 263 | + @Test |
| 264 | + public void algoReportInsights() throws Exception { |
| 265 | + HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("", 5, 5), 200, "")); |
| 266 | + BasicHttpEntity httpEntity = new BasicHttpEntity(); |
| 267 | + httpEntity.setContent(new ByteArrayInputStream("{\"response\": \"hello\"}".getBytes())); |
| 268 | + response.setEntity(httpEntity); |
| 269 | + |
| 270 | + Mockito.when(httpClient.post(anyString(), any())).thenReturn(response); |
| 271 | + |
| 272 | + AlgorithmiaClient algorithmiaClient = new AlgorithmiaClient(httpClient); |
| 273 | + |
| 274 | + AlgorithmiaInsights algorithmiaInsights = algorithmiaClient.reportInsights("{\"cats_in_image\": \"4\", \"dogs_in_image\": \"7\"}"); |
| 275 | + |
| 276 | + ArgumentCaptor<StringEntity> captor = ArgumentCaptor.forClass(StringEntity.class); |
| 277 | + |
| 278 | + Mockito.verify(httpClient).post(eq("/v1/insights"), captor.capture()); |
| 279 | + |
| 280 | + StringEntity capturedValue = captor.getValue(); |
| 281 | + |
| 282 | + String expectedString = "[{\"insight_key\": \"cats_in_image\", \"insight_value\": \"4\"}, {\"insight_key\": \"dogs_in_image\", \"insight_value\": \"7\"}]"; |
| 283 | + Assert.assertEquals(expectedString, new String(capturedValue.getContent().readAllBytes())); |
| 284 | + Assert.assertEquals("hello", algorithmiaInsights.getResponse()); |
| 285 | + } |
| 286 | + |
250 | 287 | private Algorithm createTestAlgo() { |
251 | 288 | String name = "CreateAlgoTest" + System.currentTimeMillis(); |
252 | 289 | Algorithm.Details details = new Algorithm.Details(); |
|
0 commit comments