/** * Gets all credentials registered for a device. * <p> * This default implementation simply returns the result of {@link #getAll(String, String, Handler)}. * * @param tenantId The tenant the device belongs to. * @param deviceId The device to get credentials for. * @param span The active OpenTracing span for this operation. It is not to be closed in this method! * An implementation should log (error) events on this span and it may set tags and use this span as the * parent for any spans created in this method. * @param resultHandler The handler to invoke with the result of the operation. * The <em>status</em> will be * <ul> * <li><em>200 OK</em> if credentials of the given type and authentication identifier have been found. The * <em>payload</em> will contain the credentials.</li> * <li><em>404 Not Found</em> if no credentials matching the criteria exist.</li> * </ul> * @throws NullPointerException if any of the parameters is {@code null}. */ default void getAll(final String tenantId, final String deviceId, final Span span, final Handler<AsyncResult<CredentialsResult<JsonObject>>> resultHandler) { getAll(tenantId, deviceId, resultHandler); }
/** * Verifies that service returns 404 if a client provides wrong client context. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsFailsForWrongClientContext(final TestContext ctx) { final JsonObject clientContext = new JsonObject() .put("client-id", "gateway-one"); register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", clientContext, new JsonArray(), ctx); final JsonObject testContext = new JsonObject() .put("client-id", "gateway-two"); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", testContext, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); get.complete(); })); get.await(2000); }
protected static void register( final CompleteCredentialsService svc, final String tenant, final String deviceId, final String authId, final String type, final JsonObject clientContext, final JsonArray secrets, final TestContext ctx) { final JsonObject data = new JsonObject() .put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId) .put(CredentialsConstants.FIELD_AUTH_ID, authId) .put(CredentialsConstants.FIELD_TYPE, type) .put(CredentialsConstants.FIELD_SECRETS, secrets); if (clientContext != null) { data.mergeIn(clientContext); } final Async registration = ctx.async(); svc.add("tenant", data, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CREATED)); registration.complete(); })); registration.await(); } }
/** * Verifies that the service removes credentials for a given auth-id and type. * * @param ctx The vert.x test context. */ @Test public void testRemoveCredentialsByAuthIdAndTypeSucceeds(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final Async remove = ctx.async(); getCompleteCredentialsService().remove("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx); remove.complete(); })); remove.await(); }
/** * Verifies that the service removes all credentials for a device but keeps credentials * of other devices. * * @param ctx The vert.x test context. */ @Test public void testRemoveCredentialsByDeviceSucceeds(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); register(getCompleteCredentialsService(), "tenant", "device", "myOtherId", "myOtherType", ctx); register(getCompleteCredentialsService(), "tenant", "other-device", "thirdId", "myType", ctx); final Async remove = ctx.async(); getCompleteCredentialsService().removeAll("tenant", "device", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NO_CONTENT)); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myId", "myType", ctx); assertNotRegistered(getCompleteCredentialsService(), "tenant", "myOtherId", "myOtherType", ctx); assertRegistered(getCompleteCredentialsService(), "tenant", "thirdId", "myType", ctx); remove.complete(); })); remove.await(); }
/** * Verifies that service returns existing credentials for proper client context. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsSucceedsForProperClientContext(final TestContext ctx) { final JsonObject clientContext = new JsonObject() .put("client-id", "gateway-one"); register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", clientContext, new JsonArray(), ctx); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", clientContext, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK)); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId")); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType")); get.complete(); })); get.await(2000); }
/** * Verifies that only one set of credentials can be registered for an auth-id and type (per tenant). * * @param ctx The vert.x test context. */ @Test public void testAddCredentialsRefusesDuplicateRegistration(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final JsonObject payload2 = new JsonObject() .put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, "other-device") .put(CredentialsConstants.FIELD_AUTH_ID, "myId") .put(CredentialsConstants.FIELD_TYPE, "myType") .put(CredentialsConstants.FIELD_SECRETS, new JsonArray()); final Async add = ctx.async(); getCompleteCredentialsService().add("tenant", payload2, ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_CONFLICT)); add.complete(); })); add.await(); }
protected static void assertRegistered( final CompleteCredentialsService svc, final String tenant, final String authId, final String type, final TestContext ctx) { final Async registration = ctx.async(); svc.get(tenant, type, authId, ctx.asyncAssertSuccess(t -> { assertThat(t.getStatus(), is(HttpURLConnection.HTTP_OK)); registration.complete(); })); registration.await(); }
protected static void assertNotRegistered( final CompleteCredentialsService svc, final String tenant, final String authId, final String type, final TestContext ctx) { final Async registration = ctx.async(); svc.get(tenant, type, authId, ctx.asyncAssertSuccess(t -> { assertThat(t.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); registration.complete(); })); registration.await(); }
/** * Verifies that the service returns existing credentials. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) { register(getCompleteCredentialsService(), "tenant", "device", "myId", "myType", ctx); final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK)); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId")); assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType")); get.complete(); })); get.await(); }
/** * Verifies that the service returns 404 if a client wants to retrieve non-existing credentials. * * @param ctx The vert.x test context. */ @Test public void testGetCredentialsFailsForNonExistingCredentials(final TestContext ctx) { final Async get = ctx.async(); getCompleteCredentialsService().get("tenant", "myType", "non-existing", ctx.asyncAssertSuccess(s -> { assertThat(s.getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); get.complete(); })); get.await(); }