Creating unit tests for HttpAdapter + Adding junit 5 dependency + Reformating code (#112)

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
mestizoLopez 2020-01-17 11:27:06 -06:00 committed by Artur Souza
parent 8bb452f66a
commit bcf653bc5e
4 changed files with 643 additions and 217 deletions

View File

@ -86,6 +86,12 @@
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -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 <T> The Type of the Value of the state
* @return A StateKeyValue instance
* @throws IOException If there's a issue deserialzing the response.
*/
private <T> StateKeyValue<T> buildStateKeyValue(DaprHttp.Response resonse, String requestedKey, StateOptions stateOptions, Class<T> clazz) throws IOException {
T value = objectSerializer.deserialize(resonse.getBody(), clazz);
private <T> StateKeyValue<T> buildStateKeyValue(DaprHttp.Response response, String requestedKey, StateOptions stateOptions, Class<T> 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);
}

View File

@ -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<Void> 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<Void> 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<Void> 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<Void> 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<Void> 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<String> mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", null, null, String.class);
assertEquals(mono.block(), EXPECTED_RESULT);
}
@Test
public void simpleInvokeService() {
Map<String, String> 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<String> mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", null, String.class);
assertEquals(mono.block(), EXPECTED_RESULT);
}
@Test
public void invokeServiceWithMaps() {
Map<String, String> 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<byte[]> 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<String, String> 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<Void> mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", map);
assertNull(mono.block());
}
@Test
public void invokeServiceWithRequest() {
Map<String, String> 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<Void> mono = daprClientHttpAdapter.invokeService(Verb.GET, "41", "neworder", "", map);
assertNull(mono.block());
}
@Test
public void invokeBinding() {
Map<String, String> 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<Void> mono = daprClientHttpAdapter.invokeBinding("sample-topic", "");
assertNull(mono.block());
}
@Test(expected = IllegalArgumentException.class)
public void invokeBindingNullName() {
Map<String, String> 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<Void> mono = daprClientHttpAdapter.invokeBinding(null, "");
assertNull(mono.block());
}
@Test
public void getStates() {
StateKeyValue<String> stateKeyValue = new StateKeyValue("value", "key", "etag", null);
StateKeyValue<String> 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<StateKeyValue<String>> mono = daprClientHttpAdapter.getState(stateKeyValue, stateOptions, String.class);
assertEquals(mono.block().getKey(), "key");
}
@Test
public void getStatesEmptyEtag() {
StateKeyValue<String> 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<StateKeyValue<String>> monoEmptyEtag = daprClientHttpAdapter.getState(stateEmptyEtag, null, String.class);
assertEquals(monoEmptyEtag.block().getKey(), "key");
}
@Test
public void getStatesNullEtag() {
StateKeyValue<String> 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<StateKeyValue<String>> monoNullEtag = daprClientHttpAdapter.getState(stateNullEtag, null, String.class);
assertEquals(monoNullEtag.block().getKey(), "key");
}
@Test
public void saveStates() {
StateKeyValue<String> stateKeyValue = new StateKeyValue("value", "key", "etag", null);
List<StateKeyValue<String>> 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<Void> mono = daprClientHttpAdapter.saveStates(stateKeyValueList);
assertNull(mono.block());
}
@Test
public void saveStatesNull() {
StateKeyValue<String> stateKeyValue = new StateKeyValue("value", "key", "", null);
List<StateKeyValue<String>> 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<Void> mono = daprClientHttpAdapter.saveStates(null);
assertNull(mono.block());
Mono<Void> mono1 = daprClientHttpAdapter.saveStates(stateKeyValueList);
assertNull(mono1.block());
}
@Test
public void saveStatesEtagNull() {
StateKeyValue<String> stateKeyValue = new StateKeyValue("value", "key", null, null);
List<StateKeyValue<String>> 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<Void> mono = daprClientHttpAdapter.saveStates(stateKeyValueList);
assertNull(mono.block());
}
@Test
public void saveStatesEtagEmpty() {
StateKeyValue<String> stateKeyValue = new StateKeyValue("value", "key", "", null);
List<StateKeyValue<String>> 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<Void> 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<Void> mono = daprClientHttpAdapter.saveState("key", "etag", "value", stateOptions);
assertNull(mono.block());
}
@Test
public void deleteState() {
StateKeyValue<String> 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<Void> mono = daprClientHttpAdapter.deleteState(stateKeyValue, stateOptions);
assertNull(mono.block());
}
@Test
public void deleteStateNullEtag() {
StateKeyValue<String> 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<Void> mono = daprClientHttpAdapter.deleteState(stateKeyValue, null);
assertNull(mono.block());
}
@Test
public void deleteStateEmptyEtag() {
StateKeyValue<String> 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<Void> mono = daprClientHttpAdapter.deleteState(stateKeyValue, null);
assertNull(mono.block());
}
@Test
public void deleteStateIllegalArgumentException() {
StateKeyValue<String> stateKeyValueNull = new StateKeyValue("value", null, "etag", null);
StateKeyValue<String> 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<String> 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<String> 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<Void> 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<Void> 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<Void> 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<Void> 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<Void> mono = daprClientHttpAdapter.unregisterActorTimer("DemoActor", "1", "timer");
assertNull(mono.block());
}
}

View File

@ -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<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
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<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 invokeDeleteMethod() throws IOException {
mockInterceptor.addRule()
.delete("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
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);
}
@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<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);
}
@Test
public void invokeMethodWithHeaders() throws IOException {
Map<String, String> headers = new HashMap<>();
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/state/order?orderId=41")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
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);
}
@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<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 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);
}
/**
* 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.
* <p>This test will execute the following flow:</p>
* <ol>
* <li>Exeucte client getState for Key=key1</li>
* <li>Block for result to the the state</li>
* <li>Assert the Returned State is the expected to key1</li>
* <li>Execute client getState for Key=key2</li>
* <li>Execute client deleteState for Key=key2</li>
* <li>Block for deleteState call.</li>
* <li>Block for getState for Key=key2 and Assert they 2 was not found.</li>
* </ol>
* @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<DaprHttp.Response> response = daprHttp.invokeAPI("GET", urlExistingState, null, null);
assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class));
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeAPI("GET", urlDeleteState, null, null);
Mono<DaprHttp.Response> 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<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
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<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 invokeDeleteMethod() throws IOException {
mockInterceptor.addRule()
.delete("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
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);
}
@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<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);
}
@Test
public void invokeMethodWithHeaders() throws IOException {
Map<String, String> headers = new HashMap<>();
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/state/order?orderId=41")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
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);
}
@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<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 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);
}
/**
* 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.
* <p>This test will execute the following flow:</p>
* <ol>
* <li>Exeucte client getState for Key=key1</li>
* <li>Block for result to the the state</li>
* <li>Assert the Returned State is the expected to key1</li>
* <li>Execute client getState for Key=key2</li>
* <li>Execute client deleteState for Key=key2</li>
* <li>Block for deleteState call.</li>
* <li>Block for getState for Key=key2 and Assert they 2 was not found.</li>
* </ol>
*
* @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<DaprHttp.Response> response = daprHttp.invokeAPI("GET", urlExistingState, null, null);
assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class));
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeAPI("GET", urlDeleteState, null, null);
Mono<DaprHttp.Response> 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());
}
}
}