Search in sources :

Example 1 with TenantResult

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

the class BaseTenantService method processAddRequest.

private Future<EventBusMessage> processAddRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = getRequestPayload(request.getJsonPayload());
    if (tenantId == null) {
        log.debug("request does not contain mandatory property [{}]", MessageHelper.APP_PROPERTY_TENANT_ID);
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (isValidRequestPayload(payload)) {
        log.debug("creating tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> addResult = Future.future();
        addNotPresentFieldsWithDefaultValuesForTenant(payload);
        add(tenantId, payload, addResult.completer());
        return addResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setCacheDirective(tr.getCacheDirective());
        });
    } else {
        log.debug("request contains malformed payload");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Example 2 with TenantResult

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

the class BaseTenantService method processGetRequest.

private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = request.getJsonPayload();
    if (tenantId == null && payload == null) {
        log.debug("request does not contain any query parameters");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (payload != null) {
        final String tenantIdFromPayload = getTypesafeValueForField(payload, TenantConstants.FIELD_PAYLOAD_TENANT_ID);
        if (tenantIdFromPayload == null) {
            log.debug("payload does not contain any query parameters");
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
        } else {
            log.debug("retrieving tenant [id: {}]", tenantIdFromPayload);
            final Future<TenantResult<JsonObject>> getResult = Future.future();
            get(tenantIdFromPayload, getResult.completer());
            return getResult.map(tr -> {
                return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantIdFromPayload).setCacheDirective(tr.getCacheDirective());
            });
        }
    } else {
        // deprecated API
        log.debug("retrieving tenant [{}] using deprecated variant of get tenant request", tenantId);
        final Future<TenantResult<JsonObject>> getResult = Future.future();
        get(tenantId, getResult.completer());
        return getResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantId).setCacheDirective(tr.getCacheDirective());
        });
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Example 3 with TenantResult

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

the class TenantClientImplTest method testGetTenantAddsInfoToCacheOnCacheMiss.

/**
 * Verifies that on a cache miss the adapter retrieves tenant information
 * from the Tenant service and puts it to the cache.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testGetTenantAddsInfoToCacheOnCacheMiss(final TestContext ctx) {
    // GIVEN an adapter with an empty cache
    client.setResponseCache(cache);
    final JsonObject tenantResult = newTenantResult("tenant");
    // WHEN getting tenant information
    final Async get = ctx.async();
    client.get("tenant").setHandler(ctx.asyncAssertSuccess(tenant -> get.complete()));
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    final Message response = ProtonHelper.message(tenantResult.encode());
    MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
    MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60));
    response.setCorrelationId(messageCaptor.getValue().getMessageId());
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    client.handleResponse(delivery, response);
    // THEN the tenant result has been added to the cache
    get.await();
    verify(cache).put(eq(TriTuple.of(TenantAction.get, "tenant", null)), any(TenantResult.class), any(Duration.class));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) TenantAction(org.eclipse.hono.util.TenantConstants.TenantAction) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TenantResult(org.eclipse.hono.util.TenantResult) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Duration(java.time.Duration) TenantResult(org.eclipse.hono.util.TenantResult) Test(org.junit.Test)

Example 4 with TenantResult

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

the class TenantClientImplTest method testGetTenantReturnsValueFromCache.

/**
 * Verifies that tenant information is taken from cache if cache is configured and the cache has this tenant
 * information cached.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetTenantReturnsValueFromCache(final TestContext ctx) {
    // GIVEN an adapter with a cache containing a tenant
    client.setResponseCache(cache);
    final JsonObject tenantJsonObject = newTenantResult("tenant");
    final TenantResult<TenantObject> tenantResult = client.getResult(HttpURLConnection.HTTP_OK, tenantJsonObject.toString(), null);
    when(cache.get(any(TriTuple.class))).thenReturn(tenantResult);
    // WHEN getting tenant information
    client.get("tenant").setHandler(ctx.asyncAssertSuccess(result -> {
        // THEN the tenant information is read from the cache
        ctx.assertEquals(tenantResult.getPayload(), result);
        verify(sender, never()).send(any(Message.class));
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) TestContext(io.vertx.ext.unit.TestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) Async(io.vertx.ext.unit.Async) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) ExpiringValueCache(org.eclipse.hono.cache.ExpiringValueCache) Context(io.vertx.core.Context) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) TenantAction(org.eclipse.hono.util.TenantConstants.TenantAction) Duration(java.time.Duration) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Before(org.junit.Before) TenantResult(org.eclipse.hono.util.TenantResult) TriTuple(org.eclipse.hono.util.TriTuple) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) ProtonHelper(io.vertx.proton.ProtonHelper) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) MessageHelper(org.eclipse.hono.util.MessageHelper) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) TriTuple(org.eclipse.hono.util.TriTuple) TenantObject(org.eclipse.hono.util.TenantObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 5 with TenantResult

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

the class BaseTenantService method processUpdateRequest.

private Future<EventBusMessage> processUpdateRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = getRequestPayload(request.getJsonPayload());
    if (tenantId == null) {
        log.debug("request does not contain mandatory property [{}]", MessageHelper.APP_PROPERTY_TENANT_ID);
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (isValidRequestPayload(payload)) {
        log.debug("updating tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> updateResult = Future.future();
        addNotPresentFieldsWithDefaultValuesForTenant(payload);
        update(tenantId, payload, updateResult.completer());
        return updateResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setCacheDirective(tr.getCacheDirective());
        });
    } else {
        log.debug("request contains malformed payload");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Aggregations

TenantResult (org.eclipse.hono.util.TenantResult)6 Handler (io.vertx.core.Handler)5 JsonObject (io.vertx.core.json.JsonObject)5 HttpURLConnection (java.net.HttpURLConnection)5 MessageHelper (org.eclipse.hono.util.MessageHelper)5 TenantConstants (org.eclipse.hono.util.TenantConstants)5 ClientErrorException (org.eclipse.hono.client.ClientErrorException)4 AsyncResult (io.vertx.core.AsyncResult)3 Future (io.vertx.core.Future)3 JsonArray (io.vertx.core.json.JsonArray)3 Objects (java.util.Objects)3 ServerErrorException (org.eclipse.hono.client.ServerErrorException)3 Context (io.vertx.core.Context)2 Vertx (io.vertx.core.Vertx)2 Async (io.vertx.ext.unit.Async)2 TestContext (io.vertx.ext.unit.TestContext)2 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)2 ProtonDelivery (io.vertx.proton.ProtonDelivery)2 ProtonHelper (io.vertx.proton.ProtonHelper)2 ProtonReceiver (io.vertx.proton.ProtonReceiver)2