Fixing and finishing unit tests (#97)

* Fixing and finishing unit tests

* Fixing styling Issues and adding new test
This commit is contained in:
mestizoLopez 2020-01-15 12:53:25 -06:00 committed by Artur Souza
parent a0250fd002
commit 192b37e4c0
1 changed files with 90 additions and 23 deletions

View File

@ -32,7 +32,26 @@ public class DaprHttpTest {
@Before
public void setUp() throws Exception {
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}
@Test
public void invokeMethod() throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "text/html");
headers.put("header1", "value1");
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, (byte[])null, headers);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);
}
@Test
@ -40,14 +59,15 @@ public class DaprHttpTest {
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
.respond(EXPECTED_RESULT)
.addHeader("Header","Value");
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();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}
@ -60,10 +80,10 @@ public class DaprHttpTest {
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, (String) null,null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}
@ -79,7 +99,7 @@ public class DaprHttpTest {
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}
@ -87,39 +107,86 @@ public class DaprHttpTest {
public void invokeMethodWithHeaders() throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
headers.put("header", "value");
headers.put("header1", "value1");
Map<String, String> urlParameters = new HashMap<>();
urlParameters.put("orderId", "41");
mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get?header1=value1&header=value")
.get("http://localhost:3500/v1.0/state/order?orderId=41")
.respond(EXPECTED_RESULT);
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/state/order", urlParameters, headers);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}
@Test(expected = RuntimeException.class)
public void invokeMethodRuntimeException() throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
public void invokePostMethodRuntime() throws IOException {
mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"500\",\"message\":\"Error\"}"));
.post("http://localhost:3500/v1.0/state")
.respond(500);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}
}
@Test(expected = RuntimeException.class)
public void invokePostDaprError() throws IOException {
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("text"),
"{\"errorCode\":null,\"message\":null}"));
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);
}
@Test(expected = RuntimeException.class)
public void invokePostMethodUnknownError() throws IOException {
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
DaprHttp daprHttp = new DaprHttp(3500,okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);
}
@Test
public void getHeadersAndStatus(){
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
System.out.println(daprHttp);
}
}