use of org.eclipse.hono.client.command.CommandConsumer in project hono by eclipse.
the class VertxBasedHttpProtocolAdapterTest method configureServiceClients.
/**
* Sets up the fixture.
*
* @param testInfo The test meta data.
*/
@BeforeEach
public void configureServiceClients(final TestInfo testInfo) {
LOG.info("running test case [{}]", testInfo.getDisplayName());
createClients();
prepareClients();
setServiceClients(adapter);
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
when(commandConsumerFactory.createCommandConsumer(anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
doAnswer(invocation -> {
final Handler<AsyncResult<User>> resultHandler = invocation.getArgument(2);
resultHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials")));
return null;
}).when(usernamePasswordAuthProvider).authenticate(any(UsernamePasswordCredentials.class), any(), VertxMockSupport.anyHandler());
doAnswer(invocation -> {
final JsonObject authInfo = invocation.getArgument(0);
final String username = JsonHelper.getValue(authInfo, CredentialsConstants.FIELD_USERNAME, String.class, null);
final String password = JsonHelper.getValue(authInfo, CredentialsConstants.FIELD_PASSWORD, String.class, null);
if (username == null || password == null) {
return null;
} else {
return UsernamePasswordCredentials.create(username, password);
}
}).when(usernamePasswordAuthProvider).getCredentials(any(JsonObject.class));
}
use of org.eclipse.hono.client.command.CommandConsumer in project hono by eclipse.
the class VertxBasedHttpProtocolAdapterTest method testPostTelemetryWithTtdSucceedsWithCommandInResponse.
/**
* Verifies that the adapter includes a command for the device in the response to
* a POST request which contains a time-til-disconnect.
*
* @param ctx The vert.x test context.
*/
@Test
public void testPostTelemetryWithTtdSucceedsWithCommandInResponse(final VertxTestContext ctx) {
// GIVEN an device for which a command is pending
givenATelemetrySenderForAnyTenant();
mockSuccessfulAuthentication("DEFAULT_TENANT", "device_1");
final CommandContext commandContext = givenARequestResponseCommandContext("DEFAULT_TENANT", "device_1", "doThis", "reply-to-id", null, null, MessagingType.amqp);
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
when(commandConsumerFactory.createCommandConsumer(eq("DEFAULT_TENANT"), eq("device_1"), VertxMockSupport.anyHandler(), any(), any())).thenAnswer(invocation -> {
final Handler<CommandContext> consumer = invocation.getArgument(2);
consumer.handle(commandContext);
return Future.succeededFuture(commandConsumer);
});
// WHEN the device posts a telemetry message including a TTD
httpClient.post("/telemetry").addQueryParam("hono-ttd", "3").putHeader(HttpHeaders.CONTENT_TYPE.toString(), HttpUtils.CONTENT_TYPE_JSON).basicAuthentication("testuser@DEFAULT_TENANT", "password123").putHeader(HttpHeaders.ORIGIN.toString(), ORIGIN_HEADER_VALUE).expect(ResponsePredicate.SC_OK).expect(this::assertCorsHeaders).expect(response -> {
if (!"doThis".equals(response.getHeader(Constants.HEADER_COMMAND))) {
return ResponsePredicateResult.failure("response does not contain expected hono-command header");
}
if (response.getHeader(Constants.HEADER_COMMAND_REQUEST_ID) == null) {
return ResponsePredicateResult.failure("response does not contain hono-cmd-req-id header");
}
return ResponsePredicateResult.success();
}).sendJsonObject(new JsonObject(), ctx.succeeding(r -> {
ctx.verify(() -> {
verify(commandConsumerFactory).createCommandConsumer(eq("DEFAULT_TENANT"), eq("device_1"), VertxMockSupport.anyHandler(), any(), any());
// and the command consumer has been closed again
verify(commandConsumer).close(any());
verify(commandContext).accept();
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.client.command.CommandConsumer in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testOnSubscribeIncludesStatusCodeForEachFilter.
/**
* Verifies that the adapter includes a status code for each topic filter in its SUBACK packet.
*/
@SuppressWarnings("unchecked")
@Test
public void testOnSubscribeIncludesStatusCodeForEachFilter() {
// GIVEN a device connected to an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(true);
// WHEN a device sends a SUBSCRIBE packet for several unsupported filters
final List<MqttTopicSubscription> subscriptions = new ArrayList<>();
subscriptions.add(newMockTopicSubscription("unsupported/#", MqttQoS.AT_LEAST_ONCE));
subscriptions.add(newMockTopicSubscription("bumlux/+/+/#", MqttQoS.AT_MOST_ONCE));
subscriptions.add(newMockTopicSubscription("bumlux/+/+/#", MqttQoS.AT_MOST_ONCE));
// and for subscribing to commands
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
when(commandConsumerFactory.createCommandConsumer(eq("tenant-1"), eq("device-A"), VertxMockSupport.anyHandler(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
subscriptions.add(newMockTopicSubscription(getCommandSubscriptionTopic("tenant-1", "device-A"), MqttQoS.AT_MOST_ONCE));
subscriptions.add(newMockTopicSubscription(getCommandSubscriptionTopic("tenant-1", "device-B"), MqttQoS.EXACTLY_ONCE));
final MqttSubscribeMessage msg = mock(MqttSubscribeMessage.class);
when(msg.messageId()).thenReturn(15);
when(msg.topicSubscriptions()).thenReturn(subscriptions);
final var mqttDeviceEndpoint = adapter.createMqttDeviceEndpoint(endpoint, null, OptionalInt.empty());
mqttDeviceEndpoint.onSubscribe(msg);
// THEN the adapter sends a SUBACK packet to the device
// which contains a failure status code for each unsupported filter
final ArgumentCaptor<List<MqttQoS>> codeCaptor = ArgumentCaptor.forClass(List.class);
verify(endpoint).subscribeAcknowledge(eq(15), codeCaptor.capture());
assertThat(codeCaptor.getValue()).hasSize(5);
assertThat(codeCaptor.getValue().get(0)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(1)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(2)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(3)).isEqualTo(MqttQoS.AT_MOST_ONCE);
// and sends an empty notification downstream with TTD -1
assertEmptyNotificationHasBeenSentDownstream("tenant-1", "device-A", -1);
}
Aggregations