diff options
author | freva <valerijf@yahoo-inc.com> | 2017-02-20 09:09:54 +0100 |
---|---|---|
committer | freva <valerijf@yahoo-inc.com> | 2017-02-20 09:09:54 +0100 |
commit | c0383b9ce33d6393528f06392600a3d6ee443c00 (patch) | |
tree | da618a26f1f698294c43657159d9d8da09bcbed3 /document | |
parent | 1af437c88da001d9ea12aac47a56ea9e22c910d5 (diff) |
Add tests for old format
Diffstat (limited to 'document')
-rw-r--r-- | document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java | 68 | ||||
-rw-r--r-- | document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java | 57 |
2 files changed, 125 insertions, 0 deletions
diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java index 7ad68be6b65..926317a3220 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java @@ -494,6 +494,27 @@ public class JsonReaderTestCase { } @Test + public final void testOldMap() throws IOException { + InputStream rawDoc = new ByteArrayInputStream( + Utf8.toBytes("{\"put\": \"id:unittest:testmap::whee\"," + + " \"fields\": { \"actualmap\": [" + + " { \"key\": \"nalle\", \"value\": \"kalle\"}," + + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}}")); + JsonReader r = new JsonReader(types, rawDoc, parserFactory); + DocumentParseInfo parseInfo = r.parseDocument().get(); + DocumentType docType = r.readDocumentType(parseInfo.documentId); + DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId)); + new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put); + Document doc = put.getDocument(); + FieldValue f = doc.getFieldValue(doc.getField("actualmap")); + assertSame(MapFieldValue.class, f.getClass()); + MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f; + assertEquals(2, m.size()); + assertEquals(new StringFieldValue("kalle"), m.get(new StringFieldValue("nalle"))); + assertEquals(new StringFieldValue("skalle"), m.get(new StringFieldValue("tralle"))); + } + + @Test public final void testPositionPositive() throws IOException { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"put\": \"id:unittest:testsinglepos::bamf\"," @@ -571,6 +592,29 @@ public class JsonReaderTestCase { } @Test + public final void testOldMapStringToArrayOfInt() throws IOException { + InputStream rawDoc = new ByteArrayInputStream( + Utf8.toBytes("{\"put\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + + " \"fields\": { \"actualMapStringToArrayOfInt\": [" + + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + + "]}}")); + JsonReader r = new JsonReader(types, rawDoc, parserFactory); + DocumentParseInfo parseInfo = r.parseDocument().get(); + DocumentType docType = r.readDocumentType(parseInfo.documentId); + DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId)); + new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put); + Document doc = put.getDocument(); + FieldValue f = doc.getFieldValue("actualMapStringToArrayOfInt"); + assertSame(MapFieldValue.class, f.getClass()); + MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f; + Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse")); + assertEquals(3, a.size()); + assertEquals(new IntegerFieldValue(1), a.get(0)); + assertEquals(new IntegerFieldValue(2), a.get(1)); + assertEquals(new IntegerFieldValue(3), a.get(2)); + } + + @Test public final void testAssignToString() throws IOException { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"update\": \"id:unittest:smoke::whee\"," @@ -610,6 +654,30 @@ public class JsonReaderTestCase { } @Test + public final void testOldAssignToArray() throws IOException { + InputStream rawDoc = new ByteArrayInputStream( + Utf8.toBytes("{\"update\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + + " \"fields\": { \"actualMapStringToArrayOfInt\": {" + + " \"assign\": [" + + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + + "]}}}")); + JsonReader r = new JsonReader(types, rawDoc, parserFactory); + DocumentParseInfo parseInfo = r.parseDocument().get(); + DocumentType docType = r.readDocumentType(parseInfo.documentId); + DocumentUpdate doc = new DocumentUpdate(docType, parseInfo.documentId); + new VespaJsonDocumentReader().readUpdate(parseInfo.fieldsBuffer, doc); + FieldUpdate f = doc.getFieldUpdate("actualMapStringToArrayOfInt"); + assertEquals(1, f.size()); + AssignValueUpdate assign = (AssignValueUpdate) f.getValueUpdate(0); + MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) assign.getValue(); + Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse")); + assertEquals(3, a.size()); + assertEquals(new IntegerFieldValue(1), a.get(0)); + assertEquals(new IntegerFieldValue(2), a.get(1)); + assertEquals(new IntegerFieldValue(3), a.get(2)); + } + + @Test public final void testAssignToWeightedSet() throws IOException { InputStream rawDoc = new ByteArrayInputStream( Utf8.toBytes("{\"update\": \"id:unittest:testset::whee\"," diff --git a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java index 2e5989ecbbf..989b0f2c952 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonWriterTestCase.java @@ -34,8 +34,12 @@ import org.junit.Test; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import static com.yahoo.document.json.readers.MapReader.MAP_KEY; +import static com.yahoo.document.json.readers.MapReader.MAP_VALUE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; @@ -236,6 +240,39 @@ public class JsonWriterTestCase { } @Test + public void oldMapTest() throws IOException { + String fields = "{ \"actualmap\": [" + + " { \"key\": \"nalle\", \"value\": \"kalle\"}," + + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}"; + String docId = "id:unittest:testmap::whee"; + Document doc = readDocumentFromJson(docId, fields); + // we have to do everything by hand to check, as maps are unordered, but + // are serialized as an ordered structure + + ObjectMapper m = new ObjectMapper(); + Map<?, ?> generated = m.readValue(JsonWriter.toByteArray(doc), Map.class); + assertEquals(docId, generated.get("id")); + // and from here on down there will be lots of unchecked casting and + // other fun. This is OK here, because if the casts fail, the should and + // will fail anyway + List<?> inputMap = (List<?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualmap"); + Map<?, ?> generatedMap = (Map<?, ?>) ((Map<?, ?>) generated.get("fields")).get("actualmap"); + assertEquals(populateMap(inputMap), generatedMap); + } + + // should very much blow up if the assumptions are incorrect + @SuppressWarnings("rawtypes") + private Map<Object, Object> populateMap(List<?> actualMap) { + Map<Object, Object> m = new HashMap<>(); + for (Object o : actualMap) { + Object key = ((Map) o).get(MAP_KEY); + Object value = ((Map) o).get(MAP_VALUE); + m.put(key, value); + } + return m; + } + + @Test public final void rawTest() throws IOException { String payload = new String( new JsonStringEncoder().quoteAsString(new Base64() @@ -269,6 +306,26 @@ public class JsonWriterTestCase { assertEquals(inputMap, generatedMap); } + @Test + public final void oldStringToArrayOfIntMapTest() throws IOException { + String docId = "id:unittest:testMapStringToArrayOfInt::whee"; + String fields = "{ \"actualMapStringToArrayOfInt\": [" + + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}"; + Document doc = readDocumentFromJson(docId, fields); + // we have to do everything by hand to check, as maps are unordered, but + // are serialized as an ordered structure + + ObjectMapper m = new ObjectMapper(); + Map<?, ?> generated = m.readValue(JsonWriter.toByteArray(doc), Map.class); + assertEquals(docId, generated.get("id")); + // and from here on down there will be lots of unchecked casting and + // other fun. This is OK here, because if the casts fail, the should and + // will fail anyway + List<?> inputMap = (List<?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualMapStringToArrayOfInt"); + Map<?, ?> generatedMap = (Map<?, ?> ) ((Map<?, ?>) generated.get("fields")).get("actualMapStringToArrayOfInt"); + assertEquals(populateMap(inputMap), generatedMap); + } + private Document readDocumentFromJson(String docId, String fields) throws IOException { InputStream rawDoc = new ByteArrayInputStream(asFeed(docId, fields)); |