mirror of https://github.com/dapr/java-sdk.git
Refactor params for State class + Unit test example (#473)
* Refactor order of params for State class. * Example for unit testing. * Change toString() for State class. * Add unittesting example to main README. * Fix cmd for unittesting example. * Add sleep to http inputing binding example validation. * Fix unittesting example check.
This commit is contained in:
parent
277f9958a6
commit
5d7700b570
|
@ -132,3 +132,7 @@ jobs:
|
|||
working-directory: ./examples
|
||||
run: |
|
||||
mm.py ./src/main/java/io/dapr/examples/secrets/README.md
|
||||
- name: Validate unit testing example
|
||||
working-directory: ./examples
|
||||
run: |
|
||||
mm.py ./src/main/java/io/dapr/examples/unittesting/README.md
|
||||
|
|
|
@ -145,6 +145,7 @@ Try the following examples to learn more about Dapr's Java SDK:
|
|||
* [Secrets management](./examples/src/main/java/io/dapr/examples/secrets)
|
||||
* [Distributed tracing with OpenTelemetry SDK](./examples/src/main/java/io/dapr/examples/tracing)
|
||||
* [Exception handling](./examples/src/main/java/io/dapr/examples/exception)
|
||||
* [Unit testing](./examples/src/main/java/io/dapr/examples/unittesting)
|
||||
|
||||
#### API Documentation
|
||||
|
||||
|
|
|
@ -89,6 +89,24 @@
|
|||
<artifactId>opentelemetry-exporter-zipkin</artifactId>
|
||||
<version>${opentelemetry.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-console-standalone</artifactId>
|
||||
<version>1.7.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.dapr</groupId>
|
||||
<artifactId>dapr-sdk-springboot</artifactId>
|
||||
|
|
|
@ -116,7 +116,7 @@ expected_stdout_lines:
|
|||
- '== APP == Received message through binding: {"message":"Message #2"}'
|
||||
- '== APP == Received message through binding: "Message #3"'
|
||||
background: true
|
||||
sleep: 5
|
||||
sleep: 10
|
||||
-->
|
||||
|
||||
```bash
|
||||
|
|
|
@ -67,9 +67,9 @@ public class StateClient {
|
|||
// execute transaction
|
||||
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
|
||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
||||
new State<>(myClass, FIRST_KEY_NAME, "")));
|
||||
new State<>(FIRST_KEY_NAME, myClass, "")));
|
||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
||||
new State<>(secondState, SECOND_KEY_NAME, "")));
|
||||
new State<>(SECOND_KEY_NAME, secondState, "")));
|
||||
|
||||
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
|
||||
|
||||
|
@ -143,15 +143,15 @@ expected_stdout_lines:
|
|||
- "== APP == Updating previous state and adding another state 'test state'... "
|
||||
- "== APP == Saving updated class with message: my message updated"
|
||||
- "== APP == Retrieved messages using bulk get:"
|
||||
- "== APP == StateKeyValue{value=my message updated, key='myKey', etag='2', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{value=test message, key='myKey2', etag='1', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{key='myKey', value=my message updated, etag='2', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{key='myKey2', value=test message, etag='1', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == Deleting states..."
|
||||
- "== APP == Verify delete key request is aborted if an etag different from stored is passed."
|
||||
- "== APP == Expected failure. ABORTED"
|
||||
- "== APP == Trying to delete again with correct etag."
|
||||
- "== APP == Trying to retrieve deleted states:"
|
||||
- "== APP == StateKeyValue{value=null, key='myKey', etag='null', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{value=null, key='myKey2', etag='null', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{key='myKey', value=null, etag='null', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == StateKeyValue{key='myKey2', value=null, etag='null', metadata={'{}'}, error='null', options={'null'}}"
|
||||
- "== APP == Done"
|
||||
background: true
|
||||
sleep: 5
|
||||
|
@ -181,9 +181,9 @@ Once running, the OutputBindingExample should print the output as follows:
|
|||
|
||||
== APP == Retrieved messages using bulk get:
|
||||
|
||||
== APP == StateKeyValue{value=my message updated, key='myKey', etag='2', metadata={'{}'}, error='null', options={'null'}}
|
||||
== APP == StateKeyValue{key='myKey', value=my message updated, etag='2', metadata={'{}'}, error='null', options={'null'}}
|
||||
|
||||
== APP == StateKeyValue{value=test message, key='myKey2', etag='1', metadata={'{}'}, error='null', options={'null'}}
|
||||
== APP == StateKeyValue{key='myKey2', value=test message, etag='1', metadata={'{}'}, error='null', options={'null'}}
|
||||
|
||||
== APP == Deleting states...
|
||||
|
||||
|
@ -195,9 +195,9 @@ Once running, the OutputBindingExample should print the output as follows:
|
|||
|
||||
== APP == Trying to retrieve deleted states:
|
||||
|
||||
== APP == StateKeyValue{value=null, key='myKey', etag='null', metadata={'{}'}, error='null', options={'null'}}
|
||||
== APP == StateKeyValue{key='myKey', value=null, etag='null', metadata={'{}'}, error='null', options={'null'}}
|
||||
|
||||
== APP == StateKeyValue{value=null, key='myKey2', etag='null', metadata={'{}'}, error='null', options={'null'}}
|
||||
== APP == StateKeyValue{key='myKey2', value=null, etag='null', metadata={'{}'}, error='null', options={'null'}}
|
||||
|
||||
== APP == Done
|
||||
|
||||
|
|
|
@ -72,9 +72,9 @@ public class StateClient {
|
|||
// execute transaction
|
||||
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
|
||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
||||
new State<>(myClass, FIRST_KEY_NAME, "")));
|
||||
new State<>(FIRST_KEY_NAME, myClass, "")));
|
||||
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
|
||||
new State<>(secondState, SECOND_KEY_NAME, "")));
|
||||
new State<>(SECOND_KEY_NAME, secondState, "")));
|
||||
|
||||
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
|
||||
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.unittesting;
|
||||
|
||||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.actors.ActorType;
|
||||
import io.dapr.actors.client.ActorClient;
|
||||
import io.dapr.actors.client.ActorProxyBuilder;
|
||||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.domain.State;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* 1. Build and install jars:
|
||||
* mvn clean install
|
||||
* 2. cd [repo root]/examples
|
||||
* 3. send a message to be saved as state:
|
||||
* java -jar target/dapr-java-sdk-examples-exec.jar \
|
||||
* org.junit.platform.console.ConsoleLauncher --select-class=io.dapr.examples.unittesting.DaprExampleTest
|
||||
*/
|
||||
public class DaprExampleTest {
|
||||
|
||||
@ActorType(name = "MyActor")
|
||||
public interface MyActor {
|
||||
String hello();
|
||||
}
|
||||
|
||||
private static final class MyApp {
|
||||
|
||||
private final DaprClient daprClient;
|
||||
|
||||
private final Function<ActorId, MyActor> actorProxyFactory;
|
||||
|
||||
/**
|
||||
* Example of constructor that can be used for production code.
|
||||
* @param client Dapr client.
|
||||
* @param actorClient Dapr Actor client.
|
||||
*/
|
||||
public MyApp(DaprClient client, ActorClient actorClient) {
|
||||
this.daprClient = client;
|
||||
this.actorProxyFactory = (actorId) -> new ActorProxyBuilder<>(MyActor.class, actorClient).build(actorId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of constructor that can be used for test code.
|
||||
* @param client Dapr client.
|
||||
* @param actorProxyFactory Factory method to create actor proxy instances.
|
||||
*/
|
||||
public MyApp(DaprClient client, Function<ActorId, MyActor> actorProxyFactory) {
|
||||
this.daprClient = client;
|
||||
this.actorProxyFactory = actorProxyFactory;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return daprClient.getState("appid", "statekey", String.class).block().getValue();
|
||||
}
|
||||
|
||||
public String invokeActor() {
|
||||
MyActor proxy = actorProxyFactory.apply(new ActorId("myactorId"));
|
||||
return proxy.hello();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetState() {
|
||||
DaprClient daprClient = Mockito.mock(DaprClient.class);
|
||||
Mockito.when(daprClient.getState("appid", "statekey", String.class)).thenReturn(
|
||||
Mono.just(new State<>("statekey", "myvalue", "1")));
|
||||
|
||||
MyApp app = new MyApp(daprClient, (ActorClient) null);
|
||||
|
||||
String value = app.getState();
|
||||
|
||||
assertEquals("myvalue", value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeActor() {
|
||||
MyActor actorMock = Mockito.mock(MyActor.class);
|
||||
Mockito.when(actorMock.hello()).thenReturn("hello world");
|
||||
|
||||
MyApp app = new MyApp(null, actorId -> actorMock);
|
||||
|
||||
String value = app.invokeActor();
|
||||
|
||||
assertEquals("hello world", value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
## Unit testing sample
|
||||
|
||||
This sample illustrates how applications can write unit testing with Dapr's Java SDK, JUnit 5 and Mockito.
|
||||
|
||||
## Pre-requisites
|
||||
|
||||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/).
|
||||
* Java JDK 11 (or greater): [Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) or [OpenJDK](https://jdk.java.net/13/).
|
||||
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.
|
||||
|
||||
### Checking out the code
|
||||
|
||||
Clone this repository:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/dapr/java-sdk.git
|
||||
cd java-sdk
|
||||
```
|
||||
|
||||
Then build the Maven project:
|
||||
|
||||
```sh
|
||||
# make sure you are in the `java-sdk` directory.
|
||||
mvn install
|
||||
```
|
||||
|
||||
Then change into the `examples` directory:
|
||||
```sh
|
||||
cd examples
|
||||
```
|
||||
|
||||
### Understanding the code
|
||||
This example will simulate an application code via the App class:
|
||||
|
||||
```java
|
||||
private static final class MyApp {
|
||||
|
||||
private final DaprClient daprClient;
|
||||
|
||||
private final Function<ActorId, MyActor> actorProxyFactory;
|
||||
|
||||
/**
|
||||
* Example of constructor that can be used for production code.
|
||||
* @param client Dapr client.
|
||||
* @param actorClient Dapr Actor client.
|
||||
*/
|
||||
public MyApp(DaprClient client, ActorClient actorClient) {
|
||||
this.daprClient = client;
|
||||
this.actorProxyFactory = (actorId) -> new ActorProxyBuilder<>(MyActor.class, actorClient).build(actorId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Example of constructor that can be used for test code.
|
||||
* @param client Dapr client.
|
||||
* @param actorProxyFactory Factory method to create actor proxy instances.
|
||||
*/
|
||||
public MyApp(DaprClient client, Function<ActorId, MyActor> actorProxyFactory) {
|
||||
this.daprClient = client;
|
||||
this.actorProxyFactory = actorProxyFactory;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return daprClient.getState("appid", "statekey", String.class).block().getValue();
|
||||
}
|
||||
|
||||
public String invokeActor() {
|
||||
MyActor proxy = actorProxyFactory.apply(new ActorId("myactorId"));
|
||||
return proxy.hello();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This class has two constructors. The first one can be used by production code by passing the proper instances of `DaprClient` and `ActorClient`.
|
||||
Then, it contains two methods: `getState()` will retrieve a state from `DaprClient`, while `invokeActor()` will create an instance of Actor proxy for `MyActor` interface and invoke a method on it.
|
||||
|
||||
```java
|
||||
@ActorType(name = "MyActor")
|
||||
public interface MyActor {
|
||||
String hello();
|
||||
}
|
||||
```
|
||||
|
||||
The first test validates the `getState()` method while mocking `DaprClient`:
|
||||
```java
|
||||
@Test
|
||||
public void testGetState() {
|
||||
DaprClient daprClient = Mockito.mock(DaprClient.class);
|
||||
Mockito.when(daprClient.getState("appid", "statekey", String.class)).thenReturn(
|
||||
Mono.just(new State<>("statekey", "myvalue", "1")));
|
||||
|
||||
MyApp app = new MyApp(daprClient, (ActorClient) null);
|
||||
|
||||
String value = app.getState();
|
||||
|
||||
assertEquals("myvalue", value);
|
||||
}
|
||||
```
|
||||
|
||||
The second test uses a mock implementation of the factory method and checks the actor invocation by mocking the `MyActor` interface:
|
||||
```java
|
||||
@Test
|
||||
public void testInvokeActor() {
|
||||
MyActor actorMock = Mockito.mock(MyActor.class);
|
||||
Mockito.when(actorMock.hello()).thenReturn("hello world");
|
||||
|
||||
MyApp app = new MyApp(null, actorId -> actorMock);
|
||||
|
||||
String value = app.invokeActor();
|
||||
|
||||
assertEquals("hello world", value);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Running the example
|
||||
<!-- STEP
|
||||
name: Check state example
|
||||
expected_stdout_lines:
|
||||
- "[ 2 tests found ]"
|
||||
- "[ 0 tests skipped ]"
|
||||
- "[ 2 tests started ]"
|
||||
- "[ 0 tests aborted ]"
|
||||
- "[ 2 tests successful ]"
|
||||
- "[ 0 tests failed ]"
|
||||
background: true
|
||||
sleep: 5
|
||||
-->
|
||||
|
||||
Run this example with the following command:
|
||||
```bash
|
||||
java -jar target/dapr-java-sdk-examples-exec.jar org.junit.platform.console.ConsoleLauncher --select-class=io.dapr.examples.unittesting.DaprExampleTest
|
||||
```
|
||||
|
||||
<!-- END_STEP -->
|
||||
|
||||
After running, Junit should print the output as follows:
|
||||
|
||||
```txt
|
||||
╷
|
||||
├─ JUnit Jupiter ✔
|
||||
│ └─ DaprExampleTest ✔
|
||||
│ ├─ testGetState() ✔
|
||||
│ └─ testInvokeActor() ✔
|
||||
└─ JUnit Vintage ✔
|
||||
|
||||
Test run finished after 1210 ms
|
||||
[ 3 containers found ]
|
||||
[ 0 containers skipped ]
|
||||
[ 3 containers started ]
|
||||
[ 0 containers aborted ]
|
||||
[ 3 containers successful ]
|
||||
[ 0 containers failed ]
|
||||
[ 2 tests found ]
|
||||
[ 0 tests skipped ]
|
||||
[ 2 tests started ]
|
||||
[ 0 tests aborted ]
|
||||
[ 2 tests successful ]
|
||||
[ 0 tests failed ]
|
||||
```
|
|
@ -140,7 +140,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to retrieve the action
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the retrieve of the state
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -168,7 +168,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to retrieve the state
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the retrieve of the state
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -183,7 +183,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
deleteResponse.block();
|
||||
|
||||
//Create deferred action to retrieve the state
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null), MyData.class);
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null), MyData.class);
|
||||
//execute the retrieve of the state
|
||||
myDataResponse = response.block();
|
||||
|
||||
|
@ -209,7 +209,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to retrieve the state
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the action for retrieve the state and the etag
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -231,7 +231,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null), MyData.class);
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null), MyData.class);
|
||||
//retrive the data wihout any etag
|
||||
myDataResponse = response.block();
|
||||
|
||||
|
@ -263,7 +263,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to retrieve the state
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the action for retrieve the state and the etag
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -285,7 +285,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null), MyData.class);
|
||||
response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null), MyData.class);
|
||||
//retrive the data wihout any etag
|
||||
myDataResponse = response.block();
|
||||
|
||||
|
@ -314,7 +314,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to get the state with the etag
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the get state
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -355,7 +355,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
saveResponse.block();
|
||||
|
||||
//Create deferred action to get the state with the etag
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<MyData>(stateKey, null, null),
|
||||
Mono<State<MyData>> response = daprClient.getState(STATE_STORE_NAME, new State<>(stateKey, (MyData) null, null),
|
||||
MyData.class);
|
||||
//execute the get state
|
||||
State<MyData> myDataResponse = response.block();
|
||||
|
@ -602,7 +602,7 @@ public abstract class AbstractStateClientIT extends BaseIT {
|
|||
}
|
||||
|
||||
private <T> State<T> createState(String stateKey, String etag, StateOptions options, T data) {
|
||||
return new State<>(data, stateKey, etag, options);
|
||||
return new State<>(stateKey, data, etag, options);
|
||||
}
|
||||
|
||||
protected abstract DaprClient buildDaprClient();
|
||||
|
|
|
@ -354,7 +354,7 @@ abstract class AbstractDaprClient implements DaprClient {
|
|||
*/
|
||||
@Override
|
||||
public Mono<Void> saveState(String storeName, String key, String etag, Object value, StateOptions options) {
|
||||
State<?> state = new State<>(value, key, etag, options);
|
||||
State<?> state = new State<>(key, value, etag, options);
|
||||
return this.saveBulkState(storeName, Collections.singletonList(state));
|
||||
}
|
||||
|
||||
|
|
|
@ -350,7 +350,7 @@ public class DaprClientGrpc extends AbstractDaprClient {
|
|||
if (etag.equals("")) {
|
||||
etag = null;
|
||||
}
|
||||
return new State<>(value, key, etag, item.getMetadataMap(), null);
|
||||
return new State<>(key, value, etag, item.getMetadataMap(), null);
|
||||
}
|
||||
|
||||
private <T> State<T> buildStateKeyValue(
|
||||
|
@ -365,7 +365,7 @@ public class DaprClientGrpc extends AbstractDaprClient {
|
|||
if (etag.equals("")) {
|
||||
etag = null;
|
||||
}
|
||||
return new State<>(value, requestedKey, etag, response.getMetadataMap(), stateOptions);
|
||||
return new State<>(requestedKey, value, etag, response.getMetadataMap(), stateOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -391,7 +391,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
byte[] data = this.stateSerializer.serialize(state.getValue());
|
||||
// Custom serializer, so everything is byte[].
|
||||
operations.add(new TransactionalStateOperation<>(operation.getOperation(),
|
||||
new State<>(data, state.getKey(), state.getEtag(), state.getMetadata(), state.getOptions())));
|
||||
new State<>(state.getKey(), data, state.getEtag(), state.getMetadata(), state.getOptions())));
|
||||
}
|
||||
TransactionalStateRequest<Object> req = new TransactionalStateRequest<>(internalOperationObjects, metadata);
|
||||
byte[] serializedOperationBody = INTERNAL_SERIALIZER.serialize(req);
|
||||
|
@ -438,7 +438,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
|
||||
byte[] data = this.stateSerializer.serialize(state.getValue());
|
||||
// Custom serializer, so everything is byte[].
|
||||
internalStateObjects.add(new State<>(data, state.getKey(), state.getEtag(), state.getMetadata(),
|
||||
internalStateObjects.add(new State<>(state.getKey(), data, state.getEtag(), state.getMetadata(),
|
||||
state.getOptions()));
|
||||
}
|
||||
byte[] serializedStateBody = INTERNAL_SERIALIZER.serialize(internalStateObjects);
|
||||
|
@ -513,7 +513,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
if (response.getHeaders() != null && response.getHeaders().containsKey("Etag")) {
|
||||
etag = response.getHeaders().get("Etag");
|
||||
}
|
||||
return new State<>(value, requestedKey, etag, stateOptions);
|
||||
return new State<>(requestedKey, value, etag, stateOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -546,7 +546,7 @@ public class DaprClientHttp extends AbstractDaprClient {
|
|||
// This is not a high priority since GRPC is the default (and recommended) client implementation.
|
||||
byte[] data = node.path("data").toString().getBytes(Properties.STRING_CHARSET.get());
|
||||
T value = stateSerializer.deserialize(data, type);
|
||||
result.add(new State<>(value, key, etag));
|
||||
result.add(new State<>(key, value, etag));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -81,13 +81,12 @@ public class State<T> {
|
|||
/**
|
||||
* Create an immutable state.
|
||||
* This Constructor CAN be used anytime you want the state to be saved.
|
||||
*
|
||||
* @param value - The value of the state.
|
||||
* @param key - The key of the state.
|
||||
* @param value - The value of the state.
|
||||
* @param etag - The etag of the state - for some state stores (like redis) only numbers are supported.
|
||||
* @param options - REQUIRED when saving a state.
|
||||
*/
|
||||
public State(T value, String key, String etag, StateOptions options) {
|
||||
public State(String key, T value, String etag, StateOptions options) {
|
||||
this.value = value;
|
||||
this.key = key;
|
||||
this.etag = etag;
|
||||
|
@ -99,14 +98,13 @@ public class State<T> {
|
|||
/**
|
||||
* Create an immutable state.
|
||||
* This Constructor CAN be used anytime you want the state to be saved.
|
||||
*
|
||||
* @param value - The value of the state.
|
||||
* @param key - The key of the state.
|
||||
* @param value - The value of the state.
|
||||
* @param etag - The etag of the state - for some state stores (like redis) only numbers are supported.
|
||||
* @param metadata - The metadata of the state.
|
||||
* @param options - REQUIRED when saving a state.
|
||||
*/
|
||||
public State(T value, String key, String etag, Map<String, String> metadata, StateOptions options) {
|
||||
public State(String key, T value, String etag, Map<String, String> metadata, StateOptions options) {
|
||||
this.value = value;
|
||||
this.key = key;
|
||||
this.etag = etag;
|
||||
|
@ -118,12 +116,11 @@ public class State<T> {
|
|||
/**
|
||||
* Create an immutable state.
|
||||
* This Constructor CAN be used anytime you want the state to be saved.
|
||||
*
|
||||
* @param value - The value of the state.
|
||||
* @param key - The key of the state.
|
||||
* @param value - The value of the state.
|
||||
* @param etag - The etag of the state - some state stores (like redis) only numbers are supported.
|
||||
*/
|
||||
public State(T value, String key, String etag) {
|
||||
public State(String key, T value, String etag) {
|
||||
this.value = value;
|
||||
this.key = key;
|
||||
this.etag = etag;
|
||||
|
@ -255,8 +252,8 @@ public class State<T> {
|
|||
@Override
|
||||
public String toString() {
|
||||
return "StateKeyValue{"
|
||||
+ "value=" + value
|
||||
+ ", key='" + key + "'"
|
||||
+ "key='" + key + "'"
|
||||
+ ", value=" + value
|
||||
+ ", etag='" + etag + "'"
|
||||
+ ", metadata={'" + (metadata != null ? metadata.toString() : null) + "'}"
|
||||
+ ", error='" + error + "'"
|
||||
|
|
|
@ -17,8 +17,6 @@ import io.dapr.client.domain.GetBulkStateRequestBuilder;
|
|||
import io.dapr.client.domain.GetStateRequest;
|
||||
import io.dapr.client.domain.GetStateRequestBuilder;
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
import io.dapr.client.domain.InvokeMethodRequest;
|
||||
import io.dapr.client.domain.InvokeMethodRequestBuilder;
|
||||
import io.dapr.client.domain.Response;
|
||||
import io.dapr.client.domain.State;
|
||||
import io.dapr.client.domain.StateOptions;
|
||||
|
@ -40,7 +38,6 @@ import org.mockito.ArgumentCaptor;
|
|||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.util.context.Context;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
@ -1556,8 +1553,8 @@ public class DaprClientGrpcTest {
|
|||
@Test
|
||||
public void saveBulkStateTestNullEtag() {
|
||||
List<State<?>> states = new ArrayList<State<?>>();
|
||||
states.add(new State<String>("null_etag_value", "null_etag_key", null, (StateOptions)null));
|
||||
states.add(new State<String>("empty_etag_value", "empty_etag_key", "", (StateOptions)null));
|
||||
states.add(new State<String>("null_etag_key", "null_etag_value", null, (StateOptions)null));
|
||||
states.add(new State<String>("empty_etag_key", "empty_etag_value", "", (StateOptions)null));
|
||||
|
||||
ArgumentCaptor<DaprProtos.SaveStateRequest> argument = ArgumentCaptor.forClass(DaprProtos.SaveStateRequest.class);
|
||||
doAnswer((Answer<Void>) invocation -> {
|
||||
|
@ -1741,11 +1738,11 @@ public class DaprClientGrpcTest {
|
|||
}
|
||||
|
||||
private <T> State<T> buildStateKey(T value, String key, String etag, StateOptions options) {
|
||||
return new State<>(value, key, etag, options);
|
||||
return new State<>(key, value, etag, options);
|
||||
}
|
||||
|
||||
private <T> State<T> buildStateKey(T value, String key, String etag, Map<String, String> metadata, StateOptions options) {
|
||||
return new State<>(value, key, etag, metadata, options);
|
||||
return new State<>(key, value, etag, metadata, options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -657,10 +657,10 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void getState() {
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
State<String> stateKeyValue = new State<>("value", "key", "etag", stateOptions);
|
||||
State<String> stateKeyNull = new State<>("value", null, "etag", stateOptions);
|
||||
State<String> stateKeyEmpty = new State<>("value", "", "etag", stateOptions);
|
||||
State<String> stateKeyBadPayload = new State<>("value", "keyBadPayload", "etag", stateOptions);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "etag", stateOptions);
|
||||
State<String> stateKeyNull = new State<>(null, "value", "etag", stateOptions);
|
||||
State<String> stateKeyEmpty = new State<>("", "value", "etag", stateOptions);
|
||||
State<String> stateKeyBadPayload = new State<>("keyBadPayload", "value", "etag", stateOptions);
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
|
@ -691,7 +691,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void getStatesEmptyEtag() {
|
||||
State<String> stateEmptyEtag = new State<>("value", "key", "", null);
|
||||
State<String> stateEmptyEtag = new State<>("key", "value", "", null);
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
|
@ -717,7 +717,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void getStatesNullEtag() {
|
||||
State<String> stateNullEtag = new State<>("value", "key", null, null);
|
||||
State<String> stateNullEtag = new State<>("key", "value", null, null);
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond("\"" + EXPECTED_RESULT + "\"");
|
||||
|
@ -729,7 +729,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void getStatesNoHotMono() {
|
||||
State<String> stateNullEtag = new State<>("value", "key", null, null);
|
||||
State<String> stateNullEtag = new State<>("key", "value", null, null);
|
||||
mockInterceptor.addRule()
|
||||
.get("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(500);
|
||||
|
@ -740,7 +740,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void saveStates() {
|
||||
State<String> stateKeyValue = new State<>("value", "key", "etag", null);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "etag", null);
|
||||
List<State<?>> stateKeyValueList = Collections.singletonList(stateKeyValue);
|
||||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
|
@ -783,7 +783,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void saveStatesEtagNull() {
|
||||
State<String> stateKeyValue = new State<>("value", "key", null, null);
|
||||
State<String> stateKeyValue = new State<>("key", "value", null, null);
|
||||
List<State<?>> stateKeyValueList = Collections.singletonList(stateKeyValue);
|
||||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
|
@ -795,7 +795,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void saveStatesEtagEmpty() {
|
||||
State<String> stateKeyValue = new State<>("value", "key", "", null);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "", null);
|
||||
List<State<?>> stateKeyValueList = Collections.singletonList(stateKeyValue);
|
||||
mockInterceptor.addRule()
|
||||
.post("http://127.0.0.1:3000/v1.0/state/MyStateStore")
|
||||
|
@ -838,7 +838,7 @@ public class DaprClientHttpTest {
|
|||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
State<String> stateKey = new State<>(key, data, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
TransactionalStateOperation.OperationType.UPSERT,
|
||||
stateKey);
|
||||
|
@ -861,7 +861,7 @@ public class DaprClientHttpTest {
|
|||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
State<String> stateKey = new State<>(key, data, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
TransactionalStateOperation.OperationType.UPSERT,
|
||||
stateKey);
|
||||
|
@ -884,7 +884,7 @@ public class DaprClientHttpTest {
|
|||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
State<String> stateKey = new State<>(key, data, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
TransactionalStateOperation.OperationType.UPSERT,
|
||||
stateKey);
|
||||
|
@ -907,7 +907,7 @@ public class DaprClientHttpTest {
|
|||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
|
||||
|
||||
State<String> stateKey = new State<>(data, key, etag, stateOptions);
|
||||
State<String> stateKey = new State<>(key, data, etag, stateOptions);
|
||||
TransactionalStateOperation<String> upsertOperation = new TransactionalStateOperation<>(
|
||||
TransactionalStateOperation.OperationType.UPSERT,
|
||||
stateKey);
|
||||
|
@ -949,7 +949,7 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void deleteState() {
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
State<String> stateKeyValue = new State<>("value", "key", "etag", stateOptions);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "etag", stateOptions);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
|
@ -963,7 +963,7 @@ public class DaprClientHttpTest {
|
|||
Map<String, String> metadata = new HashMap<>();
|
||||
metadata.put("key_1", "val_1");
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
State<String> stateKeyValue = new State<>("value", "key", "etag", stateOptions);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "etag", stateOptions);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key?metadata.key_1=val_1")
|
||||
.respond(EXPECTED_RESULT);
|
||||
|
@ -977,7 +977,7 @@ public class DaprClientHttpTest {
|
|||
@Test
|
||||
public void deleteStateNoHotMono() {
|
||||
StateOptions stateOptions = mock(StateOptions.class);
|
||||
State<String> stateKeyValue = new State<>("value", "key", "etag", stateOptions);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "etag", stateOptions);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(500);
|
||||
|
@ -988,7 +988,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void deleteStateNullEtag() {
|
||||
State<String> stateKeyValue = new State<>("value", "key", null, null);
|
||||
State<String> stateKeyValue = new State<>("key", "value", null, null);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
|
@ -999,7 +999,7 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void deleteStateEmptyEtag() {
|
||||
State<String> stateKeyValue = new State<>("value", "key", "", null);
|
||||
State<String> stateKeyValue = new State<>("key", "value", "", null);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
|
@ -1010,8 +1010,8 @@ public class DaprClientHttpTest {
|
|||
|
||||
@Test
|
||||
public void deleteStateIllegalArgumentException() {
|
||||
State<String> stateKeyValueNull = new State<>("value", null, "etag", null);
|
||||
State<String> stateKeyValueEmpty = new State<>("value", "", "etag", null);
|
||||
State<String> stateKeyValueNull = new State<>(null, "value", "etag", null);
|
||||
State<String> stateKeyValueEmpty = new State<>("", "value", "etag", null);
|
||||
mockInterceptor.addRule()
|
||||
.delete("http://127.0.0.1:3000/v1.0/state/MyStateStore/key")
|
||||
.respond(EXPECTED_RESULT);
|
||||
|
|
|
@ -26,7 +26,7 @@ public class StateTest {
|
|||
|
||||
@Test
|
||||
public void testSimpleStringState() {
|
||||
State<String> state = new State<>("value", KEY, ETAG);
|
||||
State<String> state = new State<>(KEY, "value", ETAG);
|
||||
assertNotNull(state);
|
||||
assertNull(state.getError());
|
||||
assertNull(state.getOptions());
|
||||
|
@ -34,8 +34,8 @@ public class StateTest {
|
|||
assertEquals(ETAG, state.getEtag());
|
||||
assertEquals("value", state.getValue());
|
||||
String expected = "StateKeyValue{"
|
||||
+ "value=value"
|
||||
+ ", key='" + KEY + "'"
|
||||
+ "key='" + KEY + "'"
|
||||
+ ", value=value"
|
||||
+ ", etag='" + ETAG + "'"
|
||||
+ ", metadata={'null'}"
|
||||
+ ", error='null'"
|
||||
|
@ -54,8 +54,8 @@ public class StateTest {
|
|||
assertEquals(KEY, state.getKey());
|
||||
assertEquals(ETAG, state.getEtag());
|
||||
String expected = "StateKeyValue{"
|
||||
+ "value=null"
|
||||
+ ", key='" + KEY + "'"
|
||||
+ "key='" + KEY + "'"
|
||||
+ ", value=null"
|
||||
+ ", etag='" + ETAG + "'"
|
||||
+ ", metadata={'null'}"
|
||||
+ ", error='null'"
|
||||
|
@ -66,15 +66,15 @@ public class StateTest {
|
|||
|
||||
@Test
|
||||
public void testSimpleStringStateWithOptions() {
|
||||
State<String> state = new State<>("value", KEY, ETAG, OPTIONS);
|
||||
State<String> state = new State<>(KEY, "value", ETAG, OPTIONS);
|
||||
assertNotNull(state);
|
||||
assertNull(state.getError());
|
||||
assertEquals(OPTIONS, state.getOptions());
|
||||
assertEquals(KEY, state.getKey());
|
||||
assertEquals(ETAG, state.getEtag());
|
||||
String expected = "StateKeyValue{"
|
||||
+ "value=value"
|
||||
+ ", key='" + KEY + "'"
|
||||
+ "key='" + KEY + "'"
|
||||
+ ", value=value"
|
||||
+ ", etag='" + ETAG + "'"
|
||||
+ ", metadata={'null'}"
|
||||
+ ", error='null'"
|
||||
|
@ -86,22 +86,22 @@ public class StateTest {
|
|||
|
||||
@Test
|
||||
public void testEqualsAndHashcode() {
|
||||
State<String> state1 = new State<>("value", KEY, ETAG, new HashMap<>(METADATA), OPTIONS);
|
||||
State<String> state2 = new State<>("value", KEY, ETAG, new HashMap<>(METADATA), OPTIONS);
|
||||
State<String> state1 = new State<>(KEY, "value", ETAG, new HashMap<>(METADATA), OPTIONS);
|
||||
State<String> state2 = new State<>(KEY, "value", ETAG, new HashMap<>(METADATA), OPTIONS);
|
||||
assertEquals(state1.toString(), state2.toString());
|
||||
assertEquals(state1.hashCode(), state2.hashCode());
|
||||
assertEquals(state1, state2);
|
||||
|
||||
Map<String, String> metadata3 = new HashMap<>(METADATA);
|
||||
metadata3.put("key5", "value5");
|
||||
State<String> state3 = new State<>("value", KEY, ETAG, metadata3, OPTIONS);
|
||||
State<String> state3 = new State<>(KEY, "value", ETAG, metadata3, OPTIONS);
|
||||
assertNotEquals(state1.toString(), state3.toString());
|
||||
assertNotEquals(state1.hashCode(), state3.hashCode());
|
||||
assertNotEquals(state1, state3);
|
||||
|
||||
Map<String, String> metadata4 = new HashMap<>(METADATA);
|
||||
metadata4.remove("key1");
|
||||
State<String> state4 = new State<>("value", KEY, ETAG, metadata4, OPTIONS);
|
||||
State<String> state4 = new State<>(KEY, "value", ETAG, metadata4, OPTIONS);
|
||||
assertNotEquals(state1.toString(), state4.toString());
|
||||
assertNotEquals(state1.hashCode(), state4.hashCode());
|
||||
assertNotEquals(state1, state4);
|
||||
|
|
Loading…
Reference in New Issue