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); }
public static JsonRepresentation newArray(final int initialSize) { final ArrayNode arrayNode = new ArrayNode(JsonNodeFactory.instance); for (int i = 0; i < initialSize; i++) { arrayNode.addNull(); } return new JsonRepresentation(arrayNode); }
private JsonRepresentation getArrayEnsured(final String path, final JsonNode node) { if (representsNull(node)) { return null; } return new JsonRepresentation(node).ensureArray(); }
private JsonRepresentation getNull(final String path, final JsonNode node) { if (node == null || node.isMissingNode()) { // exclude if node.isNull, cos that's the point of this. return null; } checkValue(path, node, "the null value"); if (!node.isNull()) { throw new IllegalArgumentException(formatExMsg(path, "is not the null value")); } return new JsonRepresentation(node); }
@Test public void forString() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("listOfStrings.json")); final Iterator<String> arrayIterator = jsonRepresentation.arrayIterator(String.class); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("a")); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("b")); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("c")); assertThat(arrayIterator.hasNext(), is(false)); }
@Test public void forString() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("listOfStrings.json")); final Iterator<String> arrayIterator = jsonRepresentation.arrayIterator(String.class); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("a")); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("b")); assertThat(arrayIterator.hasNext(), is(true)); assertThat(arrayIterator.next(), is("c")); assertThat(arrayIterator.hasNext(), is(false)); }
@Test(expected = IllegalStateException.class) public void forList() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("list.json")); jsonRepresentation.mapHas("aString"); }
@Test(expected = IllegalArgumentException.class) public void arraySetElementAt_forAttemptingToSetElementToArray() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("list.json")); jsonRepresentation.arraySetElementAt(0, arrayRepr); }
@Test(expected = IndexOutOfBoundsException.class) public void arrayGet_outOfBounds() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("emptyList.json")); jsonRepresentation.arrayGet(0); }
@Test public void arraySetElementAt_happyCaseWhenSetElementToObject() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("list.json")); jsonRepresentation.arraySetElementAt(0, objectRepr); }
@Test(expected = IndexOutOfBoundsException.class) public void arrayGet_outOfBounds() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("emptyList.json")); jsonRepresentation.arrayGet(0); }
@Test public void size_forEmptyMap() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("emptyMap.json")); assertThat(jsonRepresentation.size(), is(0)); }
@Test public void size_forNonEmptyMap() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); assertThat(jsonRepresentation.size(), is(17)); }
@Test(expected = IndexOutOfBoundsException.class) public void arraySetElementAt_outOfBounds() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("emptyList.json")); jsonRepresentation.arraySetElementAt(0, objectRepr); }
@Test public void forMap() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); assertThat(jsonRepresentation.mapHas("aString"), is(true)); assertThat(jsonRepresentation.mapHas("aSubMap.anInt"), is(true)); assertThat(jsonRepresentation.mapHas("nonExistent"), is(false)); }
@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(expected = IllegalStateException.class) public void size_forValue() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final JsonRepresentation valueRepresentation = jsonRepresentation.getRepresentation("anInt"); valueRepresentation.size(); }
@Test(expected = IllegalStateException.class) public void size_forValue() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final JsonRepresentation valueRepresentation = jsonRepresentation.getRepresentation("anInt"); valueRepresentation.size(); }
@Test(expected = IllegalStateException.class) public void arrayGet_forValue() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final JsonRepresentation valueRepresentation = jsonRepresentation.getRepresentation("anInt"); valueRepresentation.arrayGet(0); }
@Before public void setUp() throws Exception { mockObjectSpec = context.mock(ObjectSpecification.class); mockEncodableFacet = context.mock(EncodableFacet.class); mockObjectAdapter = context.mock(ObjectAdapter.class); mockAdapterManager = context.mock(AdapterManager.class); JsonValueEncoder.testSetAdapterManager(mockAdapterManager); representation = new JsonRepresentation(TextNode.valueOf("aString")); }