AbstractNode { public long getAsLong(@Nonnull String fieldName) { DockerCloudUtils.requireNonNull(fieldName, "Field name cannot be null."); checkObject(); try { return getAsBigInt(fieldName).longValueExact(); } 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 getAsLong() { AbstractNode<N> node = parentWithChildInt(42); assertThat(node.getAsLong("child")).isEqualTo(42); node = parentWithChildLong(Long.MAX_VALUE); assertThat(node.getAsLong("child")).isEqualTo(Long.MAX_VALUE); node = parentWithChildLong(Long.MIN_VALUE); assertThat(node.getAsLong("child")).isEqualTo(Long.MIN_VALUE); }
@Test public void getAsLongOutOfBound() { AbstractNode<N> nodeMax = parentWithChildBigInteger(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeMax.getAsLong("child")); AbstractNode<N> nodeMin = parentWithChildBigInteger(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeMin.getAsLong("child")); }
@Test public void getAsLongFromDecimal() { AbstractNode<N> node = parentWithChildDouble(1.0); assertThat(node.getAsLong("child")).isEqualTo(1L); node = parentWithChildBigDecimal(new BigDecimal("1E+1")); assertThat(node.getAsLong("child")).isEqualTo(10L); AbstractNode<N> nodeWithFraction = parentWithChildBigDecimal(BigDecimal.valueOf(1.1)); assertThatExceptionOfType(NodeProcessingException.class). isThrownBy(() -> nodeWithFraction.getAsLong("child")); }