mirror of https://github.com/dapr/java-sdk.git
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:
parent
8bb452f66a
commit
bcf653bc5e
|
|
@ -86,6 +86,12 @@
|
||||||
<version>1.19.0</version>
|
<version>1.19.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.5.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
||||||
|
|
@ -331,19 +331,19 @@ public class DaprClientHttpAdapter implements DaprClient {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a StateKeyValue object based on the Response
|
* 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 requestedKey The Key Requested.
|
||||||
* @param clazz The Class of the Value of the state
|
* @param clazz The Class of the Value of the state
|
||||||
* @param <T> The Type of the Value of the state
|
* @param <T> The Type of the Value of the state
|
||||||
* @return A StateKeyValue instance
|
* @return A StateKeyValue instance
|
||||||
* @throws IOException If there's a issue deserialzing the response.
|
* @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 {
|
private <T> StateKeyValue<T> buildStateKeyValue(DaprHttp.Response response, String requestedKey, StateOptions stateOptions, Class<T> clazz) throws IOException {
|
||||||
T value = objectSerializer.deserialize(resonse.getBody(), clazz);
|
T value = objectSerializer.deserialize(response.getBody(), clazz);
|
||||||
String key = requestedKey;
|
String key = requestedKey;
|
||||||
String etag = null;
|
String etag = null;
|
||||||
if (resonse.getHeaders() != null && resonse.getHeaders().containsKey("Etag")) {
|
if (response.getHeaders() != null && response.getHeaders().containsKey("Etag")) {
|
||||||
etag = objectSerializer.deserialize(resonse.getHeaders().get("Etag"), String.class);
|
etag = objectSerializer.deserialize(response.getHeaders().get("Etag"), String.class);
|
||||||
}
|
}
|
||||||
return new StateKeyValue<>(value, key, etag, stateOptions);
|
return new StateKeyValue<>(value, key, etag, stateOptions);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -23,222 +23,180 @@ import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class DaprHttpTest {
|
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
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
|
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
|
||||||
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
|
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokeMethod() throws IOException {
|
public void invokeMethod() throws IOException {
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
Map<String, String> headers = new HashMap<>();
|
headers.put("content-type", "text/html");
|
||||||
headers.put("content-type", "text/html");
|
headers.put("header1", "value1");
|
||||||
headers.put("header1", "value1");
|
mockInterceptor.addRule()
|
||||||
|
.post("http://localhost:3500/v1.0/state")
|
||||||
mockInterceptor.addRule()
|
.respond(EXPECTED_RESULT);
|
||||||
.post("http://localhost:3500/v1.0/state")
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
.respond(EXPECTED_RESULT);
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, (byte[]) null, headers);
|
||||||
|
DaprHttp.Response response = mono.block();
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, (byte[])null, headers);
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
|
}
|
||||||
DaprHttp.Response response = mono.block();
|
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
@Test
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
public void invokePostMethod() throws IOException {
|
||||||
}
|
mockInterceptor.addRule()
|
||||||
|
.post("http://localhost:3500/v1.0/state")
|
||||||
@Test
|
.respond(EXPECTED_RESULT)
|
||||||
public void invokePostMethod() throws IOException {
|
.addHeader("Header", "Value");
|
||||||
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
mockInterceptor.addRule()
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, "", null);
|
||||||
.post("http://localhost:3500/v1.0/state")
|
DaprHttp.Response response = mono.block();
|
||||||
.respond(EXPECTED_RESULT)
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
.addHeader("Header","Value");
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
|
}
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
|
||||||
|
@Test
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null,"", null);
|
public void invokeDeleteMethod() throws IOException {
|
||||||
DaprHttp.Response response = mono.block();
|
mockInterceptor.addRule()
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
.delete("http://localhost:3500/v1.0/state")
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
.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();
|
||||||
@Test
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
public void invokeDeleteMethod() throws IOException {
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
|
}
|
||||||
mockInterceptor.addRule()
|
|
||||||
.delete("http://localhost:3500/v1.0/state")
|
@Test
|
||||||
.respond(EXPECTED_RESULT);
|
public void invokeGetMethod() throws IOException {
|
||||||
|
mockInterceptor.addRule()
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
.get("http://localhost:3500/v1.0/get")
|
||||||
|
.respond(EXPECTED_RESULT);
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, (String) null,null);
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
DaprHttp.Response response = mono.block();
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET", "v1.0/get", null, null);
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
DaprHttp.Response response = mono.block();
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokeGetMethod() throws IOException {
|
public void invokeMethodWithHeaders() throws IOException {
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
mockInterceptor.addRule()
|
headers.put("header", "value");
|
||||||
.get("http://localhost:3500/v1.0/get")
|
headers.put("header1", "value1");
|
||||||
.respond(EXPECTED_RESULT);
|
Map<String, String> urlParameters = new HashMap<>();
|
||||||
|
urlParameters.put("orderId", "41");
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
mockInterceptor.addRule()
|
||||||
|
.get("http://localhost:3500/v1.0/state/order?orderId=41")
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",null, null);
|
.respond(EXPECTED_RESULT);
|
||||||
DaprHttp.Response response = mono.block();
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET", "v1.0/state/order", urlParameters, headers);
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
DaprHttp.Response response = mono.block();
|
||||||
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
}
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void invokeMethodWithHeaders() throws IOException {
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void invokePostMethodRuntime() throws IOException {
|
||||||
Map<String, String> headers = new HashMap<>();
|
mockInterceptor.addRule()
|
||||||
headers.put("header", "value");
|
.post("http://localhost:3500/v1.0/state")
|
||||||
headers.put("header1", "value1");
|
.respond(500);
|
||||||
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
Map<String, String> urlParameters = new HashMap<>();
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, null);
|
||||||
urlParameters.put("orderId", "41");
|
DaprHttp.Response response = mono.block();
|
||||||
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
mockInterceptor.addRule()
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
.get("http://localhost:3500/v1.0/state/order?orderId=41")
|
}
|
||||||
.respond(EXPECTED_RESULT);
|
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void invokePostDaprError() throws IOException {
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/state/order", urlParameters, headers);
|
mockInterceptor.addRule()
|
||||||
DaprHttp.Response response = mono.block();
|
.post("http://localhost:3500/v1.0/state")
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
.respond(500, ResponseBody.create(MediaType.parse("text"),
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
"{\"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();
|
||||||
@Test(expected = RuntimeException.class)
|
String body = serializer.deserialize(response.getBody(), String.class);
|
||||||
public void invokePostMethodRuntime() throws IOException {
|
assertEquals(EXPECTED_RESULT, body);
|
||||||
|
}
|
||||||
mockInterceptor.addRule()
|
|
||||||
.post("http://localhost:3500/v1.0/state")
|
@Test(expected = RuntimeException.class)
|
||||||
.respond(500);
|
public void invokePostMethodUnknownError() throws IOException {
|
||||||
|
mockInterceptor.addRule()
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
.post("http://localhost:3500/v1.0/state")
|
||||||
|
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
|
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
|
||||||
DaprHttp.Response response = mono.block();
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, null);
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
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 {
|
/**
|
||||||
|
* The purpose of this test is to show that it doesn't matter when the client is called, the actual coll to DAPR
|
||||||
mockInterceptor.addRule()
|
* will be done when the output Mono response call the Mono.block method.
|
||||||
.post("http://localhost:3500/v1.0/state")
|
* Like for instanche if you call getState, withouth blocking for the response, and then call delete for the same state
|
||||||
.respond(500, ResponseBody.create(MediaType.parse("text"),
|
* you just retrived but block for the delete response, when later you block for the response of the getState, you will
|
||||||
"{\"errorCode\":null,\"message\":null}"));
|
* not found the state.
|
||||||
|
* <p>This test will execute the following flow:</p>
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
* <ol>
|
||||||
|
* <li>Exeucte client getState for Key=key1</li>
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
|
* <li>Block for result to the the state</li>
|
||||||
DaprHttp.Response response = mono.block();
|
* <li>Assert the Returned State is the expected to key1</li>
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
* <li>Execute client getState for Key=key2</li>
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
* <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>
|
||||||
@Test(expected = RuntimeException.class)
|
*
|
||||||
public void invokePostMethodUnknownError() throws IOException {
|
* @throws Exception
|
||||||
|
*/
|
||||||
mockInterceptor.addRule()
|
@Test()
|
||||||
.post("http://localhost:3500/v1.0/state")
|
public void testCallbackCalledAtTheExpectedTimeTest() throws IOException {
|
||||||
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
|
String deletedStateKey = "deletedKey";
|
||||||
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
|
String existingState = "existingState";
|
||||||
|
String urlDeleteState = Constants.STATE_PATH + "/" + deletedStateKey;
|
||||||
DaprHttp daprHttp = new DaprHttp(3500,okHttpClient);
|
String urlExistingState = Constants.STATE_PATH + "/" + existingState;
|
||||||
|
mockInterceptor.addRule()
|
||||||
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
|
.get("http://localhost:3500/" + urlDeleteState)
|
||||||
DaprHttp.Response response = mono.block();
|
.respond(200, ResponseBody.create(MediaType.parse("application/json"),
|
||||||
String body = serializer.deserialize(response.getBody(), String.class);
|
deletedStateKey));
|
||||||
assertEquals(EXPECTED_RESULT, body);
|
mockInterceptor.addRule()
|
||||||
|
.delete("http://localhost:3500/" + urlDeleteState)
|
||||||
}
|
.respond(204);
|
||||||
|
mockInterceptor.addRule()
|
||||||
@Test
|
.get("http://localhost:3500/" + urlExistingState)
|
||||||
public void getHeadersAndStatus(){
|
.respond(200, ResponseBody.create(MediaType.parse("application/json"),
|
||||||
mockInterceptor.addRule()
|
existingState));
|
||||||
.post("http://localhost:3500/v1.0/state")
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
|
Mono<DaprHttp.Response> response = daprHttp.invokeAPI("GET", urlExistingState, null, null);
|
||||||
"{\"errorCode\":\"null\",\"message\":\"null\"}"));
|
assertEquals(existingState, serializer.deserialize(response.block().getBody(), String.class));
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
Mono<DaprHttp.Response> responseDeleted = daprHttp.invokeAPI("GET", urlDeleteState, null, null);
|
||||||
System.out.println(daprHttp);
|
Mono<DaprHttp.Response> responseDeleteKey = daprHttp.invokeAPI("DELETE", urlDeleteState, null, null);
|
||||||
}
|
assertNull(serializer.deserialize(responseDeleteKey.block().getBody(), String.class));
|
||||||
|
mockInterceptor.reset();
|
||||||
/**
|
mockInterceptor.addRule()
|
||||||
* The purpose of this test is to show that it doesn't matter when the client is called, the actual coll to DAPR
|
.get("http://localhost:3500/" + urlDeleteState)
|
||||||
* will be done when the output Mono response call the Mono.block method.
|
.respond(404, ResponseBody.create(MediaType.parse("application/json"),
|
||||||
* Like for instanche if you call getState, withouth blocking for the response, and then call delete for the same state
|
"{\"errorCode\":\"404\",\"message\":\"State Not Fuund\"}"));
|
||||||
* you just retrived but block for the delete response, when later you block for the response of the getState, you will
|
try {
|
||||||
* not found the state.
|
responseDeleted.block();
|
||||||
* <p>This test will execute the following flow:</p>
|
fail("Expected DaprException");
|
||||||
* <ol>
|
} catch (Exception ex) {
|
||||||
* <li>Exeucte client getState for Key=key1</li>
|
assertEquals(DaprException.class, ex.getCause().getCause().getClass());
|
||||||
* <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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue