AbstractNode { @Nonnull public N getObject(@Nonnull String fieldName) { DockerCloudUtils.requireNonNull(fieldName, "Field name cannot be null."); checkObject(); JsonNode object = node.get(fieldName); if (!(object instanceof ObjectNode)) { throw new NodeProcessingException("Child field not found or not an object: " + node + " / " + fieldName); } return newNode(object); } AbstractNode(JsonNode node); @Nonnull List<N> getArrayValues(); @Nonnull Map<String, N> getObjectValues(); @Nonnull N getObject(@Nonnull String fieldName); N getObject(@Nonnull String fieldName, @Nullable N def); N getArray(@Nonnull String fieldName); N getArray(@Nonnull String fieldName, @Nullable N def); int getAsInt(@Nonnull String fieldName); int getAsInt(@Nonnull String fieldName, int def); BigInteger getAsBigInt(@Nonnull String fieldName); BigInteger getAsBigInt(@Nonnull String fieldName, BigInteger def); long getAsLong(@Nonnull String fieldName); boolean getAsBoolean(@Nonnull String fieldName); Boolean getAsBoolean(@Nonnull String fieldName, @Nullable Boolean def); @Nonnull String getAsString(@Nonnull String fieldName); String getAsString(@Nonnull String fieldName, @Nullable String def); @Nonnull String getAsString(); boolean isNull(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void getObject() { AbstractNode<N> node = parentWithChildObject(pair("A", "1")); assertThat(node.getObject("child").getAsString("A")).isEqualTo("1"); assertThat(node.getObject("unknownChild", null)).isNull(); @SuppressWarnings("unchecked") N def = (N) emptyNode(); assertThat(node.getObject("unknownChild", def)).isSameAs(def); }
@Test public void getObjectNotAnObject() { assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> parentWithChildArray("A", "B", "C").getObject("child")); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> stringNode("A").getObject("child")); }