Fixing DaprHttp and its tests. (#93)

This commit is contained in:
Artur Souza 2020-01-13 16:40:34 -08:00 committed by GitHub
parent 880cd92515
commit 80ed8bf0c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 15 deletions

View File

@ -84,7 +84,7 @@ public class DaprClientBuilder {
synchronized (DaprClientBuilder.class) { synchronized (DaprClientBuilder.class) {
if (this.daprHttClient == null) { if (this.daprHttClient == null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
DaprHttp daprHtt = new DaprHttp(Constants.DEFAULT_BASE_HTTP_URL, port, okHttpClient); DaprHttp daprHtt = new DaprHttp(port, okHttpClient);
this.daprHttClient = new DaprClientHttpAdapter(daprHtt); this.daprHttClient = new DaprClientHttpAdapter(daprHtt);
} }

View File

@ -74,9 +74,9 @@ class DaprHttp {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/** /**
* The base url used for form urls. This is typically "http://localhost:3500". * Port used to communicate to Dapr's HTTP endpoint.
*/ */
private final String baseUrl; private final int port;
/** /**
* Http client used for all API calls. * Http client used for all API calls.
@ -91,12 +91,11 @@ class DaprHttp {
/** /**
* Creates a new instance of {@link DaprHttp}. * Creates a new instance of {@link DaprHttp}.
* *
* @param baseUrl Base url calling Dapr (e.g. http://localhost)
* @param port Port for calling Dapr. (e.g. 3500) * @param port Port for calling Dapr. (e.g. 3500)
* @param httpClient RestClient used for all API calls in this new instance. * @param httpClient RestClient used for all API calls in this new instance.
*/ */
DaprHttp(String baseUrl, int port, OkHttpClient httpClient) { DaprHttp(int port, OkHttpClient httpClient) {
this.baseUrl = String.format("%s:%d/", baseUrl, port); this.port = port;
this.httpClient = httpClient; this.httpClient = httpClient;
this.pool = Executors.newWorkStealingPool(); this.pool = Executors.newWorkStealingPool();
} }
@ -148,7 +147,7 @@ class DaprHttp {
body = RequestBody.Companion.create(content, mediaType); body = RequestBody.Companion.create(content, mediaType);
} }
HttpUrl.Builder urlBuilder = new HttpUrl.Builder(); HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
urlBuilder.host(this.baseUrl).addPathSegment(urlString); urlBuilder.scheme("http").host(Constants.DEFAULT_HOSTNAME).port(this.port).addPathSegments(urlString);
Optional.ofNullable(urlParameters).orElse(Collections.emptyMap()).entrySet().stream() Optional.ofNullable(urlParameters).orElse(Collections.emptyMap()).entrySet().stream()
.forEach(urlParameter -> urlBuilder.addQueryParameter(urlParameter.getKey(), urlParameter.getValue())); .forEach(urlParameter -> urlBuilder.addQueryParameter(urlParameter.getKey(), urlParameter.getValue()));

View File

@ -24,7 +24,7 @@ public class DaprHttpStub extends DaprHttp {
* Instantiates a stub for DaprHttp * Instantiates a stub for DaprHttp
*/ */
public DaprHttpStub() { public DaprHttpStub() {
super("http://localhost", 3000, null); super(3000, null);
} }
/** /**

View File

@ -42,7 +42,7 @@ public class DaprHttpTest {
.post("http://localhost:3500/v1.0/state") .post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT); .respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient); DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null); Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block(); DaprHttp.Response response = mono.block();
@ -58,7 +58,7 @@ public class DaprHttpTest {
.delete("http://localhost:3500/v1.0/state") .delete("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT); .respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient); DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, null); Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, null);
DaprHttp.Response response = mono.block(); DaprHttp.Response response = mono.block();
@ -74,7 +74,7 @@ public class DaprHttpTest {
.get("http://localhost:3500/v1.0/get") .get("http://localhost:3500/v1.0/get")
.respond(EXPECTED_RESULT); .respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient); DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",null, null); Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",null, null);
DaprHttp.Response response = mono.block(); DaprHttp.Response response = mono.block();
@ -91,11 +91,11 @@ public class DaprHttpTest {
headers.put("header1","value1"); headers.put("header1","value1");
mockInterceptor.addRule() mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get") .get("http://localhost:3500/v1.0/get?header1=value1&header=value")
.respond(EXPECTED_RESULT); .respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient); DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",headers, null); Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null);
DaprHttp.Response response = mono.block(); DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class); String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body); assertEquals(EXPECTED_RESULT,body);
@ -114,7 +114,7 @@ public class DaprHttpTest {
.respond(500, ResponseBody.create(MediaType.parse("application/json"), .respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"500\",\"message\":\"Error\"}")); "{\"errorCode\":\"500\",\"message\":\"Error\"}"));
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient); DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null); Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null);
DaprHttp.Response response = mono.block(); DaprHttp.Response response = mono.block();