-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f524abc
commit fca87c5
Showing
11 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/inq/wishhair/wesharewishhair/photo/domain/PhotoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/com/inq/wishhair/wesharewishhair/common/utils/FileMockingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.inq.wishhair.wesharewishhair.common.utils; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public abstract class FileMockingUtils { | ||
|
||
private static final String FILE_PATH = "src/test/resources/images/"; | ||
private static final String FILE_META_NAME = "files"; | ||
private static final String CONTENT_TYPE = "image/bmp"; | ||
|
||
public static MultipartFile createMockMultipartFile( | ||
final String fileName | ||
) throws IOException { | ||
try (final FileInputStream stream = new FileInputStream(FILE_PATH + fileName)) { | ||
return new MockMultipartFile(FILE_META_NAME, fileName, CONTENT_TYPE, stream); | ||
} | ||
} | ||
|
||
public static List<MultipartFile> createMockMultipartFiles() throws IOException { | ||
List<MultipartFile> files = new ArrayList<>(); | ||
for (int i = 1; i <= 2; i++) { | ||
files.add(createMockMultipartFile(String.format("hello%s.jpg", i))); | ||
} | ||
return files; | ||
} | ||
|
||
public static MultipartFile createEmptyFile() { | ||
return new MockMultipartFile( | ||
"file", | ||
"hello.png", | ||
"image/png", | ||
new byte[] {} | ||
); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/com/inq/wishhair/wesharewishhair/photo/domain/PhotoRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.inq.wishhair.wesharewishhair.photo.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import com.inq.wishhair.wesharewishhair.common.support.RepositoryTestSupport; | ||
import com.inq.wishhair.wesharewishhair.review.domain.entity.Review; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
||
@DisplayName("[PhotoRepository 테스트] - Domain") | ||
class PhotoRepositoryTest extends RepositoryTestSupport { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
@Autowired | ||
private PhotoRepository photoRepository; | ||
|
||
@Test | ||
@DisplayName("[리뷰 아이디를 가진 Photo 를 삭제한다]") | ||
void deleteAllByReview() { | ||
//given | ||
Review review = new Review(); | ||
ReflectionTestUtils.setField(review, "id", 1L); | ||
Photo photo = photoRepository.save(Photo.createReviewPhoto("url", review)); | ||
|
||
//when | ||
photoRepository.deleteAllByReview(1L); | ||
entityManager.clear(); | ||
|
||
//then | ||
Optional<Photo> actual = photoRepository.findById(photo.getId()); | ||
assertThat(actual).isNotPresent(); | ||
} | ||
|
||
@Test | ||
@DisplayName("[리뷰 아이디 리스트에 포함된 Photo 를 삭제한다]") | ||
void deleteAllByReviews() { | ||
//given | ||
Review review1 = new Review(); | ||
ReflectionTestUtils.setField(review1, "id", 1L); | ||
Review review2 = new Review(); | ||
ReflectionTestUtils.setField(review2, "id", 2L); | ||
photoRepository.save(Photo.createReviewPhoto("url1", review1)); | ||
photoRepository.save(Photo.createReviewPhoto("url2", review2)); | ||
|
||
//when | ||
photoRepository.deleteAllByReviews(List.of(1L, 2L)); | ||
entityManager.clear(); | ||
|
||
//then | ||
List<Photo> actual = photoRepository.findAll(); | ||
assertThat(actual).isEmpty(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/test/java/com/inq/wishhair/wesharewishhair/photo/domain/PhotoStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.inq.wishhair.wesharewishhair.photo.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.assertj.core.api.ThrowableAssert.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.inq.wishhair.wesharewishhair.common.utils.FileMockingUtils; | ||
import com.inq.wishhair.wesharewishhair.global.exception.ErrorCode; | ||
import com.inq.wishhair.wesharewishhair.global.exception.WishHairException; | ||
import com.inq.wishhair.wesharewishhair.photo.infrastructure.S3PhotoStore; | ||
|
||
@DisplayName("[PhotoStore 테스트] - Domain") | ||
class PhotoStoreTest { | ||
|
||
private static final String BUCKET_NAME = "bucket"; | ||
|
||
private final PhotoStore photoStore; | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
public PhotoStoreTest() { | ||
this.amazonS3Client = Mockito.mock(AmazonS3Client.class); | ||
this.photoStore = new S3PhotoStore(amazonS3Client, "bucket"); | ||
} | ||
|
||
@Nested | ||
@DisplayName("[이미지를 업로드한다]") | ||
class uploadFiles { | ||
|
||
@Test | ||
@DisplayName("[성공적으로 업로드한다]") | ||
void success() throws IOException { | ||
//given | ||
MultipartFile file = FileMockingUtils.createMockMultipartFile("hello1.jpg"); | ||
|
||
URL url = URI.create("http://localhost:8080/test/url").toURL(); | ||
given(amazonS3Client.getUrl(eq(BUCKET_NAME), anyString())) | ||
.willReturn(url); | ||
|
||
//when | ||
List<String> actual = photoStore.uploadFiles(List.of(file)); | ||
|
||
//then | ||
assertThat(actual).hasSize(1); | ||
assertThat(actual.get(0)).isEqualTo(url.toString()); | ||
} | ||
|
||
@Test | ||
@DisplayName("[이미지 업로드에 실패한다]") | ||
void fail() throws IOException { | ||
//given | ||
MultipartFile file = FileMockingUtils.createMockMultipartFile("hello1.jpg"); | ||
|
||
given(amazonS3Client.putObject(any(PutObjectRequest.class))) | ||
.willThrow(new WishHairException(ErrorCode.FILE_TRANSFER_EX)); | ||
|
||
//when | ||
ThrowingCallable when = () -> photoStore.uploadFiles(List.of(file)); | ||
|
||
//then | ||
assertThatThrownBy(when) | ||
.isInstanceOf(WishHairException.class) | ||
.hasMessageContaining(ErrorCode.FILE_TRANSFER_EX.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("[이미지를 삭제한다]") | ||
void fail() { | ||
//given | ||
String url = "http://localhost:8080/" + UUID.randomUUID(); | ||
|
||
//when | ||
boolean actual = photoStore.deleteFiles(List.of(url)); | ||
|
||
//then | ||
assertThat(actual).isTrue(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.