AcknowledgementsJsonParser implements Function<JsonObject, Acknowledgements> { @Override public Acknowledgements apply(final JsonObject jsonObject) { return tryToParseJsonObject(checkNotNull(jsonObject, "jsonObject")); } private  AcknowledgementsJsonParser(final AcknowledgementJsonParser<I> acknowledgementJsonParser); static AcknowledgementsJsonParser<I> getInstance(
            final AcknowledgementJsonParser<I> acknowledgementJsonParser); @Override Acknowledgements apply(final JsonObject jsonObject);  }
@Test public void parseNullJsonObject() { assertThatNullPointerException() .isThrownBy(() -> underTest.apply(null)) .withMessage("The jsonObject must not be null!") .withNoCause(); }
@Test public void parseJsonWithSeveralAcksOfSameLabel() { final AcknowledgementLabel label = AcknowledgementLabel.of("same-label"); final Acknowledgements acks = Acknowledgements.of(Arrays.asList( Acknowledgement.of(label, ThingId.dummy(), HttpStatusCode.OK, DittoHeaders.empty()), Acknowledgement.of(label, ThingId.dummy(), HttpStatusCode.FORBIDDEN, DittoHeaders.empty()) ), DittoHeaders.empty()); final Acknowledgements parsedAcknowledgements = underTest.apply(acks.toJson()); assertThat(parsedAcknowledgements).isEqualTo(acks); }
@Test public void parseValidJsonRepresentationOfNonEmptyAcknowledgements() { final JsonObject jsonRepresentation = knownAcknowledgements.toJson(); final Acknowledgements parsedAcknowledgements = underTest.apply(jsonRepresentation); assertThat(parsedAcknowledgements).isEqualTo(knownAcknowledgements); }
@Test public void parseJsonRepresentationWithoutEntityId() { final JsonFieldDefinition<String> entityIdFieldDefinition = Acknowledgements.JsonFields.ENTITY_ID; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .remove(entityIdFieldDefinition) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessageContaining(entityIdFieldDefinition.getPointer().toString()) .withNoCause(); }
@Test public void parseJsonRepresentationWithoutEntityType() { final JsonFieldDefinition<String> entityTypeFieldDefinition = Acknowledgements.JsonFields.ENTITY_TYPE; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .remove(entityTypeFieldDefinition) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessageContaining(entityTypeFieldDefinition.getPointer().toString()) .withNoCause(); }
@Test public void parseJsonRepresentationWithInvalidEntityType() { final JsonFieldDefinition<?> entityTypeFieldDefinition = Acknowledgements.JsonFields.ENTITY_TYPE; final boolean invalidEntityType = true; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .set(entityTypeFieldDefinition.getPointer(), invalidEntityType) .build(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessage("Value <%s> for <%s> is not of type <String>!", invalidEntityType, entityTypeFieldDefinition.getPointer()) .withNoCause(); }
@Test public void parseJsonRepresentationWithoutDittoHeaders() { final JsonFieldDefinition<?> dittoHeadersFieldDefinition = Acknowledgements.JsonFields.DITTO_HEADERS; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .remove(dittoHeadersFieldDefinition) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessageContaining(dittoHeadersFieldDefinition.getPointer().toString()) .withNoCause(); }
@Test public void parseJsonRepresentationWithInvalidDittoHeaders() { final JsonValue invalidDittoHeaders = JsonValue.of("dittoHeaders"); final JsonFieldDefinition<?> dittoHeadersFieldDefinition = Acknowledgements.JsonFields.DITTO_HEADERS; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .set(dittoHeadersFieldDefinition.getPointer(), invalidDittoHeaders) .build(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessage("Value <%s> for <%s> is not of type <JsonObject>!", invalidDittoHeaders, dittoHeadersFieldDefinition.getPointer()) .withNoCause(); }
@Test public void parseJsonRepresentationWithoutAcknowledgements() { final JsonFieldDefinition<JsonObject> acknowledgementsFieldDefinition = Acknowledgements.JsonFields.ACKNOWLEDGEMENTS; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .remove(acknowledgementsFieldDefinition) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessageContaining(acknowledgementsFieldDefinition.getPointer().toString()) .withNoCause(); }
@Test public void parseJsonRepresentationWithInvalidAcknowledgements() { final JsonFieldDefinition<JsonObject> acknowledgementsFieldDefinition = Acknowledgements.JsonFields.ACKNOWLEDGEMENTS; final boolean invalidAcknowledgements = true; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .set(acknowledgementsFieldDefinition.getPointer(), invalidAcknowledgements) .build(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessage("Value <%s> for <%s> is not of type <JsonObject>!", invalidAcknowledgements, acknowledgementsFieldDefinition.getPointer()) .withNoCause(); }
@Test public void parseJsonRepresentationWithInvalidAcknowledgementJsonRepresentation() { final Acknowledgements acknowledgements = Acknowledgements.of(Sets.newSet(knownAcknowledgement), dittoHeaders); final JsonFieldDefinition<JsonObject> acknowledgementsFieldDefinition = Acknowledgements.JsonFields.ACKNOWLEDGEMENTS; final int invalidAcknowledgement = 42; final JsonObject invalidAcknowledgements = JsonObject.newBuilder() .set(knownAcknowledgement.getLabel(), invalidAcknowledgement) .build(); final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(acknowledgements.toJson()) .set(acknowledgementsFieldDefinition, invalidAcknowledgements) .build(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessage("<%d> is not an Acknowledgement JSON object representation!", invalidAcknowledgement) .withNoCause(); }
@Test public void parseJsonRepresentationWithoutStatusCode() { final JsonFieldDefinition<Integer> statusCodeFieldDefinition = Acknowledgements.JsonFields.STATUS_CODE; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .remove(statusCodeFieldDefinition) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessageContaining(statusCodeFieldDefinition.getPointer().toString()) .withNoCause(); }
@Test public void parseJsonRepresentationWithUnknownStatusCode() { final JsonFieldDefinition<Integer> statusCodeFieldDefinition = Acknowledgements.JsonFields.STATUS_CODE; final int unknownStatusCode = 19; final JsonObject jsonRepresentation = JsonFactory.newObjectBuilder(knownAcknowledgements.toJson()) .set(statusCodeFieldDefinition, unknownStatusCode) .build(); assertThatExceptionOfType(JsonParseException.class) .isThrownBy(() -> underTest.apply(jsonRepresentation)) .withMessage("Status code <%d> is not supported!", unknownStatusCode) .withNoCause(); }