use of org.eclipse.hono.service.http.HttpContext 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);
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class LoraProtocolAdapterTest method handleProviderRouteSuccessfullyForOptionsRequest.
/**
* Verifies that an options request is routed to a provider correctly.
*/
@Test
public void handleProviderRouteSuccessfullyForOptionsRequest() {
final HttpContext httpContext = newHttpContext();
adapter.handleOptionsRoute(httpContext.getRoutingContext());
verify(httpContext.getRoutingContext().response()).setStatusCode(HttpResponseStatus.OK.code());
}
use of org.eclipse.hono.service.http.HttpContext in project hono by eclipse.
the class HonoBasicAuthHandler method authenticate.
@Override
public void authenticate(final RoutingContext context, final Handler<AsyncResult<User>> handler) {
parseAuthorization(context, parseAuthorization -> {
if (parseAuthorization.failed()) {
handler.handle(Future.failedFuture(parseAuthorization.cause()));
return;
}
final String suser;
final String spass;
try {
// decode the payload
final String decoded = new String(Base64.getDecoder().decode(parseAuthorization.result()));
final int colonIdx = decoded.indexOf(":");
if (colonIdx != -1) {
suser = decoded.substring(0, colonIdx);
spass = decoded.substring(colonIdx + 1);
} else {
suser = decoded;
spass = null;
}
} catch (RuntimeException e) {
handler.handle(Future.failedFuture(new HttpException(400, e)));
return;
}
final var credentials = new JsonObject().put("username", suser).put("password", spass);
final ExecutionContextAuthHandler<HttpContext> authHandler = new ExecutionContextAuthHandler<>((DeviceCredentialsAuthProvider<?>) authProvider, preCredentialsValidationHandler) {
@Override
public Future<JsonObject> parseCredentials(final HttpContext context) {
return Future.succeededFuture(credentials);
}
};
authHandler.authenticateDevice(HttpContext.from(context)).map(deviceUser -> (User) deviceUser).onComplete(handler);
});
}
use of org.eclipse.hono.service.http.HttpContext 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();
}
use of org.eclipse.hono.service.http.HttpContext 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());
}
Aggregations