/
HeaderBasedPlaceholderSubstitutionAlgorithmTest.java
148 lines (120 loc) · 6.19 KB
/
HeaderBasedPlaceholderSubstitutionAlgorithmTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.concierge.enforcement.placeholders;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mutabilitydetector.unittesting.AllowedReason.assumingFields;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import org.assertj.core.api.ThrowableAssert;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.headers.WithDittoHeaders;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayUnknownPlaceholderException;
import org.junit.Before;
import org.junit.Test;
/**
* Tests {@link HeaderBasedPlaceholderSubstitutionAlgorithm}.
*/
public class HeaderBasedPlaceholderSubstitutionAlgorithmTest {
private static final String REPLACER_KEY_1 = "my:arbitrary:replacer1";
private static final String REPLACER_1 = "{{ " + REPLACER_KEY_1 + " }}";
private static final String LEGACY_REPLACER_1 = "${my.arbitrary.replacer1}";
private static final String REPLACED_1 = "firstReplaced";
private static final String REPLACER_KEY_2 = "my:arbitrary:replacer2";
private static final String REPLACED_2 = "secondReplaced";
private static final String UNKNOWN_REPLACER_KEY = "unknown:unknown";
private static final String UNKNOWN_REPLACER = "{{ " + UNKNOWN_REPLACER_KEY + " }}";
private static final String UNKNOWN_LEGACY_REPLACER_KEY = "unknown.unknown";
private static final String UNKNOWN_LEGACY_REPLACER = "${" + UNKNOWN_LEGACY_REPLACER_KEY + "}";
private static final DittoHeaders DITTO_HEADERS = DittoHeaders.newBuilder().correlationId("foo").build();
private HeaderBasedPlaceholderSubstitutionAlgorithm underTest;
@Before
public void init() {
final Map<String, Function<DittoHeaders, String>> replacementDefinitions = new LinkedHashMap<>();
replacementDefinitions.put(REPLACER_KEY_1, (dittoHeaders -> REPLACED_1));
replacementDefinitions.put(REPLACER_KEY_2, (dittoHeaders -> REPLACED_2));
underTest = HeaderBasedPlaceholderSubstitutionAlgorithm.newInstance(replacementDefinitions);
}
@Test
public void assertImmutability() {
assertInstancesOf(HeaderBasedPlaceholderSubstitutionAlgorithm.class, areImmutable(),
assumingFields("replacementDefinitions", "knownPlaceHolders")
.areSafelyCopiedUnmodifiableCollectionsWithImmutableElements());
}
@Test
public void substituteFailsWithNullInput() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> underTest.substitute(null, DITTO_HEADERS));
}
@Test
public void substituteFailsWithNullHeaders() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> underTest.substitute("doesNotMatter", (DittoHeaders) null));
}
@Test
public void substituteFailsWithNullWithDittoHeaders() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> underTest.substitute("doesNotMatter", (WithDittoHeaders) null));
}
@Test
public void substituteReturnsInputWhenInputDoesNotContainReplacers() {
final String input = "withoutReplacers";
final String substituted = underTest.substitute(input, DITTO_HEADERS);
assertThat(substituted).isSameAs(input);
}
@Test
public void substituteReturnsReplacedWhenInputContainsPlaceholder() {
final String substituted = underTest.substitute(REPLACER_1, DITTO_HEADERS);
assertThat(substituted).isEqualTo(REPLACED_1);
}
@Test
public void substituteReturnsReplacedWhenInputContainsLegacyPlaceholder() {
final String substituted =
underTest.substitute(LEGACY_REPLACER_1, DITTO_HEADERS);
assertThat(substituted).isEqualTo(REPLACED_1);
}
@Test
public void substituteThrowsUnknownPlaceholderExceptionWhenInputContainsUnknownPlaceholder() {
assertUnknownPlaceholderExceptionIsThrown(UNKNOWN_REPLACER_KEY,
() -> underTest.substitute(UNKNOWN_REPLACER, DITTO_HEADERS));
}
@Test
public void substituteTThrowsUnknownPlaceholderExceptionWhenInputContainsUnknownLegacyPlaceholder() {
assertUnknownPlaceholderExceptionIsThrown(UNKNOWN_REPLACER_KEY,
() -> underTest.substitute(UNKNOWN_LEGACY_REPLACER, DITTO_HEADERS));
}
/**
* Nesting legacy-placeholders does not work, but at least if fails with an understandable exception. And
* legacy-placeholders should not be used anyway.
*/
@Test
public void substituteThrowsWhenWhenInputContainsNestedLegacyPlaceholder() {
final String nestedPlaceholder = "${" + LEGACY_REPLACER_1 + "}";
assertUnknownPlaceholderExceptionIsThrown("${" + REPLACER_KEY_1 + "}",
() -> underTest.substitute(nestedPlaceholder, DITTO_HEADERS));
}
private void assertUnknownPlaceholderExceptionIsThrown(final String expectedPlaceholderKey,
final ThrowableAssert.ThrowingCallable throwingCallable) {
assertThatExceptionOfType(GatewayUnknownPlaceholderException.class)
.isThrownBy(throwingCallable)
.satisfies(e -> {
assertThat(e.getMessage()).isEqualTo("The placeholder '" + expectedPlaceholderKey + "' is " +
"unknown.");
assertThat(e.getDescription()).contains("Please use one of the supported placeholders: '" +
REPLACER_KEY_1 + "', '" + REPLACER_KEY_2 + "'.");
assertThat(e.getDittoHeaders()).isEqualTo(DITTO_HEADERS);
});
}
}