ImmutableDittoHeaders extends AbstractDittoHeaders implements DittoHeaders { public static ImmutableDittoHeaders of(final Map<String, String> headers) { return new ImmutableDittoHeaders(headers); } private  ImmutableDittoHeaders(final Map<String, String> headers); static ImmutableDittoHeaders of(final Map<String, String> headers); @Override String toString();  }
@Test public void isResponseRequiredStaysFalseEvenIfRequiredAckLabelsAreSet() { final DittoHeaders underTest = DittoHeaders.newBuilder() .acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED)) .responseRequired(false) .build(); assertThat(underTest.isResponseRequired()).isFalse(); }
@Test public void timeoutIsSerializedAsString() { final int durationAmountSeconds = 2; final Duration timeout = Duration.ofSeconds(durationAmountSeconds); final DittoHeaders underTest = DittoHeaders.newBuilder() .timeout(timeout) .build(); final JsonObject jsonObject = underTest.toJson(); assertThat(jsonObject.getValue(DittoHeaderDefinition.TIMEOUT.getKey())) .contains(JsonValue.of(durationAmountSeconds * 1000 + "ms")); }
@Test public void getMetadataHeadersToPutReturnsEmptySet() { final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of(Collections.emptyMap()); assertThat(underTest.getMetadataHeadersToPut()).isEmpty(); }
@Test public void getMetadataHeadersToPutReturnsExpected() { final MetadataHeaderKey specificMetadataKey = MetadataHeaderKey.parse("/foo/bar/baz"); final JsonValue metadataValue1 = JsonValue.of(1); final MetadataHeaderKey wildcardMetadataKey = MetadataHeaderKey.parse("/*/aValue"); final JsonValue metadataValue2 = JsonValue.of(2); final MetadataHeaders expected = MetadataHeaders.newInstance(); expected.add(MetadataHeader.of(wildcardMetadataKey, metadataValue2)); expected.add(MetadataHeader.of(specificMetadataKey, metadataValue1)); final Map<String, String> headerMap = new HashMap<>(); headerMap.put(DittoHeaderDefinition.PUT_METADATA.getKey(), expected.toJsonString()); final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of(headerMap); assertThat(underTest.getMetadataHeadersToPut()).isEqualTo(expected); }
@Test public void removesDuplicatedAuthSubjectsFromHeaderMap() { final String expectedWithoutDups = AUTH_CONTEXT_WITHOUT_DUPLICATES.toJsonString(); final String withDups = AUTH_CONTEXT.toJsonString(); final Map<String, String> headersWithDuplicatedAuthSubjects = new HashMap<>(); headersWithDuplicatedAuthSubjects.put(DittoHeaderDefinition.AUTHORIZATION_CONTEXT.getKey(), withDups); final ImmutableDittoHeaders dittoHeaders = ImmutableDittoHeaders.of(headersWithDuplicatedAuthSubjects); assertThat(dittoHeaders.get(DittoHeaderDefinition.AUTHORIZATION_CONTEXT.getKey())).isEqualTo( expectedWithoutDups); }
@Test public void removesDuplicatedAuthSubjectsFromJsonRepresentation() { final String withDups = AUTH_CONTEXT.toJsonString(); final JsonObject expected = JsonFactory.newObjectBuilder() .set(DittoHeaderDefinition.AUTHORIZATION_CONTEXT.getKey(), AUTH_CONTEXT_WITHOUT_DUPLICATES.toJson()) .build(); final Map<String, String> headersWithDuplicatedAuthSubjects = new HashMap<>(); headersWithDuplicatedAuthSubjects.put(DittoHeaderDefinition.AUTHORIZATION_CONTEXT.getKey(), withDups); final ImmutableDittoHeaders dittoHeaders = ImmutableDittoHeaders.of(headersWithDuplicatedAuthSubjects); assertThat(dittoHeaders.toJson()).isEqualTo(expected); }
@Test public void tryToCheckIfEntriesSizeIsGreaterThanNegativeLong() { final long negativeLong = -3L; final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of( Maps.newHashMap(DittoHeaderDefinition.CORRELATION_ID.getKey(), KNOWN_CORRELATION_ID)); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> underTest.isEntriesSizeGreaterThan(negativeLong)) .withMessage("The size to compare to must not be negative but it was <%s>!", negativeLong) .withNoCause(); }
@Test public void entriesSizeIsGreaterThanZeroIfHeadersAreNotEmpty() { final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of( Maps.newHashMap(DittoHeaderDefinition.CORRELATION_ID.getKey(), KNOWN_CORRELATION_ID)); assertThat(underTest.isEntriesSizeGreaterThan(0)).isTrue(); }
@Test public void entriesSizeIsNotGreaterThanZeroIfHeadersAreEmpty() { final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of(new HashMap<>()); assertThat(underTest.isEntriesSizeGreaterThan(0)).isFalse(); }
@Test public void entriesSizeIsNotGreaterThanComparedSize() { final String key = DittoHeaderDefinition.CORRELATION_ID.getKey(); final String value = KNOWN_CORRELATION_ID; final long comparisonSize = key.length() + value.length(); final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of(Maps.newHashMap(key, value)); assertThat(underTest.isEntriesSizeGreaterThan(comparisonSize)).isFalse(); }
@Test public void entriesSizeIsGreaterThanComparedSize() { final String key = DittoHeaderDefinition.CORRELATION_ID.getKey(); final String value = KNOWN_CORRELATION_ID; final long entrySize = key.length() + value.length(); final ImmutableDittoHeaders underTest = ImmutableDittoHeaders.of(Maps.newHashMap(key, value)); final long comparisonSize = entrySize - 1; assertThat(underTest.isEntriesSizeGreaterThan(comparisonSize)).isTrue(); }
@Test public void truncateLargeHeaders() { final HashMap<String, String> oversizeMap = new HashMap<>(); oversizeMap.put("k", ""); oversizeMap.put("m", "1"); oversizeMap.put("f", "12"); oversizeMap.put("d", "123"); oversizeMap.put("M", "1234"); final DittoHeaders oversizeHeaders = ImmutableDittoHeaders.of(oversizeMap); final DittoHeaders truncatedHeaders = oversizeHeaders.truncate(8L); final Map<String, String> expected = new HashMap<>(oversizeMap); expected.remove("d"); expected.remove("M"); assertThat(truncatedHeaders).isEqualTo(expected); }