AccessTokenService { @Transactional public AccessToken save(AccessTokenPersist accessTokenPersist) { AccessToken accessToken = GenericConverter.mapper(accessTokenPersist, AccessToken.class); App appRecover = appRespository.findOne(accessTokenPersist.getApp().getId()); HeimdallException.checkThrow(appRecover == null, APP_NOT_EXIST); HeimdallException.checkThrow(!verifyIfPlansContainsInApp(appRecover, accessTokenPersist.getPlans()), SOME_PLAN_NOT_PRESENT_IN_APP); AccessToken existAccessToken; if (accessToken.getCode() != null) { existAccessToken = accessTokenRepository.findByCode(accessToken.getCode()); HeimdallException.checkThrow(existAccessToken != null, ACCESS_TOKEN_ALREADY_EXISTS); } else { RandomString randomString = new RandomString(12); String token = randomString.nextString(); while (accessTokenRepository.findByCode(token) != null) { token = randomString.nextString(); } accessToken.setCode(token); } accessToken = accessTokenRepository.save(accessToken); return accessToken; }  @Transactional(readOnly = true) AccessToken find(Long id); @Transactional(readOnly = true) AccessTokenPage list(AccessTokenRequest accessTokenRequest, PageableDTO pageableDTO); @Transactional(readOnly = true) List<AccessToken> list(AccessTokenRequest accessTokenRequest); @Transactional AccessToken save(AccessTokenPersist accessTokenPersist); @Transactional AccessToken update(Long id, AccessTokenPersist accessTokenPersist); @Transactional void delete(Long id); @Transactional AccessToken save(AccessTokenDTO reqBody);  }
@Test public void notPermitToSaveTheAccessTokenWithCodeExistent() { thrown.expect(BadRequestException.class); thrown.expectMessage("Token already exists"); AccessTokenPersist persist = new AccessTokenPersist(); persist.setCode("123456"); persist.setApp(new ReferenceIdDTO(10L)); Plan plan = new Plan(); plan.setId(1L); persist.setPlans(Collections.singletonList(new ReferenceIdDTO(1L))); AccessToken recoverAt = new AccessToken(); recoverAt.setCode("123456"); App appRecovered = new App(); appRecovered.setId(10L); appRecovered.setPlans(Collections.singletonList(plan)); Mockito.when(accessTokenRepository.findByCode(Mockito.anyString())).thenReturn(recoverAt); Mockito.when(appRespository.findOne(Mockito.anyLong())).thenReturn(appRecovered); service.save(persist); }
@Test public void savingAccessTokenWithCode() { AccessTokenPersist persist = new AccessTokenPersist(); persist.setCode("123456"); persist.setApp(new ReferenceIdDTO(10L)); Plan plan = new Plan(); plan.setId(1L); persist.setPlans(Collections.singletonList(new ReferenceIdDTO(1L))); AccessToken recoverAt = new AccessToken(); recoverAt.setCode("123456"); App appRecovered = new App(); appRecovered.setId(10L); appRecovered.setPlans(Collections.singletonList(plan)); Mockito.when(accessTokenRepository.findByCode(Mockito.anyString())).thenReturn(null); Mockito.when(appRespository.findOne(Mockito.anyLong())).thenReturn(appRecovered); Mockito.when(accessTokenRepository.save(Mockito.any(AccessToken.class))).thenReturn(recoverAt); AccessToken savedAt = service.save(persist); assertEquals(savedAt.getCode(), persist.getCode()); Mockito.verify(accessTokenRepository, Mockito.times(1)).save(Mockito.any(AccessToken.class)); }
@Test public void generateAccessTokenWithRandomCode() { AccessTokenPersist persist = new AccessTokenPersist(); persist.setApp(new ReferenceIdDTO(10L)); Plan plan = new Plan(); plan.setId(1L); persist.setPlans(Collections.singletonList(new ReferenceIdDTO(1L))); App appRecovered = new App(); appRecovered.setId(10L); appRecovered.setPlans(Collections.singletonList(plan)); Mockito.when(accessTokenRepository.findByCode(Mockito.anyString())).thenReturn(null); Mockito.when(appRespository.findOne(Mockito.anyLong())).thenReturn(appRecovered); service.save(persist); Mockito.verify(accessTokenRepository, Mockito.times(1)).save(Mockito.any(AccessToken.class)); }
@Test public void notPermitToSaveAccessTokenWithInvalidApp() { thrown.expect(BadRequestException.class); thrown.expectMessage("App does not exist"); AccessTokenPersist persist = new AccessTokenPersist(); persist.setApp(new ReferenceIdDTO(10L)); Mockito.when(accessTokenRepository.findByCode(Mockito.anyString())).thenReturn(null); Mockito.when(appRespository.findOne(Mockito.anyLong())).thenReturn(null); service.save(persist); Mockito.verify(accessTokenRepository, Mockito.times(1)).save(Mockito.any(AccessToken.class)); Mockito.verify(accessTokenRepository, Mockito.times(1)).findByCode(Mockito.anyString()); }
@Test public void callOneRecursiveSaveWhenRandomNumberExistInDatabase() { AccessTokenPersist persist = new AccessTokenPersist(); persist.setApp(new ReferenceIdDTO(10L)); Plan plan = new Plan(); plan.setId(1L); persist.setPlans(Collections.singletonList(new ReferenceIdDTO(1L))); App appRecovered = new App(); appRecovered.setId(10L); appRecovered.setPlans(Collections.singletonList(plan)); AccessToken recoverAt = new AccessToken(); recoverAt.setCode("123456"); Mockito.when(accessTokenRepository.findByCode(Mockito.anyString())).thenReturn(recoverAt).thenReturn(null); Mockito.when(appRespository.findOne(Mockito.anyLong())).thenReturn(appRecovered); service.save(persist); Mockito.verify(accessTokenRepository, Mockito.times(1)).save(Mockito.any(AccessToken.class)); Mockito.verify(accessTokenRepository, Mockito.times(2)).findByCode(Mockito.anyString()); }
@Test public void saveAccessTokenWithAppCallbackDTO() { AccessToken recoverAt = new AccessToken(); recoverAt.setCode("123456"); Plan plan = new Plan(); AccessTokenDTO reqBody = new AccessTokenDTO(); reqBody.setCode("developer@conductor.com.br"); reqBody.setStatus(Status.ACTIVE); reqBody.setApp(new AppCallbackDTO()); Mockito.when(accessTokenRepository.save(Mockito.any(AccessToken.class))).thenReturn(recoverAt); Mockito.when(accessTokenRepository.findByCode("123456")).thenReturn(recoverAt); Mockito.when(planRepository.findOne(1L)).thenReturn(plan); AccessToken saved = this.service.save(reqBody); assertEquals(recoverAt.getId(), saved.getId()); }