diff --git a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java index 95957218ab..041c08a11e 100644 --- a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java @@ -32,22 +32,42 @@ 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 invokePostMethod() throws IOException { + 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); - 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); + } + + @Test + public void invokePostMethod() throws IOException { + + mockInterceptor.addRule() + .post("http://localhost:3500/v1.0/state") + .respond(EXPECTED_RESULT) + .addHeader("Header","Value"); + + 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); } @@ -60,10 +80,10 @@ public void invokeDeleteMethod() throws IOException { 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 void invokeGetMethod() throws IOException { 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 void invokeGetMethod() throws IOException { 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 { + public void invokePostMethodRuntime() throws IOException { - Map headers = new HashMap<>(); - headers.put("header","value"); - headers.put("header1","value1"); + mockInterceptor.addRule() + .post("http://localhost:3500/v1.0/state") + .respond(500); + + 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 invokePostDaprError() 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, ResponseBody.create(MediaType.parse("text"), + "{\"errorCode\":null,\"message\":null}")); 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); + + } + + @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); + } -} \ No newline at end of file +}