Skip to content

Commit

Permalink
test: photo Application 게층 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Nov 4, 2023
1 parent fca87c5 commit fc9cff3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public List<String> uploadPhotos(final List<MultipartFile> files) {
}

@Transactional
public void deletePhotosByReviewId(final Review review) {
public boolean deletePhotosByReviewId(final Review review) {
deletePhotosInCloud(review);
photoRepository.deleteAllByReview(review.getId());

return true;
}

@Transactional
public void deletePhotosByWriter(final List<Review> reviews) {
public boolean deletePhotosByWriter(final List<Review> reviews) {
reviews.forEach(this::deletePhotosInCloud);
photoRepository.deleteAllByReviews(reviews.stream().map(Review::getId).toList());

return true;
}

private void deletePhotosInCloud(final Review review) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.inq.wishhair.wesharewishhair.photo.application;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.multipart.MultipartFile;

import com.inq.wishhair.wesharewishhair.common.utils.FileMockingUtils;
import com.inq.wishhair.wesharewishhair.photo.domain.Photo;
import com.inq.wishhair.wesharewishhair.photo.domain.PhotoRepository;
import com.inq.wishhair.wesharewishhair.photo.domain.PhotoStore;
import com.inq.wishhair.wesharewishhair.review.domain.entity.Review;
import com.inq.wishhair.wesharewishhair.review.fixture.ReviewFixture;

@DisplayName("[PhotoService 테스트] - Application")
class PhotoServiceTest {

private final PhotoService photoService;
private final PhotoStore photoStore;

public PhotoServiceTest() {
PhotoRepository photoRepository = Mockito.mock(PhotoRepository.class);
this.photoStore = Mockito.mock(PhotoStore.class);
this.photoService = new PhotoService(photoStore, photoRepository);
}

@Test
void uploadPhotos() throws IOException {
//given
List<MultipartFile> files = FileMockingUtils.createMockMultipartFiles();

List<String> urls = List.of("test_url1", "test_url2");
given(photoStore.uploadFiles(anyList()))
.willReturn(urls);

//when
List<String> actual = photoService.uploadPhotos(files);

//then
assertThat(actual).isEqualTo(urls);
}

@Test
void deletePhotosByReviewId() {
//given
Review review = ReviewFixture.getEmptyReview(1L);
Photo photo = Photo.createReviewPhoto("url1", review);

ReflectionTestUtils.setField(review, "photos", List.of(photo));

//when
boolean actual = photoService.deletePhotosByReviewId(review);

//then
assertThat(actual).isTrue();
verify(photoStore, times(1)).deleteFiles(anyList());
}

@Test
void deletePhotosByWriter() {
//given
Review review1 = ReviewFixture.getEmptyReview(1L);
Review review2 = ReviewFixture.getEmptyReview(2L);
Photo photo1 = Photo.createReviewPhoto("url1", review1);
Photo photo2 = Photo.createReviewPhoto("url1", review2);

ReflectionTestUtils.setField(review1, "photos", List.of(photo1));
ReflectionTestUtils.setField(review2, "photos", List.of(photo2));

//when
boolean actual = photoService.deletePhotosByWriter(List.of(review1, review2));

//then
assertThat(actual).isTrue();
verify(photoStore, times(2)).deleteFiles(anyList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.inq.wishhair.wesharewishhair.review.fixture;

import org.springframework.test.util.ReflectionTestUtils;

import com.inq.wishhair.wesharewishhair.review.domain.entity.Review;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ReviewFixture {

public static Review getEmptyReview(Long id) {
Review review = new Review();
ReflectionTestUtils.setField(review, "id", id);
return review;
}
}

0 comments on commit fc9cff3

Please sign in to comment.