/
PlaceholderFilterTest.java
289 lines (232 loc) · 14.7 KB
/
PlaceholderFilterTest.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* 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.model.placeholders;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.eclipse.ditto.model.connectivity.UnresolvedPlaceholderException;
import org.eclipse.ditto.protocoladapter.TopicPath;
import org.junit.Test;
/**
* Tests {@link PlaceholderFilter}.
*/
public class PlaceholderFilterTest {
private static final Map<String, String> HEADERS = new HashMap<>();
private static final String DEVICE_ID = "device-12345";
private static final String THING_ID = "eclipse:ditto";
private static final String KNOWN_NAMESPACE = "org.eclipse.ditto.test";
private static final String KNOWN_ID = "myThing";
private static final String KNOWN_SUBJECT = "mySubject";
private static final String KNOWN_SUBJECT2 = "$set.configuration/steps";
private static final TopicPath KNOWN_TOPIC_PATH = TopicPath.newBuilder(KNOWN_NAMESPACE + ":" + KNOWN_ID)
.twin().things().commands().modify().build();
private static final TopicPath KNOWN_TOPIC_PATH_SUBJECT1 = TopicPath.newBuilder(KNOWN_NAMESPACE + ":" + KNOWN_ID)
.live().things().messages().subject(KNOWN_SUBJECT).build();
private static final TopicPath KNOWN_TOPIC_PATH_SUBJECT2 = TopicPath.newBuilder(KNOWN_NAMESPACE + ":" + KNOWN_ID)
.live().things().messages().subject(KNOWN_SUBJECT2).build();
private static final HeadersPlaceholder headersPlaceholder = PlaceholderFactory.newHeadersPlaceholder();
private static final ThingPlaceholder thingPlaceholder = PlaceholderFactory.newThingPlaceholder();
private static final TopicPathPlaceholder topicPlaceholder = PlaceholderFactory.newTopicPathPlaceholder();
private static final FilterTuple[] filterChain = new FilterTuple[]{
FilterTuple.of(HEADERS, headersPlaceholder),
FilterTuple.of(THING_ID, thingPlaceholder)
};
private static final Placeholder[] placeholders = new Placeholder[]{
headersPlaceholder,
thingPlaceholder
};
static {
HEADERS.put("device-id", DEVICE_ID);
HEADERS.put("gateway-id", "http-protocol-adapter");
HEADERS.put("source", "commands");
}
@Test
public void testHeadersPlaceholder() {
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> headersPlaceholder.resolve(HEADERS, null));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
() -> headersPlaceholder.resolve(HEADERS, ""));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ header:unknown }}", HEADERS, headersPlaceholder));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ {{ header:device-id }} }}", HEADERS, headersPlaceholder));
assertThat(PlaceholderFilter.apply(THING_ID, HEADERS, headersPlaceholder)).isEqualTo(THING_ID);
assertThat(
PlaceholderFilter.apply("eclipse:ditto:{{ header:device-id }}", HEADERS, headersPlaceholder)).isEqualTo(
"eclipse:ditto:device-12345");
}
@Test
public void testThingPlaceholder() {
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> thingPlaceholder.resolve(THING_ID, null));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
() -> thingPlaceholder.resolve(THING_ID, ""));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ header:unknown }}", THING_ID, thingPlaceholder));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ {{ thing:name }} }}", THING_ID, thingPlaceholder));
assertThat(PlaceholderFilter.apply(THING_ID, THING_ID, thingPlaceholder)).isEqualTo(THING_ID);
assertThat(PlaceholderFilter.apply("prefix:{{ thing:namespace }}:{{ thing:name }}:suffix", THING_ID,
thingPlaceholder)).isEqualTo("prefix:eclipse:ditto:suffix");
assertThat(PlaceholderFilter.apply("testTargetAmqpCon4_{{thing:namespace}}:{{thing:name}}", THING_ID,
thingPlaceholder))
.isEqualTo("testTargetAmqpCon4_eclipse:ditto");
assertThat(PlaceholderFilter.apply("testTargetAmqpCon4_{{thing:id}}", THING_ID, thingPlaceholder)).isEqualTo(
"testTargetAmqpCon4_eclipse:ditto");
}
@Test
public void testTopicPlaceholder() {
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> topicPlaceholder.resolve(KNOWN_TOPIC_PATH, null));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
() -> topicPlaceholder.resolve(KNOWN_TOPIC_PATH, ""));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ topic:unknown }}", KNOWN_TOPIC_PATH, topicPlaceholder));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.apply("{{ {{ topic:name }} }}", KNOWN_TOPIC_PATH, topicPlaceholder));
assertThat(PlaceholderFilter.apply("eclipse:ditto", KNOWN_TOPIC_PATH, topicPlaceholder)).isEqualTo("eclipse:ditto");
assertThat(PlaceholderFilter.apply("prefix:{{ topic:channel }}:{{ topic:group }}:suffix", KNOWN_TOPIC_PATH,
topicPlaceholder)).isEqualTo("prefix:twin:things:suffix");
assertThat(PlaceholderFilter.apply("{{topic:subject}}", KNOWN_TOPIC_PATH_SUBJECT1,
topicPlaceholder)).isEqualTo(KNOWN_SUBJECT);
assertThat(PlaceholderFilter.apply("{{ topic:action-subject}}", KNOWN_TOPIC_PATH_SUBJECT2,
topicPlaceholder)).isEqualTo(KNOWN_SUBJECT2);
}
@Test
public void testThingPlaceholderDebug() {
assertThat(PlaceholderFilter.apply("testTargetAmqpCon4_{{thing:namespace}}:{{thing:name}}", THING_ID,
thingPlaceholder))
.isEqualTo("testTargetAmqpCon4_eclipse:ditto");
}
@Test
public void testValidPlaceholderVariations() {
// no whitespace
assertThat(filterChain("{{thing:namespace}}/{{thing:name}}:{{header:device-id}}",
filterChain)).isEqualTo("eclipse/ditto:" + DEVICE_ID);
// multi whitespace
assertThat(filterChain("{{ thing:namespace }}/{{ thing:name }}:{{ header:device-id }}",
filterChain)).isEqualTo("eclipse/ditto:" + DEVICE_ID);
// mixed whitespace
assertThat(filterChain("{{thing:namespace }}/{{ thing:name }}:{{header:device-id }}",
filterChain)).isEqualTo("eclipse/ditto:" + DEVICE_ID);
// no separators
assertThat(filterChain("{{thing:namespace }}{{ thing:name }}{{header:device-id }}",
filterChain)).isEqualTo("eclipseditto" + DEVICE_ID);
// whitespace separators
assertThat(filterChain("{{thing:namespace }} {{ thing:name }} {{header:device-id }}",
filterChain)).isEqualTo("eclipse ditto " + DEVICE_ID);
// pre/postfix whitespace
assertThat(filterChain(" {{thing:namespace }}{{ thing:name }}{{header:device-id }} ",
filterChain)).isEqualTo(" eclipseditto" + DEVICE_ID + " ");
// pre/postfix
assertThat(filterChain("-----{{thing:namespace }}{{ thing:name }}{{header:device-id }}-----",
filterChain)).isEqualTo("-----eclipseditto" + DEVICE_ID + "-----");
// pre/postfix and separators
assertThat(filterChain("-----{{thing:namespace }}///{{ thing:name }}///{{header:device-id }}-----",
filterChain)).isEqualTo("-----eclipse///ditto///" + DEVICE_ID + "-----");
}
@Test
public void testInvalidPlaceholderVariations() {
// illegal braces combinations
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> filterChain("{{th{{ing:namespace }}{{ thing:name }}{{header:device-id }}",
filterChain));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> filterChain("{{th}}ing:namespace }}{{ thing:name }}{{header:device-id }}",
filterChain));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> filterChain("{{thing:nam{{espace }}{{ thing:name }}{{header:device-id }}",
filterChain));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> filterChain("{{thing:nam}}espace }}{{ thing:name }}{{header:device-id }}",
filterChain));
}
@Test
public void testValidate() {
// no whitespace
PlaceholderFilter.validate("{{thing:namespace}}/{{thing:name}}:{{header:device-id}}", placeholders);
// multi whitespace
PlaceholderFilter.validate("{{ thing:namespace }}/{{ thing:name }}:{{ header:device-id }}", placeholders);
// mixed whitespace
PlaceholderFilter.validate("{{thing:namespace }}/{{ thing:name }}:{{header:device-id }}", placeholders);
// no separators
PlaceholderFilter.validate("{{thing:namespace }}{{ thing:name }}{{header:device-id }}", placeholders);
// whitespace separators
PlaceholderFilter.validate("{{thing:namespace }} {{ thing:name }} {{header:device-id }}", placeholders);
// pre/postfix whitespace
PlaceholderFilter.validate(" {{thing:namespace }}{{ thing:name }}{{header:device-id }} ", placeholders);
// pre/postfix
PlaceholderFilter.validate("-----{{thing:namespace }}{{ thing:name }}{{header:device-id }}-----", placeholders);
// pre/postfix and separators
PlaceholderFilter.validate("-----{{thing:namespace }}///{{ thing:name }}///{{header:device-id }}-----", placeholders);
}
@Test
public void testValidateFails() {
// illegal braces combinations
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{th{{ing:namespace }}{{ thing:name }}{{header:device-id }}", placeholders));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{th}}ing:namespace }}{{ thing:name }}{{header:device-id }}", placeholders));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{thing:nam{{espace }}{{ thing:name }}{{header:device-id }}", placeholders));
assertThatExceptionOfType(UnresolvedPlaceholderException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{thing:nam}}espace }}{{ thing:name }}{{header:device-id }}", placeholders));
assertThatExceptionOfType(PlaceholderFunctionTooComplexException.class).isThrownBy(
() -> PlaceholderFilter.validate("{{ header:unknown | fn:default('fallback') | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() | fn:upper() | fn:lower() }}", placeholders));
}
@Test
public void testValidateAndReplace() {
final String replacement = UUID.randomUUID().toString();
// no whitespace
assertThat(PlaceholderFilter.validateAndReplace("{{thing:namespace}}/{{thing:name}}:{{header:device-id}}", replacement, placeholders))
.isEqualTo(String.format("%s/%s:%s", replacement, replacement, replacement));
// multi whitespace
assertThat(PlaceholderFilter.validateAndReplace("{{ thing:namespace }}/{{ thing:name }}:{{ header:device-id }}", replacement, placeholders))
.isEqualTo(String.format("%s/%s:%s", replacement, replacement, replacement));
// mixed whitespace
assertThat(PlaceholderFilter.validateAndReplace("{{thing:namespace }}/{{ thing:name }}:{{header:device-id }}", replacement, placeholders))
.isEqualTo(String.format("%s/%s:%s", replacement, replacement, replacement));
// no separators
assertThat(PlaceholderFilter.validateAndReplace("{{thing:namespace }}{{ thing:name }}{{header:device-id }}", replacement, placeholders))
.isEqualTo(String.format("%s%s%s", replacement, replacement, replacement));
// whitespace separators
assertThat(PlaceholderFilter.validateAndReplace("{{thing:namespace }} {{ thing:name }} {{header:device-id }}", replacement, placeholders))
.isEqualTo(String.format("%s %s %s", replacement, replacement, replacement));
// pre/postfix whitespace
assertThat(PlaceholderFilter.validateAndReplace(" {{thing:namespace }}{{ thing:name }}{{header:device-id }} ", replacement, placeholders))
.isEqualTo(String.format(" %s%s%s ", replacement, replacement, replacement));
// pre/postfix
assertThat(PlaceholderFilter.validateAndReplace("-----{{thing:namespace }}{{ thing:name }}{{header:device-id }}-----", replacement, placeholders))
.isEqualTo(String.format("-----%s%s%s-----", replacement, replacement, replacement));
// pre/postfix and separators
assertThat(PlaceholderFilter.validateAndReplace("-----{{thing:namespace }}///{{ thing:name }}///{{header:device-id }}-----", replacement, placeholders))
.isEqualTo(String.format("-----%s///%s///%s-----", replacement, replacement, replacement));
}
private static String filterChain(final String template, final FilterTuple... tuples) {
String result = template;
for (final FilterTuple tuple : tuples) {
result = PlaceholderFilter.apply(result, tuple.value, tuple.placeholder, true);
}
return PlaceholderFilter.checkAllPlaceholdersResolved(result);
}
static class FilterTuple {
final Object value;
final Placeholder placeholder;
private FilterTuple(final Object value, final Placeholder placeholder) {
this.value = value;
this.placeholder = placeholder;
}
static FilterTuple of(final Object value, final Placeholder placeholder) {
return new FilterTuple(value, placeholder);
}
}
}