AbstractNode { public int getAsInt(@Nonnull String fieldName) { DockerCloudUtils.requireNonNull(fieldName, "Field name cannot be null."); checkObject(); try { return getAsBigInt(fieldName).intValueExact(); } catch (ArithmeticException e) { throw new NodeProcessingException("Child field not found or is not an integer value node: " + node + " / " + fieldName); } } 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 getAsInt() { AbstractNode<N> node = parentWithChildInt(42); assertThat(node.getAsInt("child")).isEqualTo(42); node = parentWithChildInt(Integer.MAX_VALUE); assertThat(node.getAsInt("child")).isEqualTo(Integer.MAX_VALUE); node = parentWithChildInt(Integer.MIN_VALUE); assertThat(node.getAsInt("child")).isEqualTo(Integer.MIN_VALUE); assertThat(node.getAsInt("unknownChild", 0)).isEqualTo(0); }
@Test public void getAsIntOutOfBound() { AbstractNode<N> nodeMax = parentWithChildLong(Integer.MAX_VALUE + 1L); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeMax.getAsInt("child")); AbstractNode<N> nodeMin = parentWithChildLong(Integer.MIN_VALUE - 1L); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeMin.getAsInt("child")); }
@Test public void getAsIntFromDecimal() { AbstractNode<N> node = parentWithChildDouble(1.0); assertThat(node.getAsInt("child")).isEqualTo(1); node = parentWithChildBigDecimal(new BigDecimal("1E+1")); assertThat(node.getAsInt("child")).isEqualTo(10); AbstractNode<N> nodeWithFraction = parentWithChildBigDecimal(BigDecimal.valueOf(1.1)); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeWithFraction.getAsInt("child")); }
@Test public void getAsIntNotAnInt() { assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> parentWithChildString("A").getAsInt("child")); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> stringNode("A").getAsInt("child")); }