From 192b37e4c00071e47e614eda2323bd8fac19842c Mon Sep 17 00:00:00 2001 From: mestizoLopez Date: Wed, 15 Jan 2020 12:53:25 -0600 Subject: [PATCH] Fixing and finishing unit tests (#97) * Fixing and finishing unit tests * Fixing styling Issues and adding new test --- .../java/io/dapr/client/DaprHttpTest.java | 113 ++++++++++++++---- 1 file changed, 90 insertions(+), 23 deletions(-) diff --git a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java index 95957218a..041c08a11 100644 --- a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java @@ -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 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 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 mono = daprHttp.invokeAPI("POST","v1.0/state",null, null); + Mono 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 mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, null); + Mono 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 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 headers = new HashMap<>(); - headers.put("header","value"); - headers.put("header1","value1"); + headers.put("header", "value"); + headers.put("header1", "value1"); + + Map 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 mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null); + Mono 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 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 mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null); + Mono 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); + } -} \ No newline at end of file + @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 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 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); + + } + +}