@Test public void update() { // given Long galleryId = 2l; String photoTitle = "Updated titile"; String photoDesc = "Desc photo"; String photoSrc = "folder/img.jpg"; Photo expectedPhoto = expectedPhotoList.get(0); PhotoForm photoForm = new PhotoForm(); photoForm.setId(expectedPhoto.getId()); photoForm.setGalleryId(galleryId); photoForm.setTitle(photoTitle); photoForm.setDescription(photoDesc); photoForm.setImageSrc(photoSrc); when(photoRepository.findOne(expectedPhoto.getId())).thenReturn(expectedPhoto); when(photoRepository.save(expectedPhoto)).thenReturn(expectedPhoto); photoService.setPhotoRepository(photoRepository); // execute Photo actualPhoto = photoService.update(photoForm); // assert PhotoTestUtils.assertPhoto(expectedPhoto, actualPhoto); assertEquals(photoForm.getGalleryId(), actualPhoto.getGalleryId()); assertEquals(photoForm.getTitle(), actualPhoto.getTitle()); assertEquals(photoForm.getDescription(), actualPhoto.getDescription()); assertEquals(photoForm.getImageSrc(), actualPhoto.getImageSrc()); verify(photoRepository, times(1)).findOne(expectedPhoto.getId()); verify(photoRepository, times(1)).save(expectedPhoto); verifyNoMoreInteractions(photoRepository); }
@Test public void deleteById() { // given Photo deletePhoto = expectedPhotoList.get(0); Long photoId = deletePhoto.getId(); when(photoRepository.findOne(photoId)).thenReturn(deletePhoto); photoService.setPhotoRepository(photoRepository); photoService.setPhotoImageService(photoImageService); // execute photoService.deleteById(photoId); // assert verify(photoRepository, times(1)).findOne(deletePhoto.getId()); verify(photoRepository, times(1)).delete(deletePhoto); verify(photoImageService, times(1)).delete(photoId); verifyNoMoreInteractions(photoRepository); verifyNoMoreInteractions(photoImageService); }
@Test public void add() { // given PhotoForm photoForm = new PhotoForm(); photoForm.setTitle(expectedPhotoList.get(0).getTitle()); photoForm.setDescription(expectedPhotoList.get(0).getDescription()); photoForm.setImageSrc(expectedPhotoList.get(0).getImageSrc()); when(photoFormConverter.convert(photoForm)).thenReturn(expectedPhotoList.get(0)); when(photoRepository.save(expectedPhotoList.get(0))).thenReturn(expectedPhotoList.get(0)); photoService.setPhotoFormConverter(photoFormConverter); photoService.setPhotoRepository(photoRepository); // execute Photo actualPhoto = photoService.add(photoForm); // assert PhotoTestUtils.assertPhoto(expectedPhotoList.get(0), actualPhoto); assertEquals(photoForm.getTitle(), actualPhoto.getTitle()); assertEquals(photoForm.getDescription(), actualPhoto.getDescription()); assertEquals(photoForm.getImageSrc(), actualPhoto.getImageSrc()); }
@Test public void updateImage() { // given MultipartFile image = Mockito.mock(MultipartFile.class); Photo expectedPhoto = expectedPhotoList.get(0); Long photoId = expectedPhoto.getId(); when(photoRepository.findOne(expectedPhoto.getId())).thenReturn(expectedPhoto); when(photoRepository.save(expectedPhoto)).thenReturn(expectedPhoto); when(photoImageService.save(expectedPhoto.getId(), image)).thenReturn("title"); photoService.setPhotoRepository(photoRepository); photoService.setPhotoImageService(photoImageService); // execute photoService.updateImage(photoId, image); // assert verify(photoRepository, times(1)).findOne(expectedPhoto.getId()); verify(photoRepository, times(1)).save(expectedPhoto); verify(photoImageService, times(1)).save(photoId, image); verifyNoMoreInteractions(photoRepository); verifyNoMoreInteractions(photoImageService); }