Skip to content

Commit

Permalink
feat: Change helper method names to get Value (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamomento authored Oct 13, 2021
1 parent f93071f commit 5317a8d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
18 changes: 9 additions & 9 deletions momento-sdk/src/intTest/java/momento/sdk/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static void testHappyPath(Cache cache) {
// Get Key that was just set
CacheGetResponse rsp = cache.get(key);
assertEquals(MomentoCacheResult.Hit, rsp.result());
assertEquals("bar", rsp.asStringUtf8().get());
assertEquals("bar", rsp.string().get());
}

@Test
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void testAsyncHappyPath(Cache client) throws Exception {
CacheGetResponse rsp = client.getAsync(key).get();

assertEquals(MomentoCacheResult.Hit, rsp.result());
assertEquals("bar", rsp.asStringUtf8().get());
assertEquals("bar", rsp.string().get());
}

@Test
Expand Down Expand Up @@ -163,7 +163,7 @@ private static void testTtlHappyPath(Cache client) throws Exception {
// Get Key that was just set
CacheGetResponse rsp = client.get(key);
assertEquals(MomentoCacheResult.Miss, rsp.result());
assertFalse(rsp.asInputStream().isPresent());
assertFalse(rsp.inputStream().isPresent());
}

@Test
Expand All @@ -188,11 +188,11 @@ private static void testMissHappyPathInternal(Cache client) {
CacheGetResponse rsp = client.get(UUID.randomUUID().toString());

assertEquals(MomentoCacheResult.Miss, rsp.result());
assertFalse(rsp.asInputStream().isPresent());
assertFalse(rsp.asByteArray().isPresent());
assertFalse(rsp.asByteBuffer().isPresent());
assertFalse(rsp.asStringUtf8().isPresent());
assertFalse(rsp.asString(Charset.defaultCharset()).isPresent());
assertFalse(rsp.inputStream().isPresent());
assertFalse(rsp.byteArray().isPresent());
assertFalse(rsp.byteBuffer().isPresent());
assertFalse(rsp.string().isPresent());
assertFalse(rsp.string(Charset.defaultCharset()).isPresent());
}

@Test
Expand Down Expand Up @@ -231,7 +231,7 @@ public void setAndGetWithByteKeyValuesMustSucceed() {

CacheGetResponse getResponse = cache.get(key);
assertEquals(getResponse.result(), MomentoCacheResult.Hit);
assertArrayEquals(value, getResponse.asByteArray().get());
assertArrayEquals(value, getResponse.byteArray().get());
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion momento-sdk/src/intTest/java/momento/sdk/MomentoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ private static void runHappyPathTest(Momento momento, String cacheName) {
// Get Key that was just set
CacheGetResponse rsp = cache.get(key);
assertEquals(MomentoCacheResult.Hit, rsp.result());
assertEquals("bar", rsp.asStringUtf8().get());
assertEquals("bar", rsp.string().get());
}
}
1 change: 1 addition & 0 deletions momento-sdk/src/main/java/momento/sdk/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public final class Cache implements Closeable {
waitTillReady();
}

// TODO: Temporary measure for beta. This will not be required soon.
private void waitTillReady() {
long start = System.currentTimeMillis();
long maxRetryDurationMillis = 5000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

public final class CacheGetResponse extends BaseResponse {
Expand All @@ -20,14 +21,14 @@ public MomentoCacheResult result() {
return this.resultMapper(this.result);
}

public Optional<byte[]> asByteArray() {
public Optional<byte[]> byteArray() {
if (result != ECacheResult.Hit) {
return Optional.empty();
}
return Optional.ofNullable(body.toByteArray());
}

public Optional<ByteBuffer> asByteBuffer() {
public Optional<ByteBuffer> byteBuffer() {
if (result != ECacheResult.Hit) {
return Optional.empty();
}
Expand All @@ -39,22 +40,18 @@ public Optional<ByteBuffer> asByteBuffer() {
*
* @return
*/
public Optional<String> asStringUtf8() {
if (result != ECacheResult.Hit) {
return Optional.empty();
}

return Optional.ofNullable(body.toStringUtf8());
public Optional<String> string() {
return string(StandardCharsets.UTF_8);
}

public Optional<String> asString(Charset charset) {
public Optional<String> string(Charset charset) {
if (result != ECacheResult.Hit) {
return Optional.empty();
}
return Optional.ofNullable(body.toString(charset));
}

public Optional<InputStream> asInputStream() {
public Optional<InputStream> inputStream() {
if (result != ECacheResult.Hit) {
return Optional.empty();
}
Expand Down

0 comments on commit 5317a8d

Please sign in to comment.