-
Notifications
You must be signed in to change notification settings - Fork 215
/
HeaderBasedPlaceholderSubstitutionAlgorithmTest.java
169 lines (140 loc) · 7.49 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.concierge.service.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.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayPlaceholderNotResolvableException;
import org.junit.Before;
import org.junit.Ignore;
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_KEY = "request.subjectId";
private static final String LEGACY_REPLACER = "${" + LEGACY_REPLACER_KEY + "}";
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));
replacementDefinitions.put(LEGACY_REPLACER_KEY, (dittoHeaders -> REPLACED_1));
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).isEqualTo(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, DITTO_HEADERS);
assertThat(substituted).isEqualTo(REPLACED_1);
}
@Test
public void substituteThrowsUnknownPlaceholderExceptionWhenInputContainsUnknownPlaceholder() {
assertUnknownPlaceholderExceptionIsThrown(UNKNOWN_REPLACER_KEY,
() -> underTest.substitute(UNKNOWN_REPLACER, DITTO_HEADERS));
}
@Test
public void partialPlaceholdersRemain() {
final String notResolvableInput = "{{";
assertThat(underTest.substitute(notResolvableInput, DITTO_HEADERS)).isEqualTo(notResolvableInput);
}
@Test
public void unknownLegacyPlaceholderThrowsException() {
assertThatExceptionOfType(GatewayPlaceholderNotResolvableException.class)
.isThrownBy(() -> underTest.substitute(UNKNOWN_LEGACY_REPLACER, DITTO_HEADERS));
}
/**
* Nesting legacy-placeholders does not work, but at least it fails with an understandable exception. And
* legacy-placeholders should not be used anyway.
*/
@Test
public void inputContainsNestedLegacyPlaceholderThrowsException() {
final String nestedPlaceholder = "${" + LEGACY_REPLACER + "}";
assertThatExceptionOfType(GatewayPlaceholderNotResolvableException.class)
.isThrownBy(() -> underTest.substitute(nestedPlaceholder, DITTO_HEADERS));
}
private void assertUnknownPlaceholderExceptionIsThrown(final String expectedPlaceholderKey,
final ThrowableAssert.ThrowingCallable throwingCallable) {
final String expectedMessage = "The placeholder '" + expectedPlaceholderKey + "' is unknown.";
final String expectedDescription = "Please use one of the supported placeholders: '" +
REPLACER_KEY_1 + "', '" + REPLACER_KEY_2 + "', '" + LEGACY_REPLACER_KEY + "'.";
assertGatewayPlaceholderNotResolvableExceptionIsThrown(expectedMessage, expectedDescription, throwingCallable);
}
private void assertUnresolvedPlaceholdersRemainExceptionIsThrown(final String notResolvableInput,
final ThrowableAssert.ThrowingCallable throwingCallable) {
final String expectedMessage = "The input contains not resolvable placeholders: '" + notResolvableInput + "'.";
assertGatewayPlaceholderNotResolvableExceptionIsThrown(expectedMessage,
GatewayPlaceholderNotResolvableException.NOT_RESOLVABLE_DESCRIPTION, throwingCallable);
}
private void assertGatewayPlaceholderNotResolvableExceptionIsThrown(final String expectedMessage,
final String expectedDescription, final ThrowableAssert.ThrowingCallable throwingCallable) {
assertThatExceptionOfType(GatewayPlaceholderNotResolvableException.class)
.isThrownBy(throwingCallable)
.satisfies(e -> {
assertThat(e.getMessage()).isEqualTo(expectedMessage);
assertThat(e.getDescription()).contains(expectedDescription);
assertThat(e.getDittoHeaders()).isEqualTo(DITTO_HEADERS);
});
}
}