@Test public void testPush() throws IOException { jsondb.push("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino" ))); jsondb.push("/test", mapper.writeValueAsString(map( "name", "Ana Chirino" ))); String json = jsondb.getAsString("/test", prettyPrint); @SuppressWarnings("unchecked") List<Map<?, ?>> items = new ArrayList<>(mapper.readValue(json, LinkedHashMap.class).values()); assertThat(items).hasSize(2); assertThat((items.get(0)).get("name")).isEqualTo("Hiram Chirino"); assertThat((items.get(1)).get("name")).isEqualTo("Ana Chirino"); }
private String load(String file) throws IOException { try (InputStream is = getClass().getClassLoader().getResourceAsStream(file)) { ByteArrayOutputStream os = new ByteArrayOutputStream(); copy(is, os); return new String(os.toByteArray(), StandardCharsets.UTF_8); } }
@Test public void testArrays() throws IOException { jsondb.set("/", mapper.writeValueAsString(new Object[]{"hi", 100})); assertThat(jsondb.getAsString("", prettyPrint).trim()).isEqualTo("[ \"hi\", 100 ]"); HashMap<String, Object> user = map( "data", new Object[]{"hi", 100, "other"} ); jsondb.set("/", mapper.writeValueAsString(user)); String result6 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result6).isEqualTo(load("result6.json").trim()); jsondb.set("/data/1", mapper.writeValueAsString("update")); String result7 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result7).isEqualTo(load("result7.json").trim()); // Validate the large arrays stay sorted. jsondb.set("/", mapper.writeValueAsString(new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13})); String result8 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result8).isEqualTo(load("result8.json").trim()); }
@Test public void testPush() throws IOException { jsondb.push("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino" ))); jsondb.push("/test", mapper.writeValueAsString(map( "name", "Ana Chirino" ))); String json = jsondb.getAsString("/test", prettyPrint); @SuppressWarnings("unchecked") List<Map<?, ?>> items = new ArrayList<>(mapper.readValue(json, LinkedHashMap.class).values()); assertThat(items).hasSize(2); assertThat((items.get(0)).get("name")).isEqualTo("Hiram Chirino"); assertThat((items.get(1)).get("name")).isEqualTo("Ana Chirino"); }
@Test public void testSet() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false, "admin", true, "age", 25, "gpa", 3.52, "token", null ); jsondb.set("/", mapper.writeValueAsString(user)); String result3 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result3).isEqualTo(load("result3.json").trim()); // Verify that if we write over an existing /developer value, it get replaced // with an object jsondb.set("/developer/users/u1000", mapper.writeValueAsString(user)); String result4 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result4).isEqualTo(load("result4.json").trim()); // Writing a new object wipe the previous values at that path. HashMap<String, Object> user2 = map( "name", "Hiram", "city", "Tampa" ); jsondb.set("/", mapper.writeValueAsString(user2)); String result5 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result5).isEqualTo(load("result5.json").trim()); }
@Test public void testArrayOfObject() throws IOException { Object[] original = new Object[]{map( "id", "foo" )}; jsondb.set("/test", mapper.writeValueAsString(original)); String result1 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result1).isEqualTo("{\n" + " \"test\" : [ {\n" + " \"id\" : \"foo\"\n" + " } ]\n" + "}".trim()); }
@Test public void testDataTypes() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false, "admin", true, "age", 25, "gpa", 3.52, "token", null ); jsondb.set("/users/u1000", mapper.writeValueAsString(user)); String result1 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result1).isEqualTo(load("result1.json").trim()); String result2 = jsondb.getAsString("/", prettyPrint).trim(); assertThat(result2).isEqualTo(result1); String result3 = jsondb.getAsString("/users/u1000", prettyPrint).trim(); assertThat(result3).isEqualTo(load("result3.json").trim()); assertThat(jsondb.getAsString("/users/u1000/name")).isEqualTo("\"Joe\""); assertThat(jsondb.getAsString("/users/u1000/developer")).isEqualTo("false"); assertThat(jsondb.getAsString("/users/u1000/admin")).isEqualTo("true"); assertThat(jsondb.getAsString("/users/u1000/age")).isEqualTo("25"); assertThat(jsondb.getAsString("/users/u1000/gpa")).isEqualTo("3.52"); assertThat(jsondb.getAsString("/users/u1000/token")).isEqualTo("null"); assertThat(jsondb.getAsString("/users/u1000/error")).isNull(); }
@Test public void testArrayOfObject() throws IOException { Object[] original = new Object[]{map( "id", "foo" )}; jsondb.set("/test", mapper.writeValueAsString(original)); String result1 = jsondb.getAsString("", prettyPrint).trim(); assertThat(result1).isEqualTo("{\n" + " \"test\" : [ {\n" + " \"id\" : \"foo\"\n" + " } ]\n" + "}".trim()); }
@Test public void testExists() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false ); jsondb.set("/", mapper.writeValueAsString(user)); assertThat(jsondb.exists("/badpath")).isFalse(); assertThat(jsondb.exists("/name")).isTrue(); }
@Test public void testGetDepth2() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino", "props", map( "city", "Tampa", "state", "FL", "more-props", map( "city", "Tampa", "state", "FL" ) ) ))); String json = jsondb.getAsString("/test", new GetOptions().depth(2)); assertThat(json).isEqualTo("{\"name\":\"Hiram Chirino\",\"props\":{\"city\":\"Tampa\",\"state\":\"FL\",\"props\":true}}"); }
@Test public void testUpdate() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino", "props", map( "city", "Tampa", "state", "FL" ) ))); // Verify that only the name fields change and the we can use // a path for the keys. jsondb.update("/test", mapper.writeValueAsString(map( "name", "Ana Chirino", "props/city", "Miami" ))); String json = jsondb.getAsString("/test"); assertThat(json).isEqualTo("{\"name\":\"Ana Chirino\",\"props\":{\"city\":\"Miami\",\"state\":\"FL\"}}"); jsondb.update("/test", mapper.writeValueAsString(map( "props", null ))); json = jsondb.getAsString("/test"); assertThat(json).isEqualTo("{\"name\":\"Ana Chirino\",\"props\":null}"); }
@Test public void testGetDepth2() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino", "props", map( "city", "Tampa", "state", "FL", "more-props", map( "city", "Tampa", "state", "FL" ) ) ))); String json = jsondb.getAsString("/test", new GetOptions().depth(2)); assertThat(json).isEqualTo("{\"name\":\"Hiram Chirino\",\"props\":{\"city\":\"Tampa\",\"state\":\"FL\",\"props\":true}}"); }
@Test public void testExists() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false ); jsondb.set("/", mapper.writeValueAsString(user)); assertThat(jsondb.exists("/badpath")).isFalse(); assertThat(jsondb.exists("/name")).isTrue(); }
@Test public void testUpdate() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino", "props", map( "city", "Tampa", "state", "FL" ) ))); // Verify that only the name fields change and the we can use // a path for the keys. jsondb.update("/test", mapper.writeValueAsString(map( "name", "Ana Chirino", "props/city", "Miami" ))); String json = jsondb.getAsString("/test"); assertThat(json).isEqualTo("{\"name\":\"Ana Chirino\",\"props\":{\"city\":\"Miami\",\"state\":\"FL\"}}"); jsondb.update("/test", mapper.writeValueAsString(map( "props", null ))); json = jsondb.getAsString("/test"); assertThat(json).isEqualTo("{\"name\":\"Ana Chirino\",\"props\":null}"); }
@Test public void testGetMissingKey() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino" ))); String json = jsondb.getAsString("/bar"); assertThat(json).isNull(); }
@Test public void testGetStartAfter() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().startAfter("user3")); assertThat(json).isEqualTo("{\"user4\":\"test 4\",\"user5\":\"test 5\",\"user6\":\"test 6\"}"); }
@Test public void testGetEndAt() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().endAt("user4")); assertThat(json).isEqualTo("{\"user1\":\"test 1\",\"user2\":\"test 2\",\"user3\":\"test 3\",\"user4\":\"test 4\"}"); }
@Test public void testGetEndBefore() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().endBefore("user5")); assertThat(json).isEqualTo("{\"user1\":\"test 1\",\"user2\":\"test 2\",\"user3\":\"test 3\",\"user4\":\"test 4\"}"); }
@Test public void testGetMissingKey() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "name", "Hiram Chirino" ))); String json = jsondb.getAsString("/bar"); assertThat(json).isNull(); }
@Test public void testGetStartAt() throws IOException { jsondb.set("/test", mapper.writeValueAsString(map( "user1", "test 1", "user2", "test 2", "user3", "test 3", "user4", "test 4", "user5", "test 5", "user6", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().startAt("user4")); assertThat(json).isEqualTo("{\"user4\":\"test 4\",\"user5\":\"test 5\",\"user6\":\"test 6\"}"); }