From ed7a2fc7d2d49a042b7844eba08f258eedeea7fd Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Thu, 15 Apr 2021 14:17:26 +0300 Subject: [PATCH] set userKey parameter as required in batch read operations #31 --- .../domain/RestClientBatchReadBody.java | 8 +- .../domain/RestClientBatchReadResponse.java | 4 - .../restclient/domain/RestClientKey.java | 27 +- .../aerospike/restclient/util/KeyBuilder.java | 11 +- .../restclient/BatchReadCorrectTests.java | 830 +++++++++--------- .../restclient/domain/RestClientKeyTest.java | 465 +++++----- 6 files changed, 662 insertions(+), 683 deletions(-) diff --git a/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadBody.java b/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadBody.java index a24a23b..c3756e0 100644 --- a/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadBody.java +++ b/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadBody.java @@ -19,22 +19,20 @@ import com.aerospike.client.BatchRead; import com.aerospike.restclient.util.RestClientErrors; import com.fasterxml.jackson.annotation.JsonProperty; - import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(value="BatchReadRequest") public class RestClientBatchReadBody { - - @ApiModelProperty(value="Key to retrieve a record", required=true) + @ApiModelProperty(value="Key to retrieve a record.", required=true) @JsonProperty(required=true) public RestClientKey key; - @ApiModelProperty(value="Whether all bins should be returned with this record") + @ApiModelProperty(value="Whether all bins should be returned with this record.") public boolean readAllBins; - @ApiModelProperty(value="List of bins to limit the record response to.", required=false) + @ApiModelProperty(value="List of bins to limit the record response to.") public String[] binNames; public RestClientBatchReadBody() {} diff --git a/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadResponse.java b/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadResponse.java index 5dcc8c5..a9272c9 100644 --- a/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadResponse.java +++ b/src/main/java/com/aerospike/restclient/domain/RestClientBatchReadResponse.java @@ -17,14 +17,12 @@ package com.aerospike.restclient.domain; import com.aerospike.client.BatchRead; - import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(value="BatchRead") public class RestClientBatchReadResponse { - public RestClientBatchReadResponse() {} public RestClientBatchReadResponse(BatchRead batchRead) { @@ -46,6 +44,4 @@ record = batchRead.record != null ? new RestClientRecord(batchRead.record) : nul @ApiModelProperty(value="List of bins to limit the record response to.") public String[] binNames; - - } diff --git a/src/main/java/com/aerospike/restclient/domain/RestClientKey.java b/src/main/java/com/aerospike/restclient/domain/RestClientKey.java index ac47528..f5d32d5 100644 --- a/src/main/java/com/aerospike/restclient/domain/RestClientKey.java +++ b/src/main/java/com/aerospike/restclient/domain/RestClientKey.java @@ -16,9 +16,6 @@ */ package com.aerospike.restclient.domain; -import java.util.Base64; -import java.util.Base64.Encoder; - import com.aerospike.client.Key; import com.aerospike.client.Value.BytesValue; import com.aerospike.client.Value.IntegerValue; @@ -27,12 +24,15 @@ import com.aerospike.restclient.util.AerospikeAPIConstants.RecordKeyType; import com.aerospike.restclient.util.KeyBuilder; import com.fasterxml.jackson.annotation.JsonProperty; - import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import java.util.Base64; +import java.util.Base64.Encoder; + @ApiModel(value="Key") public class RestClientKey { + @ApiModelProperty(required=true, example="testNS") public String namespace; @@ -40,16 +40,15 @@ public class RestClientKey { @ApiModelProperty(example="testSet") public String setName; - @JsonProperty(required=false, value="digest") - @ApiModelProperty(value="URL safe base64 encoded key digest. Returned by the server on batch responses. May be provided by client.", - example="AAAAAAAAAAAAAAAAAAAAAAAAAAA=") - public String digest; - @JsonProperty(value="keytype") - @ApiModelProperty(value="Enum describing the type of the userKey. This field is omitted in MessagePack responses.", example="STRING") + @ApiModelProperty( + value="Enum describing the type of the userKey. This field is omitted in MessagePack responses.", + example="STRING") public RecordKeyType keytype; - @ApiModelProperty(value="The user key, it may be a string, integer, or URL safe Base64 encoded bytes.", example="userKey") + @ApiModelProperty(required=true, + value="The user key, it may be a string, integer, or URL safe Base64 encoded bytes.", + example="userKey") public Object userKey; public RestClientKey() {} @@ -58,14 +57,13 @@ public RestClientKey(Key realKey) { Encoder encoder = Base64.getUrlEncoder(); namespace = realKey.namespace; setName = realKey.setName; - digest = encoder.encodeToString(realKey.digest); if (realKey.userKey != null) { if (realKey.userKey instanceof StringValue) { userKey = realKey.userKey.toString(); keytype = RecordKeyType.STRING; } else if (realKey.userKey instanceof IntegerValue || realKey.userKey instanceof LongValue) { - userKey = (long)realKey.userKey.getObject(); + userKey = realKey.userKey.getObject(); keytype = RecordKeyType.INTEGER; } else if(realKey.userKey instanceof BytesValue) { userKey = encoder.encodeToString((byte[])realKey.userKey.getObject()); @@ -75,10 +73,9 @@ public RestClientKey(Key realKey) { userKey = encoder.encodeToString(realKey.digest); keytype = RecordKeyType.DIGEST; } - digest = encoder.encodeToString(realKey.digest); } public Key toKey() { return KeyBuilder.buildKey(namespace, setName, userKey.toString(), keytype); } -} \ No newline at end of file +} diff --git a/src/main/java/com/aerospike/restclient/util/KeyBuilder.java b/src/main/java/com/aerospike/restclient/util/KeyBuilder.java index 74aac2c..5f980bd 100644 --- a/src/main/java/com/aerospike/restclient/util/KeyBuilder.java +++ b/src/main/java/com/aerospike/restclient/util/KeyBuilder.java @@ -16,11 +16,11 @@ */ package com.aerospike.restclient.util; -import java.util.Base64; - import com.aerospike.client.Key; import com.aerospike.restclient.util.AerospikeAPIConstants.RecordKeyType; +import java.util.Base64; + public class KeyBuilder { public static Key buildKey(String namespace, String set, String strKey, RecordKeyType keyType) { @@ -38,13 +38,12 @@ public static Key buildKey(String namespace, String set, String strKey, RecordKe case DIGEST: { byte[] digestBytes = getByteArrayFromstring(strKey); if (digestBytes.length != 20) { - throw new RestClientErrors.InvalidKeyError(String.format("Digest must be 20 bytes long", strKey)); + throw new RestClientErrors.InvalidKeyError(String.format("Digest must be 20 bytes long: %s", strKey)); } return new Key(namespace, digestBytes, set, null); } default: - throw new RestClientErrors.InvalidKeyError(String.format("Invalid keytype value: %s", keyType.toString())); - + throw new RestClientErrors.InvalidKeyError(String.format("Invalid keyType value: %s", keyType.toString())); } } @@ -68,7 +67,7 @@ private static byte[] getByteArrayFromstring(String strKey) { try { return Base64.getUrlDecoder().decode(strKey); } catch (IllegalArgumentException nfe) { - throw new RestClientErrors.InvalidKeyError(String.format("Unable to decode bytes", strKey)); + throw new RestClientErrors.InvalidKeyError(String.format("Unable to decode bytes: %s", strKey)); } } diff --git a/src/test/java/com/aerospike/restclient/BatchReadCorrectTests.java b/src/test/java/com/aerospike/restclient/BatchReadCorrectTests.java index 326acc6..ae54895 100644 --- a/src/test/java/com/aerospike/restclient/BatchReadCorrectTests.java +++ b/src/test/java/com/aerospike/restclient/BatchReadCorrectTests.java @@ -16,18 +16,13 @@ */ package com.aerospike.restclient; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Base64; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import com.aerospike.client.AerospikeClient; +import com.aerospike.client.BatchRead; +import com.aerospike.client.Bin; +import com.aerospike.client.Key; +import com.aerospike.client.Value.BytesValue; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -48,17 +43,17 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.aerospike.client.AerospikeClient; -import com.aerospike.client.BatchRead; -import com.aerospike.client.Bin; -import com.aerospike.client.Key; -import com.aerospike.client.Value.BytesValue; -import com.aerospike.client.Value.StringValue; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /* * A batch Read sent to the server looks like: @@ -72,418 +67,409 @@ @SpringBootTest public class BatchReadCorrectTests { - private RestBatchReadComparator batchComparator; - private BatchHandler batchHandler; - - /* Needed to run as a Spring Boot test */ - @ClassRule - public static final SpringClassRule springClassRule = new SpringClassRule(); - - @Rule - public final SpringMethodRule springMethodRule = new SpringMethodRule(); - - - @Autowired - private ObjectMapper objectMapper; - - private MockMvc mockMVC; - - @Autowired - private AerospikeClient client; - - @Autowired - private WebApplicationContext wac; - - private Key[] keys = new Key[100]; - private Key intKey = new Key("test", "junit", 1); - private Key bytesKey = new Key("test", "junit", new byte[] {1, 127, 127, 1}); - - TypeReference>>batchReturnType = new TypeReference>>() {}; - private String endpoint = "/v1/batch"; - - @Before - public void setup() { - - mockMVC = MockMvcBuilders.webAppContextSetup(wac).build(); - for (int i = 0; i < keys.length; i++) { - keys[i] = new Key("test", "junit", Integer.toString(i)); - Bin bin1 = new Bin("bin1", i); - Bin bin2 = new Bin("bin2", i * 2); - Bin bin3 = new Bin("bin3", i * 3); - client.put(null, keys[i], bin1, bin2, bin3); - } - - client.put(null, intKey, new Bin("keytype", "integer")); - client.put(null, bytesKey, new Bin("keytype", "bytes")); - } - - @After - public void clean() { - for (Key key : keys) { - client.delete(null, key); - } - client.delete(null, intKey); - client.delete(null, bytesKey); - - } - - @Parameters - public static Object[] mappers() { - return new Object[][] { - {new RestBatchReadComparator(), new JSONBatchHandler()}, - {new RestBatchReadComparator(), new MsgPackBatchHandler()} - }; - } - - public BatchReadCorrectTests(RestBatchReadComparator comparator, BatchHandler handler) { - this.batchComparator = comparator; - this.batchHandler = handler; - } - - @Test - public void testGetExistingRecords() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - - batchKeys.add(keyToBatchObject(keys[0], null)); - batchKeys.add(keyToBatchObject(keys[1], null)); - batchKeys.add(keyToBatchObject(keys[2], null)); - - batchRecs.add(new BatchRead(keys[0], true)); - batchRecs.add(new BatchRead(keys[1], true)); - batchRecs.add(new BatchRead(keys[2], true)); - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(3, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - - @Test - public void testGetExistingRecordsBinFilter() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - String[]bins = new String[] {"bin1", "bin3"}; - - batchKeys.add(keyToBatchObject(keys[0], bins)); - batchKeys.add(keyToBatchObject(keys[1], bins)); - batchKeys.add(keyToBatchObject(keys[2], bins)); - - batchRecs.add(new BatchRead(keys[0], bins)); - batchRecs.add(new BatchRead(keys[1], bins)); - batchRecs.add(new BatchRead(keys[2], bins)); - - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(3, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - - @Test - public void testBatchGetWithNonExistentRecord() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - batchKeys.add(keyToBatchObject(keys[0], null)); - batchKeys.add(keyToBatchObject(keys[1], null)); - batchKeys.add(keyToBatchObject(new Key("test", "demo", "notreal"), null)); - batchKeys.add(keyToBatchObject(keys[2], null)); - - batchRecs.add(new BatchRead(keys[0], true)); - batchRecs.add(new BatchRead(keys[1], true)); - batchRecs.add(new BatchRead(new Key("test", "demo", "notreal"), true)); - batchRecs.add(new BatchRead(keys[2], true)); - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(4, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - - @Test - public void testGetExistingRecordsWithIntKey() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - batchKeys.add(keyToBatchObject(keys[0], null)); - batchKeys.add(keyToBatchObject(keys[1], null)); - batchKeys.add(keyToBatchObject(keys[2], null)); - batchKeys.add(intKeyToBatchObject(intKey, null)); - - batchRecs.add(new BatchRead(keys[0], true)); - batchRecs.add(new BatchRead(keys[1], true)); - batchRecs.add(new BatchRead(keys[2], true)); - batchRecs.add(new BatchRead(intKey, true)); - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(4, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - - @Test - public void testGetExistingRecordsWithBytesKey() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - batchKeys.add(bytesKeyToBatchObject(bytesKey, null)); - batchRecs.add(new BatchRead(bytesKey, true)); - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(1, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - - @Test - public void testGetExistingRecordsWithDigestKey() throws Exception { - List>batchKeys = new ArrayList>(); - ListbatchRecs = new ArrayList(); - batchKeys.add(digestKeyToBatchObject(keys[0], null)); - batchRecs.add(new BatchRead(keys[0], true)); - - String payLoad = objectMapper.writeValueAsString(batchKeys); - List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); - Assert.assertEquals(1, returnedRecords.size()); - - client.get(null, batchRecs); - Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); - } - private MapkeyToBatchObject(Key key, String[]bins) { - Map restKey = new HashMap(); - MapbatchObj = new HashMap(); - restKey.put("namespace", key.namespace); - restKey.put("setName", key.setName); - restKey.put("userKey", ((StringValue)key.userKey).toString()); - if (bins != null) { - batchObj.put("binNames", bins); - } else { - batchObj.put("readAllBins", true); - } - batchObj.put("key", restKey); - return batchObj; - } - - private MapintKeyToBatchObject(Key key, String[]bins) { - Map restKey = new HashMap(); - MapbatchObj = new HashMap(); - - restKey.put("namespace", key.namespace); - restKey.put("setName", key.setName); - restKey.put("userKey", (key.userKey).toString()); - restKey.put("keytype", "INTEGER"); - if (bins != null) { - batchObj.put("bins", bins); - } else { - batchObj.put("readAllBins", true); - } - batchObj.put("key", restKey); - return batchObj; - } - - private MapbytesKeyToBatchObject(Key key, String[]bins) { - Map restKey = new HashMap(); - BytesValue value = ((BytesValue)key.userKey); - - String urlBytes = Base64.getUrlEncoder().encodeToString((byte[])value.getObject()); - MapbatchObj = new HashMap(); - - restKey.put("namespace", key.namespace); - restKey.put("setName", key.setName); - restKey.put("userKey", urlBytes); - restKey.put("keytype", "BYTES"); - - if (bins != null) { - batchObj.put("bins", bins); - } else { - batchObj.put("readAllBins", true); - } - batchObj.put("key", restKey); - return batchObj; - } - - private MapdigestKeyToBatchObject(Key key, String[]bins) { - Map restKey = new HashMap(); - - String urlDigest = Base64.getUrlEncoder().encodeToString(key.digest); - MapbatchObj = new HashMap(); - - restKey.put("namespace", key.namespace); - restKey.put("setName", key.setName); - restKey.put("userKey", urlDigest); - restKey.put("keytype", "DIGEST"); - if (bins != null) { - batchObj.put("bins", bins); - } else { - batchObj.put("readAllBins", true); - } - batchObj.put("key", restKey); - return batchObj; - } - private boolean compareRestRecordsToBatchReads(List>returnedRecords, ListbatchReads) { - if (batchReads.size() != returnedRecords.size()) { - return false; - } - for (int i = 0; i < batchReads.size(); i++) { - if (!batchComparator.compareRestBatchReadToBatchRead(returnedRecords.get(i), batchReads.get(i))) { - return false; - } - } - return true; - } - + private final RestBatchReadComparator batchComparator; + private final BatchHandler batchHandler; + + /* Needed to run as a Spring Boot test */ + @ClassRule + public static final SpringClassRule springClassRule = new SpringClassRule(); + + @Rule + public final SpringMethodRule springMethodRule = new SpringMethodRule(); + + + @Autowired + private ObjectMapper objectMapper; + + private MockMvc mockMVC; + + @Autowired + private AerospikeClient client; + + @Autowired + private WebApplicationContext wac; + + private final Key[] keys = new Key[100]; + private final Key intKey = new Key("test", "junit", 1); + private final Key bytesKey = new Key("test", "junit", new byte[]{1, 127, 127, 1}); + + TypeReference>> batchReturnType = new TypeReference>>() { + }; + private final String endpoint = "/v1/batch"; + + @Before + public void setup() { + mockMVC = MockMvcBuilders.webAppContextSetup(wac).build(); + for (int i = 0; i < keys.length; i++) { + keys[i] = new Key("test", "junit", Integer.toString(i)); + Bin bin1 = new Bin("bin1", i); + Bin bin2 = new Bin("bin2", i * 2); + Bin bin3 = new Bin("bin3", i * 3); + client.put(null, keys[i], bin1, bin2, bin3); + } + + client.put(null, intKey, new Bin("keytype", "integer")); + client.put(null, bytesKey, new Bin("keytype", "bytes")); + } + + @After + public void clean() { + for (Key key : keys) { + client.delete(null, key); + } + client.delete(null, intKey); + client.delete(null, bytesKey); + } + + @Parameters + public static Object[] mappers() { + return new Object[][]{ + {new RestBatchReadComparator(), new JSONBatchHandler()}, + {new RestBatchReadComparator(), new MsgPackBatchHandler()} + }; + } + + public BatchReadCorrectTests(RestBatchReadComparator comparator, BatchHandler handler) { + this.batchComparator = comparator; + this.batchHandler = handler; + } + + @Test + public void testGetExistingRecords() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList<>(); + + batchKeys.add(keyToBatchObject(keys[0], null)); + batchKeys.add(keyToBatchObject(keys[1], null)); + batchKeys.add(keyToBatchObject(keys[2], null)); + + batchRecs.add(new BatchRead(keys[0], true)); + batchRecs.add(new BatchRead(keys[1], true)); + batchRecs.add(new BatchRead(keys[2], true)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(3, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + @Test + public void testGetExistingRecordsBinFilter() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList<>(); + String[] bins = new String[]{"bin1", "bin3"}; + + batchKeys.add(keyToBatchObject(keys[0], bins)); + batchKeys.add(keyToBatchObject(keys[1], bins)); + batchKeys.add(keyToBatchObject(keys[2], bins)); + + batchRecs.add(new BatchRead(keys[0], bins)); + batchRecs.add(new BatchRead(keys[1], bins)); + batchRecs.add(new BatchRead(keys[2], bins)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(3, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + @Test + public void testBatchGetWithNonExistentRecord() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList(); + batchKeys.add(keyToBatchObject(keys[0], null)); + batchKeys.add(keyToBatchObject(keys[1], null)); + batchKeys.add(keyToBatchObject(new Key("test", "demo", "notreal"), null)); + batchKeys.add(keyToBatchObject(keys[2], null)); + + batchRecs.add(new BatchRead(keys[0], true)); + batchRecs.add(new BatchRead(keys[1], true)); + batchRecs.add(new BatchRead(new Key("test", "demo", "notreal"), true)); + batchRecs.add(new BatchRead(keys[2], true)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(4, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + @Test + public void testGetExistingRecordsWithIntKey() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList<>(); + batchKeys.add(keyToBatchObject(keys[0], null)); + batchKeys.add(keyToBatchObject(keys[1], null)); + batchKeys.add(keyToBatchObject(keys[2], null)); + batchKeys.add(intKeyToBatchObject(intKey, null)); + + batchRecs.add(new BatchRead(keys[0], true)); + batchRecs.add(new BatchRead(keys[1], true)); + batchRecs.add(new BatchRead(keys[2], true)); + batchRecs.add(new BatchRead(intKey, true)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(4, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + @Test + public void testGetExistingRecordsWithBytesKey() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList<>(); + batchKeys.add(bytesKeyToBatchObject(bytesKey, null)); + batchRecs.add(new BatchRead(bytesKey, true)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(1, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + @Test + public void testGetExistingRecordsWithDigestKey() throws Exception { + List> batchKeys = new ArrayList<>(); + List batchRecs = new ArrayList<>(); + batchKeys.add(digestKeyToBatchObject(keys[0], null)); + batchRecs.add(new BatchRead(keys[0], true)); + + String payLoad = objectMapper.writeValueAsString(batchKeys); + List> returnedRecords = batchHandler.perform(mockMVC, endpoint, payLoad); + Assert.assertEquals(1, returnedRecords.size()); + + client.get(null, batchRecs); + Assert.assertTrue(compareRestRecordsToBatchReads(returnedRecords, batchRecs)); + } + + private Map keyToBatchObject(Key key, String[] bins) { + Map restKey = new HashMap<>(); + Map batchObj = new HashMap<>(); + restKey.put("namespace", key.namespace); + restKey.put("setName", key.setName); + restKey.put("userKey", key.userKey.toString()); + if (bins != null) { + batchObj.put("binNames", bins); + } else { + batchObj.put("readAllBins", true); + } + batchObj.put("key", restKey); + return batchObj; + } + + private Map intKeyToBatchObject(Key key, String[] bins) { + Map restKey = new HashMap<>(); + Map batchObj = new HashMap<>(); + + restKey.put("namespace", key.namespace); + restKey.put("setName", key.setName); + restKey.put("userKey", (key.userKey).toString()); + restKey.put("keytype", "INTEGER"); + if (bins != null) { + batchObj.put("bins", bins); + } else { + batchObj.put("readAllBins", true); + } + batchObj.put("key", restKey); + return batchObj; + } + + private Map bytesKeyToBatchObject(Key key, String[] bins) { + Map restKey = new HashMap<>(); + BytesValue value = ((BytesValue) key.userKey); + + String urlBytes = Base64.getUrlEncoder().encodeToString((byte[]) value.getObject()); + Map batchObj = new HashMap<>(); + + restKey.put("namespace", key.namespace); + restKey.put("setName", key.setName); + restKey.put("userKey", urlBytes); + restKey.put("keytype", "BYTES"); + + if (bins != null) { + batchObj.put("bins", bins); + } else { + batchObj.put("readAllBins", true); + } + batchObj.put("key", restKey); + return batchObj; + } + + private Map digestKeyToBatchObject(Key key, String[] bins) { + Map restKey = new HashMap<>(); + + String urlDigest = Base64.getUrlEncoder().encodeToString(key.digest); + Map batchObj = new HashMap<>(); + + restKey.put("namespace", key.namespace); + restKey.put("setName", key.setName); + restKey.put("userKey", urlDigest); + restKey.put("keytype", "DIGEST"); + if (bins != null) { + batchObj.put("bins", bins); + } else { + batchObj.put("readAllBins", true); + } + batchObj.put("key", restKey); + return batchObj; + } + + private boolean compareRestRecordsToBatchReads(List> returnedRecords, + List batchReads) { + if (batchReads.size() != returnedRecords.size()) { + return false; + } + for (int i = 0; i < batchReads.size(); i++) { + if (!batchComparator.compareRestBatchReadToBatchRead(returnedRecords.get(i), + batchReads.get(i))) { + return false; + } + } + return true; + } } - -class RestBatchReadComparator { - - @SuppressWarnings("unchecked") - public boolean compareRestBatchReadToBatchRead(MaprestRecord, BatchRead batchRead) { - MaprestKey = (Map) restRecord.get("key"); - - if (restKey.get("userKey") != null) { - if (!compareKey(batchRead.key, restKey.get("userKey").toString(), (String)restKey.get("keytype"))) { - return false; - } - } else { - if (!compareDigests((String) restKey.get("digest"), batchRead.key)) { - return false; - } - } - - if (!restKey.get("namespace").equals(batchRead.key.namespace)) { - return false; - } - if (restKey.get("setName") != null) { - if (!restKey.get("setName").equals(batchRead.key.setName)) { - return false; - } - } else if (batchRead.key.setName != null) { - return false; - } - if (restRecord.get("record") != null) { - return ASTestUtils.compareRCRecordToASRecord((Map) restRecord.get("record"), batchRead.record); - } else { - return batchRead.record == null; - } - } - - private boolean compareKey(Key userKey, String restKey, String keyType) { - if (keyType.equals("DIGEST")) { - byte[]restDigest = Base64.getUrlDecoder().decode(restKey); - return Arrays.equals(restDigest, userKey.digest); - } else if (keyType.equals("BYTES")) { - byte[] restBytes = Base64.getUrlDecoder().decode(restKey); - BytesValue keyBytesVal = (BytesValue)userKey.userKey; - byte[] recVal = (byte[]) keyBytesVal.getObject(); - return Arrays.equals(restBytes, recVal); - } - return restKey.equals(userKey.userKey.toString()); - } - - private boolean compareDigests(String restDigest, Key userKey) { - byte[] restBytes = Base64.getUrlDecoder().decode(restDigest); - return Arrays.equals(restBytes, userKey.digest); - } - +class RestBatchReadComparator { + + @SuppressWarnings("unchecked") + public boolean compareRestBatchReadToBatchRead(Map restRecord, BatchRead batchRead) { + Map restKey = (Map) restRecord.get("key"); + + if (restKey.get("userKey") != null) { + if (!compareKey(batchRead.key, restKey.get("userKey").toString(), (String) restKey.get("keytype"))) { + return false; + } + } else { + if (!compareDigests((String) restKey.get("digest"), batchRead.key)) { + return false; + } + } + + if (!restKey.get("namespace").equals(batchRead.key.namespace)) { + return false; + } + if (restKey.get("setName") != null) { + if (!restKey.get("setName").equals(batchRead.key.setName)) { + return false; + } + } else if (batchRead.key.setName != null) { + return false; + } + if (restRecord.get("record") != null) { + return ASTestUtils.compareRCRecordToASRecord((Map) restRecord.get("record"), batchRead.record); + } else { + return batchRead.record == null; + } + } + + private boolean compareKey(Key userKey, String restKey, String keyType) { + if (keyType.equals("DIGEST")) { + byte[] restDigest = Base64.getUrlDecoder().decode(restKey); + return Arrays.equals(restDigest, userKey.digest); + } else if (keyType.equals("BYTES")) { + byte[] restBytes = Base64.getUrlDecoder().decode(restKey); + BytesValue keyBytesVal = (BytesValue) userKey.userKey; + byte[] recVal = (byte[]) keyBytesVal.getObject(); + return Arrays.equals(restBytes, recVal); + } + return restKey.equals(userKey.userKey.toString()); + } + + private boolean compareDigests(String restDigest, Key userKey) { + byte[] restBytes = Base64.getUrlDecoder().decode(restDigest); + return Arrays.equals(restBytes, userKey.digest); + } } - /* * The handler interface performs a batch request via a json string, and returns a List> * Implementations are provided for specifying JSON and MsgPack as return formats */ interface BatchHandler { - public List> perform(MockMvc mockMVC, String testEndpoint, String payload) - throws JsonProcessingException, Exception; + public List> perform(MockMvc mockMVC, String testEndpoint, String payload) + throws Exception; } -class MsgPackBatchHandler implements BatchHandler{ - - ObjectMapper msgPackMapper; - - public MsgPackBatchHandler() { - msgPackMapper = new ObjectMapper(new MessagePackFactory()); - } - - private List> getReturnedBatches(MockHttpServletResponse res) { - byte[] response = res.getContentAsByteArray(); - TypeReference>> btype = new TypeReference>>() {}; - List> batchResponse = null; - try { - batchResponse = msgPackMapper.readValue(response, btype); - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return batchResponse; - - } - @Override - public List> perform(MockMvc mockMVC, String testEndpoint, String payload) - throws JsonProcessingException, Exception { - - MockHttpServletResponse res = mockMVC.perform(post(testEndpoint) - .contentType(MediaType.APPLICATION_JSON) - .accept("application/msgpack") - .content(payload)).andExpect(status().isOk()) - .andReturn().getResponse(); - - return getReturnedBatches(res); - } +class MsgPackBatchHandler implements BatchHandler { + + ObjectMapper msgPackMapper; + + public MsgPackBatchHandler() { + msgPackMapper = new ObjectMapper(new MessagePackFactory()); + } + + private List> getReturnedBatches(MockHttpServletResponse res) { + byte[] response = res.getContentAsByteArray(); + TypeReference>> btype = new TypeReference>>() { + }; + List> batchResponse = null; + try { + batchResponse = msgPackMapper.readValue(response, btype); + } catch (IOException e) { + e.printStackTrace(); + } + return batchResponse; + } + + @Override + public List> perform(MockMvc mockMVC, String testEndpoint, String payload) + throws Exception { + + MockHttpServletResponse res = mockMVC.perform(post(testEndpoint) + .contentType(MediaType.APPLICATION_JSON) + .accept("application/msgpack") + .content(payload)).andExpect(status().isOk()) + .andReturn().getResponse(); + + return getReturnedBatches(res); + } } class JSONBatchHandler implements BatchHandler { - ObjectMapper msgPackMapper; - - public JSONBatchHandler() { - msgPackMapper = new ObjectMapper(); - } - - private List> getReturnedBatches(MockHttpServletResponse res) { - String response = null; - try { - response = res.getContentAsString(); - } catch (UnsupportedEncodingException e1) { - e1.printStackTrace(); - } - - TypeReference>> btype = new TypeReference>>() {}; - List> batchResponse = null; - try { - batchResponse = msgPackMapper.readValue(response, btype); - } catch (JsonParseException e) { - e.printStackTrace(); - } catch (JsonMappingException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return batchResponse; - - } - @Override - public List> perform(MockMvc mockMVC, String testEndpoint, String payload) - throws JsonProcessingException, Exception { - - MockHttpServletResponse res = mockMVC.perform(post(testEndpoint) - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON) - .content(payload)).andExpect(status().isOk()) - .andReturn().getResponse(); - return getReturnedBatches(res); - - } + ObjectMapper msgPackMapper; + + public JSONBatchHandler() { + msgPackMapper = new ObjectMapper(); + } + + private List> getReturnedBatches(MockHttpServletResponse res) { + String response = null; + try { + response = res.getContentAsString(); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + + TypeReference>> btype = new TypeReference>>() { + }; + List> batchResponse = null; + try { + batchResponse = msgPackMapper.readValue(response, btype); + } catch (IOException e) { + e.printStackTrace(); + } + return batchResponse; + } + + @Override + public List> perform(MockMvc mockMVC, String testEndpoint, String payload) + throws Exception { + + MockHttpServletResponse res = mockMVC.perform(post(testEndpoint) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content(payload)).andExpect(status().isOk()) + .andReturn().getResponse(); + return getReturnedBatches(res); + } } diff --git a/src/test/java/com/aerospike/restclient/domain/RestClientKeyTest.java b/src/test/java/com/aerospike/restclient/domain/RestClientKeyTest.java index e130e5b..9003002 100644 --- a/src/test/java/com/aerospike/restclient/domain/RestClientKeyTest.java +++ b/src/test/java/com/aerospike/restclient/domain/RestClientKeyTest.java @@ -16,16 +16,6 @@ */ package com.aerospike.restclient.domain; -import java.util.Arrays; -import java.util.Base64; -import java.util.Base64.Decoder; -import java.util.Base64.Encoder; -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - import com.aerospike.client.Key; import com.aerospike.restclient.ASTestUtils; import com.aerospike.restclient.util.AerospikeAPIConstants; @@ -33,229 +23,242 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Base64; +import java.util.Base64.Decoder; +import java.util.Base64.Encoder; +import java.util.HashMap; +import java.util.Map; public class RestClientKeyTest { - private static ObjectMapper mapper = new ObjectMapper(); - private static String ns = "test"; - private static String set = "set"; - private static Decoder decoder = Base64.getUrlDecoder(); - private static Encoder encoder = Base64.getUrlEncoder(); - private static TypeReferencercKeyType = new TypeReference() {}; - - @Test - public void testNoArgConstructor() { - new RestClientKey(); - } - - @Test - public void testCreationFromStringKey() { - Key testKey = new Key(ns, set, "key"); - RestClientKey constructedKey = new RestClientKey(testKey); - - Assert.assertEquals(testKey.namespace, constructedKey.namespace); - Assert.assertEquals(testKey.setName, constructedKey.setName); - Assert.assertEquals("key", constructedKey.userKey); - String constructedDigest = constructedKey.digest; - Assert.assertEquals(constructedKey.keytype, RecordKeyType.STRING); - Assert.assertTrue(Arrays.equals(decoder.decode(constructedDigest), testKey.digest)); - } - - @Test - public void testCreationFromIntegerKey() { - Key testKey = new Key(ns, set, 5l); - RestClientKey constructedKey = new RestClientKey(testKey); - - Assert.assertEquals(testKey.namespace, constructedKey.namespace); - Assert.assertEquals(testKey.setName, constructedKey.setName); - Assert.assertEquals(5l, constructedKey.userKey); - Assert.assertEquals(constructedKey.keytype, RecordKeyType.INTEGER); - - String constructedDigest = constructedKey.digest; - Assert.assertTrue(Arrays.equals(decoder.decode(constructedDigest), testKey.digest)); - } - - @Test - public void testCreationFromBytesKey() { - byte[] testBytes = {1,2,3,4}; - Key testKey = new Key(ns, set, testBytes); - RestClientKey constructedKey = new RestClientKey(testKey); - - Assert.assertEquals(testKey.namespace, constructedKey.namespace); - Assert.assertEquals(testKey.setName, constructedKey.setName); - Assert.assertEquals(constructedKey.keytype, RecordKeyType.BYTES); - - Assert.assertTrue(Arrays.equals(testBytes, decoder.decode((String)constructedKey.userKey))); - String constructedDigest = constructedKey.digest; - Assert.assertTrue(Arrays.equals(decoder.decode(constructedDigest), testKey.digest)); - } - - @Test public void testCreationWithDigest() { - byte[] testBytes = new byte[20]; - Key testKey = new Key(ns, testBytes, null, null); - RestClientKey constructedKey = new RestClientKey(testKey); - - Assert.assertEquals(testKey.namespace, constructedKey.namespace); - - String constructedDigest = constructedKey.digest; - Assert.assertTrue(Arrays.equals(decoder.decode(constructedDigest), testKey.digest)); - } - - @Test - public void testConversionToStringKey() { - Key expectedKey = new Key(ns, set, "test"); - RestClientKey rcKey = new RestClientKey(); - rcKey.keytype = RecordKeyType.STRING; - rcKey.setName = set; - rcKey.namespace = ns; - rcKey.userKey = "test"; - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testConversionToLongKey() { - Key expectedKey = new Key(ns, set, 5l); - RestClientKey rcKey = new RestClientKey(); - rcKey.keytype = RecordKeyType.INTEGER; - rcKey.setName = set; - rcKey.namespace = ns; - rcKey.userKey = 5; - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testConversionToBytesKey() { - byte[] keyBytes = {1,2,3,4}; - Key expectedKey = new Key(ns, set, keyBytes); - RestClientKey rcKey = new RestClientKey(); - rcKey.keytype = RecordKeyType.BYTES; - rcKey.setName = set; - rcKey.namespace = ns; - rcKey.userKey = encoder.encodeToString(keyBytes); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test public void testConversionToDigestKey() { - byte[] testBytes = new byte[20]; - Key expectedKey = new Key(ns, testBytes, null, null); - RestClientKey rcKey = new RestClientKey(); - rcKey.keytype = RecordKeyType.DIGEST; - rcKey.namespace = ns; - rcKey.userKey = encoder.encodeToString(testBytes); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test public void testConversionToDigest() { - byte[] testBytes = new byte[20]; - Key testKey = new Key(ns, testBytes, null, null); - RestClientKey constructedKey = new RestClientKey(testKey); - - Assert.assertEquals(testKey.namespace, constructedKey.namespace); - - String constructedDigest = constructedKey.digest; - Assert.assertTrue(Arrays.equals(decoder.decode(constructedDigest), testKey.digest)); - } - - @Test - public void testTwoWayConversionStringKey() { - Key expectedKey = new Key(ns, set, "test"); - RestClientKey rcKey = new RestClientKey(expectedKey); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testConversionTwoWayLongKey() { - Key expectedKey = new Key(ns, set, 5l); - RestClientKey rcKey = new RestClientKey(expectedKey); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testConversionTwoWayToBytesKey() { - byte[] keyBytes = {1,2,3,4}; - Key expectedKey = new Key(ns, set, keyBytes); - RestClientKey rcKey = new RestClientKey(expectedKey); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testConversionTwoWayToDigestKey() { - byte[] testBytes = new byte[20]; - Key expectedKey = new Key(ns, testBytes, null, null); - RestClientKey rcKey = new RestClientKey(expectedKey); - - Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); - } - - @Test - public void testObjectMappedStringKey() throws Exception { - String jsonKey = getJsonRCKey(ns, set, "key", RecordKeyType.STRING); - RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); - - Assert.assertEquals(rcKey.namespace, ns); - Assert.assertEquals(rcKey.setName, set); - Assert.assertEquals(rcKey.userKey, "key"); - Assert.assertEquals(rcKey.keytype, RecordKeyType.STRING); - } - - @Test - public void testObjectMappedIntegerKey() throws Exception { - String jsonKey = getJsonRCKey(ns, set, 5l, RecordKeyType.INTEGER); - RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); - - Assert.assertEquals(rcKey.namespace, ns); - Assert.assertEquals(rcKey.setName, set); - Assert.assertEquals(rcKey.userKey, 5); - Assert.assertEquals(rcKey.keytype, RecordKeyType.INTEGER); - } - - @Test - public void testObjectMappedDigestKey() throws Exception { - byte[] testBytes = new byte[20]; - String strBytes = encoder.encodeToString(testBytes); - String jsonKey = getJsonRCKey(ns, set, strBytes, RecordKeyType.DIGEST); - RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); - - Assert.assertEquals(rcKey.namespace, ns); - Assert.assertEquals(rcKey.setName, set); - Assert.assertEquals(rcKey.userKey, strBytes); - Assert.assertEquals(rcKey.keytype, RecordKeyType.DIGEST); - } - - @Test - public void testObjectMappedBYTESKey() throws Exception { - byte[] keyBytes = {1,2,3,4}; - String strBytes = encoder.encodeToString(keyBytes); - - String jsonKey = getJsonRCKey(ns, set, strBytes, RecordKeyType.BYTES); - RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); - - Assert.assertEquals(rcKey.namespace, ns); - Assert.assertEquals(rcKey.setName, set); - Assert.assertEquals(rcKey.userKey, strBytes); - Assert.assertEquals(rcKey.keytype, RecordKeyType.BYTES); - } - - private String getJsonRCKey(String namespace, String set, Object userKey, RecordKeyType keytype) throws JsonProcessingException { - MapkeyMap = new HashMap<>(); - keyMap.put(AerospikeAPIConstants.NAMESPACE, namespace); - if (set != null ) { - keyMap.put(AerospikeAPIConstants.SETNAME, set); - } - if (userKey != null) { - keyMap.put(AerospikeAPIConstants.USER_KEY, userKey); - } - if (keytype != null) { - keyMap.put(AerospikeAPIConstants.KEY_TYPE, keytype.toString()); - } - return mapper.writeValueAsString(keyMap); - } + private static final ObjectMapper mapper = new ObjectMapper(); + private static final String ns = "test"; + private static final String set = "set"; + private static final Decoder decoder = Base64.getUrlDecoder(); + private static final Encoder encoder = Base64.getUrlEncoder(); + private static final TypeReference rcKeyType = new TypeReference() { + }; + + @Test + public void testNoArgConstructor() { + new RestClientKey(); + } + + @Test + public void testCreationFromStringKey() { + Key testKey = new Key(ns, set, "key"); + RestClientKey constructedKey = new RestClientKey(testKey); + + Assert.assertEquals(testKey.namespace, constructedKey.namespace); + Assert.assertEquals(testKey.setName, constructedKey.setName); + Assert.assertEquals("key", constructedKey.userKey); + byte[] constructedDigest = constructedKey.toKey().digest; + Assert.assertEquals(constructedKey.keytype, RecordKeyType.STRING); + Assert.assertArrayEquals(constructedDigest, testKey.digest); + } + + @Test + public void testCreationFromIntegerKey() { + Key testKey = new Key(ns, set, 5L); + RestClientKey constructedKey = new RestClientKey(testKey); + + Assert.assertEquals(testKey.namespace, constructedKey.namespace); + Assert.assertEquals(testKey.setName, constructedKey.setName); + Assert.assertEquals(5L, constructedKey.userKey); + Assert.assertEquals(constructedKey.keytype, RecordKeyType.INTEGER); + + byte[] constructedDigest = constructedKey.toKey().digest; + Assert.assertArrayEquals(constructedDigest, testKey.digest); + } + + @Test + public void testCreationFromBytesKey() { + byte[] testBytes = {1, 2, 3, 4}; + Key testKey = new Key(ns, set, testBytes); + RestClientKey constructedKey = new RestClientKey(testKey); + + Assert.assertEquals(testKey.namespace, constructedKey.namespace); + Assert.assertEquals(testKey.setName, constructedKey.setName); + Assert.assertEquals(constructedKey.keytype, RecordKeyType.BYTES); + + Assert.assertArrayEquals(testBytes, decoder.decode((String) constructedKey.userKey)); + byte[] constructedDigest = constructedKey.toKey().digest; + Assert.assertArrayEquals(constructedDigest, testKey.digest); + } + + @Test + public void testCreationWithDigest() { + byte[] testBytes = new byte[20]; + Key testKey = new Key(ns, testBytes, null, null); + RestClientKey constructedKey = new RestClientKey(testKey); + + Assert.assertEquals(testKey.namespace, constructedKey.namespace); + + byte[] constructedDigest = constructedKey.toKey().digest; + Assert.assertArrayEquals(constructedDigest, testKey.digest); + } + + @Test + public void testConversionToStringKey() { + Key expectedKey = new Key(ns, set, "test"); + RestClientKey rcKey = new RestClientKey(); + rcKey.keytype = RecordKeyType.STRING; + rcKey.setName = set; + rcKey.namespace = ns; + rcKey.userKey = "test"; + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionToLongKey() { + Key expectedKey = new Key(ns, set, 5L); + RestClientKey rcKey = new RestClientKey(); + rcKey.keytype = RecordKeyType.INTEGER; + rcKey.setName = set; + rcKey.namespace = ns; + rcKey.userKey = 5; + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionToBytesKey() { + byte[] keyBytes = {1, 2, 3, 4}; + Key expectedKey = new Key(ns, set, keyBytes); + RestClientKey rcKey = new RestClientKey(); + rcKey.keytype = RecordKeyType.BYTES; + rcKey.setName = set; + rcKey.namespace = ns; + rcKey.userKey = encoder.encodeToString(keyBytes); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionToDigestKey() { + byte[] testBytes = new byte[20]; + Key expectedKey = new Key(ns, testBytes, null, null); + RestClientKey rcKey = new RestClientKey(); + rcKey.keytype = RecordKeyType.DIGEST; + rcKey.namespace = ns; + rcKey.userKey = encoder.encodeToString(testBytes); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionToDigest() { + byte[] testBytes = new byte[20]; + Key testKey = new Key(ns, testBytes, null, null); + RestClientKey constructedKey = new RestClientKey(testKey); + + Assert.assertEquals(testKey.namespace, constructedKey.namespace); + + byte[] constructedDigest = constructedKey.toKey().digest; + Assert.assertArrayEquals(constructedDigest, testKey.digest); + } + + @Test + public void testTwoWayConversionStringKey() { + Key expectedKey = new Key(ns, set, "test"); + RestClientKey rcKey = new RestClientKey(expectedKey); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionTwoWayLongKey() { + Key expectedKey = new Key(ns, set, 5L); + RestClientKey rcKey = new RestClientKey(expectedKey); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionTwoWayToBytesKey() { + byte[] keyBytes = {1, 2, 3, 4}; + Key expectedKey = new Key(ns, set, keyBytes); + RestClientKey rcKey = new RestClientKey(expectedKey); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testConversionTwoWayToDigestKey() { + byte[] testBytes = new byte[20]; + Key expectedKey = new Key(ns, testBytes, null, null); + RestClientKey rcKey = new RestClientKey(expectedKey); + + Assert.assertTrue(ASTestUtils.compareKeys(expectedKey, rcKey.toKey())); + } + + @Test + public void testObjectMappedStringKey() throws Exception { + String jsonKey = getJsonRCKey(ns, set, "key", RecordKeyType.STRING); + RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); + + Assert.assertEquals(rcKey.namespace, ns); + Assert.assertEquals(rcKey.setName, set); + Assert.assertEquals(rcKey.userKey, "key"); + Assert.assertEquals(rcKey.keytype, RecordKeyType.STRING); + } + + @Test + public void testObjectMappedIntegerKey() throws Exception { + String jsonKey = getJsonRCKey(ns, set, 5L, RecordKeyType.INTEGER); + RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); + + Assert.assertEquals(rcKey.namespace, ns); + Assert.assertEquals(rcKey.setName, set); + Assert.assertEquals(rcKey.userKey, 5); + Assert.assertEquals(rcKey.keytype, RecordKeyType.INTEGER); + } + + @Test + public void testObjectMappedDigestKey() throws Exception { + byte[] testBytes = new byte[20]; + String strBytes = encoder.encodeToString(testBytes); + String jsonKey = getJsonRCKey(ns, set, strBytes, RecordKeyType.DIGEST); + RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); + + Assert.assertEquals(rcKey.namespace, ns); + Assert.assertEquals(rcKey.setName, set); + Assert.assertEquals(rcKey.userKey, strBytes); + Assert.assertEquals(rcKey.keytype, RecordKeyType.DIGEST); + } + + @Test + public void testObjectMappedBYTESKey() throws Exception { + byte[] keyBytes = {1, 2, 3, 4}; + String strBytes = encoder.encodeToString(keyBytes); + + String jsonKey = getJsonRCKey(ns, set, strBytes, RecordKeyType.BYTES); + RestClientKey rcKey = mapper.readValue(jsonKey, rcKeyType); + + Assert.assertEquals(rcKey.namespace, ns); + Assert.assertEquals(rcKey.setName, set); + Assert.assertEquals(rcKey.userKey, strBytes); + Assert.assertEquals(rcKey.keytype, RecordKeyType.BYTES); + } + + private String getJsonRCKey(String namespace, String set, Object userKey, + RecordKeyType keyType) throws JsonProcessingException { + Map keyMap = new HashMap<>(); + keyMap.put(AerospikeAPIConstants.NAMESPACE, namespace); + if (set != null) { + keyMap.put(AerospikeAPIConstants.SETNAME, set); + } + if (userKey != null) { + keyMap.put(AerospikeAPIConstants.USER_KEY, userKey); + } + if (keyType != null) { + keyMap.put(AerospikeAPIConstants.KEY_TYPE, keyType.toString()); + } + return mapper.writeValueAsString(keyMap); + } }