JsonValueFieldDefinition extends AbstractJsonFieldDefinition<T> { public static <T extends JsonValue> JsonValueFieldDefinition<T> newInstance(final CharSequence pointer, final Class<T> valueType, final Function<JsonValue, Boolean> checkJavaTypeFunction, final Function<JsonValue, T> mappingFunction, final JsonFieldMarker ... markers) { requireNonNull(mappingFunction, "The mapping function must not be null!"); return new JsonValueFieldDefinition<>(pointer, valueType, checkJavaTypeFunction, mappingFunction, markers); } private  JsonValueFieldDefinition(final CharSequence pointer,
            final Class<T> valueType,
            final Function<JsonValue, Boolean> checkJavaTypeFunction,
            final Function<JsonValue, T> mappingFunction,
            final JsonFieldMarker ... markers); static JsonValueFieldDefinition<T> newInstance(final CharSequence pointer,
            final Class<T> valueType,
            final Function<JsonValue, Boolean> checkJavaTypeFunction,
            final Function<JsonValue, T> mappingFunction,
            final JsonFieldMarker ... markers);  }
@Test(expected = UnsupportedOperationException.class) public void getMarkersReturnsUnmodifiableSet() { final JsonValueFieldDefinition<JsonValue> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonValue.class, JsonValue::isObject, JsonValue::asObject, knownFieldMarkers); final Set<JsonFieldMarker> markers = underTest.getMarkers(); markers.add(mock(JsonFieldMarker.class)); }
@Test(expected = NullPointerException.class) public void tryToInvokeIsMarkedWithOnlyNullMarker() { final JsonValueFieldDefinition<JsonArray> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonArray.class, JsonValue::isArray, JsonValue::asArray, knownFieldMarkers); underTest.isMarkedAs(null); }
@Test public void isMarkedAsReturnsTrueIfDefinitionContainsAskedMarkers() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, regularTypeMarkerMock}; final JsonValueFieldDefinition<JsonObject> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonObject.class, JsonValue::isObject, JsonValue::asObject, knownFieldMarkers); assertThat(underTest.isMarkedAs(schemaVersionMarkerMock, regularTypeMarkerMock)).isTrue(); }
@Test public void isMarkedAsReturnsFalseIfDefinitionContainsNotAllAskedMarkers() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, specialTypeMarkerMock}; final JsonValueFieldDefinition<JsonArray> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonArray.class, JsonValue::isArray, JsonValue::asArray, knownFieldMarkers); assertThat(underTest.isMarkedAs(schemaVersionMarkerMock, regularTypeMarkerMock)).isFalse(); }
@Test public void isMarkedAsReturnsTrueIfDefinitionContainsAskedMarker() { knownFieldMarkers = new JsonFieldMarker[] {schemaVersionMarkerMock, regularTypeMarkerMock}; final JsonValueFieldDefinition<JsonObject> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonObject.class, JsonValue::isObject, JsonValue::asObject, knownFieldMarkers); assertThat(underTest.isMarkedAs(regularTypeMarkerMock)).isTrue(); }
@Test public void stringRepresentationContainsExpected() { final JsonValueFieldDefinition<JsonArray> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonArray.class, JsonValue::isArray, JsonValue::asArray, knownFieldMarkers); assertThat(underTest.toString()) .contains("pointer") .contains("valueType") .contains("JsonArray") .contains("checkJavaTypeFunction") .contains("mappingFunction") .contains("markers"); }
@Test public void tryToMapJavaNullValue() { final JsonValueFieldDefinition<JsonObject> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonObject.class, JsonValue::isObject, JsonValue::asObject); assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> underTest.mapValue(null)) .withMessage("The %s must not be (Java) null!", "JsonValue to be mapped") .withNoCause(); }
@Test public void mapJsonNullLiteralWithJsonObjectFieldDefinitionReturnsExpected() { final JsonValue nullLiteral = JsonFactory.nullLiteral(); final JsonValueFieldDefinition<JsonObject> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonObject.class, JsonValue::isObject, JsonValue::asObject); final JsonObject mapped = underTest.mapValue(nullLiteral); assertThat(mapped).isNullLiteral().isEmpty(); }
@Test public void mapJsonArrayWithJsonArrayObjectFieldDefinitionReturnsExpected() { final JsonValue sourceArray = JsonFactory.newArrayBuilder().add("foo", "bar").add(true).build(); final JsonValueFieldDefinition<JsonArray> underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonArray.class, JsonValue::isArray, JsonValue::asArray); final JsonArray mapped = underTest.mapValue(sourceArray); assertThat(mapped).isEqualTo(sourceArray); }
@Test(expected = NullPointerException.class) public void tryToCreateNewInstanceWithNullJsonPointer() { JsonValueFieldDefinition.newInstance(null, JsonArray.class, JsonValue::isArray, JsonValue::asArray, knownFieldMarkers); }
@Test(expected = NullPointerException.class) public void tryToCreateNewInstanceWithNullValueType() { JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, null, JsonValue::isObject, JsonValue::asObject, knownFieldMarkers); }
@Test public void getPointerReturnsExpected() { final JsonValueFieldDefinition underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, JsonValue.class, JsonValue::isArray, JsonValue::asArray, knownFieldMarkers); assertThat(underTest.getPointer()).isEqualTo(KNOWN_JSON_POINTER); }
@Test public void getValueTypeReturnsExpected() { final Class<JsonObject> valueType = JsonObject.class; final JsonValueFieldDefinition underTest = JsonValueFieldDefinition.newInstance(KNOWN_JSON_POINTER, valueType, JsonValue::isObject, JsonValue::asObject, knownFieldMarkers); assertThat(Objects.equals(underTest.getValueType(), valueType)).isTrue(); }