/
DittoMessageMapperTest.java
200 lines (163 loc) · 8.5 KB
/
DittoMessageMapperTest.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
/*
* 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.amqpbridge.mapping.mapper;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.amqpbridge.AmqpBridgeModelFactory;
import org.eclipse.ditto.model.amqpbridge.ExternalMessage;
import org.eclipse.ditto.model.base.common.DittoConstants;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.JsonifiableAdaptable;
import org.eclipse.ditto.protocoladapter.ProtocolFactory;
import org.junit.Ignore;
@Ignore
public class DittoMessageMapperTest extends MessageMapperTest {
@Override
protected MessageMapper createMapper() {
return new DittoMessageMapper();
}
@Override
protected String createSupportedContentType() {
return DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE;
}
@Override
protected List<MessageMapperConfiguration> createValidConfig() {
List<MessageMapperConfiguration> options = new LinkedList<>();
// options.add(MessageMapperConfiguration.from(Collections.emptyMap()));
Map<String, String> map = new HashMap<>();
map.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
options.add(DefaultMessageMapperConfiguration.of(map));
return options;
}
@Override
protected Map<MessageMapperConfiguration, Throwable> createInvalidConfig() {
// there are none
Map<MessageMapperConfiguration, Throwable> map = new HashMap<>();
map.put(DefaultMessageMapperConfiguration.of(Collections.emptyMap()), new IllegalArgumentException("Missing option " +
"<contentType>"));
return map;
}
@Override
protected MessageMapperConfiguration createIncomingConfig() {
Map<String, String> map = new HashMap<>();
map.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
return DefaultMessageMapperConfiguration.of(map);
}
@Override
protected Map<ExternalMessage, Adaptable> createValidIncomingMappings() {
return Stream.of(
valid1(),
valid2()
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private Map.Entry<ExternalMessage, Adaptable> valid1() {
Map<String, String> headers = new HashMap<>();
headers.put("header-key", "header-value");
headers.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
JsonifiableAdaptable adaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(ProtocolFactory.newAdaptableBuilder
(ProtocolFactory.newTopicPathBuilder("asd" +
":jkl").things().twin().commands().modify().build())
.withHeaders(DittoHeaders.of(headers))
.withPayload(ProtocolFactory
.newPayloadBuilder(JsonPointer.of("/features"))
.withValue(JsonFactory.nullLiteral())
.build())
.build());
ExternalMessage message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText(adaptable.toJsonString()).build();
Adaptable expected = ProtocolFactory.newAdaptableBuilder(adaptable).build();
return new AbstractMap.SimpleEntry<>(message, expected);
}
private Map.Entry<ExternalMessage, Adaptable> valid2() {
Map<String, String> headers = new HashMap<>();
headers.put("header-key", "header-value");
headers.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
JsonObject json = JsonFactory.newObjectBuilder()
.set("path","/some/path")
.build();
Adaptable expected = ProtocolFactory.newAdaptableBuilder(ProtocolFactory.jsonifiableAdaptableFromJson(json))
.withHeaders(DittoHeaders.of(headers))
.build();
ExternalMessage message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText(json.toString()).build();
return new AbstractMap.SimpleEntry<ExternalMessage, Adaptable>(message, expected);
}
@Override
protected Map<ExternalMessage, Throwable> createInvalidIncomingMappings() {
Map<ExternalMessage, Throwable> mappings = new HashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put("header-key", "header-value");
headers.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
ExternalMessage message;
message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText("").build();
mappings.put(message, new IllegalArgumentException("Failed to extract string payload from message:"));
// --
message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText("{}").build();
mappings.put(message, new IllegalArgumentException("Failed to map '{}'",
new JsonMissingFieldException("/path")));
// --
message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText("no json").build();
mappings.put(message, new IllegalArgumentException("Failed to map 'no json'",
new JsonParseException("Failed to create JSON object from string!")));
return mappings;
}
@Override
protected MessageMapperConfiguration createOutgoingConfig() {
Map<String, String> map = new HashMap<>();
map.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
return DefaultMessageMapperConfiguration.of(map);
}
@Override
protected Map<Adaptable, ExternalMessage> createValidOutgoingMappings() {
Map<Adaptable, ExternalMessage> mappings = new HashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put("header-key", "header-value");
headers.put(MessageMapperConfigurationProperties.CONTENT_TYPE, DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE);
JsonifiableAdaptable adaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(ProtocolFactory.newAdaptableBuilder
(ProtocolFactory.newTopicPathBuilder("asd" +
":jkl").things().twin().commands().modify().build())
.withHeaders(DittoHeaders.of(headers))
.withPayload(ProtocolFactory
.newPayloadBuilder(JsonPointer.of("/features"))
.withValue(JsonFactory.nullLiteral())
.build())
.build());
ExternalMessage message =
AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText(adaptable.toJsonString()).build();
mappings.put(adaptable, message);
final JsonObject json = JsonFactory.newObjectBuilder()
.set("topic", "org.eclipse.ditto/thing1/things/twin/commands/create")
.set("path","/some/path")
.build();
adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(json);
adaptable = ProtocolFactory.wrapAsJsonifiableAdaptable(ProtocolFactory.newAdaptableBuilder(adaptable)
.withHeaders(DittoHeaders.of(headers)).build());
message = AmqpBridgeModelFactory.newExternalMessageBuilderForCommand(headers).withText(adaptable.toJsonString()).build();
mappings.put(adaptable, message);
return mappings;
}
@Override
protected Map<Adaptable, Throwable> createInvalidOutgoingMappings() {
// adaptible is strongly typed and can always be jsonified, no invalid test needed.
return Collections.emptyMap();
}
}