use of org.eclipse.hono.deviceregistry.service.tenant.TenantKey in project hono by eclipse.
the class AbstractCredentialsServiceTest method testGetCredentialsPreservesOriginalErrorStatus.
/**
* Verifies that when the <em>processGet</em> method returns an error result, its status
* is adopted for the <em>get</em> method result.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsPreservesOriginalErrorStatus(final VertxTestContext ctx) {
final AbstractCredentialsService credentialsService = new AbstractCredentialsService() {
@Override
protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
return Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_BAD_GATEWAY));
}
};
final String tenantId = "tenant";
final String type = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD;
final String authId = UUID.randomUUID().toString();
final NoopSpan span = NoopSpan.INSTANCE;
credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult -> {
ctx.verify(() -> {
assertThat(getCredentialsResult.getCacheDirective()).isNotNull();
assertThat(getCredentialsResult.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
assertThat(getCredentialsResult.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
});
// another test with auto-provisioning enabled
credentialsService.setDeviceAndGatewayAutoProvisioner(getDeviceAndGatewayAutoProvisionerMock());
credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult2 -> {
ctx.verify(() -> {
assertThat(getCredentialsResult2.getCacheDirective()).isNotNull();
assertThat(getCredentialsResult2.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
assertThat(getCredentialsResult2.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
});
ctx.completeNow();
}));
}));
}
use of org.eclipse.hono.deviceregistry.service.tenant.TenantKey in project hono by eclipse.
the class CredentialsServiceImpl method processGet.
@Override
protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
return this.store.findCredentials(key, span.context()).map(r -> {
if (r.isEmpty()) {
return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
}
final var result = r.get();
final var secrets = result.getCredentials().stream().map(JsonObject::mapFrom).filter(filter(key.getType(), key.getAuthId())).filter(credential -> DeviceRegistryUtils.matchesWithClientContext(credential, clientContext)).flatMap(c -> c.getJsonArray(CredentialsConstants.FIELD_SECRETS).stream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)).filter(CredentialsServiceImpl::filterSecrets).collect(Collectors.toList());
if (secrets.isEmpty()) {
// nothing was left after filtering ... not found
return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
}
final var payload = new JsonObject().put(Constants.JSON_FIELD_DEVICE_ID, result.getDeviceId()).put(CredentialsConstants.FIELD_TYPE, key.getType()).put(CredentialsConstants.FIELD_AUTH_ID, key.getAuthId()).put(CredentialsConstants.FIELD_SECRETS, new JsonArray(secrets));
return CredentialsResult.from(HttpURLConnection.HTTP_OK, payload, getCacheDirective(key.getType(), config.getCredentialsTtl().toSeconds()));
});
}
Aggregations