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));
}
}
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());
});
}
}
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));
}
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));
}));
}
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));
}
}
Aggregations