@Before public void before() { JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=PostgreSQL"); this.dbi = new DBI(ds); this.jsondb = new SqlJsonDB(dbi, null); try { this.jsondb.dropTables(); } catch (Exception e) { } this.jsondb.createTables(); }
@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()); }
@Override public boolean exists(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = countJsonRecords(dbi, like) > 0; }); return rc[0]; }
@Override public boolean delete(String path) { String baseDBPath = JsonRecordSupport.convertToDBPath(path); String like = baseDBPath+"%"; boolean rc[] = new boolean[]{false}; withTransaction(dbi -> { rc[0] = deleteJsonRecords(dbi, baseDBPath, like) > 0; }); if( bus!=null && rc[0] ) { bus.broadcast("jsondb-deleted", prefix(trimSuffix(path, "/"), "/")); } return rc[0]; }
@Test public void testDelete() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false ); jsondb.set("/", mapper.writeValueAsString(user)); Map<?, ?> result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(2); assertThat(jsondb.delete("/badpath")).isFalse(); result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(2); assertThat(jsondb.delete("/name")).isTrue(); result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(1); }
@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 testGetLimitDeeper() throws IOException { jsondb.update("/test", mapper.writeValueAsString(map( "user1/value", "test 1", "user2/value", "test 2", "user3/value", "test 3", "user4/value", "test 4", "user5/value", "test 5", "user6/value", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().limitToFirst(3)); assertThat(json).isEqualTo("{\"user1\":{\"value\":\"test 1\"},\"user2\":{\"value\":\"test 2\"},\"user3\":{\"value\":\"test 3\"}}"); }
@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 testGetMetricsForIntegration1() throws IOException { String json = jsondb.getAsString(JsonDBRawMetrics.path("intId1"), new GetOptions().prettyPrint(true)); Map<String,RawMetrics> metrics = Json.reader().forType(new TypeReference<Map<String,RawMetrics>>() {}).readValue(json); assertThat(metrics.size()).isEqualTo(3); assertThat(metrics.keySet()).contains("HISTORY1"); }
@Test public void shouldAscertainPropertyPairExistence() { jsondb.set("/pair/:id", "{\"key\": \"value\"}"); assertThat(jsondb.fetchIdsByPropertyValue("/pair", "key", "value")).containsOnly("/pair/:id"); assertThat(jsondb.fetchIdsByPropertyValue("/pair", "key", "nope")).isEmpty(); }
@Test public void testInvalidKeys() throws IOException { for (String s : Arrays.asList("[", "]", ".", "%", "$", "#", "\n")) { try { jsondb.set("/test"+s, mapper.writeValueAsString(map( "key", "Hiram Chirino" ))); fail("Excpected JsonDBException"); } catch (JsonDBException e) { assertThat(e.getMessage()).startsWith("Invalid key."); } } for (String s : Arrays.asList("[", "]", ".", "%", "$", "#", "/", "\n")) { try { jsondb.set("/test", mapper.writeValueAsString(map( "bad"+s+"key", "Hiram Chirino" ))); fail("Excpected JsonDBException"); } catch (JsonDBException e) { assertThat(e.getMessage()).startsWith("Invalid key."); } } }
@Test public void testDeadPodCurator() throws IOException, ParseException { String integrationId = "intId1"; MetricsCollector collector = new MetricsCollector(null, jsondb, null); //Update pod1 metrics and kill pod1 Set<String> livePodIds = new HashSet<String>( Arrays.asList("pod2", "pod3", "pod4", "pod5")); jsondb.update(JsonDBRawMetrics.path("intId1","pod1"), Json.writer().writeValueAsString(raw("intId1","1","pod1",12L,"31-01-2018 10:22:56"))); Map<String,RawMetrics> metrics = jsondbRM.getRawMetrics(integrationId); IntegrationMetricsSummary summary = intMH .compute(integrationId, metrics, livePodIds); assertThat(summary.getMessages()).isEqualTo(18); assertThat(summary.getErrors()).isEqualTo(3); //Oldest living pod is now pod2 assertThat(summary.getStart().get()).isEqualTo(sdf.parse("31-01-2018 10:22:56")); collector.close(); }
@Test public void testDelete() throws IOException { HashMap<String, Object> user = map( "name", "Joe", "developer", false ); jsondb.set("/", mapper.writeValueAsString(user)); Map<?, ?> result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(2); assertThat(jsondb.delete("/badpath")).isFalse(); result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(2); assertThat(jsondb.delete("/name")).isTrue(); result = mapper.readValue(jsondb.getAsString(""), HashMap.class); assertThat(result).hasSize(1); }
@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 testGetLimitDeeper() throws IOException { jsondb.update("/test", mapper.writeValueAsString(map( "user1/value", "test 1", "user2/value", "test 2", "user3/value", "test 3", "user4/value", "test 4", "user5/value", "test 5", "user6/value", "test 6" ))); String json = jsondb.getAsString("/test", new GetOptions().limitToFirst(3)); assertThat(json).isEqualTo("{\"user1\":{\"value\":\"test 1\"},\"user2\":{\"value\":\"test 2\"},\"user3\":{\"value\":\"test 3\"}}"); }
@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"); }
.pollInterval(1, SECONDS) .untilAsserted(() -> { String db = jsondb.getAsString("/", new GetOptions().prettyPrint(true)); assertThat(db).isEqualTo(expectedDBState); });