ExecutionContextAuthHandler implements AuthHandler<T> { @Override public final Future<DeviceUser> authenticateDevice(final T context) { Objects.requireNonNull(context); return parseCredentials(context) .compose(authInfo -> { final DeviceCredentialsAuthProvider<?> authProvider = getAuthProvider(context); if (authProvider == null) { return Future.failedFuture(new IllegalStateException("no auth provider found")); } return authenticateDevice(context, authInfo, authProvider); }); } protected  ExecutionContextAuthHandler(
            final DeviceCredentialsAuthProvider<?> authProvider,
            final PreCredentialsValidationHandler<T> preCredentialsValidationHandler); @Override final Future<DeviceUser> authenticateDevice(final T context); @Override DeviceCredentialsAuthProvider<?> getAuthProvider(final T context); static final String PROPERTY_CLIENT_IDENTIFIER; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testPreCredentialsValidationHandlerGetsInvoked(final VertxTestContext ctx) { final Checkpoint preCredValidationHandlerInvokedCheckpoint = ctx.checkpoint(); final Checkpoint testPassedCheckpoint = ctx.checkpoint(); final TestExecutionContext context = new TestExecutionContext(); final JsonObject parsedCredentials = new JsonObject().put("someKey", "someValue"); final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class); final DeviceUser deviceUser = new DeviceUser("tenant", "device"); final DeviceCredentialsAuthProvider<?> provider = mock(DeviceCredentialsAuthProvider.class); doReturn(deviceCredentials).when(provider).getCredentials(any(JsonObject.class)); doAnswer(invocation -> { final Handler handler = invocation.getArgument(2); handler.handle(Future.succeededFuture(deviceUser)); return null; }).when(provider).authenticate(any(), any(), any()); final PreCredentialsValidationHandler<TestExecutionContext> preCredValidationHandler = mock( PreCredentialsValidationHandler.class); doAnswer(invocation -> { preCredValidationHandlerInvokedCheckpoint.flag(); return Future.succeededFuture(); }).when(preCredValidationHandler).handle(eq(deviceCredentials), eq(context)); final ExecutionContextAuthHandler<TestExecutionContext> authHandler = new ExecutionContextAuthHandler<>(provider, preCredValidationHandler) { @Override public Future<JsonObject> parseCredentials(final TestExecutionContext context) { return Future.succeededFuture(parsedCredentials); } }; authHandler.authenticateDevice(context).onComplete(ctx.succeeding(user -> { ctx.verify(() -> { assertThat(user).isEqualTo(deviceUser); }); testPassedCheckpoint.flag(); })); }
@SuppressWarnings("unchecked") @Test public void testPreCredentialsValidationHandlerFailsAuthentication(final VertxTestContext ctx) { final Checkpoint preCredValidationHandlerInvokedCheckpoint = ctx.checkpoint(); final Checkpoint testPassedCheckpoint = ctx.checkpoint(); final TestExecutionContext context = new TestExecutionContext(); final JsonObject parsedCredentials = new JsonObject().put("someKey", "someValue"); final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class); final Exception preCredValidationHandlerError = new RuntimeException("some error"); final DeviceCredentialsAuthProvider<?> provider = mock(DeviceCredentialsAuthProvider.class); doReturn(deviceCredentials).when(provider).getCredentials(any(JsonObject.class)); doAnswer(invocation -> { ctx.failNow(new IllegalStateException("authenticate method of provider shouldn't have been called")); return null; }).when(provider).authenticate(any(), any(), any()); final PreCredentialsValidationHandler<TestExecutionContext> preCredValidationHandler = mock( PreCredentialsValidationHandler.class); doAnswer(invocation -> { preCredValidationHandlerInvokedCheckpoint.flag(); return Future.failedFuture(preCredValidationHandlerError); }).when(preCredValidationHandler).handle(eq(deviceCredentials), eq(context)); final ExecutionContextAuthHandler<TestExecutionContext> authHandler = new ExecutionContextAuthHandler<>(provider, preCredValidationHandler) { @Override public Future<JsonObject> parseCredentials(final TestExecutionContext context) { return Future.succeededFuture(parsedCredentials); } }; authHandler.authenticateDevice(context).onComplete(ctx.failing(thr -> { ctx.verify(() -> { assertThat(thr).isEqualTo(preCredValidationHandlerError); }); testPassedCheckpoint.flag(); })); }