### Question:
LoginProcess { public void cleanup() { if (isFailed()) { DEBUG.message("Authentication failed - destroying new session"); authContext.destroySession(); if (authContext.isSessionUpgrade()) { DEBUG.message("Session upgrade failed - restoring original session"); authContext.restoreOldSession(); } else { authContext.destroyOldSession(); } } else if (isSuccessful()) { if (authContext.isForceAuth()) { DEBUG.message("Force Auth succeeded - restoring updated session."); authContext.destroySession(); authContext.restoreOldSession(); } else if (authContext.hasOldSession() && SystemProperties.getAsBoolean(Constants.DESTROY_SESSION_AFTER_UPGRADE)) { DEBUG.message("Session upgrade succeeded - destroying old session"); authContext.destroyOldSession(); } } } LoginProcess(LoginAuthenticator loginAuthenticator, LoginConfiguration loginConfiguration,
            AuthenticationContext authContext, CoreServicesWrapper coreServicesWrapper); LoginStage getLoginStage(); Callback[] getCallbacks(); PagePropertiesCallback getPagePropertiesCallback(); LoginProcess next(Callback[] callbacks); boolean isSuccessful(); boolean isFailed(); SSOToken getSSOToken(); String getSuccessURL(); String getFailureURL(); String getOrgDN(); AuthenticationContext getAuthContext(); LoginConfiguration getLoginConfiguration(); void cleanup();  }### Answer:
@Test public void shouldDestroySessionAfterFailedAuthentication() { given(authContext.getStatus()).willReturn(AuthContext.Status.FAILED); loginProcess.cleanup(); verify(authContext).destroySession(); }
@Test public void shouldRestoreOldSessionAfterFailedSessionUpgrade() { given(authContext.getStatus()).willReturn(AuthContext.Status.FAILED); given(authContext.isSessionUpgrade()).willReturn(true); loginProcess.cleanup(); verify(authContext).restoreOldSession(); }
@Test public void shouldDestroyAnyOldSessionAfterFailedAuth_NotSessionUpgrade() { given(authContext.getStatus()).willReturn(AuthContext.Status.FAILED); given(authContext.isSessionUpgrade()).willReturn(false); loginProcess.cleanup(); verify(authContext).destroyOldSession(); }
@Test public void shouldRestoreOldSessionAfterSuccessfulForceAuth() { given(authContext.getStatus()).willReturn(AuthContext.Status.SUCCESS); given(authContext.isForceAuth()).willReturn(true); loginProcess.cleanup(); verify(authContext).destroySession(); verify(authContext).restoreOldSession(); }
@Test public void shouldDestroyOldSessionAfterSessionUpgrade() { System.setProperty(Constants.DESTROY_SESSION_AFTER_UPGRADE, "true"); given(authContext.getStatus()).willReturn(AuthContext.Status.SUCCESS); given(authContext.hasOldSession()).willReturn(true); loginProcess.cleanup(); verify(authContext).destroyOldSession(); }
@Test public void shouldNotDestroyOldSessionIfSystemPropertyNotSet() { given(authContext.getStatus()).willReturn(AuthContext.Status.SUCCESS); given(authContext.isSessionUpgrade()).willReturn(true); loginProcess.cleanup(); verify(authContext, never()).destroyOldSession(); }