Skip to content

Commit 2394c91

Browse files
committed
Cleanup tests by using MockSupport classes.
Signed-off-by: Carsten Lohmann <[email protected]>
1 parent 275332b commit 2394c91

File tree

23 files changed

+145
-255
lines changed

23 files changed

+145
-255
lines changed

adapters/amqp-vertx/src/test/java/org/eclipse/hono/adapter/amqp/impl/VertxBasedAmqpProtocolAdapterTest.java

+13-21
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
import org.eclipse.hono.service.monitoring.ConnectionEventProducer;
7070
import org.eclipse.hono.service.resourcelimits.ResourceLimitChecks;
7171
import org.eclipse.hono.service.test.ProtocolAdapterTestSupport;
72+
import org.eclipse.hono.test.TracingMockSupport;
73+
import org.eclipse.hono.test.VertxMockSupport;
7274
import org.eclipse.hono.util.Adapter;
7375
import org.eclipse.hono.util.CommandConstants;
7476
import org.eclipse.hono.util.Constants;
@@ -136,16 +138,9 @@ public void setup() {
136138

137139
metrics = mock(AmqpAdapterMetrics.class);
138140
vertx = mock(Vertx.class);
139-
context = mock(Context.class);
140-
doAnswer(invocation -> {
141-
final Handler<Void> codeToRun = invocation.getArgument(0);
142-
codeToRun.handle(null);
143-
return null;
144-
}).when(context).runOnContext(any());
141+
context = VertxMockSupport.mockContext(vertx);
145142

146-
span = mock(Span.class);
147-
final SpanContext spanContext = mock(SpanContext.class);
148-
when(span.context()).thenReturn(spanContext);
143+
span = TracingMockSupport.mockSpan();
149144

150145
this.properties = givenDefaultConfigurationProperties();
151146
createClientFactories();
@@ -179,7 +174,6 @@ protected AmqpAdapterProperties givenDefaultConfigurationProperties() {
179174
*
180175
* @param ctx The test context to use for running asynchronous tests.
181176
*/
182-
@SuppressWarnings("unchecked")
183177
@Test
184178
public void testStartUsesClientProvidedAmqpServer(final VertxTestContext ctx) {
185179
// GIVEN an adapter with a client provided Amqp Server
@@ -192,8 +186,8 @@ public void testStartUsesClientProvidedAmqpServer(final VertxTestContext ctx) {
192186
startupTracker.future().onComplete(ctx.succeeding(result -> {
193187
ctx.verify(() -> {
194188
// THEN the client provided server is started
195-
verify(server).connectHandler(any(Handler.class));
196-
verify(server).listen(any(Handler.class));
189+
verify(server).connectHandler(VertxMockSupport.anyHandler());
190+
verify(server).listen(VertxMockSupport.anyHandler());
197191
});
198192
ctx.completeNow();
199193
}));
@@ -461,7 +455,6 @@ public void testUploadEventRejectsPresettledMessage(final VertxTestContext ctx)
461455
* <em>EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION</em> event a with TTD -1. An unauthenticated device is used in
462456
* this test setup to simulate the client device.
463457
*/
464-
@SuppressWarnings("unchecked")
465458
@Test
466459
public void testAdapterOpensSenderLinkAndNotifyDownstreamApplication() {
467460
// GIVEN an AMQP adapter configured to use a user-defined server
@@ -471,7 +464,7 @@ public void testAdapterOpensSenderLinkAndNotifyDownstreamApplication() {
471464
// WHEN an unauthenticated device opens a receiver link with a valid source address
472465
final ProtonConnection deviceConnection = mock(ProtonConnection.class);
473466
when(deviceConnection.attachments()).thenReturn(mock(Record.class));
474-
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), any(Handler.class), any(), any()))
467+
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), VertxMockSupport.anyHandler(), any(), any()))
475468
.thenReturn(Future.succeededFuture(mock(CommandConsumer.class)));
476469
final String sourceAddress = String.format("%s/%s/%s", getCommandEndpoint(), TEST_TENANT_ID, TEST_DEVICE);
477470
final ProtonSender sender = getSender(sourceAddress);
@@ -501,7 +494,7 @@ public void testAdapterClosesCommandConsumerWhenDeviceClosesReceiverLink() {
501494
// and a device that wants to receive commands
502495
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
503496
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
504-
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), any(Handler.class), any(), any()))
497+
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), VertxMockSupport.anyHandler(), any(), any()))
505498
.thenReturn(Future.succeededFuture(commandConsumer));
506499
final String sourceAddress = String.format("%s", getCommandEndpoint());
507500
final ProtonSender sender = getSender(sourceAddress);
@@ -596,7 +589,7 @@ private void testAdapterClosesCommandConsumer(
596589
// that wants to receive commands
597590
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
598591
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
599-
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), any(Handler.class), any(), any()))
592+
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), VertxMockSupport.anyHandler(), any(), any()))
600593
.thenReturn(Future.succeededFuture(commandConsumer));
601594
final String sourceAddress = getCommandEndpoint();
602595
final ProtonSender sender = getSender(sourceAddress);
@@ -647,7 +640,7 @@ public void testAdapterSkipsTtdEventOnCmdConnectionCloseIfRemoveConsumerFails(fi
647640
// that wants to receive commands
648641
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
649642
when(commandConsumer.close(any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_PRECON_FAILED)));
650-
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), any(Handler.class), any(), any()))
643+
when(commandConsumerFactory.createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_DEVICE), VertxMockSupport.anyHandler(), any(), any()))
651644
.thenReturn(Future.succeededFuture(commandConsumer));
652645
final String sourceAddress = getCommandEndpoint();
653646
final ProtonSender sender = getSender(sourceAddress);
@@ -1144,7 +1137,7 @@ public void testLinkForSendingCommandsCloseAfterTimeout() {
11441137
final Handler<Long> task = invocation.getArgument(1);
11451138
task.handle(1L);
11461139
return 1L;
1147-
}).when(vertx).setTimer(anyLong(), any(Handler.class));
1140+
}).when(vertx).setTimer(anyLong(), VertxMockSupport.anyHandler());
11481141

