JsonValueParser { public static <A, O, V> Consumer<String> fromString(final DittoJsonHandler<A, O, V> jsonHandler) { return jsonString -> tryToParseJsonValue(jsonString, jsonHandler); } private  JsonValueParser(); static Consumer<String> fromString(final DittoJsonHandler<A, O, V> jsonHandler); static Function<String, JsonValue> fromString(); static Function<Reader, JsonValue> fromReader();  }
@Test public void checkExceptionHandlingWithCustomDittoJsonHandler() { final String invalidJsonObjectString = "{\"foo\":123"; final DittoJsonHandler<?, ?, ?> jsonHandlerMock = Mockito.mock(DittoJsonHandler.class); final Consumer<String> underTest = JsonValueParser.fromString(jsonHandlerMock); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.accept(invalidJsonObjectString)) .withMessage("Failed to parse JSON string '%s'!", invalidJsonObjectString) .withCauseExactlyInstanceOf(com.eclipsesource.json.ParseException.class); }
@Test public void parseStringToArray() { final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(knownJsonArray.toString()); assertThat(actual).isEqualTo(knownJsonArray); }
@Test public void parseStringToObject() { final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(knownJsonObject.toString()); assertThat(actual).isEqualTo(knownJsonObject); }
@Test public void parseStringToBoolean() { final JsonValue jsonBoolean = JsonValue.of(true); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(jsonBoolean.toString()); assertThat(actual).isEqualTo(jsonBoolean); }
@Test public void parseStringToInt() { final JsonValue jsonInt = JsonValue.of(Integer.MAX_VALUE); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(jsonInt.toString()); assertThat(actual).isEqualTo(jsonInt); }
@Test public void parseStringToLong() { final JsonValue jsonLong = JsonValue.of(Integer.MAX_VALUE + 1L); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(jsonLong.toString()); assertThat(actual).isEqualTo(jsonLong); }
@Test public void parseStringToDecimalNumber() { final JsonValue jsonDouble = JsonValue.of(23.42F); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(jsonDouble.toString()); assertThat(actual).isEqualTo(jsonDouble); }
@Test public void parseStringToString() { final JsonValue jsonString = JsonValue.of("Insprinc haftbandun, infar wîgandun!"); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply(jsonString.toString()); assertThat(actual).isEqualTo(jsonString); }
@Test public void testParseNullLiteralToNull() { final JsonValue jsonNullLiteral = JsonFactory.nullLiteral(); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); final JsonValue actual = underTest.apply("null"); assertThat(actual).isEqualTo(jsonNullLiteral); }
@Test public void wrapsNumberFormatException() { final String jsonLong = BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.TEN).toPlainString(); final Function<String, JsonValue> underTest = JsonValueParser.fromString(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonLong)) .withMessage("Failed to parse JSON string '%s'!", jsonLong) .withCauseExactlyInstanceOf(java.lang.NumberFormatException.class); }
@Test public void wrapsNullPointerException() { final Function<String, JsonValue> underTest = JsonValueParser.fromString(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(null)) .withMessage("Failed to parse JSON string '%s'!", null) .withCauseExactlyInstanceOf(java.lang.NullPointerException.class); }