Skip to content

Commit 6e9ee5a

Browse files
adding new method signature plus test (#1570)
* adding new method signature plus test Signed-off-by: salaboy <[email protected]> * re adding imports Signed-off-by: salaboy <[email protected]> * fixing style Signed-off-by: salaboy <[email protected]> * checking empty metadata Signed-off-by: salaboy <[email protected]> * copy meta for safety and check if key is present Signed-off-by: salaboy <[email protected]> * Centralize Maven dependency version management (#1564) Signed-off-by: salaboy <[email protected]> * Fix dependencies multi app build and add proper test deps (#1572) * Force Jackson version to override the SB Jackson version Signed-off-by: Artur Ciocanu <[email protected]> * Move all the Jackson deps to parent POM. Signed-off-by: Artur Ciocanu <[email protected]> * Ensure app JAR build order Signed-off-by: Artur Ciocanu <[email protected]> * Remove explicit Jackson from sdk-tests module. Signed-off-by: Artur Ciocanu <[email protected]> * Make sure <scope>test</scope> is used for test dependencies. Signed-off-by: Artur Ciocanu <[email protected]> * Remove extra Jackson modules. Signed-off-by: Artur Ciocanu <[email protected]> --------- Signed-off-by: Artur Ciocanu <[email protected]> Signed-off-by: salaboy <[email protected]> * reverting pom Signed-off-by: salaboy <[email protected]> * fix codestyle Signed-off-by: salaboy <[email protected]> * using metaCopy Signed-off-by: salaboy <[email protected]> --------- Signed-off-by: salaboy <[email protected]> Signed-off-by: Artur Ciocanu <[email protected]> Co-authored-by: artur-ciocanu <[email protected]>
1 parent 3d950b0 commit 6e9ee5a

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

sdk/src/main/java/io/dapr/client/AbstractDaprClient.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.ArrayList;
5252
import java.util.Arrays;
5353
import java.util.Collections;
54+
import java.util.HashMap;
5455
import java.util.List;
5556
import java.util.Map;
5657
import java.util.stream.Collectors;
@@ -509,6 +510,27 @@ public Mono<Void> saveState(String storeName, String key, String etag, Object va
509510
return this.saveBulkState(storeName, Collections.singletonList(state));
510511
}
511512

513+
/**
514+
* {@inheritDoc}
515+
*/
516+
@Override
517+
public Mono<Void> saveState(String storeName, String key, String etag, Object value, Map<String, String> meta,
518+
StateOptions options) {
519+
Map<String, String> metaCopy = null;
520+
if (meta == null) {
521+
metaCopy = new HashMap<>();
522+
} else {
523+
metaCopy = new HashMap<>(meta);
524+
}
525+
526+
if (value != null) {
527+
metaCopy.putIfAbsent("contentType", stateSerializer.getContentType());
528+
}
529+
530+
State<?> state = new State<>(key, value, etag, metaCopy, options);
531+
return this.saveBulkState(storeName, Collections.singletonList(state));
532+
}
533+
512534
/**
513535
* {@inheritDoc}
514536
*/

sdk/src/main/java/io/dapr/client/DaprClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,21 @@ Mono<Void> executeStateTransaction(String storeName,
498498
*/
499499
Mono<Void> saveState(String storeName, String key, String etag, Object value, StateOptions options);
500500

501+
502+
/**
503+
* Save/Update a state.
504+
*
505+
* @param storeName The name of the state store.
506+
* @param key The key of the state.
507+
* @param etag The etag to be used.
508+
* @param value The value of the state.
509+
* @param meta The metadata to be set to the state.
510+
* @param options The Options to use for each state.
511+
* @return a Mono plan of type Void.
512+
*/
513+
Mono<Void> saveState(String storeName, String key, String etag, Object value, Map<String, String> meta,
514+
StateOptions options);
515+
501516
/**
502517
* Delete a state.
503518
*

sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,64 @@ public void saveStateNoOptionsTest() {
12411241
result.block();
12421242
}
12431243

1244+
@Test
1245+
public void saveStateWithMetaTest() {
1246+
String key = "key1";
1247+
String etag = "ETag1";
1248+
String value = "State value";
1249+
Map<String, String> metadata = new HashMap<>();
1250+
metadata.put("custom", "customValue");
1251+
ArgumentCaptor<DaprProtos.SaveStateRequest> argument = ArgumentCaptor.forClass(DaprProtos.SaveStateRequest.class);
1252+
doAnswer((Answer<Void>) invocation -> {
1253+
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
1254+
observer.onNext(Empty.getDefaultInstance());
1255+
observer.onCompleted();
1256+
return null;
1257+
}).when(daprStub).saveState(argument.capture(), any());
1258+
1259+
1260+
Mono<Void> result = client.saveState(STATE_STORE_NAME, key, etag, value, metadata,null);
1261+
result.block();
1262+
assertEquals("customValue", argument.getValue().getStates(0).getMetadata().get("custom"));
1263+
}
1264+
1265+
@Test
1266+
public void saveStateWithMetaContentTypeTest() {
1267+
String key = "key1";
1268+
String etag = "ETag1";
1269+
String value = "State value";
1270+
Map<String, String> metadata = new HashMap<>();
1271+
ArgumentCaptor<DaprProtos.SaveStateRequest> argument = ArgumentCaptor.forClass(DaprProtos.SaveStateRequest.class);
1272+
doAnswer((Answer<Void>) invocation -> {
1273+
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
1274+
observer.onNext(Empty.getDefaultInstance());
1275+
observer.onCompleted();
1276+
return null;
1277+
}).when(daprStub).saveState(argument.capture(), any());
1278+
1279+
1280+
Mono<Void> result = client.saveState(STATE_STORE_NAME, key, etag, value, metadata,null);
1281+
result.block();
1282+
assertEquals("application/json", argument.getValue().getStates(0).getMetadata().get("contentType"));
1283+
}
1284+
1285+
@Test
1286+
public void saveStateWithMetaEmptyTest() {
1287+
String key = "key1";
1288+
String etag = "ETag1";
1289+
ArgumentCaptor<DaprProtos.SaveStateRequest> argument = ArgumentCaptor.forClass(DaprProtos.SaveStateRequest.class);
1290+
doAnswer((Answer<Void>) invocation -> {
1291+
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
1292+
observer.onNext(Empty.getDefaultInstance());
1293+
observer.onCompleted();
1294+
return null;
1295+
}).when(daprStub).saveState(argument.capture(), any());
1296+
1297+
Mono<Void> result = client.saveState(STATE_STORE_NAME, key, etag, null, null,null);
1298+
result.block();
1299+
assertTrue(argument.getValue().getStates(0).getMetadata().keySet().isEmpty());
1300+
}
1301+
12441302
@Test
12451303
public void saveStateTest() {
12461304
String key = "key1";

0 commit comments

Comments
 (0)