@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 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 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(); }