-
Notifications
You must be signed in to change notification settings - Fork 215
/
UpdateTwinWithLiveResponseMessageMapperTest.java
300 lines (264 loc) · 15.5 KB
/
UpdateTwinWithLiveResponseMessageMapperTest.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
/*
* Copyright (c) 2021 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.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.common.DittoConstants;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeader;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.service.messaging.TestConstants;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.JsonifiableAdaptable;
import org.eclipse.ditto.protocol.ProtocolFactory;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveAttributeResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import akka.actor.ActorSystem;
/**
* Unit test for {@link UpdateTwinWithLiveResponseMessageMapper}.
*/
public final class UpdateTwinWithLiveResponseMessageMapperTest {
private static final DittoProtocolAdapter DITTO_PROTOCOL_ADAPTER = DittoProtocolAdapter.newInstance();
private static final AuthorizationContext KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT =
AuthorizationContext.newInstance(
DittoAuthorizationContextType.PRE_AUTHENTICATED_CONNECTION,
AuthorizationSubject.newInstance("pre-authenticated:test-connection"));
private static final ThingId KNOWN_THING_ID = ThingId.generateRandom();
private static final String KNOWN_FEATURE_ID = "a-feature";
private static final FeatureProperties KNOWN_FEATURE_PROPERTIES = FeatureProperties.newBuilder()
.set("a", "bar")
.set("b", 42)
.build();
private static final MetadataHeaderKey KNOWN_METADATA_HEADER_KEY_0 = MetadataHeaderKey.parse("*/updated-by");
private static final MetadataHeaderKey KNOWN_METADATA_HEADER_KEY_1 = MetadataHeaderKey.parse("*/updated-via");
private static final MetadataHeaderKey KNOWN_METADATA_HEADER_KEY_2 = MetadataHeaderKey.parse("*/update-hint");
private static final MetadataHeaderKey KNOWN_METADATA_HEADER_KEY_3 = MetadataHeaderKey.parse("*/updated-at");
private static final MetadataHeaderKey KNOWN_METADATA_HEADER_KEY_4 = MetadataHeaderKey.parse("*/unresolved");
private static final JsonValue KNOWN_METADATA_VALUE_0 = JsonValue.of("{{ request:subjectId }}");
private static final JsonValue KNOWN_METADATA_VALUE_1 = JsonValue.of("device-live-response");
private static final String CUSTOM_HEADER_KEY = "some-custom-hint";
private static final String CUSTOM_HEADER_VALUE = "my-awesome-hint";
private static final JsonValue KNOWN_METADATA_VALUE_2 = JsonValue.of("{{ header:" + CUSTOM_HEADER_KEY + " }}");
private static final JsonValue KNOWN_METADATA_VALUE_3 = JsonValue.of("{{ time:now }}");
private static final String UNRESOLVED_HEADER_KEY = "unresolvable-header";
private static final String UNRESOLVABLE_HEADER_DEFAULT_FALLBACK = "none";
private static final JsonValue KNOWN_METADATA_VALUE_4 = JsonValue.of("{{header:" + UNRESOLVED_HEADER_KEY +
"|fn:default('" + UNRESOLVABLE_HEADER_DEFAULT_FALLBACK + "')}}");
private static final MetadataHeaders KNOWN_METADATA_HEADERS = MetadataHeaders.newInstance();
static {
KNOWN_METADATA_HEADERS.add(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_0, KNOWN_METADATA_VALUE_0));
KNOWN_METADATA_HEADERS.add(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_1, KNOWN_METADATA_VALUE_1));
KNOWN_METADATA_HEADERS.add(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_2, KNOWN_METADATA_VALUE_2));
KNOWN_METADATA_HEADERS.add(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_3, KNOWN_METADATA_VALUE_3));
KNOWN_METADATA_HEADERS.add(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_4, KNOWN_METADATA_VALUE_4));
}
private Connection connection;
private ActorSystem actorSystem;
private MessageMapper underTest;
@Before
public void setUp() {
connection = TestConstants.createConnection();
actorSystem = ActorSystem.create("Test", TestConstants.CONFIG);
underTest = new UpdateTwinWithLiveResponseMessageMapper(actorSystem, Mockito.mock(Config.class));
}
@After
public void tearDown() {
if (actorSystem != null) {
actorSystem.terminate();
actorSystem = null;
}
}
@Test
public void mapNonJsonToEmptyMappingResult() {
final Map<String, String> headers = new HashMap<>();
underTest.configure(connection, TestConstants.CONNECTIVITY_CONFIG, createMapperConfig(Map.of()), actorSystem);
final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withText("foobar")
.build();
final List<Adaptable> mappingResult = underTest.map(externalMessage);
assertThat(mappingResult).isEmpty();
}
@Test
public void mapTwinResponseToEmptyMappingResult() {
final List<Adaptable> result = mapResponse(
createRetrieveAttributeResponse("cid", TopicPath.Channel.TWIN),
KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT, Map.of(), Map.of());
assertThat(result).isEmpty();
}
@Test
public void mapRetrieveThingTwinResponseToEmptyMappingResult() {
final List<Adaptable> result = mapResponse(
createRetrieveThingResponse("cid", TopicPath.Channel.TWIN),
KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT, Map.of(), Map.of());
assertThat(result).isEmpty();
}
@Test
public void mapRetrieveThingLiveResponseToExpectedMergeThingMappingResult() {
final String correlationId = "mapRetrieveThingLiveResponseToExpectedMergeThingMappingResult";
final List<Adaptable> result = mapResponse(
createRetrieveThingResponse(correlationId, TopicPath.Channel.LIVE),
KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT, Map.of(), Map.of());
assertThat(result).isNotEmpty();
final Signal<?> firstMappedSignal = getFirstMappedSignal(result);
assertThat(firstMappedSignal).isInstanceOf(MergeThing.class);
final MergeThing mergeThing = (MergeThing) firstMappedSignal;
assertThat((CharSequence) mergeThing.getResourcePath()).isEqualTo(JsonPointer.empty());
assertThat(mergeThing.getValue()).isEqualTo(JsonObject.newBuilder()
.set("/thingId", JsonValue.of(KNOWN_THING_ID))
.set("/features/" + KNOWN_FEATURE_ID + "/properties", KNOWN_FEATURE_PROPERTIES.toJson())
.build());
assertThat(mergeThing.getDittoHeaders().getCorrelationId())
.contains(correlationId + UpdateTwinWithLiveResponseMessageMapper.CORRELATION_ID_SUFFIX);
assertThat(mergeThing.getDittoHeaders().getChannel()).isEmpty();
}
@Test
public void mapLiveResponseToExpectedMergeThingMappingResult() {
final String correlationId = "mapLiveResponseToExpectedMergeThingMappingResult";
final List<Adaptable> result = mapResponse(
createRetrieveAttributeResponse(correlationId, TopicPath.Channel.LIVE),
KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT, Map.of(), Map.of());
assertRetrieveAttributeResponseResultedMerge(correlationId, result);
}
@Test
public void mapLiveResponseWithSpecifiedHeadersForMergeToExpectedMergeThingMappingResult() {
final String correlationId = "mapLiveResponseWithSpecifiedHeadersForMergeToExpectedMergeThingMappingResult";
final List<Adaptable> result = mapResponse(
createRetrieveAttributeResponse(correlationId, TopicPath.Channel.LIVE),
KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT,
Map.of(
UpdateTwinWithLiveResponseMessageMapper.DITTO_HEADERS_FOR_MERGE,
JsonObject.newBuilder()
.set(DittoHeaderDefinition.RESPONSE_REQUIRED.getKey(), JsonValue.of(false))
.set(DittoHeaderDefinition.PUT_METADATA.getKey(), KNOWN_METADATA_HEADERS.toJson())
.build()
),
Map.of(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE));
assertRetrieveAttributeResponseResultedMerge(correlationId, result);
final Signal<?> firstMappedSignal = getFirstMappedSignal(result);
final MergeThing mergeThing = (MergeThing) firstMappedSignal;
assertThat(mergeThing.getDittoHeaders().isResponseRequired())
.isFalse();
assertThat(mergeThing.getDittoHeaders())
.containsKey(DittoHeaderDefinition.PUT_METADATA.getKey());
assertThat(mergeThing.getDittoHeaders().getMetadataHeadersToPut().stream())
.contains(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_0,
JsonValue.of(KNOWN_EXTERNAL_MESSAGE_AUTHORIZATION_CONTEXT.getFirstAuthorizationSubject()
.orElseThrow().toString()))
)
.contains(MetadataHeader.of(KNOWN_METADATA_HEADER_KEY_1, KNOWN_METADATA_VALUE_1))
.matches(l -> l.stream()
.filter(mh -> mh.getKey().equals(KNOWN_METADATA_HEADER_KEY_2))
.allMatch(mh -> mh.getValue().isString() &&
mh.getValue().asString().equals(CUSTOM_HEADER_VALUE)),
"contains '" + KNOWN_METADATA_HEADER_KEY_2 + "' with value " + CUSTOM_HEADER_VALUE
)
.matches(l -> l.stream()
.filter(mh -> mh.getKey().equals(KNOWN_METADATA_HEADER_KEY_3))
.allMatch(mh -> mh.getValue().isString() &&
Instant.parse(mh.getValue().asString()).isBefore(Instant.now())
),
"contains '" + KNOWN_METADATA_HEADER_KEY_3 + "' with instant before now"
)
.matches(l -> l.stream()
.filter(mh -> mh.getKey().equals(KNOWN_METADATA_HEADER_KEY_4))
.allMatch(mh -> mh.getValue().isString() &&
mh.getValue().asString().equals(UNRESOLVABLE_HEADER_DEFAULT_FALLBACK)),
"contains '" + KNOWN_METADATA_HEADER_KEY_4 + "' with fallback value " +
UNRESOLVABLE_HEADER_DEFAULT_FALLBACK
);
}
private static void assertRetrieveAttributeResponseResultedMerge(final String correlationId,
final List<Adaptable> result) {
assertThat(result).isNotEmpty();
final Signal<?> firstMappedSignal = getFirstMappedSignal(result);
assertThat(firstMappedSignal).isInstanceOf(MergeThing.class);
final MergeThing mergeThing = (MergeThing) firstMappedSignal;
assertThat((CharSequence) mergeThing.getResourcePath()).isEqualTo(JsonPointer.of("attributes/foo"));
assertThat(mergeThing.getValue()).isEqualTo(JsonValue.of("bar"));
assertThat(mergeThing.getDittoHeaders().getCorrelationId())
.contains(correlationId + UpdateTwinWithLiveResponseMessageMapper.CORRELATION_ID_SUFFIX);
}
private static ThingQueryCommandResponse<?> createRetrieveAttributeResponse(final String correlationId,
final TopicPath.Channel channel) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel(channel.getName())
.correlationId(correlationId)
.build();
return RetrieveAttributeResponse.of(KNOWN_THING_ID, JsonPointer.of("foo"), JsonValue.of("bar"), dittoHeaders);
}
private static ThingQueryCommandResponse<?> createRetrieveThingResponse(final String correlationId,
final TopicPath.Channel channel) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.channel(channel.getName())
.correlationId(correlationId)
.build();
return RetrieveThingResponse.of(KNOWN_THING_ID, Thing.newBuilder()
.setFeature(KNOWN_FEATURE_ID, KNOWN_FEATURE_PROPERTIES)
.setId(KNOWN_THING_ID)
.build(), null, null, dittoHeaders);
}
private List<Adaptable> mapResponse(final ThingQueryCommandResponse<?> response,
final AuthorizationContext authorizationContext,
final Map<String, JsonValue> options,
final Map<String, String> additionalHeaders) {
final Map<String, String> headers = new HashMap<>();
headers.put(ExternalMessage.CONTENT_TYPE_HEADER, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
headers.putAll(additionalHeaders);
underTest.configure(connection, TestConstants.CONNECTIVITY_CONFIG, createMapperConfig(options), actorSystem);
final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers)
.withAuthorizationContext(authorizationContext)
.withText(getJsonifiableAdaptable(response).toJsonString())
.build();
return underTest.map(externalMessage);
}
private static JsonifiableAdaptable getJsonifiableAdaptable(final Signal<?> signal) {
return ProtocolFactory.wrapAsJsonifiableAdaptable(DITTO_PROTOCOL_ADAPTER.toAdaptable(signal));
}
private static Signal<?> getFirstMappedSignal(final List<Adaptable> mappingResult) {
return mappingResult.stream().findFirst()
.map(DITTO_PROTOCOL_ADAPTER::fromAdaptable)
.orElseGet(() -> fail("Mapping Result did not contain a Signal."));
}
private DefaultMessageMapperConfiguration createMapperConfig(final Map<String, JsonValue> options) {
return DefaultMessageMapperConfiguration.of("valid", options, Collections.emptyMap(), Collections.emptyMap());
}
}