@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()); }
@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; }
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; } }
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; }  }
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); }
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);  }