From 92676da985bc017a9232d2841a2550f0f4428657 Mon Sep 17 00:00:00 2001 From: Jesse S Date: Wed, 13 Sep 2023 09:27:09 -0700 Subject: [PATCH] tests: Add tests that nest geoJSON in a CDT. (#150) --- .../restclient/RecordPostCorrectTests.java | 36 +++++++++-- .../converters/BinConverterTests.java | 36 ++++++++--- ...MsgpackPost.java => MsgpackPostTests.java} | 60 +++++++++++++++++-- 3 files changed, 114 insertions(+), 18 deletions(-) rename src/test/java/com/aerospike/restclient/msgpack/{MsgpackPost.java => MsgpackPostTests.java} (80%) diff --git a/src/test/java/com/aerospike/restclient/RecordPostCorrectTests.java b/src/test/java/com/aerospike/restclient/RecordPostCorrectTests.java index 26c383e1..5f6b77d6 100644 --- a/src/test/java/com/aerospike/restclient/RecordPostCorrectTests.java +++ b/src/test/java/com/aerospike/restclient/RecordPostCorrectTests.java @@ -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; @@ -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 } }; @@ -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 binMap = new HashMap<>(); + Map geoJson = new HashMap<>(); + List 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 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 actualGeoJsonList = (List) record.bins.get("geo_json_list"); + Assert.assertEquals(((Value.GeoJSONValue) actualGeoJsonList.get(0)).getObject(), geoStr); + } + @Test public void PostBoolean() throws Exception { Map binMap = new HashMap<>(); diff --git a/src/test/java/com/aerospike/restclient/converters/BinConverterTests.java b/src/test/java/com/aerospike/restclient/converters/BinConverterTests.java index afc7f14e..356ea97e 100644 --- a/src/test/java/com/aerospike/restclient/converters/BinConverterTests.java +++ b/src/test/java/com/aerospike/restclient/converters/BinConverterTests.java @@ -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; @@ -43,12 +44,12 @@ public void testStringBin() { @Test public void testLongBin() { - singleObjectBinTest(5l); + singleObjectBinTest(5L); } @Test public void testFloatBin() { - singleObjectBinTest(5l); + singleObjectBinTest(5L); } @Test @@ -56,7 +57,7 @@ public void testMapBin() { Map testMap = new HashMap<>(); testMap.put("str", "hello"); testMap.put("float", 3.14); - testMap.put("float", 5l); + testMap.put("long", 5L); singleObjectBinTest(testMap); } @@ -66,13 +67,13 @@ public void testComplexCDTBin() { Map nestedMap = new HashMap<>(); nestedMap.put("str", "world"); nestedMap.put("int", 10); - nestedMap.put("float", 5l); + nestedMap.put("long", 5L); List 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); @@ -83,6 +84,7 @@ 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\"}")); @@ -90,7 +92,6 @@ public void testGeoJSONBin() { 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 @@ -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 binMap = new HashMap<>(); binMap.put(bin1.name, bin1.value.getObject()); @@ -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 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 geoVal = new HashMap<>(); + geoVal.put("type", "Point"); + geoVal.put("coordinates", new double[]{-122.0, 37.5}); + Map mapBin = new HashMap<>(); + mapBin.put("map", geoVal); + + Map testMapBin = new HashMap<>(); + testMapBin.put("map", Value.getAsGeoJSON(mapper.writeValueAsString(geoVal))); + Bin testBin = new Bin("bin1", testMapBin); + Map binMap = new HashMap<>(); + binMap.put("bin1", mapBin); Bin[] bins = BinConverter.binsFromMap(binMap); Assert.assertTrue(binsContain(bins, testBin)); } diff --git a/src/test/java/com/aerospike/restclient/msgpack/MsgpackPost.java b/src/test/java/com/aerospike/restclient/msgpack/MsgpackPostTests.java similarity index 80% rename from src/test/java/com/aerospike/restclient/msgpack/MsgpackPost.java rename to src/test/java/com/aerospike/restclient/msgpack/MsgpackPostTests.java index a4889417..87d98157 100644 --- a/src/test/java/com/aerospike/restclient/msgpack/MsgpackPost.java +++ b/src/test/java/com/aerospike/restclient/msgpack/MsgpackPostTests.java @@ -53,7 +53,7 @@ @RunWith(SpringRunner.class) @SpringBootTest -public class MsgpackPost { +public class MsgpackPostTests { private MockMvc mockMVC; @@ -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> sOMapType = new TypeReference>() { + private final TypeReference> sOMapType = new TypeReference<>() { }; @Before @@ -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 retMap = (Map) 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 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 retRec = mapper.readValue(resContent, sOMapType); + + @SuppressWarnings("unchecked") Map bins = (Map) retRec.get("bins"); + Map map = (Map) 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(); @@ -192,14 +242,14 @@ public void testStoringMapWithIntkeys() throws Exception { @SuppressWarnings("unchecked") Map retMap = (Map) 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 iMap = new HashMap<>(); - iMap.put(1l, 2l); + iMap.put(1L, 2L); client.put(null, testKey, new Bin("map", iMap)); @@ -218,7 +268,7 @@ public void testGettingMapWithIntKey() throws Exception { @SuppressWarnings("unchecked") Map lMap = (Map) bins.get("map"); Assert.assertEquals(lMap.size(), 1); - Assert.assertEquals(lMap.get(1L), 2l); + Assert.assertEquals(lMap.get(1L), 2L); }