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