Skip to content

Commit 2e419de

Browse files
author
jakemuntarsi
committed
Merge remote-tracking branch 'origin/DEV-57' into Dev-73_JavaTestClientFailure
2 parents 9571cd8 + 676c7ea commit 2e419de

4 files changed

Lines changed: 82 additions & 9 deletions

File tree

build.sbt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ version := "1.0.16"
88
autoScalaLibrary := false
99

1010
// More compiler warnings
11-
scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature", "-Xlint", "-target:jvm-1.11")
11+
scalacOptions ++= Seq("-deprecation", "-unchecked", "-source 8", "-feature", "-Xlint", "-target:jvm-1.11")
1212

1313
libraryDependencies ++= Seq(
1414
"com.google.code.gson" % "gson" % "2.8.6",
1515
"org.apache.httpcomponents" % "httpasyncclient" % "4.1.4",
1616
"commons-io" % "commons-io" % "2.8.0",
1717
"com.novocode" % "junit-interface" % "0.11" % "test->default",
1818
"org.junit.jupiter" % "junit-jupiter-api" % "5.7.0" % "test",
19-
"org.projectlombok" % "lombok" % "1.18.2"
19+
"org.projectlombok" % "lombok" % "1.18.2",
20+
"org.mockito" % "mockito-core" % "3.6.0" % Test
2021
)
2122

2223
// Disable using the Scala version in published artifacts

src/main/java/com/algorithmia/algo/AlgorithmiaClient.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import com.algorithmia.data.DataDirectory;
66
import com.algorithmia.data.DataFile;
77
import com.google.gson.Gson;
8-
import com.google.gson.reflect.TypeToken;
98
import org.apache.http.HttpResponse;
109
import org.apache.http.entity.ContentType;
1110
import org.apache.http.entity.StringEntity;
1211
import org.apache.http.util.EntityUtils;
1312

1413
import java.io.IOException;
14+
import java.util.ArrayList;
1515
import java.util.HashMap;
16+
import java.util.List;
1617
import java.util.Map;
1718

1819
/**
@@ -30,6 +31,11 @@ protected AlgorithmiaClient(Auth auth, String apiAddress, int maxConnections) {
3031
this.client = new HttpClient(auth, apiAddress, maxConnections);
3132
}
3233

34+
//For testing
35+
public AlgorithmiaClient(HttpClient client) {
36+
this.client = client;
37+
}
38+
3339
/**
3440
* Initialize an Algorithm object from this client
3541
* @param algoUri the algorithm's URI, e.g., algo://user/algoname
@@ -288,6 +294,19 @@ public DataFile file(String path) {
288294
return new DataFile(client, path);
289295
}
290296

297+
public AlgorithmiaInsights reportInsights(String input) throws IOException {
298+
Gson gson = new Gson();
299+
Map<String, String> map = gson.fromJson(input, new TypeToken<Map<String, String>>().getType());
300+
List<String> insightPayload = new ArrayList<>();
301+
map.forEach((key, value) -> {
302+
String item = String.format("{\"insight_key\": \"%s\", \"insight_value\": \"%s\"}", key, value);
303+
insightPayload.add(item);
304+
});
305+
HttpResponse response = this.client.post("/v1/insights", new StringEntity(insightPayload.toString(), ContentType.APPLICATION_JSON));
306+
String responseString = EntityUtils.toString(response.getEntity());
307+
return gson.fromJson(responseString, AlgorithmiaInsights.class);
308+
}
309+
291310
public void close() throws IOException {
292311
this.client.close();
293312
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.algorithmia.algo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
@Setter
13+
@Getter
14+
public class AlgorithmiaInsights {
15+
private String response;
16+
}

src/test/java/com/algorithmia/algo/AlgorithmTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
package com.algorithmia.algo;
22

3+
import com.algorithmia.client.HttpClient;
34
import com.google.gson.Gson;
45
import com.google.gson.JsonElement;
56

67
import org.apache.commons.codec.binary.Base64;
78
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;
814
import org.junit.Before;
915
import org.junit.Test;
1016
import org.junit.Assume;
1117
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;
1223

24+
import java.io.ByteArrayInputStream;
1325
import java.util.HashMap;
1426
import java.util.Map;
1527
import java.util.concurrent.*;
1628

29+
import static org.mockito.ArgumentMatchers.any;
30+
import static org.mockito.ArgumentMatchers.anyString;
31+
import static org.mockito.ArgumentMatchers.eq;
32+
1733
public class AlgorithmTest {
1834

1935
private String defaultKey;
20-
private String adminKey;
21-
private String testAddress;
36+
@Mock
37+
private HttpClient httpClient;
2238

2339
@Before
2440
public void setup() {
2541
defaultKey = System.getenv("ALGORITHMIA_DEFAULT_API_KEY");
26-
adminKey = System.getenv("ALGORITHMIA_ADMIN_API_KEY");
27-
testAddress = System.getenv("ALGORITHMIA_API_TEST_ADDRESS");
2842
Assume.assumeNotNull(defaultKey);
29-
Assume.assumeNotNull(adminKey);
30-
Assume.assumeNotNull(testAddress);
43+
MockitoAnnotations.openMocks(this);
3144
}
3245

3346
@Test
@@ -247,6 +260,30 @@ public void algoDeleteAlgo() throws Exception {
247260
Assert.assertEquals(204, response.getStatusLine().getStatusCode());
248261
}
249262

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+
250287
private Algorithm createTestAlgo() {
251288
String name = "CreateAlgoTest" + System.currentTimeMillis();
252289
Algorithm.Details details = new Algorithm.Details();

0 commit comments

Comments
 (0)