public static JsonMap jsonMapFrom(final Object map) { if (map instanceof JsonMap) { return (JsonMap) map; } else { return new JsonMap(map); } }
public long getLong(final String key, final long defaultValue) { final Long result = getLong(key); return result != null ? result : defaultValue; }
public Integer getInt(final String key, final Integer defaultValue) { final Integer result = getInt(key); return result != null ? result : defaultValue; }
@Test public void shouldRemoveKeyFromInnerObject() { final Map<String, ?> map = singletonMap("key", new HashMap<String, String>() {{ put("removeMe", "hello world"); }}); final JsonMap jsonMap = jsonMapFrom(map); jsonMap.get("key").remove("removeMe"); assertThat(jsonMap, is(jsonMapFrom(singletonMap("key", emptyMap())))); }
@Test public void shouldGetNullForMissingString() { final Map<String, ?> map = singletonMap("key", null); assertThat(jsonMapFrom(map).getString("key"), is(nullValue())); assertThat(jsonMapFrom(map).getString("doesnotexist"), is(nullValue())); }
@Test public void shouldGetStringFromBoolean() { final Map<String, ?> map = new HashMap<String, Boolean>() {{ put("somethingTrue", TRUE); put("somethingFalse", FALSE); }}; assertThat(jsonMapFrom(map).getString("somethingTrue"), is("true")); assertThat(jsonMapFrom(map).getString("somethingFalse"), is("false")); }
@Test public void shouldRemoveKeyFromTopLevel() { final Map<String, ?> map = new HashMap<String, String>() {{ put("removeMe", "hello world"); }}; final JsonMap jsonMap = jsonMapFrom(map); jsonMap.remove("removeMe"); assertThat(jsonMap, is(jsonMapFrom(emptyMap()))); }
@Test public void shouldFallbackOnDefaultBoolean() { final JsonMap empty = jsonMapFrom(new HashMap<String, Object>()); assertThat(empty.getBoolean("key", TRUE), is(TRUE)); }
@Test public void shouldGetLongFromString() { final Map<String, ?> map = singletonMap("key", "1"); assertThat(jsonMapFrom(map).getLong("key"), is(1L)); }
public String getString(final String key, final String defaultValue) { final String result = getString(key); return result != null ? result : defaultValue; }
@Test public void shouldReturnNullIfInstantValueIsNotPresentAndNoDefaultSpecified() { final JsonMap empty = jsonMapFrom(new HashMap<String, Object>()); assertThat(empty.getInstant("key"), is(nullValue())); }
@Test public void shouldThrowExceptionWhenGettingDateFromWrongStringFormat() { final Map<String, ?> map = singletonMap("key", "Tue Jul 13 00:00:00"); assertThrows(DateTimeParseException.class, () -> { jsonMapFrom(map).getDate("key"); }); }
public Double getDouble(final String key, final double defaultValue) { final Double result = getDouble(key); return result != null ? result : defaultValue; }
public boolean getBoolean(final String key, final boolean defaultValue) { final Boolean result = getBoolean(key); return result != null ? result : defaultValue; }
public static TestClock now(final long millis) { return new TestClock(Instant.ofEpochMilli(millis)); }
public Float getFloat(final String key, final float defaultValue) { final Float result = getFloat(key); return result != null ? result : defaultValue; }
@Test public void shouldGetString() { final Map<String, ?> map = singletonMap("key", "value"); assertThat(jsonMapFrom(map).getString("key"), is("value")); }
public static TestClock now() { return new TestClock(Instant.now()); }
public static JsonMap jsonMapFrom(final Map<?,?> map) { return new JsonMap(map); }
public static TestClock now(final Clock clock) { return new TestClock(Instant.now(clock)); }