Skip to content

Commit

Permalink
tests: Add tests that nest geoJSON in a CDT. (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdogmcsteezy authored Sep 13, 2023
1 parent f67fb9f commit 92676da
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 18 deletions.
36 changes: 31 additions & 5 deletions src/test/java/com/aerospike/restclient/RecordPostCorrectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

import com.aerospike.client.Record;
import com.aerospike.client.*;
import com.aerospike.restclient.config.JSONMessageConverter;
import com.aerospike.restclient.config.MsgPackConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -88,14 +89,16 @@ public void clean() {
public static Object[] getParams() {
return new Object[][]{
{
new JSONPostPerformer(MediaType.APPLICATION_JSON.toString(), new ObjectMapper()), true
new JSONPostPerformer(MediaType.APPLICATION_JSON.toString(),
JSONMessageConverter.getJSONObjectMapper()), true
}, {
new MsgPackPostPerformer("application/msgpack", new ObjectMapper(new MessagePackFactory())),
new MsgPackPostPerformer("application/msgpack", MsgPackConverter.getASMsgPackObjectMapper()),
true
}, {
new JSONPostPerformer(MediaType.APPLICATION_JSON.toString(), new ObjectMapper()), false
new JSONPostPerformer(MediaType.APPLICATION_JSON.toString(),
JSONMessageConverter.getJSONObjectMapper()), false
}, {
new MsgPackPostPerformer("application/msgpack", new ObjectMapper(new MessagePackFactory())),
new MsgPackPostPerformer("application/msgpack", MsgPackConverter.getASMsgPackObjectMapper()),
false
}
};
Expand Down Expand Up @@ -289,6 +292,29 @@ public void PostGeoJson() throws Exception {
Assert.assertEquals(((Value.GeoJSONValue) record.bins.get("geo_json")).getObject(), geoStr);
}

@Ignore("Fails because GeoJSON can't be nested in a CDT for JSON. Only MSGPack")
@Test
public void PostNestedGeoJson() throws Exception {
Map<String, Object> binMap = new HashMap<>();
Map<String, Object> geoJson = new HashMap<>();
List<Double> coordinates = new ArrayList<>();
coordinates.add(-80.604333);
coordinates.add(28.608389);
String geoStr = "{\"coordinates\":[-80.604333,28.608389],\"type\":\"Point\"}";
geoJson.put("coordinates", coordinates);
geoJson.put("type", "Point");
List<Object> geoJsonList = new ArrayList<>();
geoJsonList.add(geoJson);

binMap.put("geo_json_list", geoJsonList);

postPerformer.perform(mockMVC, testEndpoint, binMap);

Record record = client.get(null, this.testKey);
List<Object> actualGeoJsonList = (List<Object>) record.bins.get("geo_json_list");
Assert.assertEquals(((Value.GeoJSONValue) actualGeoJsonList.get(0)).getObject(), geoStr);
}

@Test
public void PostBoolean() throws Exception {
Map<String, Object> binMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import gnu.crypto.util.Base64;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
Expand All @@ -43,20 +44,20 @@ public void testStringBin() {

@Test
public void testLongBin() {
singleObjectBinTest(5l);
singleObjectBinTest(5L);
}

@Test
public void testFloatBin() {
singleObjectBinTest(5l);
singleObjectBinTest(5L);
}

@Test
public void testMapBin() {
Map<String, Object> testMap = new HashMap<>();
testMap.put("str", "hello");
testMap.put("float", 3.14);
testMap.put("float", 5l);
testMap.put("long", 5L);
singleObjectBinTest(testMap);
}

Expand All @@ -66,13 +67,13 @@ public void testComplexCDTBin() {
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("str", "world");
nestedMap.put("int", 10);
nestedMap.put("float", 5l);
nestedMap.put("long", 5L);
List<Object> nestedList = new ArrayList<>();
nestedList.add(1);
nestedList.add("str");
testMap.put("str", "hello");
testMap.put("float", 3.14);
testMap.put("float", 5l);
testMap.put("long", 5L);
testMap.put("nestedMap", nestedMap);
testMap.put("nestedList", nestedList);
singleObjectBinTest(testMap);
Expand All @@ -83,14 +84,14 @@ public void testBytesBin() {
singleObjectBinTest(new byte[]{1, 2, 3});
}

// Really only tests whether the GeoJSONValue is passed through the BinConverter untouched.
@Test
public void testGeoJSONBin() {
Bin testBin = new Bin("bin1", new GeoJSONValue("{\"coordinates\": [-122.0, 37.5], \"type\": \"Point\"}"));
Map<String, Object> binMap = new HashMap<>();
binMap.put("bin1", new GeoJSONValue("{\"coordinates\": [-122.0, 37.5], \"type\": \"Point\"}"));
Bin[] bins = BinConverter.binsFromMap(binMap);
Assert.assertTrue(binsContain(bins, testBin));
// singleObjectBinTest(new GeoJSONValue("{\"coordinates\": [-122.0, 37.5], \"type\": \"Point\"}"));
}

@Test
Expand All @@ -105,7 +106,7 @@ public void testNullBin() {
@Test
public void testMultipleBins() {
Bin bin1 = new Bin("bin1", "str");
Bin bin2 = new Bin("bin2", 3l);
Bin bin2 = new Bin("bin2", 3L);

Map<String, Object> binMap = new HashMap<>();
binMap.put(bin1.name, bin1.value.getObject());
Expand All @@ -127,7 +128,26 @@ public void testGeoJSON() throws JsonProcessingException {
testMap.put("coordinates", new double[]{-122.0, 37.5});
Bin testBin = new Bin("bin1", Value.getAsGeoJSON(mapper.writeValueAsString(testMap)));
Map<String, Object> binMap = new HashMap<>();
binMap.put("bin1", Value.getAsGeoJSON(mapper.writeValueAsString(testMap)));
binMap.put("bin1", testMap);
Bin[] bins = BinConverter.binsFromMap(binMap);
Assert.assertTrue(binsContain(bins, testBin));
}

@Ignore("Fails. Will pass if we ever support geojson nested in CDTs with json. Currently, only supported using msgpack.")
@Test
public void testNestedGeoJSON() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> geoVal = new HashMap<>();
geoVal.put("type", "Point");
geoVal.put("coordinates", new double[]{-122.0, 37.5});
Map<String, Object> mapBin = new HashMap<>();
mapBin.put("map", geoVal);

Map<String, Object> testMapBin = new HashMap<>();
testMapBin.put("map", Value.getAsGeoJSON(mapper.writeValueAsString(geoVal)));
Bin testBin = new Bin("bin1", testMapBin);
Map<String, Object> binMap = new HashMap<>();
binMap.put("bin1", mapBin);
Bin[] bins = BinConverter.binsFromMap(binMap);
Assert.assertTrue(binsContain(bins, testBin));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest
public class MsgpackPost {
public class MsgpackPostTests {

private MockMvc mockMVC;

Expand All @@ -68,7 +68,7 @@ public class MsgpackPost {
private final Key testKey = new Key("test", "msgpack", "post");

private final String testEndpoint = ASTestUtils.buildEndpointV1("kvs", "test", "msgpack", "post");
private final TypeReference<Map<String, Object>> sOMapType = new TypeReference<Map<String, Object>>() {
private final TypeReference<Map<String, Object>> sOMapType = new TypeReference<>() {
};

@Before
Expand Down Expand Up @@ -175,6 +175,56 @@ public void testGettingGeoJson() throws Exception {
Assert.assertEquals(new String(etype.getData(), StandardCharsets.UTF_8), geoString);
}

@Test
public void testStoringNestedGeoJson() throws Exception {
String geoString = "{\"coordinates\": [-122.0, 37.5], \"type\": \"Point\"}";
MessageBufferPacker packer = new MessagePack.PackerConfig().newBufferPacker();

// Store a {map:{map={geo=GEOJSON_OBJECT}}}
packer.packMapHeader(1);
packer.packString("map");
packer.packMapHeader(1);
packer.packString("geo");
packer.packExtensionTypeHeader((byte) 23, geoString.length());
packer.addPayload(geoString.getBytes(StandardCharsets.UTF_8));

mockMVC.perform(post(testEndpoint).contentType(mtString).content(packer.toByteArray()))
.andExpect(status().isCreated());

Record rec = client.get(null, testKey);
@SuppressWarnings("unchecked") Map<String, Object> retMap = (Map<String, Object>) rec.bins.get("map");

Assert.assertEquals(retMap.size(), 1);
GeoJSONValue geoV = (GeoJSONValue) retMap.get("geo");
Assert.assertEquals(ParticleType.GEOJSON, geoV.getType());
Assert.assertEquals(geoString, geoV.toString());
}

@Test
public void testGettingNestedGeoJson() throws Exception {
Map<String, Object> mapBin = new HashMap<>();
String geoString = "{\"coordinates\": [-122.0, 37.5], \"type\": \"Point\"}";
GeoJSONValue geoV = new GeoJSONValue(geoString);
mapBin.put("geo", geoV);

client.put(null, testKey, new Bin("map", mapBin));

byte[] resContent = mockMVC.perform(get(testEndpoint).accept(mtString))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsByteArray();

Map<String, Object> retRec = mapper.readValue(resContent, sOMapType);

@SuppressWarnings("unchecked") Map<String, Object> bins = (Map<String, Object>) retRec.get("bins");
Map<String, Object> map = (Map<String, Object>) bins.get("map");
MessagePackExtensionType etype = (MessagePackExtensionType) map.get("geo");

Assert.assertEquals(etype.getType(), ParticleType.GEOJSON);
Assert.assertEquals(new String(etype.getData(), StandardCharsets.UTF_8), geoString);
}

@Test
public void testStoringMapWithIntkeys() throws Exception {
MessageBufferPacker packer = new MessagePack.PackerConfig().newBufferPacker();
Expand All @@ -192,14 +242,14 @@ public void testStoringMapWithIntkeys() throws Exception {
@SuppressWarnings("unchecked") Map<Long, Long> retMap = (Map<Long, Long>) rec.bins.get("map");

Assert.assertEquals(retMap.size(), 1);
Assert.assertEquals(retMap.get(1L), (Long) 2l);
Assert.assertEquals(retMap.get(1L), (Long) 2L);
}

@Test
public void testGettingMapWithIntKey() throws Exception {

Map<Long, Long> iMap = new HashMap<>();
iMap.put(1l, 2l);
iMap.put(1L, 2L);

client.put(null, testKey, new Bin("map", iMap));

Expand All @@ -218,7 +268,7 @@ public void testGettingMapWithIntKey() throws Exception {
@SuppressWarnings("unchecked") Map<Object, Object> lMap = (Map<Object, Object>) bins.get("map");

Assert.assertEquals(lMap.size(), 1);
Assert.assertEquals(lMap.get(1L), 2l);
Assert.assertEquals(lMap.get(1L), 2L);

}

Expand Down

0 comments on commit 92676da

Please sign in to comment.