Search in sources :

Example 1 with CommandEndpoint

use of org.eclipse.hono.util.CommandEndpoint in project hono by eclipse.

the class LoraProviderTestBase method testGenerateCommandMessage.

/**
 * Verifies that command messages are formatted correctly.
 */
@Test
public void testGenerateCommandMessage() {
    final CommandEndpoint commandEndpoint = new CommandEndpoint();
    commandEndpoint.setPayloadProperties(Map.of("py-property", "my-property-value"));
    commandEndpoint.setUri("https://my-lns.io/{{deviceId}}/command");
    final LoraCommand command = provider.getCommand(commandEndpoint, TEST_DEVICE_ID, Buffer.buffer("bumlux".getBytes(StandardCharsets.UTF_8)), "2");
    assertThat(command.getUri()).isEqualTo("https://my-lns.io/" + TEST_DEVICE_ID + "/command");
    assertCommandFormat(command.getPayload());
}
Also used : CommandEndpoint(org.eclipse.hono.util.CommandEndpoint) LoraCommand(org.eclipse.hono.adapter.lora.LoraCommand) Test(org.junit.jupiter.api.Test)

Example 2 with CommandEndpoint

use of org.eclipse.hono.util.CommandEndpoint in project hono by eclipse.

the class LoraProtocolAdapterTest method handleCommandForLNS.

/**
 * Verifies that an uplink message triggers a command subscription.
 */
