use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class HttpTestBase method testUploadMessagesWithTtdThatReplyWithCommand.
private void testUploadMessagesWithTtdThatReplyWithCommand(final HttpCommandEndpointConfiguration endpointConfig, final Tenant tenant, final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_TIME_TILL_DISCONNECT, "5");
final MultiMap cmdResponseRequestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_COMMAND_RESPONSE_STATUS, "200");
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
final String commandTargetDeviceId = endpointConfig.isSubscribeAsGateway() ? helper.setupGatewayDeviceBlocking(tenantId, deviceId, 5) : deviceId;
final String subscribingDeviceId = endpointConfig.isSubscribeAsGatewayForSingleDevice() ? commandTargetDeviceId : deviceId;
testUploadMessages(ctx, tenantId, msg -> {
return msg.getTimeUntilDisconnectNotification().map(notification -> {
logger.trace("received piggy backed message [ttd: {}]: {}", notification.getTtd(), msg);
ctx.verify(() -> {
assertThat(notification.getTenantId()).isEqualTo(tenantId);
assertThat(notification.getDeviceId()).isEqualTo(subscribingDeviceId);
});
// now ready to send a command
final JsonObject inputData = new JsonObject().put(COMMAND_JSON_KEY, (int) (Math.random() * 100));
return helper.sendCommand(tenantId, commandTargetDeviceId, COMMAND_TO_SEND, "application/json", inputData.toBuffer(), notification.getMillisecondsUntilExpiry()).map(response -> {
ctx.verify(() -> {
assertThat(response.getContentType()).isEqualTo("text/plain");
assertThat(response.getDeviceId()).isEqualTo(commandTargetDeviceId);
assertThat(response.getTenantId()).isEqualTo(tenantId);
assertThat(response.getCreationTime()).isNotNull();
});
return (Void) null;
});
}).orElseGet(Future::succeededFuture);
}, count -> {
final Buffer buffer = Buffer.buffer("hello " + count);
return sendHttpRequestForGatewayOrDevice(buffer, requestHeaders, endpointConfig, commandTargetDeviceId, true).map(httpResponse -> {
final String requestId = httpResponse.getHeader(Constants.HEADER_COMMAND_REQUEST_ID);
ctx.verify(() -> {
// assert that the response contains a command
assertWithMessage("response no. %s '%s' header", count, Constants.HEADER_COMMAND).that(httpResponse.getHeader(Constants.HEADER_COMMAND)).isNotNull();
assertThat(httpResponse.getHeader(Constants.HEADER_COMMAND)).isEqualTo(COMMAND_TO_SEND);
assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_TYPE.toString())).isEqualTo("application/json");
assertThat(requestId).isNotNull();
assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_LENGTH.toString())).isNotEqualTo("0");
});
return requestId;
}).compose(receivedCommandRequestId -> {
// send a response to the command now
final String responseUri = endpointConfig.getCommandResponseUri(tenantId, commandTargetDeviceId, receivedCommandRequestId);
logger.debug("sending response to command [uri: {}]", responseUri);
final Buffer body = Buffer.buffer("ok");
final Future<HttpResponse<Buffer>> result;
if (endpointConfig.isSubscribeAsGateway()) {
// GW uses PUT when acting on behalf of a device
result = httpClient.update(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
} else {
result = httpClient.create(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
}
return result.recover(thr -> {
// wrap exception, making clear it occurred when sending the command response, not the preceding telemetry/event message
final String msg = "Error sending command response: " + thr.getMessage();
return Future.failedFuture(new RuntimeException(msg, thr));
});
});
});
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class HttpTestBase method testAutoProvisioningViaGateway.
/**
* Verifies that an edge device is auto-provisioned if it connects via a gateway equipped with the corresponding
* authority.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testAutoProvisioningViaGateway(final VertxTestContext ctx) throws InterruptedException {
final Tenant tenant = new Tenant();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gateway = new Device().setAuthorities(Collections.singleton(RegistryManagementConstants.AUTHORITY_AUTO_PROVISIONING_ENABLED));
final String edgeDeviceId = helper.getRandomDeviceId(tenantId);
final Promise<Void> provisioningNotificationReceived = Promise.promise();
helper.createAutoProvisioningMessageConsumers(ctx, provisioningNotificationReceived, tenantId, edgeDeviceId).compose(ok -> helper.registry.addDeviceForTenant(tenantId, tenant, gatewayId, gateway, PWD)).compose(ok -> {
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, getBasicAuth(tenantId, gatewayId, PWD)).add(HttpHeaders.ORIGIN, ORIGIN_URI);
final String uri = String.format("%s/%s/%s", getEndpointUri(), tenantId, edgeDeviceId);
return httpClient.update(uri, Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
}).compose(ok -> provisioningNotificationReceived.future()).compose(ok -> helper.registry.getRegistrationInfo(tenantId, edgeDeviceId)).onComplete(ctx.succeeding(registrationResult -> {
ctx.verify(() -> {
final var info = registrationResult.bodyAsJsonObject();
IntegrationTestSupport.assertDeviceStatusProperties(info.getJsonObject(RegistryManagementConstants.FIELD_STATUS), true);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class HttpTestBase method testUploadMessageFailsForDisabledGateway.
/**
* Verifies that the HTTP adapter rejects messages from a disabled gateway for an enabled device with a 403.
*
* @param ctx The test context
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadMessageFailsForDisabledGateway(final VertxTestContext ctx) {
// GIVEN a device that is connected via a disabled gateway
final Tenant tenant = new Tenant();
final Device gateway = new Device().setEnabled(Boolean.FALSE);
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device device = new Device().setVia(Collections.singletonList(gatewayId));
helper.registry.addDeviceForTenant(tenantId, tenant, gatewayId, gateway, PWD).compose(ok -> helper.registry.registerDevice(tenantId, deviceId, device)).compose(ok -> {
// WHEN the gateway tries to upload a message for the device
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, getBasicAuth(tenantId, gatewayId, PWD));
return httpClient.update(String.format("%s/%s/%s", getEndpointUri(), tenantId, deviceId), Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_FORBIDDEN));
}).onComplete(ctx.succeedingThenComplete());
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class HttpTestBase method testUploadMessagesUsingClientCertificate.
/**
* Verifies that a number of messages uploaded to Hono's HTTP adapter using client certificate based authentication
* can be successfully consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessagesUsingClientCertificate(final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.ORIGIN, ORIGIN_URI);
helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
final var tenant = Tenants.createTenantForTrustAnchor(cert);
return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
testUploadMessages(ctx, tenantId, count -> {
return httpClientWithClientCert.create(getEndpointUri(), Buffer.buffer("hello " + count), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED)).compose(this::verifyAccessControlExposedHeaders);
});
}
use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.
the class HttpTestBase method testUploadMessageFailsForDisabledDevice.
/**
* Verifies that the HTTP adapter rejects messages from a disabled device with a 404.
*
* @param ctx The test context
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadMessageFailsForDisabledDevice(final VertxTestContext ctx) {
// GIVEN a disabled device
final Tenant tenant = new Tenant();
final Device device = new Device().setEnabled(Boolean.FALSE);
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, device, PWD).compose(ok -> {
// WHEN the device tries to upload a message
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization);
return httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_NOT_FOUND));
}).onComplete(ctx.succeedingThenComplete());
}
Aggregations