AppService { public App save(AppPersist appDTO) { if (appDTO.getClientId() != null) { App app = appRepository.findByClientId(appDTO.getClientId()); HeimdallException.checkThrow(Objects.nonNull(app), CLIENT_ID_ALREADY); } else { RandomString randomString = new RandomString(12); String token = randomString.nextString(); while (appRepository.findByClientId(token) != null) { token = randomString.nextString(); } appDTO.setClientId(token); } App app = GenericConverter.mapper(appDTO, App.class); Developer dev = devRepository.findOne(app.getDeveloper().getId()); HeimdallException.checkThrow(dev == null, DEVELOPER_NOT_EXIST); amqpCacheService.dispatchClean(); return appRepository.save(app); }  @Transactional(readOnly = true) App find(Long id); @Transactional(readOnly = true) AppPage list(AppRequestDTO appDTO, PageableDTO pageableDTO); @Transactional(readOnly = true) List<App> list(AppRequestDTO appDTO); App save(AppPersist appDTO); App update(Long id, AppDTO appDTO); void delete(Long id); @Transactional App save(AppCallbackDTO reqBody);  }
@Test public void testSaveWithCodeInformed() { appPersist.setClientId("8d19n91d8"); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(null); Mockito.when(appRepository.findByClientId("0q9w8e7r6t")).thenReturn(app1); Mockito.when(appRepository.save(Mockito.any(App.class))).thenReturn(app); Mockito.when(devRepository.findOne(Mockito.anyLong())).thenReturn(developer); App saved = this.appService.save(appPersist); assertEquals(app.getId(), saved.getId()); }
@Test public void testSaveWithCodeIsAlready() { thrown.expect(BadRequestException.class); thrown.expectMessage("clientId already used"); appPersist.setClientId("0q9w8e7r6t"); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(null); Mockito.when(appRepository.findByClientId("0q9w8e7r6t")).thenReturn(app1); Mockito.when(appRepository.save(Mockito.any(App.class))).thenReturn(app); Mockito.when(devRepository.findOne(Mockito.anyLong())).thenReturn(developer); this.appService.save(appPersist); }
@Test public void SaveWithAppCallbackDTOTest() { AppCallbackDTO reqBody = new AppCallbackDTO(); reqBody.setName("Test Name"); reqBody.setDeveloper("developer@conductor.com.br"); appPersist.setClientId("8d19n91d8"); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(null); Mockito.when(appRepository.save(Mockito.any(App.class))).thenReturn(app); Mockito.when(devRepository.findOne(Mockito.anyLong())).thenReturn(developer); Mockito.when(devRepository.findByEmail(Mockito.anyString())).thenReturn(developer); App saved = this.appService.save(reqBody); assertEquals(app.getId(), saved.getId()); }
@Test public void SaveWithAppNotNullCallbackDTOTest() { AppCallbackDTO reqBody = new AppCallbackDTO(); reqBody.setName("Test Name"); reqBody.setDeveloper("developer@conductor.com.br"); appPersist.setClientId("8d19n91d8"); Mockito.when(planRepository.findOne(1L)).thenReturn(new Plan()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); Mockito.when(appRepository.save(Mockito.any(App.class))).thenReturn(app); Mockito.when(devRepository.findOne(Mockito.anyLong())).thenReturn(developer); Mockito.when(devRepository.findByEmail(Mockito.anyString())).thenReturn(developer); App saved = this.appService.save(reqBody); assertEquals(app.getId(), saved.getId()); }
@Test public void testSaveWithCodeAutoGenerate() { Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(null); Mockito.when(appRepository.findByClientId("0q9w8e7r6t")).thenReturn(app1); Mockito.when(appRepository.save(Mockito.any(App.class))).thenReturn(app); Mockito.when(devRepository.findOne(Mockito.anyLong())).thenReturn(developer); App saved = this.appService.save(appPersist); assertEquals(app.getId(), saved.getId()); }