@SuppressWarnings("unchecked")
@Test
public void handleCommandForLNS() {
    givenATelemetrySenderForAnyTenant();
    final LoraProvider providerMock = getLoraProviderMock();
    final HttpServerRequest request = mock(HttpServerRequest.class);
    final HttpContext httpContext = newHttpContext();
    when(httpContext.request()).thenReturn(request);
    final CommandEndpoint commandEndpoint = new CommandEndpoint();
    commandEndpoint.setHeaders(Map.of("my-header", "my-header-value"));
    commandEndpoint.setUri("https://my-server.com/commands/{{deviceId}}/send");
    setGatewayDeviceCommandEndpoint(commandEndpoint);
    final CommandConsumer commandConsumer = mock(CommandConsumer.class);
    when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
    when(commandConsumerFactory.createCommandConsumer(any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
    adapter.handleProviderRoute(httpContext, providerMock);
    final ArgumentCaptor<Handler<CommandContext>> handlerArgumentCaptor = VertxMockSupport.argumentCaptorHandler();
    verify(commandConsumerFactory).createCommandConsumer(eq(TEST_TENANT_ID), eq(TEST_GATEWAY_ID), handlerArgumentCaptor.capture(), isNull(), any());
    final Handler<CommandContext> commandHandler = handlerArgumentCaptor.getValue();
    final Command command = mock(Command.class);
    when(command.getTenant()).thenReturn(TEST_TENANT_ID);
    when(command.getDeviceId()).thenReturn(TEST_DEVICE_ID);
    when(command.getGatewayId()).thenReturn(TEST_GATEWAY_ID);
    when(command.getPayload()).thenReturn(Buffer.buffer("bumlux"));
    when(command.isValid()).thenReturn(true);
    final CommandContext commandContext = mock(CommandContext.class);
    when(commandContext.getCommand()).thenReturn(command);
    when(commandContext.getTracingSpan()).thenReturn(processMessageSpan);
    final JsonObject json = new JsonObject().put("my-payload", "bumlux");
    final LoraCommand loraCommand = new LoraCommand(json, "https://my-server.com/commands/deviceId/send");
    when(providerMock.getCommand(any(), any(), any(), any())).thenReturn(loraCommand);
    when(providerMock.getDefaultHeaders()).thenReturn(Map.of("my-provider-header", "my-provider-header-value"));
    final HttpRequest<Buffer> httpClientRequest = mock(HttpRequest.class, withSettings().defaultAnswer(RETURNS_SELF));
    final HttpResponse<Buffer> httpResponse = mock(HttpResponse.class);
    when(httpResponse.statusCode()).thenReturn(HttpURLConnection.HTTP_NO_CONTENT);
    when(httpClientRequest.sendJson(any(JsonObject.class))).thenReturn(Future.succeededFuture(httpResponse));
    when(webClient.postAbs(anyString())).thenReturn(httpClientRequest);
    commandHandler.handle(commandContext);
    verify(webClient, times(1)).postAbs("https://my-server.com/commands/deviceId/send");
    verify(httpClientRequest, times(1)).putHeader("my-header", "my-header-value");
    verify(httpClientRequest, times(1)).putHeader("my-provider-header", "my-provider-header-value");
    verify(httpClientRequest, times(1)).sendJson(json);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) LoraProvider(org.eclipse.hono.adapter.lora.providers.LoraProvider) CommandContext(org.eclipse.hono.client.command.CommandContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpContext(org.eclipse.hono.service.http.HttpContext) TracingHandler(org.eclipse.hono.service.http.TracingHandler) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) Command(org.eclipse.hono.client.command.Command) CommandEndpoint(org.eclipse.hono.util.CommandEndpoint) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) Test(org.junit.jupiter.api.Test)

Example 3 with CommandEndpoint

use of org.eclipse.hono.util.CommandEndpoint in project hono by eclipse.

the class LoraProtocolAdapterTest method handleProviderRouteSuccessfullyForUplinkMessage.

/**
 * Verifies that an uplink message is routed to a provider correctly.
 */
@Test
public void handleProviderRouteSuccessfullyForUplinkMessage() {
    givenATelemetrySenderForAnyTenant();
    final LoraProvider providerMock = getLoraProviderMock();
    final HttpServerRequest request = mock(HttpServerRequest.class);
    final HttpContext httpContext = newHttpContext();
    when(httpContext.request()).thenReturn(request);
    setGatewayDeviceCommandEndpoint(new CommandEndpoint());
    final CommandConsumer commandConsumer = mock(CommandConsumer.class);
    when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
    when(commandConsumerFactory.createCommandConsumer(any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
    adapter.handleProviderRoute(httpContext, providerMock);
    verify(httpContext.getRoutingContext()).put(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER, TEST_PROVIDER);
    @SuppressWarnings("unchecked") final ArgumentCaptor<Map<String, Object>> props = ArgumentCaptor.forClass(Map.class);
    verify(telemetrySender).sendTelemetry(argThat(tenant -> TEST_TENANT_ID.equals(tenant.getTenantId())), argThat(assertion -> TEST_DEVICE_ID.equals(assertion.getDeviceId())), eq(QoS.AT_MOST_ONCE), eq(LoraConstants.CONTENT_TYPE_LORA_BASE + TEST_PROVIDER), eq(Buffer.buffer(TEST_PAYLOAD)), props.capture(), any());
    assertThat(props.getValue()).containsEntry(LoraConstants.APP_PROPERTY_FUNCTION_PORT, TEST_FUNCTION_PORT);
    final String metaData = (String) props.getValue().get(LoraConstants.APP_PROPERTY_META_DATA);
    assertThat(metaData).isNotNull();
    final JsonObject metaDataJson = new JsonObject(metaData);
    assertThat(metaDataJson.getInteger(LoraConstants.APP_PROPERTY_FUNCTION_PORT)).isEqualTo(TEST_FUNCTION_PORT);
    verify(httpContext.getRoutingContext().response()).setStatusCode(HttpResponseStatus.ACCEPTED.code());
    verify(processMessageSpan).finish();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) LoraProviderMalformedPayloadException(org.eclipse.hono.adapter.lora.providers.LoraProviderMalformedPayloadException) RoutingContext(io.vertx.ext.web.RoutingContext) Context(io.vertx.core.Context) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) CommandContext(org.eclipse.hono.client.command.CommandContext) Set(java.util.Set) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) CommandEndpoint(org.eclipse.hono.util.CommandEndpoint) HttpAdapterMetrics(org.eclipse.hono.adapter.http.HttpAdapterMetrics) QoS(org.eclipse.hono.util.QoS) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpContext(org.eclipse.hono.service.http.HttpContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpResponse(io.vertx.ext.web.client.HttpResponse) LoraProvider(org.eclipse.hono.adapter.lora.providers.LoraProvider) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) SpanBuilder(io.opentracing.Tracer.SpanBuilder) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) TracingHandler(org.eclipse.hono.service.http.TracingHandler) ProtocolAdapterTestSupport(org.eclipse.hono.adapter.test.ProtocolAdapterTestSupport) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Sample(io.micrometer.core.instrument.Timer.Sample) Mockito.verify(org.mockito.Mockito.verify) SpanContext(io.opentracing.SpanContext) HttpRequest(io.vertx.ext.web.client.HttpRequest) HttpProtocolAdapterProperties(org.eclipse.hono.adapter.http.HttpProtocolAdapterProperties) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LoraProvider(org.eclipse.hono.adapter.lora.providers.LoraProvider) HttpServerRequest(io.vertx.core.http.HttpServerRequest) CommandEndpoint(org.eclipse.hono.util.CommandEndpoint) HttpContext(org.eclipse.hono.service.http.HttpContext) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) JsonObject(io.vertx.core.json.JsonObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 4 with CommandEndpoint

use of org.eclipse.hono.util.CommandEndpoint in project hono by eclipse.

the class LoraProtocolAdapterTest method handleCommandSubscriptionSuccessfullyForUplinkMessage.

/**
 * Verifies that an uplink message triggers a command subscription.
 */
@Test
public void handleCommandSubscriptionSuccessfullyForUplinkMessage() {
    givenATelemetrySenderForAnyTenant();
    final LoraProvider providerMock = getLoraProviderMock();
    final HttpServerRequest request = mock(HttpServerRequest.class);
    final HttpContext httpContext = newHttpContext();
    when(httpContext.request()).thenReturn(request);
    setGatewayDeviceCommandEndpoint(new CommandEndpoint());
    final CommandConsumer commandConsumer = mock(CommandConsumer.class);
    when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
    when(commandConsumerFactory.createCommandConsumer(any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
    adapter.handleProviderRoute(httpContext, providerMock);
    verify(commandConsumerFactory).createCommandConsumer(eq("myTenant"), eq("myLoraGateway"), VertxMockSupport.anyHandler(), isNull(), any());
}
Also used : LoraProvider(org.eclipse.hono.adapter.lora.providers.LoraProvider) HttpServerRequest(io.vertx.core.http.HttpServerRequest) CommandEndpoint(org.eclipse.hono.util.CommandEndpoint) HttpContext(org.eclipse.hono.service.http.HttpContext) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) Test(org.junit.jupiter.api.Test)

Aggregations

CommandEndpoint (org.eclipse.hono.util.CommandEndpoint)4 Test (org.junit.jupiter.api.Test)4 HttpServerRequest (io.vertx.core.http.HttpServerRequest)3 LoraProvider (org.eclipse.hono.adapter.lora.providers.LoraProvider)3 CommandConsumer (org.eclipse.hono.client.command.CommandConsumer)3 HttpContext (org.eclipse.hono.service.http.HttpContext)3 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 JsonObject (io.vertx.core.json.JsonObject)2 Command (org.eclipse.hono.client.command.Command)2 CommandContext (org.eclipse.hono.client.command.CommandContext)2 TracingHandler (org.eclipse.hono.service.http.TracingHandler)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Sample (io.micrometer.core.instrument.Timer.Sample)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 Context (io.vertx.core.Context)1