use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantManagementIT method buildTenantPayload.
/**
* Creates a tenant payload.
* <p>
* The tenant payload contains configurations for the http, mqtt and a custom adapter.
*
* @return The tenant object.
*/
private static Tenant buildTenantPayload() {
final Tenant tenant = new Tenant();
tenant.putExtension("plan", "unlimited");
// explicitly (add the messaging-type extension (implicitly added by DeviceRegistryHttpClient)
// here so that it is also part of the expected output and hence verified
tenant.putExtension(TenantConstants.FIELD_EXT_MESSAGING_TYPE, IntegrationTestSupport.getConfiguredMessagingType().name());
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(true).setDeviceAuthenticationRequired(true));
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(true).setDeviceAuthenticationRequired(true));
tenant.addAdapterConfig(new Adapter("custom").setEnabled(false).setDeviceAuthenticationRequired(false).putExtension("maxInstances", 4));
return tenant;
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantManagementIT method testAddTenantFailsForMalformedAdapterConfiguration.
/**
* Verifies that the service returns a 400 status code for a request to add a tenant containing
* multiple adapter configurations for the same adapter type.
*
* @param context The Vert.x test context.
*/
@Test
public void testAddTenantFailsForMalformedAdapterConfiguration(final VertxTestContext context) {
final Adapter httpAdapter = new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP);
final JsonObject requestBody = JsonObject.mapFrom(buildTenantPayload());
requestBody.getJsonArray(RegistryManagementConstants.FIELD_ADAPTERS).add(JsonObject.mapFrom(httpAdapter));
final String tenantId = getHelper().getRandomTenantId();
getHelper().registry.addTenant(tenantId, requestBody, "application/json", HttpURLConnection.HTTP_BAD_REQUEST).onComplete(context.succeeding(response -> {
context.verify(() -> IntegrationTestSupport.assertErrorPayload(response));
context.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadMessageSupportsShortAndLongEndpointNames.
/**
* Verifies that the adapter will accept uploading messages to standard as well as shortened topic names.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadMessageSupportsShortAndLongEndpointNames(final VertxTestContext ctx) {
// GIVEN an adapter with downstream telemetry & event consumers
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
givenAnEventSenderForAnyTenant();
// WHEN a device publishes events and telemetry messages
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
final Buffer payload = Buffer.buffer("some payload");
final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
when(messageFromDevice.messageId()).thenReturn(5555555);
when(messageFromDevice.payload()).thenReturn(payload);
ResourceIdentifier resourceId = ResourceIdentifier.from("telemetry", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("event", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("t", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("e", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
resourceId = ResourceIdentifier.from("unknown", "my-tenant", "4712");
when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(t).isInstanceOf(ClientErrorException.class));
}));
ctx.completeNow();
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageIncludesRetainAnnotation.
/**
* Verifies that the adapter includes a message annotation in a downstream message if the device publishes a message
* with its <em>retain</em> flag set.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryMessageIncludesRetainAnnotation(final VertxTestContext ctx) {
// GIVEN an adapter with a downstream telemetry consumer
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
// WHEN a device publishes a message with its retain flag set
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
final Buffer payload = Buffer.buffer("hello");
final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
when(messageFromDevice.messageId()).thenReturn(5555555);
when(messageFromDevice.topicName()).thenReturn("t/my-tenant/4712");
when(messageFromDevice.isRetain()).thenReturn(Boolean.TRUE);
when(messageFromDevice.payload()).thenReturn(payload);
final MqttContext context = newMqttContext(messageFromDevice, endpoint, span);
adapter.uploadTelemetryMessage(context, "my-tenant", "4712", payload).onComplete(ctx.succeeding(ok -> {
ctx.verify(() -> {
// THEN the device has received a PUBACK
verify(endpoint).publishAcknowledge(5555555);
// and the message has been sent downstream
// including the "retain" annotation
verify(telemetrySender).sendTelemetry(argThat(tenant -> tenant.getTenantId().equals("my-tenant")), argThat(assertion -> assertion.getDeviceId().equals("4712")), eq(QoS.AT_LEAST_ONCE), any(), any(), argThat(props -> props.get(MessageHelper.ANNOTATION_X_OPT_RETAIN).equals(Boolean.TRUE)), any());
verify(metrics).reportTelemetry(eq(MetricsTags.EndpointType.TELEMETRY), eq("my-tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter 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());
}
Aggregations