From bcf653bc5e97f9b6e317c7b9a3c13b2db15fa40d Mon Sep 17 00:00:00 2001 From: mestizoLopez Date: Fri, 17 Jan 2020 11:27:06 -0600 Subject: [PATCH] Creating unit tests for HttpAdapter + Adding junit 5 dependency + Reformating code (#112) Co-authored-by: Artur Souza --- sdk/pom.xml | 6 + .../io/dapr/client/DaprClientHttpAdapter.java | 10 +- .../client/DaprClientHttpAdapterTest.java | 462 ++++++++++++++++++ .../java/io/dapr/client/DaprHttpTest.java | 382 +++++++-------- 4 files changed, 643 insertions(+), 217 deletions(-) create mode 100644 sdk/src/test/java/io/dapr/client/DaprClientHttpAdapterTest.java diff --git a/sdk/pom.xml b/sdk/pom.xml index fe0f3cf95..3e7a595f3 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -86,6 +86,12 @@ 1.19.0 test + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + diff --git a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java index 6a5b2840f..e17664e77 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java @@ -331,19 +331,19 @@ public class DaprClientHttpAdapter implements DaprClient { /** * Builds a StateKeyValue object based on the Response - * @param resonse The response of the HTTP Call + * @param response The response of the HTTP Call * @param requestedKey The Key Requested. * @param clazz The Class of the Value of the state * @param The Type of the Value of the state * @return A StateKeyValue instance * @throws IOException If there's a issue deserialzing the response. */ - private StateKeyValue buildStateKeyValue(DaprHttp.Response resonse, String requestedKey, StateOptions stateOptions, Class clazz) throws IOException { - T value = objectSerializer.deserialize(resonse.getBody(), clazz); + private StateKeyValue buildStateKeyValue(DaprHttp.Response response, String requestedKey, StateOptions stateOptions, Class clazz) throws IOException { + T value = objectSerializer.deserialize(response.getBody(), clazz); String key = requestedKey; String etag = null; - if (resonse.getHeaders() != null && resonse.getHeaders().containsKey("Etag")) { - etag = objectSerializer.deserialize(resonse.getHeaders().get("Etag"), String.class); + if (response.getHeaders() != null && response.getHeaders().containsKey("Etag")) { + etag = objectSerializer.deserialize(response.getHeaders().get("Etag"), String.class); } return new StateKeyValue<>(value, key, etag, stateOptions); } diff --git a/sdk/src/test/java/io/dapr/client/DaprClientHttpAdapterTest.java b/sdk/src/test/java/io/dapr/client/DaprClientHttpAdapterTest.java new file mode 100644 index 000000000..147671192 --- /dev/null +++ b/sdk/src/test/java/io/dapr/client/DaprClientHttpAdapterTest.java @@ -0,0 +1,462 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ +package io.dapr.client; + +import io.dapr.client.domain.StateKeyValue; +import io.dapr.client.domain.StateOptions; +import io.dapr.client.domain.Verb; +import io.dapr.utils.ObjectSerializer; +import okhttp3.OkHttpClient; +import okhttp3.mock.Behavior; +import okhttp3.mock.MockInterceptor; +import org.junit.Before; +import org.junit.Test; +import reactor.core.publisher.Mono; + +import java.io.IOException; +import java.util.*; + +import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; + +public class DaprClientHttpAdapterTest { + + private DaprClientHttpAdapter daprClientHttpAdapter; + + private DaprHttp daprHttp; + + private OkHttpClient okHttpClient; + + private MockInterceptor mockInterceptor; + + private ObjectSerializer serializer = new ObjectSerializer(); + + private final String EXPECTED_RESULT = "{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}"; + + + @Before + public void setUp() throws Exception { + mockInterceptor = new MockInterceptor(Behavior.UNORDERED); + okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build(); + } + + @Test + public void publishEventInvokation() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + DaprClientHttpAdapter daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.publishEvent("A", event, null); + assertNull(mono.block()); + } + + @Test + public void publishEvent() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.publishEvent("A", event); + assertNull(mono.block()); + } + + @Test(expected = IllegalArgumentException.class) + public void publishEventIfTopicIsNull() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.publishEvent("", event); + assertNull(mono.block()); + } + + @Test(expected = IllegalArgumentException.class) + public void invokeServiceVerbNull() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(null, "", "", null, null, null); + assertNull(mono.block()); + } + + @Test + public void invokeServiceIllegalArgumentException() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.invokeService(null, "", "", null, null, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.invokeService(Verb.POST, null, "", null, null, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.invokeService(Verb.POST, "", "", null, null, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.invokeService(Verb.POST, "1", null, null, null, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.invokeService(Verb.POST, "1", "", null, null, null).block(); + }); + } + + + @Test(expected = IllegalArgumentException.class) + public void invokeServiceMethodNull() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/publish/A") + .respond(EXPECTED_RESULT); + String event = "{ \"message\": \"This is a test\" }"; + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.POST, "1", "", null, null, null); + assertNull(mono.block()); + } + + @Test + public void invokeService() { + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/invoke/41/method/neworder") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", null, null, String.class); + assertEquals(mono.block(), EXPECTED_RESULT); + } + + @Test + public void simpleInvokeService() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/invoke/41/method/neworder") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", null, String.class); + assertEquals(mono.block(), EXPECTED_RESULT); + } + + @Test + public void invokeServiceWithMaps() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/invoke/41/method/neworder") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", (byte[]) null, map); + String monoString = new String(mono.block()); + assertEquals(monoString, EXPECTED_RESULT); + } + + @Test + public void invokeServiceWithOutRequest() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/invoke/41/method/neworder") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", map); + assertNull(mono.block()); + } + + @Test + public void invokeServiceWithRequest() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/invoke/41/method/neworder") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", "", map); + assertNull(mono.block()); + } + + @Test + public void invokeBinding() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/bindings/sample-topic") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeBinding("sample-topic", ""); + assertNull(mono.block()); + } + + @Test(expected = IllegalArgumentException.class) + public void invokeBindingNullName() { + Map map = new HashMap<>(); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/bindings/sample-topic") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeBinding(null, ""); + assertNull(mono.block()); + } + + + @Test + public void getStates() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "etag", null); + StateKeyValue stateKeyNull = new StateKeyValue("value", null, "etag", null); + StateOptions stateOptions = mock(StateOptions.class); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.getState(stateKeyNull, null, String.class).block(); + }); + Mono> mono = daprClientHttpAdapter.getState(stateKeyValue, stateOptions, String.class); + assertEquals(mono.block().getKey(), "key"); + } + + @Test + public void getStatesEmptyEtag() { + StateKeyValue stateEmptyEtag = new StateKeyValue("value", "key", "", null); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono> monoEmptyEtag = daprClientHttpAdapter.getState(stateEmptyEtag, null, String.class); + assertEquals(monoEmptyEtag.block().getKey(), "key"); + } + + @Test + public void getStatesNullEtag() { + StateKeyValue stateNullEtag = new StateKeyValue("value", "key", null, null); + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono> monoNullEtag = daprClientHttpAdapter.getState(stateNullEtag, null, String.class); + assertEquals(monoNullEtag.block().getKey(), "key"); + } + + @Test + public void saveStates() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "etag", null); + List> stateKeyValueList = Arrays.asList(stateKeyValue); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/state") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveStates(stateKeyValueList); + assertNull(mono.block()); + } + + @Test + public void saveStatesNull() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "", null); + List> stateKeyValueList = new ArrayList(); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/state") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveStates(null); + assertNull(mono.block()); + Mono mono1 = daprClientHttpAdapter.saveStates(stateKeyValueList); + assertNull(mono1.block()); + } + + @Test + public void saveStatesEtagNull() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", null, null); + List> stateKeyValueList = Arrays.asList(stateKeyValue); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/state") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveStates(stateKeyValueList); + assertNull(mono.block()); + } + + @Test + public void saveStatesEtagEmpty() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "", null); + List> stateKeyValueList = Arrays.asList(stateKeyValue); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/state") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveStates(stateKeyValueList); + assertNull(mono.block()); + } + + @Test + public void simpleSaveStates() { + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/state") + .respond(EXPECTED_RESULT); + StateOptions stateOptions = mock(StateOptions.class); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveState("key", "etag", "value", stateOptions); + assertNull(mono.block()); + } + + + @Test + public void deleteState() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "etag", null); + StateOptions stateOptions = mock(StateOptions.class); + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.deleteState(stateKeyValue, stateOptions); + assertNull(mono.block()); + } + + @Test + public void deleteStateNullEtag() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", null, null); + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.deleteState(stateKeyValue, null); + assertNull(mono.block()); + } + + @Test + public void deleteStateEmptyEtag() { + StateKeyValue stateKeyValue = new StateKeyValue("value", "key", "", null); + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.deleteState(stateKeyValue, null); + assertNull(mono.block()); + } + + @Test + public void deleteStateIllegalArgumentException() { + StateKeyValue stateKeyValueNull = new StateKeyValue("value", null, "etag", null); + StateKeyValue stateKeyValueEmpty = new StateKeyValue("value", "", "etag", null); + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/state/key") + .respond(EXPECTED_RESULT); + daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.deleteState(null, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.deleteState(stateKeyValueNull, null).block(); + }); + assertThrows(IllegalArgumentException.class, () -> { + daprClientHttpAdapter.deleteState(stateKeyValueEmpty, null).block(); + }); + } + + @Test + public void invokeActorMethod() throws IOException { + DaprHttp daprHttpMock = mock(DaprHttp.class); + mockInterceptor.addRule() + .post("http://localhost:3000/v1.0/actors/DemoActor/1/method/Payment") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.invokeActorMethod("DemoActor", "1", "Payment", ""); + assertEquals(mono.block(), EXPECTED_RESULT); + } + + + @Test + public void getActorState() { + mockInterceptor.addRule() + .get("http://localhost:3000/v1.0/actors/DemoActor/1/state/order") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.getActorState("DemoActor", "1", "order"); + assertEquals(mono.block(), EXPECTED_RESULT); + } + + + @Test + public void saveActorStateTransactionally() { + mockInterceptor.addRule() + .put("http://localhost:3000/v1.0/actors/DemoActor/1/state") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.saveActorStateTransactionally("DemoActor", "1", ""); + assertNull(mono.block()); + } + + @Test + public void registerActorReminder() { + mockInterceptor.addRule() + .put("http://localhost:3000/v1.0/actors/DemoActor/1/reminders/reminder") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.registerActorReminder("DemoActor", "1", "reminder", ""); + assertNull(mono.block()); + } + + @Test + public void unregisterActorReminder() { + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/actors/DemoActor/1/reminders/reminder") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.unregisterActorReminder("DemoActor", "1", "reminder"); + assertNull(mono.block()); + } + + @Test + public void registerActorTimer() { + mockInterceptor.addRule() + .put("http://localhost:3000/v1.0/actors/DemoActor/1/timers/timer") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.registerActorTimer("DemoActor", "1", "timer", ""); + assertNull(mono.block()); + } + + @Test + public void unregisterActorTimer() { + mockInterceptor.addRule() + .delete("http://localhost:3000/v1.0/actors/DemoActor/1/timers/timer") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3000, okHttpClient); + daprClientHttpAdapter = new DaprClientHttpAdapter(daprHttp); + Mono mono = daprClientHttpAdapter.unregisterActorTimer("DemoActor", "1", "timer"); + assertNull(mono.block()); + } + +} \ No newline at end of file diff --git a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java index 5b2a7d323..25167bb5c 100644 --- a/sdk/src/test/java/io/dapr/client/DaprHttpTest.java +++ b/sdk/src/test/java/io/dapr/client/DaprHttpTest.java @@ -23,222 +23,180 @@ import static org.junit.Assert.*; public class DaprHttpTest { - private OkHttpClient okHttpClient; + private OkHttpClient okHttpClient; - private MockInterceptor mockInterceptor; + private MockInterceptor mockInterceptor; - private ObjectSerializer serializer = new ObjectSerializer(); + private ObjectSerializer serializer = new ObjectSerializer(); - private final String EXPECTED_RESULT = "{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}"; + private final String EXPECTED_RESULT = "{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}"; - @Before - public void setUp() throws Exception { - mockInterceptor = new MockInterceptor(Behavior.UNORDERED); - 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 - 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); - - } - - @Test - public void invokeDeleteMethod() throws IOException { - - mockInterceptor.addRule() - .delete("http://localhost:3500/v1.0/state") - .respond(EXPECTED_RESULT); - - DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); - - 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); - - } - - @Test - public void invokeGetMethod() throws IOException { - - mockInterceptor.addRule() - .get("http://localhost:3500/v1.0/get") - .respond(EXPECTED_RESULT); - - DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); - - 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); - - } - - @Test - public void invokeMethodWithHeaders() throws IOException { - - Map headers = new HashMap<>(); - headers.put("header", "value"); - headers.put("header1", "value1"); - - Map urlParameters = new HashMap<>(); - urlParameters.put("orderId", "41"); - - mockInterceptor.addRule() - .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/state/order", urlParameters, headers); - DaprHttp.Response response = mono.block(); - String body = serializer.deserialize(response.getBody(), String.class); - assertEquals(EXPECTED_RESULT, body); - - } - - @Test(expected = RuntimeException.class) - public void invokePostMethodRuntime() throws IOException { - - 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() - .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); - } - - /** - * The purpose of this test is to show that it doesn't matter when the client is called, the actual coll to DAPR - * will be done when the output Mono response call the Mono.block method. - * Like for instanche if you call getState, withouth blocking for the response, and then call delete for the same state - * you just retrived but block for the delete response, when later you block for the response of the getState, you will - * not found the state. - *

This test will execute the following flow:

- *
    - *
  1. Exeucte client getState for Key=key1
  2. - *
  3. Block for result to the the state
  4. - *
  5. Assert the Returned State is the expected to key1
  6. - *
  7. Execute client getState for Key=key2
  8. - *
  9. Execute client deleteState for Key=key2
  10. - *
  11. Block for deleteState call.
  12. - *
  13. Block for getState for Key=key2 and Assert they 2 was not found.
  14. - *
- * @throws Exception - */ - @Test () - public void testCallbackCalledAtTheExpectedTimeTest() throws IOException { - String deletedStateKey = "deletedKey"; - String existingState = "existingState"; - String urlDeleteState = Constants.STATE_PATH + "/" + deletedStateKey; - String urlExistingState = Constants.STATE_PATH + "/" + existingState; - mockInterceptor.addRule() - .get("http://localhost:3500/" + urlDeleteState) - .respond(200, ResponseBody.create(MediaType.parse("application/json"), - deletedStateKey)); - mockInterceptor.addRule() - .delete("http://localhost:3500/" + urlDeleteState) - .respond(204); - mockInterceptor.addRule() - .get("http://localhost:3500/" +urlExistingState) - .respond(200, ResponseBody.create(MediaType.parse("application/json"), - existingState)); - DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); - Mono response = daprHttp.invokeAPI("GET", urlExistingState, null, null); - assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class)); - Mono responseDeleted = daprHttp.invokeAPI("GET", urlDeleteState, null, null); - Mono responseDeleteKey = daprHttp.invokeAPI("DELETE", urlDeleteState, null, null); - assertNull(serializer.deserialize(responseDeleteKey.block().getBody(), String.class)); - mockInterceptor.reset(); - mockInterceptor.addRule() - .get("http://localhost:3500/" +urlDeleteState) - .respond(404, ResponseBody.create(MediaType.parse("application/json"), - "{\"errorCode\":\"404\",\"message\":\"State Not Fuund\"}")); - try { - responseDeleted.block(); - fail("Expected DaprException"); - } catch (Exception ex) { - assertEquals(DaprException.class, ex.getCause().getCause().getClass()); - } + @Before + public void setUp() throws Exception { + mockInterceptor = new MockInterceptor(Behavior.UNORDERED); + 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 + 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); + } + + @Test + public void invokeDeleteMethod() throws IOException { + mockInterceptor.addRule() + .delete("http://localhost:3500/v1.0/state") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); + 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); + } + + @Test + public void invokeGetMethod() throws IOException { + mockInterceptor.addRule() + .get("http://localhost:3500/v1.0/get") + .respond(EXPECTED_RESULT); + DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); + 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); + } + + @Test + public void invokeMethodWithHeaders() throws IOException { + Map headers = new HashMap<>(); + headers.put("header", "value"); + headers.put("header1", "value1"); + Map urlParameters = new HashMap<>(); + urlParameters.put("orderId", "41"); + mockInterceptor.addRule() + .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/state/order", urlParameters, headers); + DaprHttp.Response response = mono.block(); + String body = serializer.deserialize(response.getBody(), String.class); + assertEquals(EXPECTED_RESULT, body); + } + + @Test(expected = RuntimeException.class) + public void invokePostMethodRuntime() throws IOException { + 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() + .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); + } + + /** + * The purpose of this test is to show that it doesn't matter when the client is called, the actual coll to DAPR + * will be done when the output Mono response call the Mono.block method. + * Like for instanche if you call getState, withouth blocking for the response, and then call delete for the same state + * you just retrived but block for the delete response, when later you block for the response of the getState, you will + * not found the state. + *

This test will execute the following flow:

+ *
    + *
  1. Exeucte client getState for Key=key1
  2. + *
  3. Block for result to the the state
  4. + *
  5. Assert the Returned State is the expected to key1
  6. + *
  7. Execute client getState for Key=key2
  8. + *
  9. Execute client deleteState for Key=key2
  10. + *
  11. Block for deleteState call.
  12. + *
  13. Block for getState for Key=key2 and Assert they 2 was not found.
  14. + *
+ * + * @throws Exception + */ + @Test() + public void testCallbackCalledAtTheExpectedTimeTest() throws IOException { + String deletedStateKey = "deletedKey"; + String existingState = "existingState"; + String urlDeleteState = Constants.STATE_PATH + "/" + deletedStateKey; + String urlExistingState = Constants.STATE_PATH + "/" + existingState; + mockInterceptor.addRule() + .get("http://localhost:3500/" + urlDeleteState) + .respond(200, ResponseBody.create(MediaType.parse("application/json"), + deletedStateKey)); + mockInterceptor.addRule() + .delete("http://localhost:3500/" + urlDeleteState) + .respond(204); + mockInterceptor.addRule() + .get("http://localhost:3500/" + urlExistingState) + .respond(200, ResponseBody.create(MediaType.parse("application/json"), + existingState)); + DaprHttp daprHttp = new DaprHttp(3500, okHttpClient); + Mono response = daprHttp.invokeAPI("GET", urlExistingState, null, null); + assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class)); + Mono responseDeleted = daprHttp.invokeAPI("GET", urlDeleteState, null, null); + Mono responseDeleteKey = daprHttp.invokeAPI("DELETE", urlDeleteState, null, null); + assertNull(serializer.deserialize(responseDeleteKey.block().getBody(), String.class)); + mockInterceptor.reset(); + mockInterceptor.addRule() + .get("http://localhost:3500/" + urlDeleteState) + .respond(404, ResponseBody.create(MediaType.parse("application/json"), + "{\"errorCode\":\"404\",\"message\":\"State Not Fuund\"}")); + try { + responseDeleted.block(); + fail("Expected DaprException"); + } catch (Exception ex) { + assertEquals(DaprException.class, ex.getCause().getCause().getClass()); } + } }