/** * Checks if the status code indicating the outcome of the invocation * of the operation represents an error. * * @return {@code true} if this is a response message and the status code represents an error. */ public boolean hasErrorStatus() { final Integer status = getStatus(); if (status == null) { return false; } return status >= HttpURLConnection.HTTP_BAD_REQUEST && status < 600; }
final String tenantId = response.getTenant(); final String deviceId = response.getDeviceId(); final Integer status = response.getStatus(); final boolean isApplCorrelationId = response.isAppCorrelationId(); final String cacheDirective = response.getCacheDirective();
final String tenantId = response.getTenant(); final String deviceId = response.getDeviceId(); final Integer status = response.getStatus(); final boolean isApplCorrelationId = response.isAppCorrelationId(); final String cacheDirective = response.getCacheDirective();
/** * Composes the given future so that the given <em>OpenTracing</em> span is finished when the future completes. * <p> * The result or exception of the given future will be used to set a {@link Tags#HTTP_STATUS} tag on the span * and to set a {@link Tags#ERROR} tag in case of an exception or a result with error status. * * @param span The span to finish. * @param resultFuture The future to be composed. * @return The composed future. */ protected Future<EventBusMessage> finishSpanOnFutureCompletion(final Span span, final Future<EventBusMessage> resultFuture) { return resultFuture.compose(eventBusMessage -> { Tags.HTTP_STATUS.set(span, eventBusMessage.getStatus()); if (eventBusMessage.hasErrorStatus()) { Tags.ERROR.set(span, true); } span.finish(); return Future.succeededFuture(eventBusMessage); }).recover(t -> { Tags.HTTP_STATUS.set(span, ServiceInvocationException.extractStatusCode(t)); TracingHelper.logError(span, t); span.finish(); return Future.failedFuture(t); }); } }
/** * Verifies that the base service accepts a request for adding and updating clear text * hashed password credentials. * * @param ctx The vert.x test context. */ @Test public void testUpdateSucceedsForClearTextPassword(final TestContext ctx) { final JsonObject secret = new JsonObject().put(CredentialsConstants.FIELD_SECRETS_PWD_PLAIN, "initial"); final JsonObject credentials = createValidCredentialsObject( CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, secret); final EventBusMessage msg = createRequestForPayload(CredentialsConstants.CredentialsAction.add, credentials); service.processRequest(msg) .compose(r -> { ctx.assertEquals(HttpURLConnection.HTTP_CREATED, r.getStatus()); verify(pwdEncoder).encode("initial"); final JsonObject updatedSecret = new JsonObject().put(CredentialsConstants.FIELD_SECRETS_PWD_PLAIN, "updated"); final JsonObject updatedCredentials = createValidCredentialsObject( CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, updatedSecret); secret.put(CredentialsConstants.FIELD_SECRETS_PWD_PLAIN, "updated"); final EventBusMessage updateMsg = createRequestForPayload(CredentialsConstants.CredentialsAction.update, updatedCredentials); return service.processRequest(updateMsg); }).setHandler(ctx.asyncAssertSuccess(r -> { ctx.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, r.getStatus()); verify(pwdEncoder).encode("updated"); })); }
/** * Verifies that the base service accepts a request for adding * credentials that contains a secret with a time stamp including * an offset. * * @param ctx The vert.x test context. */ @Test public void testAddSucceedsForLongTimestamp(final TestContext ctx) { final JsonObject secret = new JsonObject() .put(CredentialsConstants.FIELD_SECRETS_NOT_BEFORE, "2007-04-05T12:30-02:00"); final JsonObject testData = createValidCredentialsObject(secret); final EventBusMessage msg = createRequestForPayload(CredentialsConstants.CredentialsAction.add, testData); service.processRequest(msg).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus()); })); }
/** * Verifies that the base service routes a deprecated request for retrieving * a tenant by its identifier to the corresponding <em>get</em> method. * * @param ctx The vert.x test context. */ @Test public void testDeprecatedGetByIdSucceeds(final TestContext ctx) { final EventBusMessage request = EventBusMessage.forOperation(TenantConstants.TenantAction.get.toString()) .setJsonPayload(new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, "my-tenant")); tenantService.processRequest(request).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_OK, response.getStatus()); ctx.assertEquals("getById", response.getJsonPayload().getString("operation")); })); }
/** * Verifies that the base service routes a request for retrieving * a tenant by its trusted certificate authority to the corresponding * <em>get</em> method. * * @param ctx The vert.x test context. */ @Test public void testGetByCaSucceeds(final TestContext ctx) { final EventBusMessage request = EventBusMessage.forOperation(TenantConstants.TenantAction.get.toString()) .setJsonPayload(new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN, "CN=test")); tenantService.processRequest(request).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_OK, response.getStatus()); ctx.assertEquals("getByCa", response.getJsonPayload().getString("operation")); })); }
/** * Verifies that the base service routes a request for retrieving * a tenant by its identifier to the corresponding <em>get</em> method. * * @param ctx The vert.x test context. */ @Test public void testGetByIdSucceeds(final TestContext ctx) { final EventBusMessage request = createRequest(TenantConstants.TenantAction.get, null); tenantService.processRequest(request).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_OK, response.getStatus()); ctx.assertEquals(TEST_TENANT, response.getJsonPayload().getString(TenantConstants.FIELD_PAYLOAD_TENANT_ID)); })); }
/** * Verifies that the base service accepts a request for adding * credentials that contains the minimum required properties. * * @param ctx The vert.x test context. */ @Test public void testAddSucceedsForMinimalData(final TestContext ctx) { final JsonObject testData = createValidCredentialsObject(); final EventBusMessage msg = createRequestForPayload(CredentialsConstants.CredentialsAction.add, testData); service.processRequest(msg).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus()); })); }
/** * Verifies that the base service accepts a request for adding * a tenant that contains the minimum required properties. * * @param ctx The vert.x test context. */ @Test public void testAddSucceedsForMinimalData(final TestContext ctx) { final JsonObject testPayload = createValidTenantPayload(); final EventBusMessage request = createRequest(TenantConstants.TenantAction.add, testPayload); tenantService.processRequest(request).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus()); ctx.assertEquals(TEST_TENANT, response.getTenant()); })); }
/** * Verifies that the base service accepts a request for adding * valid bcrypt hashed password credentials. * * @param ctx The vert.x test context. */ @Test public void testAddSucceedsForValidBcryptSecret(final TestContext ctx) { // see https://www.dailycred.com/article/bcrypt-calculator final CredentialsObject credentials = CredentialsObject.fromHashedPassword( "4711", "theDevice", "$2a$10$UK9lmSMlYmeXqABkTrDRsu1nlZRnAmGnBdPIWZoDajtjyxX18Dry.", CredentialsConstants.HASH_FUNCTION_BCRYPT, null, null, null); final EventBusMessage msg = createRequestForPayload(CredentialsConstants.CredentialsAction.add, JsonObject.mapFrom(credentials)); service.processRequest(msg).setHandler(ctx.asyncAssertSuccess(response -> { ctx.assertEquals(HttpURLConnection.HTTP_CREATED, response.getStatus()); })); }