-
Notifications
You must be signed in to change notification settings - Fork 216
/
DittoProtocolAdapterParameterizedTest.java
414 lines (370 loc) · 20.5 KB
/
DittoProtocolAdapterParameterizedTest.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright (c) 2020 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.protocol.adapter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.protocol.TopicPath.Channel.LIVE;
import static org.eclipse.ditto.protocol.TopicPath.Channel.NONE;
import static org.eclipse.ditto.protocol.TopicPath.Channel.TWIN;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse;
import org.eclipse.ditto.policies.model.signals.commands.PolicyErrorResponse;
import org.eclipse.ditto.policies.model.signals.commands.modify.PolicyModifyCommand;
import org.eclipse.ditto.policies.model.signals.commands.modify.PolicyModifyCommandResponse;
import org.eclipse.ditto.policies.model.signals.commands.query.PolicyQueryCommand;
import org.eclipse.ditto.policies.model.signals.commands.query.PolicyQueryCommandResponse;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.UnknownChannelException;
import org.eclipse.ditto.protocol.adapter.connectivity.ConnectivityCommandAdapterProvider;
import org.eclipse.ditto.protocol.adapter.provider.AcknowledgementAdapterProvider;
import org.eclipse.ditto.protocol.adapter.provider.PolicyCommandAdapterProvider;
import org.eclipse.ditto.protocol.adapter.provider.ThingCommandAdapterProvider;
import org.eclipse.ditto.things.model.signals.commands.ThingErrorResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThingResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommandResponse;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommandResponse;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingMerged;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Unit test for {@link org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter}.
*/
@SuppressWarnings({"unchecked", "rawtypes", "java:S3740"})
@RunWith(Parameterized.class)
public final class DittoProtocolAdapterParameterizedTest {
private static final Logger LOGGER = LoggerFactory.getLogger(DittoProtocolAdapterParameterizedTest.class);
private static final DittoHeaders LIVE_DITTO_HEADERS = DittoHeaders.newBuilder().channel(LIVE.getName()).build();
// mock all adapters and give them a name
private static final Adapter<ThingQueryCommand<?>> thingQueryCommandAdapter =
mock(Adapter.class, "ThingQueryCommandAdapter");
private static final Adapter<ThingQueryCommandResponse<?>> thingQueryCommandResponseAdapter =
mock(Adapter.class, "ThingQueryCommandResponseAdapter");
private static final Adapter<ThingModifyCommand<?>> thingModifyCommandAdapter =
mock(Adapter.class, "ThingModifyCommandAdapter");
private static final Adapter<ThingModifyCommandResponse<?>> thingModifyCommandResponseAdapter =
mock(Adapter.class, "ThingModifyCommandResponseAdapter");
private static final Adapter<MergeThing> thingMergeCommandAdapter =
mock(Adapter.class, "ThingModifyCommandAdapter");
private static final Adapter<MergeThingResponse> thingMergeCommandResponseAdapter =
mock(Adapter.class, "ThingModifyCommandResponseAdapter");
private static final Adapter<ThingEvent<?>> thingEventAdapter = mock(Adapter.class, "ThingEventAdapter");
private static final Adapter<ThingMerged> thingMergedEventAdapter = mock(Adapter.class, "ThingMergedEventAdapter");
private static final Adapter<MessageCommand<?, ?>> messageCommandAdapter =
mock(Adapter.class, "MessageCommandAdapter");
private static final Adapter<MessageCommandResponse<?, ?>> messageCommandResponseAdapter =
mock(Adapter.class, "MessageCommandResponseAdapter");
private static final Adapter<ThingErrorResponse> thingErrorResponseAdapter =
mock(Adapter.class, "ThingErrorResponseAdapter");
private static final Adapter<PolicyQueryCommand<?>> policyQueryCommandAdapter =
mock(Adapter.class, "PolicyQueryCommandAdapter");
private static final Adapter<PolicyQueryCommandResponse<?>> policyQueryCommandResponseAdapter =
mock(Adapter.class, "PolicyQueryCommandResponseAdapter");
private static final Adapter<PolicyModifyCommand<?>> policyModifyCommandAdapter =
mock(Adapter.class, "PolicyModifyCommandAdapter");
private static final Adapter<PolicyModifyCommandResponse<?>> policyModifyCommandResponseAdapter =
mock(Adapter.class, "PolicyModifyCommandResponseAdapter");
private static final Adapter<PolicyErrorResponse> policyErrorResponseAdapter =
mock(Adapter.class, "PolicyErrorResponseAdapter");
// build test parameters with expected outcome
private static final List<TestParameter> PARAMS;
static {
PARAMS = new ArrayList<>();
PARAMS.add(new TestParameter(ThingQueryCommand.class, thingQueryCommandAdapter, TWIN, TWIN, LIVE));
PARAMS.add(
new TestParameter(ThingQueryCommandResponse.class, thingQueryCommandResponseAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(ThingModifyCommand.class, thingModifyCommandAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(ThingModifyCommandResponse.class, thingModifyCommandResponseAdapter, TWIN, TWIN,
LIVE));
PARAMS.add(new TestParameter(MergeThing.class, thingMergeCommandAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(MergeThingResponse.class, thingMergeCommandResponseAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(ThingErrorResponse.class, thingErrorResponseAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(MessageCommand.class, messageCommandAdapter, LIVE, LIVE));
PARAMS.add(new TestParameter(MessageCommandResponse.class, messageCommandResponseAdapter, LIVE, LIVE));
PARAMS.add(new TestParameter(ThingEvent.class, thingEventAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(ThingMerged.class, thingMergedEventAdapter, TWIN, TWIN, LIVE));
PARAMS.add(new TestParameter(PolicyQueryCommand.class, policyQueryCommandAdapter, NONE, NONE));
PARAMS.add(new TestParameter(PolicyQueryCommandResponse.class, policyQueryCommandResponseAdapter, NONE, NONE));
PARAMS.add(new TestParameter(PolicyModifyCommand.class, policyModifyCommandAdapter, NONE, NONE));
PARAMS.add(
new TestParameter(PolicyModifyCommandResponse.class, policyModifyCommandResponseAdapter, NONE, NONE));
PARAMS.add(new TestParameter(PolicyErrorResponse.class, policyErrorResponseAdapter, NONE, NONE));
}
private ProtocolAdapter underTest;
/**
* Contains the input signal and the expected outcome (which adapter is called with which channel).
*/
@Parameterized.Parameter
public TestParameter parameter;
/**
* The method to call on the {@link ProtocolAdapter} (is determined via reflection to avoid hard coding every
* combination in the test).
*/
@Parameterized.Parameter(1)
public Method toAdaptable;
/**
* The ditto headers of the signal. The ProtocolAdapter determines the (live) channel from the headers.
*/
@Parameterized.Parameter(2)
public DittoHeaders dittoHeaders;
/**
* The protocol adapter may be called with a specific channel. If this parameter is null the method without
* specifying a channel (e.g. toAdaptable(Signal)) is called.
*/
@Parameterized.Parameter(3)
public TopicPath.Channel givenChannel;
/**
* The channel that is expected in the call to the {@link Adapter}. If this parameter {@code null} the test expects
* an exception.
*/
@Parameterized.Parameter(4)
public TopicPath.Channel expectedChannel;
/**
* Just to have a better description for the test.
*/
@Parameterized.Parameter(5)
public String methodSignature;
@Parameterized.Parameters(name = "{index} {0} | {5} | given: {3} | exp: {4} | header: {2}")
public static Collection<Object[]> data() {
final Collection<Object[]> data = new ArrayList<>();
PARAMS.forEach(testParameter ->
collectInterfaces(testParameter.signalBaseClass)
.forEach(signalClass -> {
// no channel provided via argument
findToAdaptable(signalClass).ifPresent(m -> data.add(new Object[]{testParameter, m,
DittoHeaders.empty(), null, testParameter.expectedDefaultChannel,
signatureAsString(m)}));
// channel (live) provided via header, no channel provided via argument
findToAdaptable(signalClass).ifPresent(m -> data.add(new Object[]{testParameter, m,
LIVE_DITTO_HEADERS, null, testParameter.supportsLive() ? LIVE : null,
signatureAsString(m)}));
// channel provided via argument
Arrays.asList(TopicPath.Channel.values()).forEach(ch -> {
// null means unsupported -> exception is thrown
final TopicPath.Channel expectedChannel
= Arrays.asList(testParameter.supportedChannels).contains(ch) ? ch : null;
findToAdaptableWithChannel(signalClass).ifPresent(
m -> data.add(new Object[]{testParameter, m,
DittoHeaders.empty(), ch, expectedChannel, signatureAsString(m)}));
});
})
);
return data;
}
@Before
public void setUp() {
final ThingCommandAdapterProvider thingCommandAdapterProvider = mock(ThingCommandAdapterProvider.class);
final PolicyCommandAdapterProvider policyCommandAdapterProvider = mock(PolicyCommandAdapterProvider.class);
final AcknowledgementAdapterProvider acknowledgementAdapterProvider =
mock(AcknowledgementAdapterProvider.class);
final ConnectivityCommandAdapterProvider connectivityCommandAdapterProvider =
mock(ConnectivityCommandAdapterProvider.class);
final StreamingSubscriptionCommandAdapter streamingSubscriptionCommandAdapter =
mock(StreamingSubscriptionCommandAdapter.class);
final StreamingSubscriptionEventAdapter streamingSubscriptionEventAdapter =
mock(StreamingSubscriptionEventAdapter.class);
when(thingCommandAdapterProvider.getQueryCommandAdapter())
.thenReturn(thingQueryCommandAdapter);
when(thingCommandAdapterProvider.getQueryCommandResponseAdapter())
.thenReturn(thingQueryCommandResponseAdapter);
when(thingCommandAdapterProvider.getModifyCommandAdapter())
.thenReturn(thingModifyCommandAdapter);
when(thingCommandAdapterProvider.getModifyCommandResponseAdapter())
.thenReturn(thingModifyCommandResponseAdapter);
when(thingCommandAdapterProvider.getMessageCommandAdapter())
.thenReturn(messageCommandAdapter);
when(thingCommandAdapterProvider.getMessageCommandResponseAdapter())
.thenReturn(messageCommandResponseAdapter);
when(thingCommandAdapterProvider.getErrorResponseAdapter())
.thenReturn(thingErrorResponseAdapter);
when(thingCommandAdapterProvider.getEventAdapter())
.thenReturn(thingEventAdapter);
when(thingCommandAdapterProvider.getMergeCommandAdapter())
.thenReturn(thingMergeCommandAdapter);
when(thingCommandAdapterProvider.getMergeCommandResponseAdapter())
.thenReturn(thingMergeCommandResponseAdapter);
when(thingCommandAdapterProvider.getMergedEventAdapter())
.thenReturn(thingMergedEventAdapter);
when(policyCommandAdapterProvider.getQueryCommandAdapter())
.thenReturn(policyQueryCommandAdapter);
when(policyCommandAdapterProvider.getQueryCommandResponseAdapter())
.thenReturn(policyQueryCommandResponseAdapter);
when(policyCommandAdapterProvider.getModifyCommandAdapter())
.thenReturn(policyModifyCommandAdapter);
when(policyCommandAdapterProvider.getModifyCommandResponseAdapter())
.thenReturn(policyModifyCommandResponseAdapter);
when(policyCommandAdapterProvider.getErrorResponseAdapter())
.thenReturn(policyErrorResponseAdapter);
final AdapterResolver adapterResolver = new DefaultAdapterResolver(thingCommandAdapterProvider,
policyCommandAdapterProvider, connectivityCommandAdapterProvider, acknowledgementAdapterProvider,
streamingSubscriptionCommandAdapter, streamingSubscriptionEventAdapter);
underTest = DittoProtocolAdapter.newInstance(HeaderTranslator.empty(), thingCommandAdapterProvider,
policyCommandAdapterProvider, connectivityCommandAdapterProvider, acknowledgementAdapterProvider,
streamingSubscriptionCommandAdapter, streamingSubscriptionEventAdapter,
adapterResolver);
reset(thingQueryCommandAdapter);
reset(thingQueryCommandResponseAdapter);
reset(thingModifyCommandAdapter);
reset(thingModifyCommandResponseAdapter);
reset(thingMergeCommandAdapter);
reset(thingMergeCommandResponseAdapter);
reset(thingEventAdapter);
reset(thingMergedEventAdapter);
reset(thingErrorResponseAdapter);
reset(policyQueryCommandAdapter);
reset(policyQueryCommandResponseAdapter);
reset(policyModifyCommandAdapter);
reset(policyModifyCommandResponseAdapter);
reset(policyErrorResponseAdapter);
reset(messageCommandAdapter);
reset(messageCommandResponseAdapter);
}
@Test
public void testCorrectAdapterCalledOrExceptionThrownWithDefaultChannel() {
Throwable caughtException = null;
// channel may be determined from ditto headers e.g. when converting from signal to adaptable
when(parameter.signal.getDittoHeaders()).thenReturn(dittoHeaders);
try {
final Object[] args;
if (givenChannel != null) {
// call the variant with fixed channel (channel is not determined from header and no default is used)
args = new Object[]{parameter.signal, givenChannel};
} else {
// call the variant without channel (channel is determined from header or default is used)
args = new Object[]{parameter.signal};
}
LOGGER.info("calling: {}({}){} for signal {} and headers {}", toAdaptable.getName(),
signatureAsString(toAdaptable),
givenChannel == null ? "" : " with channel '" + givenChannel + "'",
parameter.signal,
dittoHeaders.entrySet());
toAdaptable.invoke(underTest, args);
} catch (InvocationTargetException e) {
caughtException = e.getCause();
} catch (Exception e) {
caughtException = e;
}
if (caughtException != null) {
LOGGER.info("exception: {} ({})", caughtException.getClass().getName(), caughtException.getMessage());
}
if (expectedChannel != null) {
assertThat(caughtException).isNull();
// expect the correct adapter is called with correct arguments
verify(parameter.expectedAdapter).toAdaptable(parameter.signal, expectedChannel);
} else {
// toAdaptable was called with invalid argument/channel -> expect exception
assertThat(caughtException).isInstanceOf(UnknownChannelException.class);
}
}
/**
* @return all interfaces implemented by the given array of classes
*/
private static Set<Class<?>> collectInterfaces(Class<?>... c) {
final Set<Class<?>> ifcs = new HashSet<>();
for (final Class<?> cls : c) {
if (Signal.class.isAssignableFrom(cls)) {
ifcs.add(cls);
}
final Class<?>[] interfaces = cls.getInterfaces();
ifcs.addAll(collectInterfaces(interfaces));
}
return ifcs;
}
/**
* Finds the method to test via reflection to avoid hard-coding every combination in the test.
*
* @param clazz type of the first parameter
* @return the {@code toAdaptable} method to test or empty optional
*/
private static Optional<Method> findToAdaptableWithChannel(final Class<?> clazz) {
return Arrays.stream(ProtocolAdapter.class.getMethods())
.filter(m -> "toAdaptable".equals(m.getName()))
.filter(m -> 2 == m.getParameterCount())
.filter(m -> clazz.equals(m.getParameterTypes()[0]))
.filter(m -> TopicPath.Channel.class.equals(m.getParameterTypes()[1]))
.findFirst();
}
/**
* Finds the method to test via reflection to avoid hard-coding every combination in the test.
*
* @param clazz type of the first parameter
* @return the {@code toAdaptable} method to test or empty optional
*/
private static Optional<Method> findToAdaptable(final Class<?> clazz) {
return Arrays.stream(ProtocolAdapter.class.getMethods())
.filter(m -> "toAdaptable".equals(m.getName()))
.filter(m -> clazz.equals(m.getParameterTypes()[0]))
.filter(m -> 1 == m.getParameterCount())
.findFirst();
}
private static String signatureAsString(final Method method) {
return "toAdaptable(" +
Arrays.stream(method.getParameterTypes())
.map(Class::getSimpleName).collect(Collectors.joining(",")) +
")";
}
/**
* Container class for test parameters.
*/
private static class TestParameter<T extends Signal<T>> {
final Class<T> signalBaseClass;
final T signal;
final Adapter<T> expectedAdapter;
final TopicPath.Channel expectedDefaultChannel;
final TopicPath.Channel[] supportedChannels;
private TestParameter(final Class<T> signalBaseClass, final Adapter<T> expectedAdapter,
final TopicPath.Channel expectedDefaultChannel, TopicPath.Channel... supportedChannels) {
this.signalBaseClass = signalBaseClass;
this.signal = mockSignal(signalBaseClass);
this.expectedAdapter = expectedAdapter;
this.expectedDefaultChannel = expectedDefaultChannel;
this.supportedChannels = supportedChannels;
}
private static <T extends Signal<T>> T mockSignal(Class<T> signalClass) {
final T mockedSignal = mock(signalClass, withSettings().name(signalClass.getSimpleName()));
when(mockedSignal.getDittoHeaders()).thenReturn(DittoHeaders.empty());
return mockedSignal;
}
private boolean supportsLive() {
return Arrays.asList(supportedChannels).contains(LIVE);
}
@Override
public String toString() {
return signalBaseClass.getSimpleName();
}
}
}