### Question:
AnonymousUserRequestsNewApiTokenService { public void anonymousRequestToGetNewApiTokenForUserMailAdress(String userEmail) { LOG.info("New api token requested for email address: {})",logSanitizer.sanitize(userEmail,50)); assertion.isValidEmailAddress(userEmail); Optional<User> found = userRepository.findByEmailAdress(userEmail); if (! found.isPresent()) { LOG.warn("Anonymous request to get new api token, but user unknown: {})",logSanitizer.sanitize(userEmail,50)); return; } User user = saveUserWithNewOneTimeToken(found); sendUserNewApiTokenRequested(userEmail, user); }  void anonymousRequestToGetNewApiTokenForUserMailAdress(String userEmail);  }### Answer:
@Test public void when_emailadress_not_found_no_exception_is_thrown() throws Exception { when(mockedUserRepository.findByEmailAdress("user@test.com")).thenReturn(Optional.empty()); serviceToTest.anonymousRequestToGetNewApiTokenForUserMailAdress("user@test.com"); }
@Test public void when_emailadress_found_a_new_async_event_is_sent_eventbus() throws Exception { User user = new User(); user.emailAdress="user@test.com"; user.name="testuser"; when(mockedUserRepository.findByEmailAdress("user@test.com")).thenReturn(Optional.of(user)); serviceToTest.anonymousRequestToGetNewApiTokenForUserMailAdress("user@test.com"); ArgumentCaptor<DomainMessage> domainMessageCaptor = ArgumentCaptor.forClass(DomainMessage.class); verify(mockedEventBusService).sendAsynchron(domainMessageCaptor.capture()); DomainMessage messageSendByService = domainMessageCaptor.getValue(); assertNotNull("no message send!", messageSendByService); UserMessage refreshApiKeyMessage = messageSendByService.get(MessageDataKeys.USER_ONE_TIME_TOKEN_INFO); assertNotNull("no refersh api key data inside message!", refreshApiKeyMessage); assertNull(refreshApiKeyMessage.getUserId()); assertEquals("user@test.com", refreshApiKeyMessage.getEmailAdress()); }
@Test public void when_emailadress_found__onetimetoken_created_and_persisted() throws Exception { User user = new User(); user.emailAdress="user@test.com"; user.name="testuser"; when(mockedUserRepository.findByEmailAdress("user@test.com")).thenReturn(Optional.of(user)); serviceToTest.anonymousRequestToGetNewApiTokenForUserMailAdress("user@test.com"); assertEquals(FAKE_ONE_TIME_TOKEN,user.oneTimeToken); assertNotNull(user.oneTimeTokenDate); verify(mockedUserRepository).save(user); }