Skip to content

Commit

Permalink
chore: include set value to Set Response (#101)
Browse files Browse the repository at this point in the history
include set value to Set Response
  • Loading branch information
poppoerika authored Jan 18, 2022
1 parent 4f15ad3 commit 89383db
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
1 change: 1 addition & 0 deletions momento-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ val jwtVersion = rootProject.ext["jwtVersion"]

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
testImplementation("commons-io:commons-io:2.11.0")

platform("io.opentelemetry:opentelemetry-bom:$opentelemetryVersion")
implementation("io.opentelemetry:opentelemetry-api:$opentelemetryVersion")
Expand Down
28 changes: 28 additions & 0 deletions momento-sdk/src/intTest/java/momento/sdk/ScsDataTestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package momento.sdk;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import momento.sdk.messages.CacheSetResponse;
import momento.sdk.messages.MomentoCacheResult;
import org.apache.commons.io.IOUtils;

final class ScsDataTestHelper {
private ScsDataTestHelper() {}

static void assertSetResponse(String expectedValue, CacheSetResponse setResponse)
throws IOException {
assertEquals(MomentoCacheResult.Ok, setResponse.result());
assertEquals(expectedValue, setResponse.string().get());
assertArrayEquals(expectedValue.getBytes(), setResponse.byteArray().get());
assertEquals(ByteBuffer.wrap(expectedValue.getBytes()), setResponse.byteBuffer().get());
assertTrue(
IOUtils.contentEquals(
IOUtils.toInputStream(expectedValue, StandardCharsets.UTF_8),
setResponse.inputStream().get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static momento.sdk.OtelTestHelpers.stopIntegrationTestOtel;
import static momento.sdk.OtelTestHelpers.verifyGetTrace;
import static momento.sdk.OtelTestHelpers.verifySetTrace;
import static momento.sdk.ScsDataTestHelper.assertSetResponse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -132,7 +133,7 @@ private void runSetAndGetWithHitTest(SimpleCacheClient target) throws Exception

// Successful Set
CompletableFuture<CacheSetResponse> setResponse = target.setAsync(cacheName, key, value);
assertEquals(MomentoCacheResult.Ok, setResponse.get().result());
assertSetResponse(value, setResponse.get());

// Successful Get with Hit
CompletableFuture<CacheGetResponse> getResponse = target.getAsync(cacheName, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import static momento.sdk.OtelTestHelpers.stopIntegrationTestOtel;
import static momento.sdk.OtelTestHelpers.verifyGetTrace;
import static momento.sdk.OtelTestHelpers.verifySetTrace;
import static momento.sdk.ScsDataTestHelper.assertSetResponse;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.opentelemetry.sdk.OpenTelemetrySdk;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -42,7 +44,7 @@ void tearDown() throws Exception {
}

@Test
void getReturnsHitAfterSet() {
void getReturnsHitAfterSet() throws IOException {
runSetAndGetWithHitTest(SimpleCacheClient.builder(authToken, DEFAULT_ITEM_TTL_SECONDS).build());
}

Expand Down Expand Up @@ -132,13 +134,13 @@ public void setAndGetWithByteKeyValuesMustSucceed() {
assertArrayEquals(value, getResponse.byteArray().get());
}

private void runSetAndGetWithHitTest(SimpleCacheClient target) {
private void runSetAndGetWithHitTest(SimpleCacheClient target) throws IOException {
String key = UUID.randomUUID().toString();
String value = UUID.randomUUID().toString();

// Successful Set
CacheSetResponse setResponse = target.set(cacheName, key, value);
assertEquals(MomentoCacheResult.Ok, setResponse.result());
assertSetResponse(value, setResponse);

// Successful Get with Hit
CacheGetResponse getResponse = target.get(cacheName, key);
Expand Down
2 changes: 1 addition & 1 deletion momento-sdk/src/main/java/momento/sdk/ScsDataClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public boolean cancel(boolean mayInterruptIfRunning) {
new FutureCallback<SetResponse>() {
@Override
public void onSuccess(SetResponse rsp) {
returnFuture.complete(new CacheSetResponse(rsp.getResult()));
returnFuture.complete(new CacheSetResponse(rsp.getResult(), value));
span.ifPresent(
theSpan -> {
theSpan.setStatus(StatusCode.OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package momento.sdk.messages;

import com.google.protobuf.ByteString;
import grpc.cache_client.ECacheResult;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

/** Result of the set operation on Cache. */
public final class CacheSetResponse extends BaseResponse {
private final ECacheResult result;
private final ByteString value;

public CacheSetResponse(ECacheResult result) {
public CacheSetResponse(ECacheResult result, ByteString value) {
this.result = result;
this.value = value;
}

/**
Expand All @@ -19,4 +27,50 @@ public CacheSetResponse(ECacheResult result) {
public MomentoCacheResult result() {
return this.resultMapper(this.result);
}

/**
* Value set in the cache as a byte array.
*
* @return Value set for the given key.
*/
public Optional<byte[]> byteArray() {
return Optional.ofNullable(value.toByteArray());
}

/**
* Value set in the cache as a {@link ByteBuffer}.
*
* @return Value set for the given key.
*/
public Optional<ByteBuffer> byteBuffer() {
return Optional.ofNullable(value.asReadOnlyByteBuffer());
}

/**
* Value set in the cache as a UTF-8 {@link String}
*
* @return Value set for the given key.
*/
public Optional<String> string() {
return string(StandardCharsets.UTF_8);
}

/**
* Value set in the cache as {@link String}.
*
* @param charset to express the bytes as String.
* @return Value set for the given key.
*/
public Optional<String> string(Charset charset) {
return Optional.ofNullable(value.toString(charset));
}

/**
* Value as an {@link InputStream}
*
* @return Value set for the given key.
*/
public Optional<InputStream> inputStream() {
return Optional.ofNullable(value.newInput());
}
}

0 comments on commit 89383db

Please sign in to comment.