11491142
adapter.onCommandReceived(tenantObject, deviceLink, context);
11501143
// THEN the adapter releases the command
@@ -1405,13 +1398,12 @@ private VertxBasedAmqpProtocolAdapter newAdapter(
14051398
*
14061399
* @return The configured server instance.
14071400
*/
1408-
@SuppressWarnings("unchecked")
14091401
private ProtonServer getAmqpServer() {
14101402

14111403
final ProtonServer server = mock(ProtonServer.class);
14121404
when(server.actualPort()).thenReturn(0, Constants.PORT_AMQP);
1413-
when(server.connectHandler(any(Handler.class))).thenReturn(server);
1414-
when(server.listen(any(Handler.class))).then(invocation -> {
1405+
when(server.connectHandler(VertxMockSupport.anyHandler())).thenReturn(server);
1406+
when(server.listen(VertxMockSupport.anyHandler())).then(invocation -> {
14151407
final Handler<AsyncResult<ProtonServer>> handler = invocation.getArgument(0);
14161408
handler.handle(Future.succeededFuture(server));
14171409
return server;

adapters/coap-vertx-base/src/test/java/org/eclipse/hono/adapter/coap/AbstractVertxBasedCoapAdapterTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import org.eclipse.hono.service.metric.MetricsTags.TtdStatus;
6161
import org.eclipse.hono.service.resourcelimits.ResourceLimitChecks;
6262
import org.eclipse.hono.service.test.ProtocolAdapterTestSupport;
63+
import org.eclipse.hono.test.TracingMockSupport;
64+
import org.eclipse.hono.test.VertxMockSupport;
6365
import org.eclipse.hono.util.Adapter;
6466
import org.eclipse.hono.util.CommandConstants;
6567
import org.eclipse.hono.util.Constants;
@@ -112,19 +114,17 @@ public void setup() {
112114
startupHandler = mock(Handler.class);
113115
metrics = mock(CoapAdapterMetrics.class);
114116

115-
span = mock(Span.class);
116-
final SpanContext spanContext = mock(SpanContext.class);
117-
when(span.context()).thenReturn(spanContext);
117+
span = TracingMockSupport.mockSpan();
118118

119119
this.properties = givenDefaultConfigurationProperties();
120120
createClientFactories();
121121
prepareClients();
122122

123123
commandConsumer = mock(CommandConsumer.class);
124124
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
125-
when(commandConsumerFactory.createCommandConsumer(anyString(), anyString(), any(Handler.class), any(), any()))
125+
when(commandConsumerFactory.createCommandConsumer(anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any()))
126126
.thenReturn(Future.succeededFuture(commandConsumer));
127-
when(commandConsumerFactory.createCommandConsumer(anyString(), anyString(), anyString(), any(Handler.class), any(), any()))
127+
when(commandConsumerFactory.createCommandConsumer(anyString(), anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any()))
128128
.thenReturn(Future.succeededFuture(commandConsumer));
129129

130130
resourceLimitChecks = mock(ResourceLimitChecks.class);
@@ -604,7 +604,6 @@ public void testUploadEventFailsForRejectedOutcome() {
604604
* Verifies that the adapter releases an incoming command if the forwarding of the preceding telemetry
605605
* message did not succeed.
606606
*/
607-
@SuppressWarnings("unchecked")
608607
@Test
609608
public void testUploadTelemetryReleasesCommandForFailedDownstreamSender() {
610609

@@ -615,7 +614,7 @@ public void testUploadTelemetryReleasesCommandForFailedDownstreamSender() {
615614

616615
// and a commandConsumerFactory that upon creating a consumer will invoke it with a command
617616
final CommandContext commandContext = givenAOneWayCommandContext("tenant", "device", "doThis", null, null);
618-
when(commandConsumerFactory.createCommandConsumer(eq("tenant"), eq("device"), any(Handler.class), any(), any()))
617+
when(commandConsumerFactory.createCommandConsumer(eq("tenant"), eq("device"), VertxMockSupport.anyHandler(), any(), any()))
619618
.thenAnswer(invocation -> {
620619
final Handler<CommandContext> consumer = invocation.getArgument(2);
621620
consumer.handle(commandContext);

adapters/coap-vertx-base/src/test/java/org/eclipse/hono/adapter/coap/CoapContextTest.java

+7-16
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
package org.eclipse.hono.adapter.coap;
1616

17-
import static org.mockito.ArgumentMatchers.any;
1817
import static org.mockito.ArgumentMatchers.anyLong;
1918
import static org.mockito.ArgumentMatchers.eq;
2019
import static org.mockito.Mockito.mock;
2120
import static org.mockito.Mockito.never;
2221
import static org.mockito.Mockito.verify;
23-
import static org.mockito.Mockito.when;
2422

2523
import org.eclipse.californium.core.server.resources.CoapExchange;
2624
import org.eclipse.hono.auth.Device;
25+
import org.eclipse.hono.test.TracingMockSupport;
26+
import org.eclipse.hono.test.VertxMockSupport;
2727
import org.eclipse.hono.util.Adapter;
2828
import org.eclipse.hono.util.Constants;
2929
import org.eclipse.hono.util.TenantObject;
@@ -33,8 +33,6 @@
3333
import org.junit.jupiter.params.provider.ValueSource;
3434

3535
import io.opentracing.Span;
36-
import io.opentracing.SpanContext;
37-
import io.vertx.core.Handler;
3836
import io.vertx.core.Vertx;
3937

4038

@@ -53,16 +51,12 @@ public class CoapContextTest {
5351
@BeforeEach
5452
void setUp() {
5553
vertx = mock(Vertx.class);
56-
57-
span = mock(Span.class);
58-
final SpanContext spanContext = mock(SpanContext.class);
59-
when(span.context()).thenReturn(spanContext);
54+
span = TracingMockSupport.mockSpan();
6055
}
6156

6257
/**
6358
* Verifies that no ACK timer is started for a timeout value &lt;= 0.
6459
*/
65-
@SuppressWarnings("unchecked")
6660
@ParameterizedTest
6761
@ValueSource(longs = {-1L, 0L})
6862
void testStartAckTimerDoesNotStartTimer(final long timeout) {
@@ -73,14 +67,13 @@ void testStartAckTimerDoesNotStartTimer(final long timeout) {
7367
final Device authenticatedDevice = new Device(tenant.getTenantId(), "device-id");
7468
final CoapContext ctx = CoapContext.fromRequest(exchange, authenticatedDevice, authenticatedDevice, "4711", span);
7569
ctx.startAcceptTimer(vertx, tenant, timeout);
76-
verify(vertx, never()).setTimer(anyLong(), any(Handler.class));
70+
verify(vertx, never()).setTimer(anyLong(), VertxMockSupport.anyHandler());
7771
}
7872

7973
/**
8074
* Verifies that the tenant specific value set for the ACK timeout gets
8175
* precedence over the global adapter configuration.
8276
*/
83-
@SuppressWarnings("unchecked")
8477
@Test
8578
void testStartAckTimerUsesTenantSpecificTimeout() {
8679
final CoapExchange exchange = mock(CoapExchange.class);
@@ -90,13 +83,12 @@ void testStartAckTimerUsesTenantSpecificTimeout() {
9083
final Device authenticatedDevice = new Device(tenant.getTenantId(), "device-id");
9184
final CoapContext ctx = CoapContext.fromRequest(exchange, authenticatedDevice, authenticatedDevice, "4711", span);
9285
ctx.startAcceptTimer(vertx, tenant, 500);
93-
verify(vertx).setTimer(eq(200L), any(Handler.class));
86+
verify(vertx).setTimer(eq(200L), VertxMockSupport.anyHandler());
9487
}
9588

9689
/**
9790
* Verifies that the global ACK timeout is used if no tenant specific value is configured.
9891
*/
99-
@SuppressWarnings("unchecked")
10092
@Test
10193
void testStartAckTimerFallsBackToGlobalTimeout() {
10294
final CoapExchange exchange = mock(CoapExchange.class);
@@ -105,13 +97,12 @@ void testStartAckTimerFallsBackToGlobalTimeout() {
10597
final Device authenticatedDevice = new Device(tenant.getTenantId(), "device-id");
10698
final CoapContext ctx = CoapContext.fromRequest(exchange, authenticatedDevice, authenticatedDevice, "4711", span);
10799
ctx.startAcceptTimer(vertx, tenant, 500);
108-
verify(vertx).setTimer(eq(500L), any(Handler.class));
100+
verify(vertx).setTimer(eq(500L), VertxMockSupport.anyHandler());
109101
}
110102

111103
/**
112104
* Verifies that the global ACK timeout is used if a tenant specific value is configured that is not a number.
113105
*/
114-
@SuppressWarnings("unchecked")
115106
@Test
116107
void testStartAckTimerHandlesNonNumberPropertyValue() {
117108
final CoapExchange exchange = mock(CoapExchange.class);
@@ -121,6 +112,6 @@ void testStartAckTimerHandlesNonNumberPropertyValue() {
121112
final Device authenticatedDevice = new Device(tenant.getTenantId(), "device-id");
122113
final CoapContext ctx = CoapContext.fromRequest(exchange, authenticatedDevice, authenticatedDevice, "4711", span);
123114
ctx.startAcceptTimer(vertx, tenant, 500);
124-
verify(vertx).setTimer(eq(500L), any(Handler.class));
115+
verify(vertx).setTimer(eq(500L), VertxMockSupport.anyHandler());
125116
}
126117
}

0 commit comments

Comments
 (0)