public Instant getInstant(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final String value = getString(key); return value != null ? parse(value) : null; } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
public boolean getBoolean(final String key, final boolean defaultValue) { final Boolean result = getBoolean(key); return result != null ? result : defaultValue; }
public JsonMap get(final String s) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { String[] segments = s.split("\\."); if (segments.length < 2) { final Object object = ((Map) jsonObject).get(s); return object != null ? new JsonMap(object) : null; } else { return get(segments[0]).get(s.substring(s.indexOf(".")+1)); } } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
@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 shouldCreateStatusRepresentationWithDetailInclUrl() { // given final StatusRepresentation json = statusRepresentationOf( applicationStatus( mock(ApplicationInfo.class), mock(ClusterInfo.class), mock(SystemInfo.class), mock(VersionInfo.class), mock(TeamInfo.class), singletonList( statusDetail("someDetail", OK, "some message", link("item", "http://example.org/some/url", "some title")) ) ) ); // then assertThat(json.application.status, is(OK)); final JsonMap jsonMap = jsonMapFrom(json.application.statusDetails.get("someDetail")); final JsonMap link = jsonMap.get("links").asListOf(JsonMap.class).get(0); assertThat(link.getString("href"), is("http://example.org/some/url")); assertThat(link.getString("title"), is("some title")); assertThat(link.getString("rel"), is("item")); }
@Test public void shouldGetInnerValueByDottedKeys() { /** * { * "outer" : { * "innter": { * "key" : "value" * } * } */ final Map<String, ?> inner = singletonMap("key", "value"); final Map<String, ?> map = singletonMap("outer", singletonMap("inner", inner)); assertThat(jsonMapFrom(map).get("outer.inner").getString("key"), is("value")); }
@Test public void shouldGetLongFromString() { final Map<String, ?> map = singletonMap("key", "1"); assertThat(jsonMapFrom(map).getLong("key"), is(1L)); }
@Test public void shouldFallbackOnDefaultInt() { final JsonMap empty = jsonMapFrom(new HashMap<String, Object>()); assertThat(empty.getInt("key", 1), is(1)); }
@Test public void shouldFallbackOnDefaultBoolean() { final JsonMap empty = jsonMapFrom(new HashMap<String, Object>()); assertThat(empty.getBoolean("key", TRUE), is(TRUE)); }
@Test public void shouldGetDouble() { final Map<String, ?> map = singletonMap("key", 2.0); assertThat(jsonMapFrom(map).getDouble("key"), is(2.0)); }
@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())))); }
public Date getDate(final String key) { if (jsonObject == null) { throw new NullPointerException("json object is null"); } if (is(Map.class)) { final JsonMap value = get(key); if (value == null) { return null; } else { if (value.is(Date.class)) { return (Date) value.jsonObject; } if (value.is(String.class)) { return from(parse(value.toString())); } else { throw new ClassCastException("Value of " + key + " is not a date but a " + value.getClass().getSimpleName()); } } } else { throw new IllegalArgumentException("not a map but a " + jsonObject.getClass().getSimpleName()); } }
@Test public void shouldGetInnerMap() { final Map<String, ?> inner = singletonMap("key", "value"); final Map<String, ?> map = singletonMap("inner", inner); assertThat(jsonMapFrom(map).get("inner"), is(jsonMapFrom(inner))); }
@Test public void shouldGetDate() { final Date date = new Date(); final Map<String, ?> map = singletonMap("key", date); assertThat(jsonMapFrom(map).getDate("key"), is(date)); }
@Test public void shouldReturnNullIfInstantValueIsNotPresentAndNoDefaultSpecified() { final JsonMap empty = jsonMapFrom(new HashMap<String, Object>()); assertThat(empty.getInstant("key"), is(nullValue())); }
public String getString(final String key, final String defaultValue) { final String result = getString(key); return result != null ? result : defaultValue; }
@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()))); }
public Integer getInt(final String key, final Integer defaultValue) { final Integer result = getInt(key); return result != null ? result : defaultValue; }
public long getLong(final String key, final long defaultValue) { final Long result = getLong(key); return result != null ? result : defaultValue; }