Skip to content

Commit

Permalink
Update javadoc; add option tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Mar 22, 2024
1 parent 00f7d19 commit 46f1613
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 52 deletions.
2 changes: 1 addition & 1 deletion java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public CompletableFuture<String> xadd(@NonNull String key, Map<String, String> v

@Override
public CompletableFuture<String> xadd(
@NonNull String key, Map<String, String> values, StreamAddOptions options) {
@NonNull String key, @NonNull Map<String, String> values, @NonNull StreamAddOptions options) {
String[] arguments =
ArrayUtils.addAll(
ArrayUtils.addFirst(options.toArgs(), key),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ public interface StreamBaseCommands {
* @see <a href="https://redis.io/commands/xadd/">redis.io</a> for details.
* @param key The key of the stream.
* @param values field-value pairs to be added to the entry.
* @param options options.
* @param options Stream add options.
* @return The id of the added entry, or <code>null</code> if <code>options.makeStream</code> is
* set to <code>false</code> and no stream with the matching <code>key</code> exists.
* @example
* <pre>{@code
* String streamId = client.xadd("key", Map.of("name", "Sara", "surname", "OConnor").get();
* System.out.println("Stream: " + streamId);
* // Stream options to not make the stream if "key" is not a stream, as use stream id of "sid"
* StreamAddOptions options = StreamAddOptions.builder().id("sid").makeStream(Boolean.FALSE).build();
* String streamId = client.xadd("key", Map.of("name", "Sara", "surname", "OConnor"), options).get();
* if (streamId != null) {
* assert streamId.equals("sid");
* }
* }</pre>
*/
CompletableFuture<String> xadd(String key, Map<String, String> values, StreamAddOptions options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ public T xadd(String key, Map<String, String> values) {
* @see <a href="https://redis.io/commands/xadd/">redis.io</a> for details.
* @param key The key of the stream.
* @param values field-value pairs to be added to the entry.
* @param options options.
* @param options Stream add options.
* @return Command Response - The id of the added entry, or <code>null</code> if <code>
* options.makeStream</code> is set to <code>false</code> and no stream with the matching
* <code>key</code> exists.
Expand Down
54 changes: 7 additions & 47 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1680,53 +1680,13 @@ public void xadd_returns_success() {
assertEquals(returnId, payload);
}

@SneakyThrows
@Test
public void xadd_with_nomakestream_maxlen_options_returns_success() {
// setup
String key = "testKey";
Map<String, String> fieldValues = new LinkedHashMap<>();
fieldValues.put("testField1", "testValue1");
fieldValues.put("testField2", "testValue2");
StreamAddOptions options =
StreamAddOptions.builder()
.id("id")
.makeStream(false)
.trim(new StreamAddOptions.Maxlen(true, 5L))
.build();

String[] arguments =
new String[] {
key,
NO_MAKE_STREAM_REDIS_API,
TRIM_MAXLEN_REDIS_API,
TRIM_EXACT_REDIS_API,
Long.toString(5L),
"id"
};
arguments = ArrayUtils.addAll(arguments, convertMapToKeyValueStringArray(fieldValues));

String returnId = "testId";

CompletableFuture<String> testResponse = mock(CompletableFuture.class);
when(testResponse.get()).thenReturn(returnId);

// match on protobuf request
when(commandManager.<String>submitNewCommand(eq(XAdd), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.xadd(key, fieldValues, options);
String payload = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(returnId, payload);
}

private static List<Arguments> getStreamAddOptions() {
return List.of(
Arguments.of(
Pair.of(
// no TRIM option
StreamAddOptions.builder().id("id").makeStream(Boolean.FALSE).build(),
new String[] {"testKey", NO_MAKE_STREAM_REDIS_API, "id"}),
Pair.of(
// MAXLEN with LIMIT
StreamAddOptions.builder()
Expand Down Expand Up @@ -1792,14 +1752,14 @@ private static List<Arguments> getStreamAddOptions() {
@SneakyThrows
@ParameterizedTest
@MethodSource("getStreamAddOptions")
public void xadd_with_options_returns_success(Pair<StreamAddOptions, String[]> input) {
public void xadd_with_options_returns_success(Pair<StreamAddOptions, String[]> optionAndArgs) {
// setup
String key = "testKey";
Map<String, String> fieldValues = new LinkedHashMap<>();
fieldValues.put("testField1", "testValue1");
fieldValues.put("testField2", "testValue2");
String[] arguments =
ArrayUtils.addAll(input.getRight(), convertMapToKeyValueStringArray(fieldValues));
ArrayUtils.addAll(optionAndArgs.getRight(), convertMapToKeyValueStringArray(fieldValues));
String returnId = "testId";

CompletableFuture<String> testResponse = mock(CompletableFuture.class);
Expand All @@ -1810,7 +1770,7 @@ public void xadd_with_options_returns_success(Pair<StreamAddOptions, String[]> i
.thenReturn(testResponse);

// exercise
CompletableFuture<String> response = service.xadd(key, fieldValues, input.getLeft());
CompletableFuture<String> response = service.xadd(key, fieldValues, optionAndArgs.getLeft());
String payload = response.get();

// verify
Expand Down

0 comments on commit 46f1613

Please sign in to comment.