private JsonRepresentation getMembersOfType(String memberTypeOf) { final JsonRepresentation members = getRepresentation("members"); return JsonRepresentation.newMap().mapPut( Iterables.filter(members.mapIterable(), havingMemberTypeOf(memberTypeOf))); }
@Override public UserReprRenderer with(final AuthenticationSession authenticationSession) { representation.mapPut("userName", authenticationSession.getUserName()); final JsonRepresentation roles = JsonRepresentation.newArray(); for (final String role : authenticationSession.getRoles()) { roles.arrayAdd(role); } representation.mapPut("roles", roles); return this; }
public JsonRepresentation getRepresentation(final String pathTemplate, final Object... args) { final String pathStr = String.format(pathTemplate, args); final JsonNode node = getNode(pathStr); if (representsNull(node)) { return null; } return new JsonRepresentation(node); }
private void addLinkToUp() { final JsonRepresentation link = LinkBuilder.newBuilder(rendererContext, Rel.UP.getName(), RepresentationType.HOME_PAGE, "").build(); final LinkFollowSpecs linkFollower = getLinkFollowSpecs().follow("links"); if (linkFollower.matches(link)) { final HomePageReprRenderer renderer = new HomePageReprRenderer(getRendererContext(), linkFollower, JsonRepresentation.newMap()); link.mapPut("value", renderer.render()); } getLinks().arrayAdd(link); }
@Test public void forMap() throws IOException { final JsonRepresentation mapRepresentation = jsonRepresentation.getRepresentation("aLink"); assertThat(mapRepresentation.getString("rel"), is("someRel")); assertThat(mapRepresentation.isMap(), is(true)); }
@Test public void getProperties() throws JsonParseException, JsonMappingException, IOException { final JsonRepresentation properties = representation.getProperties(); final JsonRepresentation members = representation.getMembers(); assertThat(members.size(), is(12)); assertThat(properties.isMap(), is(true)); assertThat(properties.size(), is(10)); }
@Test public void whenStartingWithList() throws IOException { final JsonRepresentation listRepresentation = jsonRepresentation.getRepresentation("anotherSubMap.aListOfLinks"); final JsonRepresentation representation = listRepresentation.getRepresentation("[rel=multiRel method=GET]"); assertThat(representation.isArray(), is(true)); assertThat(representation.size(), is(2)); }
@Test public void whenCharWrapper() throws Exception { allowingObjectSpecToReturnSpecIdFor(Character.class); allowingObjectAdapterToReturn(Character.valueOf('a')); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, null, false); assertThat(representation.isString("value"), is(true)); assertThat(representation.getChar("value"), is(Character.valueOf('a'))); assertThat(representation.getString("format"), is(nullValue())); assertThat(representation.getString("extensions.x-isis-format"), is("char")); }
@Test public void forListWithSingleCriteriaMatchingOne() throws IOException { final JsonRepresentation representation = jsonRepresentation.getRepresentation("anotherSubMap.aListOfLinks[rel=aRel]"); assertThat(representation.isLink(), is(true)); assertThat(representation.asLink().getHref(), is("http://foo/bar")); }
@Test(expected = IllegalStateException.class) public void size_forValue() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final JsonRepresentation valueRepresentation = jsonRepresentation.getRepresentation("anInt"); valueRepresentation.size(); }
@Test public void whenBigInteger() throws Exception { allowingObjectSpecToReturnSpecIdFor(BigInteger.class); allowingObjectAdapterToReturn(new BigInteger("12345678901234567890")); JsonValueEncoder.appendValueAndFormat(mockObjectSpec, mockObjectAdapter, representation, "big-integer(22)", false); assertThat(representation.isString("value"), is(true)); assertThat(representation.isBigInteger("value"), is(true)); assertThat(representation.getBigInteger("value"), is(new BigInteger("12345678901234567890"))); assertThat(representation.getString("format"), is("big-integer(22)")); assertThat(representation.getString("extensions.x-isis-format"), is("javamathbiginteger")); }
@Test public void size_forEmptyMap() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("emptyMap.json")); assertThat(jsonRepresentation.size(), is(0)); }
/** * REVIEW - looks similar to above methods, but now unused; can it be deleted? */ public static JsonRepresentation readParameterMapAsMap(final Map<String, String[]> parameterMap) { final JsonRepresentation map = JsonRepresentation.newMap(); for (final Map.Entry<String, String[]> parameter : parameterMap.entrySet()) { map.mapPut(parameter.getKey(), parameter.getValue()[0]); } return map; }
@Test public void forList() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("list.json")); assertThat(jsonRepresentation.isArray(), is(true)); assertThat(jsonRepresentation.isMap(), is(false)); assertThat(jsonRepresentation.isValue(), is(false)); }
@Test public void newArrayInitialSize() throws Exception { final JsonRepresentation jsonRepresentation = JsonRepresentation.newArray(2); assertThat(jsonRepresentation.size(), is(2)); assertThat(jsonRepresentation.arrayGet(0).isNull(), is(true)); assertThat(jsonRepresentation.arrayGet(1).isNull(), is(true)); }