### Question:
TokenCreationService { public String createJwtTokenWithoutCredentials(String user) { boolean isZosmfUsedAndAvailable = false; try { isZosmfUsedAndAvailable = providers.isZosfmUsed() && providers.isZosmfAvailable(); } catch (AuthenticationServiceException ex) { } if (isZosmfUsedAndAvailable) { try { String passTicket = passTicketService.generate(user, zosmfApplId); return ((TokenAuthentication) zosmfAuthenticationProvider.authenticate(new UsernamePasswordAuthenticationToken(user, passTicket))) .getCredentials(); } catch (IRRPassTicketGenerationException e) { throw new AuthenticationTokenException("Problem with generating PassTicket"); } } else { final String domain = "security-domain"; final String jwtTokenString = authenticationService.createJwtToken(user, domain, null); return authenticationService.createTokenAuthentication(user, jwtTokenString).getCredentials(); } }  String createJwtTokenWithoutCredentials(String user);  }### Answer:
@Test void givenZosmfIsUnavailable_whenTokenIsRequested_thenTokenCreatedByApiMlIsReturned() { when(providers.isZosmfAvailable()).thenReturn(false); when(authenticationService.createJwtToken(eq(VALID_USER_ID), any(), any())).thenReturn(VALID_APIML_TOKEN); when(authenticationService.createTokenAuthentication(VALID_USER_ID, VALID_APIML_TOKEN)).thenReturn(new TokenAuthentication(VALID_USER_ID, VALID_APIML_TOKEN)); String jwtToken = underTest.createJwtTokenWithoutCredentials(VALID_USER_ID); assertThat(jwtToken, is(VALID_APIML_TOKEN)); }
@Test void givenZosmfIsntPresentBecauseOfError_whenTokenIsRequested_shouldReturnTokenCreatedByApiMl() { when(providers.isZosmfAvailable()).thenThrow(new AuthenticationServiceException("zOSMF id invalid")); when(authenticationService.createJwtToken(eq(VALID_USER_ID), any(), any())).thenReturn(VALID_APIML_TOKEN); when(authenticationService.createTokenAuthentication(VALID_USER_ID, VALID_APIML_TOKEN)).thenReturn(new TokenAuthentication(VALID_USER_ID, VALID_APIML_TOKEN)); String jwtToken = underTest.createJwtTokenWithoutCredentials(VALID_USER_ID); assertThat(jwtToken, is(VALID_APIML_TOKEN)); }
@Test void givenZosmfIsAvailable_whenTokenIsRequested_thenTokenCreatedByZosmfIsReturned() throws IRRPassTicketGenerationException { when(providers.isZosmfAvailable()).thenReturn(true); when(providers.isZosfmUsed()).thenReturn(true); when(passTicketService.generate(VALID_USER_ID, VALID_ZOSMF_APPLID)).thenReturn("validPassticket"); when(zosmfAuthenticationProvider.authenticate(any())).thenReturn(new TokenAuthentication(VALID_USER_ID, VALID_ZOSMF_TOKEN)); String jwtToken = underTest.createJwtTokenWithoutCredentials(VALID_USER_ID); assertThat(jwtToken, is(VALID_ZOSMF_TOKEN)); }
@Test void givenZosmfIsAvailableButPassticketGenerationFails_whenTokenIsRequested_thenExceptionIsThrown() throws IRRPassTicketGenerationException { when(providers.isZosmfAvailable()).thenReturn(true); when(providers.isZosfmUsed()).thenReturn(true); when(passTicketService.generate(VALID_USER_ID, VALID_ZOSMF_APPLID)).thenThrow(new IRRPassTicketGenerationException(4, 4, 4)); assertThrows(AuthenticationTokenException.class, () -> underTest.createJwtTokenWithoutCredentials(VALID_USER_ID) ); }