@Test public void constructor_emptyMap_objectWithEmptyJsonShouldBeCreated() { DocumentBody body = new BasicDocumentBody(new HashMap()); Assert.assertTrue(Arrays.equals("{}".getBytes(), body.asBytes())); Assert.assertNotNull(body.asMap()); Assert.assertTrue(body.asMap().size() == 0); }
// Invalid input should result in am object with empty JSON body @Test(expected = IllegalArgumentException.class) public void constructor_invalidInput_objectWithEmptyJsonShouldBeCreated() { DocumentBody body = new BasicDocumentBody("[]".getBytes()); Assert.assertTrue(Arrays.equals("{}".getBytes(), body.asBytes())); Assert.assertNotNull(body.asMap()); Assert.assertTrue(body.asMap().size() == 0); }
@Test public void constructor_byteArray_correctObjectShouldBeCreated() throws Exception { DocumentBody body = new BasicDocumentBody(jsonData); Assert.assertTrue(Arrays.equals(jsonData, body.asBytes())); Assert.assertNotNull(body.asMap()); Map<String, Object> actualMap = body.asMap(); assertMapIsCorrect(actualMap); }
@Test public void asMap_differentNumberTypes_jacksonPicksNaturalMapping() throws IOException { byte[] d = FileUtils.readFileToByteArray( TestUtils.loadFixture("fixture/basic_bdbody_test_as_map.json")); DocumentBody body = new BasicDocumentBody(d); Assert.assertEquals("-101", body.asMap().get("StringValue")); Map<String, Object> m = body.asMap(); Assert.assertTrue(m.get("LongValue") instanceof Long); Assert.assertTrue(m.get("LongValue").equals(2147483648l)); // Integer.MAX_VALUE + 1 Assert.assertTrue(m.get("IntegerValue") instanceof Integer); Assert.assertTrue(m.get("IntegerValue").equals(2147483647)); // Integer.MAX_VALUE }
private void validateDBBody(DocumentBody body) { for (String name : body.asMap().keySet()) { if (name.startsWith("_")) { throw new IllegalArgumentException("Field name start with '_' is not allowed. "); } } }