### Question:
IdentityApplication extends Application<IdentityApplication.ApplicationHelper, IdentityConnectorContext> { @Override protected void shutDown() throws Exception { logger.log(Level.INFO, "Shutdown Identity Service"); if ((identityService != null) && identityService.isRunning()) { identityService.stopAsync().awaitTerminated(); } super.shutDown(); } private  IdentityApplication(Builder builder);   }### Answer:
@Test public void createApplicationAndFullConnectorLifecycle() throws Exception { IdentityApplication subject = new IdentityApplication.Builder(mockConnector, new String[] {}) .setHelper(mockHelper) .build(); doAnswer( invocation -> { mockConnector.traverse(); return identityScheduler; }) .when(identityScheduler) .start(); subject.start(); InOrder inOrder = inOrder(mockConnector); inOrder.verify(mockConnector).init(mockContext); inOrder.verify(mockConnector).traverse(); subject.shutdown("From Test"); inOrder.verify(mockConnector).destroy(); }
@Test public void startAndStopFromTraverser() throws Exception { IdentityApplication subject = new IdentityApplication.Builder(mockConnector, new String[] {}) .setIdentityService(mockIdentityService) .setHelper(mockHelper) .build(); SettableFuture<ShutdownHolder> function = SettableFuture.create(); Mockito.when(identitySchedulerBuilder.setShutdownHolder(any())) .thenAnswer(invocation -> { Object[] args = invocation.getArguments(); function.set((ShutdownHolder) args[0]); return identitySchedulerBuilder; }); subject.start(); function.get(1L, TimeUnit.SECONDS).shutdown(); verify(mockShutdownThread).start(); }
@Test public void shutdownWithNotRunningTraverser() throws InterruptedException { when(identityScheduler.isStarted()).thenReturn(false); IdentityApplication subject = new IdentityApplication.Builder(mockConnector, new String[] {}) .setIdentityService(mockIdentityService) .setHelper(mockHelper) .build(); subject.start(); subject.shutdown("TestEvent"); InOrder inOrder = inOrder(spyLogger, identityScheduler, mockConnector); inOrder.verify(spyLogger).log(eq(Level.INFO), any(), anyString()); inOrder.verify(identityScheduler).isStarted(); inOrder.verify(mockConnector).destroy(); }
@Test public void shutdownWithRunningTraverser() throws InterruptedException { when(identityScheduler.isStarted()).thenReturn(true); IdentityApplication subject = new IdentityApplication.Builder(mockConnector, new String[] {}) .setIdentityService(mockIdentityService) .setHelper(mockHelper) .build(); subject.start(); subject.shutdown("TestEvent"); InOrder inOrder = inOrder(spyLogger, identityScheduler, mockConnector); inOrder.verify(spyLogger).log(eq(Level.INFO), any(), anyString()); inOrder.verify(identityScheduler).isStarted(); inOrder.verify(identityScheduler).stop(); inOrder.verify(mockConnector).destroy(); }
@Test public void shutdownHookTaskShouldCallShutdown() { ShutdownHook shutdownHook = mockApplication.new ShutdownHook(); shutdownHook.run(); verify(mockApplication).shutdown(any()); }