JavaValueFieldDefinition extends AbstractJsonFieldDefinition<T> { public static <T> JavaValueFieldDefinition<T> newInstance(final CharSequence pointer, final Class<T> valueType, final Function<JsonValue, Boolean> checkJavaTypeFunction, final Function<JsonValue, T> mappingFunction, final JsonFieldMarker... markers) { return new JavaValueFieldDefinition<>(pointer, valueType, checkJavaTypeFunction, mappingFunction, markers); } private  JavaValueFieldDefinition(final CharSequence pointer,
            final Class<T> valueType,
            final Function<JsonValue, Boolean> checkJavaTypeFunction,
            final Function<JsonValue, T> mappingFunction,
            final JsonFieldMarker... markers); static JavaValueFieldDefinition<T> newInstance(final CharSequence pointer,
            final Class<T> valueType,
            final Function<JsonValue, Boolean> checkJavaTypeFunction,
            final Function<JsonValue, T> mappingFunction,
            final JsonFieldMarker... markers);  }
@Test(expected = NullPointerException.class) public void tryToCreateNewInstanceWithNullJsonPointer() { JavaValueFieldDefinition.newInstance(null, String.class, JsonValue::isString, JsonValue::asString, knownFieldMarkers); }
@Test(expected = NullPointerException.class) public void tryToCreateNewInstanceWithNullValueType() { JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, null, JsonValue::isString, JsonValue::asString, knownFieldMarkers); }
@Test public void getPointerReturnsExpected() { final JavaValueFieldDefinition underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, String.class, JsonValue::isString, JsonValue::asString, knownFieldMarkers); assertThat(underTest.getPointer()).isEqualTo(KNOWN_JSON_POINTER); }
@Test public void getValueTypeReturnsExpected() { final Class<String> valueType = String.class; final JavaValueFieldDefinition underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, valueType, JsonValue::isString, JsonValue::asString, knownFieldMarkers); assertThat(Objects.equals(underTest.getValueType(), valueType)).isTrue(); }
@Test(expected = UnsupportedOperationException.class) public void getMarkersReturnsUnmodifiableSet() { final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, int.class, JsonValue::isNumber, JsonValue::asInt, knownFieldMarkers); final Set<JsonFieldMarker> markers = underTest.getMarkers(); markers.add(mock(JsonFieldMarker.class)); }
@Test(expected = NullPointerException.class) public void tryToInvokeIsMarkedWithOnlyNullMarker() { final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, int.class, JsonValue::isNumber, JsonValue::asInt, knownFieldMarkers); underTest.isMarkedAs(null); }
@Test public void isMarkedAsReturnsTrueIfDefinitionContainsAskedMarkers() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, regularTypeMarkerMock}; final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, int.class, JsonValue::isNumber, JsonValue::asInt, knownFieldMarkers); assertThat(underTest.isMarkedAs(schemaVersionMarkerMock, regularTypeMarkerMock)).isTrue(); }
@Test public void isMarkedAsReturnsFalseIfDefinitionContainsNotAllAskedMarkers() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, specialTypeMarkerMock}; final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, int.class, JsonValue::isNumber, JsonValue::asInt, knownFieldMarkers); assertThat(underTest.isMarkedAs(schemaVersionMarkerMock, regularTypeMarkerMock)).isFalse(); }
@Test public void isMarkedAsReturnsTrueIfDefinitionContainsAskedMarker() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, regularTypeMarkerMock}; final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, int.class, JsonValue::isNumber, JsonValue::asInt, knownFieldMarkers); assertThat(underTest.isMarkedAs(regularTypeMarkerMock)).isTrue(); }
@Test public void stringRepresentationContainsExpected() { final JavaValueFieldDefinition<Double> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, double.class, JsonValue::isNumber, JsonValue::asDouble, knownFieldMarkers); assertThat(underTest.toString()) .contains("pointer") .contains("valueType") .contains("double") .contains("checkJavaTypeFunction") .contains("mappingFunction") .contains("markers"); }
@Test public void tryToMapJavaNullValue() { final JavaValueFieldDefinition<String> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, String.class, JsonValue::isString, JsonValue::asString); assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> underTest.mapValue(null)) .withMessage("The %s must not be (Java) null!", "JsonValue to be mapped") .withNoCause(); }
@Test public void mapValueReturnsExpected() { final String stringValue = "Foo"; final JsonValue jsonValue = JsonFactory.newValue(stringValue); final JavaValueFieldDefinition<String> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, String.class, JsonValue::isString, JsonValue::asString); final String mapped = underTest.mapValue(jsonValue); assertThat(mapped).isEqualTo(stringValue); }
@Test public void mapJsonNullLiteralWithStringFieldDefinition() { final Class<String> valueType = String.class; final JsonValue nullLiteral = JsonFactory.nullLiteral(); final JavaValueFieldDefinition<String> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, valueType, JsonValue::isString, JsonValue::asString); assertThat(underTest.mapValue(nullLiteral)).isNull(); }
@Test public void mapJsonNullLiteralWithIntFieldDefinition() { final Class<Integer> valueType = Integer.class; final JsonValue nullLiteral = JsonFactory.nullLiteral(); final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, valueType, JsonValue::isNumber, JsonValue::asInt); assertThat(underTest.mapValue(nullLiteral)).isNull(); }
@Test public void tryToMapBooleanWithIntFieldDefinition() { final Class<Integer> valueType = Integer.class; final JsonValue boolJsonValue = JsonFactory.newValue(true); final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, valueType, JsonValue::isNumber, JsonValue::asInt); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.mapValue(boolJsonValue)) .withMessage("Value <%s> for <%s> is not of type <%s>!", boolJsonValue, KNOWN_JSON_POINTER, valueType.getSimpleName()) .withNoCause(); }
@Test public void mapIntWithIntFieldDefinitionReturnsExpected() { final int intValue = 42; final JsonValue intJsonValue = JsonFactory.newValue(intValue); final JavaValueFieldDefinition<Integer> underTest = JavaValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, Integer.class, JsonValue::isNumber, JsonValue::asInt); final int mapped = underTest.mapValue(intJsonValue); assertThat(mapped).isEqualTo(intValue); }