Skip to content

Commit

Permalink
test : RestDocs 수정중
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Sep 11, 2023
1 parent 55e1a91 commit 3b9eac6
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ public record TopicCreateRequest(
PermissionType permissionType,
List<Long> pins
) {

public static TopicCreateRequest of(
TopicCreateRequestWithOutImage request,
MultipartFile image
) {
return new TopicCreateRequest(
request.name(),
image,
request.description(),
request.publicity(),
request.permissionType(),
request.pins()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mapbefine.mapbefine.topic.dto.request;

import com.mapbefine.mapbefine.topic.domain.PermissionType;
import com.mapbefine.mapbefine.topic.domain.Publicity;
import java.util.List;

public record TopicCreateRequestWithOutImage(
String name,
String description,
Publicity publicity,
PermissionType permissionType,
List<Long> pins
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mapbefine.mapbefine.topic.application.TopicCommandService;
import com.mapbefine.mapbefine.topic.application.TopicQueryService;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequestWithOutImage;
import com.mapbefine.mapbefine.topic.dto.request.TopicMergeRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicUpdateRequest;
import com.mapbefine.mapbefine.topic.dto.response.TopicDetailResponse;
Expand All @@ -22,6 +23,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/topics")
Expand All @@ -40,8 +42,15 @@ public TopicController(

@LoginRequired
@PostMapping("/new")
public ResponseEntity<Void> create(AuthMember member, @RequestPart TopicCreateRequest request) {
Long topicId = topicCommandService.saveTopic(member, request);
public ResponseEntity<Void> create(
AuthMember member,
@RequestPart TopicCreateRequestWithOutImage request,
@RequestPart MultipartFile image
) {
System.out.println(request);
System.out.println(image);
TopicCreateRequest topicCreateRequest = TopicCreateRequest.of(request, image);
Long topicId = topicCommandService.saveTopic(member, topicCreateRequest);

return ResponseEntity.created(URI.create("/topics/" + topicId))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,15 @@ void addImage_Success() {
}

private ExtractableResponse<Response> createPinImage(long pinId) {
String imageFilePath = getClass().getClassLoader().getResource("test.png").getPath();
String imageFilePath = getClass().getClassLoader()
.getResource("test.png")
.getPath();
File mockFile = new File(imageFilePath);

return RestAssured.given().log().all()
.header(AUTHORIZATION, authHeader)
.multiPart(
"image",
mockFile,
MediaType.MULTIPART_FORM_DATA_VALUE
)
.multiPart(
"pinId",
pinId,
MediaType.APPLICATION_JSON_VALUE
)
.multiPart("image", mockFile, MediaType.MULTIPART_FORM_DATA_VALUE)
.multiPart("pinId", pinId, MediaType.APPLICATION_JSON_VALUE)
.when().post("/pins/images")
.then().log().all()
.extract();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;

Expand All @@ -16,6 +17,7 @@
import com.mapbefine.mapbefine.pin.dto.response.PinDetailResponse;
import com.mapbefine.mapbefine.pin.dto.response.PinImageResponse;
import com.mapbefine.mapbefine.pin.dto.response.PinResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -147,17 +149,22 @@ void findAll() throws Exception {
@Test
@DisplayName("핀 이미지 추가")
void addImage() throws Exception {
PinImageCreateRequest pinImageCreateRequest = new PinImageCreateRequest(
1L,
FileFixture.createFile()
// "https://map-befine-official.github.io/favicon.png"
);
String imageFilePath = getClass().getClassLoader()
.getResource("test.png")
.getPath();
File mockFile = new File(imageFilePath);

// PinImageCreateRequest pinImageCreateRequest = new PinImageCreateRequest(
// 1L,
// FileFixture.createFile()
// );

mockMvc.perform(
MockMvcRequestBuilders.post("/pins/images")
.header(AUTHORIZATION, testAuthHeaderProvider.createAuthHeaderById(1L))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(pinImageCreateRequest))
.content(objectMapper.writeValueAsString(1L))
.content(objectMapper.writeValueAsString(mockFile))
).andDo(restDocs.document());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.mapbefine.mapbefine.topic.domain.Topic;
import com.mapbefine.mapbefine.topic.domain.TopicRepository;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequestWithOutImage;
import com.mapbefine.mapbefine.topic.dto.request.TopicMergeRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicUpdateRequest;
import com.mapbefine.mapbefine.topic.dto.response.TopicDetailResponse;
import com.mapbefine.mapbefine.topic.dto.response.TopicResponse;
import io.restassured.*;
import io.restassured.response.*;
import java.io.File;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,6 +38,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

class TopicIntegrationTest extends IntegrationTest {

Expand Down Expand Up @@ -71,9 +74,8 @@ void setMember() {
@Test
@DisplayName("Pin 목록 없이 Topic을 생성하면 201을 반환한다")
void createNewTopicWithoutPins_Success() {
TopicCreateRequest 준팍의_또간집 = new TopicCreateRequest(
TopicCreateRequestWithOutImage 준팍의_또간집 = new TopicCreateRequestWithOutImage(
"준팍의 또간집",
FileFixture.createFile(),
"준팍이 2번 이상 간집 ",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand All @@ -88,12 +90,24 @@ void createNewTopicWithoutPins_Success() {
assertThat(response.header("Location")).isNotBlank();
}

private ExtractableResponse<Response> createNewTopic(TopicCreateRequest request, String authHeader) {
private ExtractableResponse<Response> createNewTopic(TopicCreateRequestWithOutImage request, String authHeader) {
String imageFilePath = getClass().getClassLoader()
.getResource("test.png")
.getPath();
File mockFile = new File(imageFilePath);

// MockMultipartFile mockFile = new MockMultipartFile( // 이것은 왜 그런 것일까??
// "test",
// "test.png",
// "image/png",
// "byteCode".getBytes()
// );

return RestAssured.given()
.log().all()
.header(AUTHORIZATION, authHeader)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.body(request)
.multiPart("image", mockFile, MediaType.MULTIPART_FORM_DATA_VALUE)
.multiPart("request", request, MediaType.APPLICATION_JSON_VALUE)
.when().post("/topics/new")
.then().log().all()
.extract();
Expand All @@ -110,9 +124,8 @@ void createNewTopicWithPins_Success() {
.map(Pin::getId)
.toList();

TopicCreateRequest 준팍의_또간집 = new TopicCreateRequest(
TopicCreateRequestWithOutImage 준팍의_또간집 = new TopicCreateRequestWithOutImage(
"준팍의 또간집",
FileFixture.createFile(),
"준팍이 2번 이상 간집 ",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand All @@ -131,17 +144,15 @@ void createNewTopicWithPins_Success() {
@DisplayName("여러개의 토픽을 병합하면 201을 반환한다")
void createMergeTopic_Success() {
// given
TopicCreateRequest 준팍의_또간집 = new TopicCreateRequest(
TopicCreateRequestWithOutImage 준팍의_또간집 = new TopicCreateRequestWithOutImage(
"준팍의 또간집",
FileFixture.createFile(),
"준팍이 2번 이상 간집 ",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Collections.emptyList()
);
TopicCreateRequest 준팍의_또안간집 = new TopicCreateRequest(
TopicCreateRequestWithOutImage 준팍의_또안간집 = new TopicCreateRequestWithOutImage(
"준팍의 또안간집",
FileFixture.createFile(),
"준팍이 2번 이상 안간집 ",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand Down Expand Up @@ -181,9 +192,8 @@ void createMergeTopic_Success() {
@DisplayName("Topic을 수정하면 200을 반환한다")
void updateTopic_Success() {
ExtractableResponse<Response> newTopic = createNewTopic(
new TopicCreateRequest(
new TopicCreateRequestWithOutImage(
"준팍의 또간집",
FileFixture.createFile(),
"준팍이 두번 간집",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand Down Expand Up @@ -218,9 +228,8 @@ void updateTopic_Success() {
@DisplayName("Topic을 삭제하면 204를 반환한다")
void deleteTopic_Success() {
ExtractableResponse<Response> newTopic = createNewTopic(
new TopicCreateRequest(
new TopicCreateRequestWithOutImage(
"준팍의 또간집",
FileFixture.createFile(),
"준팍이 두번 간집 ",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand Down Expand Up @@ -263,9 +272,8 @@ void findTopics_Success() {
@DisplayName("Topic 상세 정보를 조회하면 200을 반환한다")
void findTopicDetail_Success() {
//given
TopicCreateRequest request = new TopicCreateRequest(
TopicCreateRequestWithOutImage request = new TopicCreateRequestWithOutImage(
"topicName",
FileFixture.createFile(),
"description",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand All @@ -292,9 +300,8 @@ void findTopicDetail_Success() {
@DisplayName("Topic 상세 정보 여러개를 조회하면 200을 반환한다")
void findTopicDetailsByIds_Success() {
//given
TopicCreateRequest request = new TopicCreateRequest(
TopicCreateRequestWithOutImage request = new TopicCreateRequestWithOutImage(
"topicName",
FileFixture.createFile(),
"description",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

import com.mapbefine.mapbefine.FileFixture;
import com.mapbefine.mapbefine.common.RestDocsIntegration;
Expand All @@ -12,17 +13,28 @@
import com.mapbefine.mapbefine.topic.domain.PermissionType;
import com.mapbefine.mapbefine.topic.domain.Publicity;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequestWithOutImage;
import com.mapbefine.mapbefine.topic.dto.request.TopicMergeRequest;
import com.mapbefine.mapbefine.topic.dto.request.TopicUpdateRequest;
import com.mapbefine.mapbefine.topic.dto.response.TopicDetailResponse;
import com.mapbefine.mapbefine.topic.dto.response.TopicResponse;
import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

class TopicControllerTest extends RestDocsIntegration { // TODO: 2023/07/25 Image 칼람 추가됨으로 인해 수정 필요

Expand Down Expand Up @@ -58,21 +70,25 @@ class TopicControllerTest extends RestDocsIntegration { // TODO: 2023/07/25 Imag
@DisplayName("토픽 새로 생성")
void create() throws Exception {
given(topicCommandService.saveTopic(any(), any())).willReturn(1L);
File mockFile = new File(getClass().getClassLoader().getResource("test.png").getPath());
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();

TopicCreateRequest topicCreateRequest = new TopicCreateRequest(
TopicCreateRequestWithOutImage request = new TopicCreateRequestWithOutImage(
"준팍의 안갈집",
FileFixture.createFile(),
"준팍이 두번 다시 안갈집",
Publicity.PUBLIC,
PermissionType.ALL_MEMBERS,
List.of(1L, 2L, 3L)
);

param.add("image", mockFile);
param.add("request", request);

mockMvc.perform(
MockMvcRequestBuilders.post("/topics/new")
.header(AUTHORIZATION, testAuthHeaderProvider.createAuthHeaderById(1L))
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(topicCreateRequest))
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.content(objectMapper.writeValueAsString(mockFile))
).andDo(restDocs.document());
}

Expand Down

0 comments on commit 3b9eac6

Please sign in to comment.