-
Notifications
You must be signed in to change notification settings - Fork 214
/
SignalFilterWithFilterTest.java
348 lines (305 loc) · 18 KB
/
SignalFilterWithFilterTest.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* 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.connectivity.service.messaging.persistence;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.base.model.auth.AuthorizationModelFactory.newAuthContext;
import static org.eclipse.ditto.base.model.auth.AuthorizationModelFactory.newAuthSubject;
import static org.eclipse.ditto.connectivity.model.Topic.LIVE_EVENTS;
import static org.eclipse.ditto.connectivity.model.Topic.LIVE_MESSAGES;
import static org.eclipse.ditto.connectivity.model.Topic.TWIN_EVENTS;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectionType;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.ConnectivityStatus;
import org.eclipse.ditto.connectivity.model.HeaderMapping;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitorRegistry;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.messages.model.Message;
import org.eclipse.ditto.messages.model.MessageDirection;
import org.eclipse.ditto.messages.model.MessageHeaders;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessage;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingFieldSelector;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingModified;
import org.junit.Test;
/**
* Tests {@link SignalFilter} for filtering with namespace + RQL filter.
*/
public final class SignalFilterWithFilterTest {
private static final String URI = "amqp://user:pass@host:1111/path";
private static final ConnectionId CONNECTION_ID = ConnectionId.of("id");
private static final ThingId THING_ID = ThingId.of("foo:bar13");
private static final AuthorizationSubject AUTHORIZED = newAuthSubject("authorized");
private static final AuthorizationSubject UNAUTHORIZED = newAuthSubject("unauthorized");
private static final HeaderMapping HEADER_MAPPING =
ConnectivityModelFactory.newHeaderMapping(Collections.singletonMap("reply-to", "{{fn:delete()}}"));
private final ConnectionMonitorRegistry connectionMonitorRegistry = TestConstants.Monitoring.MONITOR_REGISTRY_MOCK;
@Test
public void applySignalFilterWithNamespaces() {
// targetA does filter for namespaces "org.eclipse.ditto" and "foo"
final Target targetA = ConnectivityModelFactory.newTargetBuilder()
.address("twin/a")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(List.of("org.eclipse.ditto", "foo"))
.build())
.build();
// targetB does filter for namespaces "org.example"
final Target targetB = ConnectivityModelFactory.newTargetBuilder()
.address("twin/b")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(List.of("org.example"))
.build())
.build();
// targetC does filter for namespaces "foo", but uses the "UNAUTHORIZED" subjects
final Target targetC = ConnectivityModelFactory.newTargetBuilder()
.address("twin/c")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, UNAUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(List.of("foo"))
.build())
.build();
final Connection connection = ConnectivityModelFactory
.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, URI)
.targets(List.of(targetA, targetB, targetC))
.build();
final Thing thing = Thing.newBuilder()
.setId(THING_ID) // WHEN: the namespace of the modified thing is "foo"
.setAttribute(JsonPointer.of("test"), JsonValue.of(42))
.build();
final DittoHeaders headers = DittoHeaders.newBuilder()
.readGrantedSubjects(List.of(AUTHORIZED))
.build();
final ThingModified thingModified = ThingModified.of(thing, 3L, Instant.now(),
headers, null);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final List<Target> filteredTargets = signalFilter.filter(thingModified);
assertThat(filteredTargets).containsOnly(targetA); // THEN: only targetA should be in the filtered targets
}
@Test
public void applySignalFilterWithRqlFilter() {
final List<String> allNamespaces = Collections.emptyList();
// targetA does filter for all namespaces and filters that attribute "test" > 23
final String filterA = "gt(attributes/test,23)";
final Target targetA = ConnectivityModelFactory.newTargetBuilder()
.address("twin/a")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(LIVE_EVENTS)
.withNamespaces(allNamespaces)
.withFilter(filterA)
.build())
.build();
// targetB does filter for all namespaces and filters that attribute "test" > 50
final String filterB = "gt(attributes/test,50)";
final Target targetB = ConnectivityModelFactory.newTargetBuilder()
.address("twin/b")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(LIVE_EVENTS)
.withNamespaces(allNamespaces)
.withFilter(filterB)
.build())
.build();
// targetC does filter for all namespaces and filters that attribute "test" > 23, but uses the "UNAUTHORIZED" subjects
final String filterC = "gt(attributes/test,50)";
final Target targetC = ConnectivityModelFactory.newTargetBuilder()
.address("twin/c")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, UNAUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(LIVE_EVENTS)
.withNamespaces(allNamespaces)
.withFilter(filterC)
.build())
.build();
final Connection connection = ConnectivityModelFactory
.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10, ConnectivityStatus.OPEN, URI)
.targets(Arrays.asList(targetA, targetB, targetC))
.build();
final Thing thing = Thing.newBuilder()
.setId(THING_ID)
.setAttribute(JsonPointer.of("test"), JsonValue.of(42)) // WHEN: the "test" value is 42
.build();
final DittoHeaders headers = DittoHeaders.newBuilder()
.readGrantedSubjects(Collections.singletonList(AUTHORIZED))
.channel(TopicPath.Channel.LIVE.getName())
.build();
final ThingModified thingModified = ThingModified.of(thing, 3L, Instant.now(),
headers, null);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final List<Target> filteredTargets = signalFilter.filter(thingModified);
assertThat(filteredTargets).containsOnly(targetA); // THEN: only targetA should be in the filtered targets
}
@Test
public void applySignalFilterWithNamespacesAndRqlFilter() {
// targetA does filter for namespaces "org.eclipse.ditto" and "foo" and filters that attribute "test" > 23
final List<String> namespacesA = Arrays.asList("org.eclipse.ditto", "foo");
final String filterA = "gt(attributes/test,23)";
final Target targetA = ConnectivityModelFactory.newTargetBuilder()
.address("twin/a")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(namespacesA)
.withFilter(filterA)
.build())
.build();
// targetB does filter for namespaces "org.example" and filters that attribute "test" < 50
final List<String> namespacesB = Collections.singletonList("org.example");
final String filterB = "lt(attributes/test,50)";
final Target targetB = ConnectivityModelFactory.newTargetBuilder()
.address("twin/b")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(namespacesB)
.withFilter(filterB)
.build())
.build();
// targetC does filter for namespaces "foo" and filters that attribute "test" == 42, but uses the "UNAUTHORIZED" subjects
final List<String> namespacesC = Collections.singletonList("foo");
final String filterC = "eq(attributes/test,42)";
final Target targetC = ConnectivityModelFactory.newTargetBuilder()
.address("twin/c")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, UNAUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(namespacesC)
.withFilter(filterC)
.build())
.build();
// targetD does filter for namespaces "foo" and filters that attribute "test" == 42
final List<String> namespacesD = Collections.singletonList("foo");
final String filterD = "eq(attributes/test,42)";
final Target targetD = ConnectivityModelFactory.newTargetBuilder()
.address("twin/d")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withNamespaces(namespacesD)
.withFilter(filterD)
.build())
.build();
final Connection connection =
ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10,
ConnectivityStatus.OPEN, URI)
.targets(Arrays.asList(targetA, targetB, targetC, targetD))
.build();
final Thing thing = Thing.newBuilder()
.setId(THING_ID) // WHEN: the namespace of the modified thing is "foo"
.setAttribute(JsonPointer.of("test"), JsonValue.of(42)) // WHEN: the "test" value is 42
.build();
final DittoHeaders headers = DittoHeaders.newBuilder()
.readGrantedSubjects(Collections.singletonList(AUTHORIZED))
.build();
final ThingModified thingModified = ThingModified.of(thing, 3L, Instant.now(),
headers, null);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final List<Target> filteredTargets = signalFilter.filter(thingModified);
assertThat(filteredTargets).containsOnly(targetA,
targetD); // THEN: only targetA and targetD should be in the filtered targets
}
@Test
public void applySignalFilterForMessagesWithExtraFieldsAndRqlFilter() {
// targetA does filter for resource path "/inbox/messages/fubar"
final String filterA = "eq(resource:path,'/inbox/messages/fubar')";
final Target targetA = ConnectivityModelFactory.newTargetBuilder()
.address("message/a")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(LIVE_MESSAGES)
.withExtraFields(ThingFieldSelector.fromString("attributes"))
.withFilter(filterA)
.build())
.build();
// targetB does filter for resource path "/inbox/messages/booo"
final String filterB = "eq(resource:path,'/inbox/messages/booo')";
final Target targetB = ConnectivityModelFactory.newTargetBuilder()
.address("message/a")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.headerMapping(HEADER_MAPPING)
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(LIVE_MESSAGES)
.withExtraFields(ThingFieldSelector.fromString("attributes"))
.withFilter(filterB)
.build())
.build();
final Connection connection =
ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID, ConnectionType.AMQP_10,
ConnectivityStatus.OPEN, URI)
.targets(Arrays.asList(targetA, targetB))
.build();
final DittoHeaders headers = DittoHeaders.newBuilder()
.channel(TopicPath.Channel.LIVE.getName())
.readGrantedSubjects(Collections.singletonList(AUTHORIZED))
.build();
final SendThingMessage<Object> sendThingMessage = SendThingMessage.of(THING_ID,
Message.newBuilder(MessageHeaders.newBuilder(MessageDirection.TO, THING_ID, "fubar").build())
.build(), headers);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final List<Target> filteredTargets = signalFilter.filter(sendThingMessage);
assertThat(filteredTargets).containsOnly(targetA); // THEN: only targetA should be in the filtered targets
}
/**
* Test that target filtering works also for desired properties events. Issue #1599
*/
@Test
public void applySignalFilterOnFeatureDesiredPropertiesModified() {
final Target target = ConnectivityModelFactory.newTargetBuilder().address("address")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withFilter("like(resource:path,'/features/" + TestConstants.Feature.FEATURE_ID + "*')")
.build()).build();
final Connection connection = TestConstants.createConnection(CONNECTION_ID, target);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final Signal<?> signal = TestConstants.featureDesiredPropertiesModified(Collections.singletonList(AUTHORIZED));
final List<Target> filteredTargets = signalFilter.filter(signal);
Assertions.assertThat(filteredTargets).hasSize(1).contains(target);
}
/**
* Test that target filtering works using feature:id placeholder
*/
@Test
public void applySignalFilterWithFeatureIdPlaceholder() {
Target target = ConnectivityModelFactory.newTargetBuilder().address("address")
.authorizationContext(newAuthContext(DittoAuthorizationContextType.UNSPECIFIED, AUTHORIZED))
.topics(ConnectivityModelFactory.newFilteredTopicBuilder(TWIN_EVENTS)
.withFilter("eq(feature:id,'Feature')")
.build()
)
.build();
final Connection connection = TestConstants.createConnection(CONNECTION_ID, target);
final SignalFilter signalFilter = new SignalFilter(connection, connectionMonitorRegistry);
final Signal<?> signal = TestConstants.featurePropertiesModified(Collections.singletonList(AUTHORIZED));
final List<Target> filteredTargets = signalFilter.filter(signal);
Assertions.assertThat(filteredTargets).hasSize(1).contains(target);
}
}