DefaultDittoHeadersBuilder extends AbstractDittoHeadersBuilder<DefaultDittoHeadersBuilder, DittoHeaders> { static DefaultDittoHeadersBuilder of(final Map<String, String> headers) { if (headers instanceof DittoHeaders) { return new DefaultDittoHeadersBuilder((DittoHeaders) headers); } return new DefaultDittoHeadersBuilder(headers); } private  DefaultDittoHeadersBuilder(final Map<String, String> headers); private  DefaultDittoHeadersBuilder(final DittoHeaders dittoHeaders);   }
@Test public void constructBuilderFromHeadersWorksExpected() { final DittoHeaders dittoHeaders = underTest.correlationId(CORRELATION_ID) .readSubjects(READ_SUBJECTS) .schemaVersion(JSON_SCHEMA_VERSION) .build(); final DittoHeaders anotherDittoHeaders = of(dittoHeaders).build(); assertThat(dittoHeaders).isEqualTo(anotherDittoHeaders); }
@Test public void buildWithEmptyCorrelationIdFromMapThrowsDittoHeaderInvalidException() { final Map<String, String> headerMap = new HashMap<>(); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), ""); DittoBaseAssertions.assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> DittoHeaders.of(headerMap)); }
@Test public void jsonRepresentationOfDittoHeadersWithResponseRequiredOnlyIsExpected() { final DittoHeaders dittoHeaders = underTest.responseRequired(false).build(); final JsonObject jsonObject = dittoHeaders.toJson(); assertThat(jsonObject) .hasSize(1) .contains(JsonKey.of(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey()), JsonFactory.newValue(false)); }
@Test public void tryToCreateInstanceWithMapContainingInvalidHeader() { final String readSubjectsKey = DittoHeaderDefinition.READ_SUBJECTS.getKey(); final String invalidJsonArrayString = "['Frank',42,'Grimes']"; final Map<String, String> initialHeaders = new HashMap<>(); initialHeaders.put(DittoHeaderDefinition.DRY_RUN.getKey(), "true"); initialHeaders.put("foo", "bar"); initialHeaders.put(readSubjectsKey, invalidJsonArrayString); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> of(initialHeaders)) .withMessage("The value '%s' of the header '%s' is not a valid JSON array.", invalidJsonArrayString, readSubjectsKey) .withNoCause(); }
@Test public void tryToCreateInstanceWithJsonObjectContainingInvalidHeader() { final String schemaVersionKey = DittoHeaderDefinition.SCHEMA_VERSION.getKey(); final String invalidSchemaVersionValue = "meh"; final JsonObject headersJsonObject = JsonFactory.newObjectBuilder() .set(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey(), false) .set(schemaVersionKey, invalidSchemaVersionValue) .set("foo", "bar") .build(); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> of(headersJsonObject)) .withMessage("The value '%s' of the header '%s' is not a valid int.", invalidSchemaVersionValue, schemaVersionKey) .withNoCause(); }
@Test public void ensureResponseRequiredStaysFalseEvenWhenAcksAreRequested() { final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .responseRequired(false) .acknowledgementRequest(AcknowledgementRequest.of(AcknowledgementLabel.of("some-ack"))) .build(); assertThat(dittoHeaders) .containsEntry(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey(), Boolean.FALSE.toString()); }
@Test public void putHeadersContainingValidMetadata() { final MetadataHeaderKey metadataKey = MetadataHeaderKey.parse("*/issuedAt"); final JsonValue metadataValue = JsonValue.of(String.valueOf(Instant.now())); final Map<String, String> headerMap = new HashMap<>(); headerMap.put("foo", "bar"); headerMap.put(metadataKey.toString(), metadataValue.toString()); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), String.valueOf(UUID.randomUUID())); final DittoHeaders dittoHeaders = underTest.putHeaders(headerMap).build(); assertThat(dittoHeaders).isEqualTo(headerMap); }
@Test public void putHeadersContainingInvalidMetadataKey() { final String metadataKey = "*/*/issuedAt"; final JsonValue metadataValue = JsonValue.of(String.valueOf(Instant.now())); final JsonArray metadataHeaderEntries = JsonArray.of(JsonObject.newBuilder() .set(MetadataHeader.JsonFields.METADATA_KEY, metadataKey) .set(MetadataHeader.JsonFields.METADATA_VALUE, metadataValue) .build()); final Map<String, String> headerMap = new HashMap<>(); headerMap.put("foo", "bar"); headerMap.put(DittoHeaderDefinition.PUT_METADATA.getKey(), metadataHeaderEntries.toString()); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), String.valueOf(UUID.randomUUID())); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> underTest.putHeaders(headerMap)) .withMessage("The value '%s' of the header 'put-metadata' is not a valid MetadataHeaders.", metadataHeaderEntries) .satisfies(dittoHeaderInvalidException -> assertThat( dittoHeaderInvalidException.getDescription()) .contains("The metadata header key <" + metadataKey + "> is invalid!" + " A wildcard path of a metadata header key must have exactly two levels but it had" + " <3>!")) .withCauseInstanceOf(JsonParseException.class); }
@Test public void createBuilderFromMapContainingValidMetadata() { final MetadataHeaderKey metadataKey = MetadataHeaderKey.parse("*/issuedAt"); final JsonValue metadataValue = JsonValue.of(String.valueOf(Instant.now())); final Map<String, String> headerMap = new HashMap<>(); headerMap.put("foo", "bar"); headerMap.put(metadataKey.toString(), metadataValue.toString()); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), String.valueOf(UUID.randomUUID())); final DefaultDittoHeadersBuilder underTest = of(headerMap); assertThat(underTest.build()).isEqualTo(headerMap); }
@Test public void createBuilderFromMapContainingInvalidMetadataKey() { final String metadataKey = "*/*/issuedAt"; final JsonObject metadataHeaderEntry = JsonObject.newBuilder() .set(MetadataHeader.JsonFields.METADATA_KEY, metadataKey) .set(MetadataHeader.JsonFields.METADATA_VALUE, JsonValue.of(String.valueOf(Instant.now()))) .build(); final JsonArray metadataHeaderEntries = JsonArray.of(metadataHeaderEntry); final Map<String, String> headerMap = new HashMap<>(); headerMap.put("foo", "bar"); headerMap.put(DittoHeaderDefinition.PUT_METADATA.getKey(), metadataHeaderEntries.toString()); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), String.valueOf(UUID.randomUUID())); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> DefaultDittoHeadersBuilder.of(headerMap)) .satisfies(dittoHeaderInvalidException -> assertThat(dittoHeaderInvalidException.getDescription()) .contains("The metadata header key <" + metadataKey + "> is invalid!" + " A wildcard path of a metadata header key must have exactly two levels but it had" + " <3>!")) .withCauseInstanceOf(JsonParseException.class); }
@Test public void createBuilderFromMapContainingInvalidMetadataValue() { final String invalidValue = String.valueOf(Instant.now()); final Map<String, String> headerMap = new HashMap<>(); headerMap.put("foo", "bar"); headerMap.put(DittoHeaderDefinition.PUT_METADATA.getKey(), invalidValue); headerMap.put(DittoHeaderDefinition.CORRELATION_ID.getKey(), String.valueOf(UUID.randomUUID())); assertThatExceptionOfType(DittoHeaderInvalidException.class) .isThrownBy(() -> DefaultDittoHeadersBuilder.of(headerMap)) .withMessage("The value '%s' of the header '%s' is not a valid MetadataHeaders.", invalidValue, DittoHeaderDefinition.PUT_METADATA.getKey()) .satisfies(dittoHeaderInvalidException -> assertThat(dittoHeaderInvalidException.getDescription()) .hasValueSatisfying(description -> assertThat(description) .startsWith("Failed to parse JSON string '" + invalidValue + "'!"))) .withCauseInstanceOf(JsonParseException.class); }