use of org.junit.jupiter.params.provider.MethodSource in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithoutLastKnownGatewayIsGivingDevicePrecedence.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds with a result containing just
* the mapping of *the given device* to its command handling adapter instance, even though an adapter instance is
* also registered for the other gateway given in the viaGateway.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesWithoutLastKnownGatewayIsGivingDevicePrecedence(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
final String deviceId = "testDevice";
final String adapterInstance = "adapterInstance";
final String otherAdapterInstance = "otherAdapterInstance";
final String gatewayId = "gw-1";
final Set<String> viaGateways = new HashSet<>(Set.of(gatewayId));
viaGateways.addAll(extraUnusedViaGateways);
// GIVEN testDevice has no last known gateway registered
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture());
// and testDevice's and gw-1's command handling adapter instances are set to
// adapterInstance and otherAdapterInstance respectively
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), otherAdapterInstance)));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertNotNull(result);
assertGetInstancesResultMapping(result, deviceId, adapterInstance);
// be sure that only the mapping for the device is returned, not the mappings for the gateway
assertGetInstancesResultSize(result, 1);
});
ctx.completeNow();
}));
}
use of org.junit.jupiter.params.provider.MethodSource in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesForMultipleSubscribedVias.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds with a result containing
* the mappings of gateways, even though there is no last known gateway set for the device.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesForMultipleSubscribedVias(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
final String deviceId = "testDevice";
final String adapterInstance = "adapterInstance";
final String otherAdapterInstance = "otherAdapterInstance";
final String gatewayId = "gw-1";
final String otherGatewayId = "gw-2";
final Set<String> viaGateways = new HashSet<>(Set.of(gatewayId, otherGatewayId));
viaGateways.addAll(extraUnusedViaGateways);
// GIVEN testDevice has no last known gateway registered
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture());
// and gw-1's and gw-2's command handling adapter instance are set to
// adapterInstance and otherAdapterInstance respectively
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, otherGatewayId), otherAdapterInstance)));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertNotNull(result);
assertGetInstancesResultMapping(result, gatewayId, adapterInstance);
assertGetInstancesResultMapping(result, otherGatewayId, otherAdapterInstance);
assertGetInstancesResultSize(result, 2);
});
ctx.completeNow();
}));
}
use of org.junit.jupiter.params.provider.MethodSource in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesForSingleResultAndLastKnownGatewayNotInVia.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation fails if an adapter instance has
* been registered for the last known gateway associated with the given device, but that gateway isn't in the
* given viaGateways set.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesForSingleResultAndLastKnownGatewayNotInVia(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
final String deviceId = "testDevice";
final String adapterInstance = "adapterInstance";
final String gatewayId = "gw-1";
final Set<String> viaGateways = new HashSet<>(Set.of("otherGatewayId"));
viaGateways.addAll(extraUnusedViaGateways);
// GIVEN testDevice's last known gateway is set to gw-1
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture(gatewayId));
// and a command handling adapter instance is set for gw-1 only
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance)));
// but not for the gateways in the via list
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of()));
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), adapterInstance)));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
assertThat(t).isInstanceOf(ClientErrorException.class);
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_NOT_FOUND);
});
ctx.completeNow();
}));
}
use of org.junit.jupiter.params.provider.MethodSource in project hono by eclipse.
the class CacheBasedDeviceConnectionInfoTest method testGetCommandHandlingAdapterInstancesWithLastKnownGatewayIsGivingDevicePrecedence.
/**
* Verifies that the <em>getCommandHandlingAdapterInstances</em> operation succeeds with a result containing just
* the mapping of *the given device* to its command handling adapter instance, even though an adapter instance is
* also registered for the last known gateway associated with the given device.
*
* @param extraUnusedViaGateways Test values.
* @param ctx The vert.x context.
*/
@ParameterizedTest(name = PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("extraUnusedViaGateways")
public void testGetCommandHandlingAdapterInstancesWithLastKnownGatewayIsGivingDevicePrecedence(final Set<String> extraUnusedViaGateways, final VertxTestContext ctx) {
final String deviceId = "testDevice";
final String adapterInstance = "adapterInstance";
final String otherAdapterInstance = "otherAdapterInstance";
final String gatewayId = "gw-1";
final Set<String> viaGateways = new HashSet<>(Set.of(gatewayId));
viaGateways.addAll(extraUnusedViaGateways);
// GIVEN testDevice's last known gateway is set to gw-1
when(cache.get(CacheBasedDeviceConnectionInfo.getGatewayEntryKey(Constants.DEFAULT_TENANT, deviceId))).thenReturn(Future.succeededFuture(gatewayId));
// and testDevice's and gw-1's command handling adapter instances are set to
// adapterInstance and otherAdapterInstance respectively
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, gatewayId))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), otherAdapterInstance)));
when(cache.getAll(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKeys(Constants.DEFAULT_TENANT, deviceId, viaGateways))).thenReturn(Future.succeededFuture(Map.of(CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, deviceId), adapterInstance, CacheBasedDeviceConnectionInfo.getAdapterInstanceEntryKey(Constants.DEFAULT_TENANT, gatewayId), otherAdapterInstance)));
info.getCommandHandlingAdapterInstances(Constants.DEFAULT_TENANT, deviceId, viaGateways, span).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
assertThat(result).isNotNull();
assertGetInstancesResultMapping(result, deviceId, adapterInstance);
// be sure that only the mapping for the device is returned, not the mappings for the gateway
assertGetInstancesResultSize(result, 1);
});
ctx.completeNow();
}));
}
use of org.junit.jupiter.params.provider.MethodSource in project hono by eclipse.
the class CommandAndControlAmqpIT method testSendCommandFailsForCommandRejectedByDevice.
/**
* Verifies that the adapter forwards the <em>rejected</em> disposition, received from a device, back to the
* application.
* <p>
* If Kafka is used, this means a corresponding error command response is published.
*
* @param endpointConfig The endpoints to use for sending/receiving commands.
* @param ctx The vert.x test context.
* @throws InterruptedException if not all commands and responses are exchanged in time.
*/
@ParameterizedTest(name = IntegrationTestSupport.PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("allCombinations")
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testSendCommandFailsForCommandRejectedByDevice(final AmqpCommandEndpointConfiguration endpointConfig, final VertxTestContext ctx) throws InterruptedException {
final String commandTargetDeviceId = endpointConfig.isSubscribeAsGateway() ? helper.setupGatewayDeviceBlocking(tenantId, deviceId, 5) : deviceId;
final int totalNoOfCommandsToSend = 3;
connectAndSubscribe(ctx, commandTargetDeviceId, endpointConfig, (cmdReceiver, cmdResponseSender) -> createRejectingCommandConsumer(ctx, cmdReceiver), totalNoOfCommandsToSend);
if (ctx.failed()) {
return;
}
final String replyId = "reply-id";
final CountDownLatch commandsFailed = new CountDownLatch(totalNoOfCommandsToSend);
final AtomicInteger commandsSent = new AtomicInteger(0);
final AtomicLong lastReceivedTimestamp = new AtomicLong();
final long start = System.currentTimeMillis();
final long commandTimeout = IntegrationTestSupport.getSendCommandTimeout();
final Handler<Void> failureNotificationReceivedHandler = v -> {
lastReceivedTimestamp.set(System.currentTimeMillis());
commandsFailed.countDown();
};
final VertxTestContext setup = new VertxTestContext();
final Future<MessageConsumer> kafkaAsyncErrorResponseConsumer = IntegrationTestSupport.isUsingKafkaMessaging() ? helper.createDeliveryFailureCommandResponseConsumer(ctx, tenantId, HttpURLConnection.HTTP_BAD_REQUEST, response -> {
ctx.verify(() -> {
DownstreamMessageAssertions.assertMessageContainsTimeToLive(response, TTL_COMMAND_RESPONSE);
});
failureNotificationReceivedHandler.handle(null);
}, REJECTED_COMMAND_ERROR_MESSAGE::equals) : Future.succeededFuture(null);
kafkaAsyncErrorResponseConsumer.onComplete(setup.succeedingThenComplete());
assertWithMessage("setup of command response consumer finished within %s seconds", IntegrationTestSupport.getTestSetupTimeout()).that(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
while (commandsSent.get() < totalNoOfCommandsToSend) {
final CountDownLatch commandSent = new CountDownLatch(1);
context.runOnContext(go -> {
final String correlationId = String.valueOf(commandsSent.getAndIncrement());
final Buffer msg = Buffer.buffer("value: " + commandsSent.get());
helper.applicationClient.sendAsyncCommand(tenantId, commandTargetDeviceId, "setValue", "text/plain", msg, correlationId, replyId, null).onComplete(sendAttempt -> {
if (IntegrationTestSupport.isUsingAmqpMessaging()) {
if (sendAttempt.succeeded()) {
log.info("sending command {} via AMQP succeeded unexpectedly", commandsSent.get());
} else {
if (sendAttempt.cause() instanceof ClientErrorException && ((ClientErrorException) sendAttempt.cause()).getErrorCode() == HttpURLConnection.HTTP_BAD_REQUEST && REJECTED_COMMAND_ERROR_MESSAGE.equals(sendAttempt.cause().getMessage())) {
log.debug("sending command {} failed as expected: {}", commandsSent.get(), sendAttempt.cause().toString());
failureNotificationReceivedHandler.handle(null);
} else {
log.info("sending command {} failed with an unexpected error", commandsSent.get(), sendAttempt.cause());
}
}
} else if (sendAttempt.failed()) {
log.debug("sending command {} via Kafka failed unexpectedly", commandsSent.get(), sendAttempt.cause());
}
commandSent.countDown();
});
});
commandSent.await();
}
final long timeToWait = 300 + (totalNoOfCommandsToSend * commandTimeout);
if (!commandsFailed.await(timeToWait, TimeUnit.MILLISECONDS)) {
log.info("Timeout of {} milliseconds reached, stop waiting for commands", timeToWait);
}
final long commandsCompleted = totalNoOfCommandsToSend - commandsFailed.getCount();
log.info("commands sent: {}, commands failed: {} after {} milliseconds", commandsSent.get(), commandsCompleted, lastReceivedTimestamp.get() - start);
Optional.ofNullable(kafkaAsyncErrorResponseConsumer.result()).map(MessageConsumer::close).orElseGet(Future::succeededFuture).onComplete(ar -> {
if (commandsCompleted == commandsSent.get()) {
ctx.completeNow();
} else {
ctx.failNow(new IllegalStateException("did not complete all commands sent"));
}
});
}
Aggregations