Skip to content

Commit

Permalink
test: ProfileImageControllerTest 픽스처 추가 및 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Oct 1, 2023
1 parent 53b9123 commit a51de17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.ddang.ddang.image.presentation;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.ddang.ddang.configuration.CommonControllerSliceTest;
import com.ddang.ddang.exception.GlobalExceptionHandler;
import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import java.net.MalformedURLException;
import com.ddang.ddang.image.presentation.fixture.ProfileImageControllerFixture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.net.MalformedURLException;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SuppressWarnings("NonAsciiCharacters")
class ProfileImageControllerTest extends CommonControllerSliceTest {
class ProfileImageControllerTest extends ProfileImageControllerFixture {

MockMvc mockMvc;

Expand All @@ -36,85 +35,67 @@ void setUp() {
@Test
void 지정한_사용자_아이디에_대한_사용자_이미지를_조회한다() throws Exception {
// given
final byte[] imageBytes = "이것은 이미지 파일의 바이트 코드입니다.".getBytes();
final Resource mockResource = new ByteArrayResource(imageBytes);

given(imageService.readProfileImage(anyLong())).willReturn(mockResource);
given(imageService.readProfileImage(anyLong())).willReturn(이미지_파일_리소스);

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", 1L))
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", 프로필_이미지_아이디))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.IMAGE_JPEG))
.andExpect(content().bytes(imageBytes));
.andExpect(content().bytes(이미지_파일_바이트));
}

@Test
void 사용자_이미지_조회시_지정한_아이디에_대한_이미지가_없는_경우_404를_반환한다() throws Exception {
// given
final Long invalidAuctionImageId = 1L;

given(imageService.readProfileImage(anyLong())).willThrow(new ImageNotFoundException(
"지정한 이미지를 찾을 수 없습니다."
));
given(imageService.readProfileImage(anyLong())).willThrow(new ImageNotFoundException("지정한 이미지를 찾을 수 없습니다."));

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", invalidAuctionImageId))
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", 존재하지_않는_프로필_이미지_아이디))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").exists());
}

@Test
void 사용자_이미지_조회시_유효한_프로토콜이나_URL이_아닌_경우_500을_반환한다() throws Exception {
// given
final Long invalidAuctionImageId = 1L;

given(imageService.readProfileImage(anyLong())).willThrow(new MalformedURLException());

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", invalidAuctionImageId))
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/images/{id}", 프로필_이미지_아이디))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").exists());
}

@Test
void 지정한_아이디에_대한_경매_이미지를_조회한다() throws Exception {
// given
final byte[] imageBytes = "이것은 이미지 파일의 바이트 코드입니다.".getBytes();
final Resource mockResource = new ByteArrayResource(imageBytes);

given(imageService.readAuctionImage(anyLong())).willReturn(mockResource);
given(imageService.readAuctionImage(anyLong())).willReturn(이미지_파일_리소스);

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", 1L))
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", 경매_이미지_아이디))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.IMAGE_JPEG))
.andExpect(content().bytes(imageBytes));
.andExpect(content().bytes(이미지_파일_바이트));
}

@Test
void 경매_이미지_조회시_지정한_아이디에_대한_이미지가_없는_경우_404를_반환한다() throws Exception {
// given
final Long invalidAuctionImageId = 1L;

given(imageService.readAuctionImage(anyLong())).willThrow(new ImageNotFoundException(
"지정한 이미지를 찾을 수 없습니다."
));
given(imageService.readAuctionImage(anyLong())).willThrow(new ImageNotFoundException("지정한 이미지를 찾을 수 없습니다."));

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", invalidAuctionImageId))
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", 존재하지_않는_경매_이미지_아이디))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").exists());
}

@Test
void 경매_이미지_조회시_유효한_프로토콜이나_URL이_아닌_경우_500을_반환한다() throws Exception {
// given
final Long invalidAuctionImageId = 1L;

given(imageService.readAuctionImage(anyLong())).willThrow(new MalformedURLException());

// when & then
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", invalidAuctionImageId))
mockMvc.perform(RestDocumentationRequestBuilders.get("/auctions/images/{id}", 경매_이미지_아이디))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").exists());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ddang.ddang.image.presentation.fixture;

import com.ddang.ddang.configuration.CommonControllerSliceTest;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

@SuppressWarnings("NonAsciiCharacters")
public class ProfileImageControllerFixture extends CommonControllerSliceTest {

protected byte[] 이미지_파일_바이트 = "이것은 이미지 파일의 바이트 코드입니다.".getBytes();
protected Resource 이미지_파일_리소스 = new ByteArrayResource(이미지_파일_바이트);
protected Long 프로필_이미지_아이디 = 1L;
protected Long 존재하지_않는_프로필_이미지_아이디 = -999L;
protected Long 경매_이미지_아이디 = 1L;
protected Long 존재하지_않는_경매_이미지_아이디 = -999L;
}

0 comments on commit a51de17

Please sign in to comment.