Skip to content

Commit

Permalink
[TEST] 랜덤 주제 조회 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Nov 7, 2023
1 parent dd8cbca commit 02570af
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public class TopicController {
private final TopicService topicService;

@Operation(summary = "랜덤 주제 조회", description = "랜덤 주제를 임의로 조회합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(
responseCode = "200",
description = "랜덤 주제 조회",
content = @Content(schema = @Schema(implementation = TopicResponseDTO.class)))
})
@GetMapping("/random")
public ResponseEntity<ApiResponse> getRandom() {
TopicResponseDTO response = topicService.getRandom();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import com.smeme.server.model.topic.Topic;

import io.swagger.v3.oas.annotations.media.Schema;

public record TopicResponseDTO(
@Schema(description = "랜덤 주제 id", example = "1")
Long topicId,
@Schema(description = "랜덤 주제 내용", example = "가보고 싶은 해외 여행지가 있다면 소개해 주세요!")
String content
) {
public static TopicResponseDTO of(Topic topic) {
Expand Down
52 changes: 52 additions & 0 deletions src/main/resources/static/docs/open-api-3.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@
}
}
}
},
"/api/v2/topics/random" : {
"get" : {
"tags" : [ "Topic" ],
"summary" : "랜덤 주제 조회",
"description" : "랜덤 주제 조회",
"operationId" : "랜덤 주제 조회 성공 Example",
"responses" : {
"200" : {
"description" : "200",
"content" : {
"application/json;charset=UTF-8" : {
"schema" : {
"$ref" : "#/components/schemas/api-v2-topics-random459952356"
},
"examples" : {
"랜덤 주제 조회 성공 Example" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"랜덤 주제 조회 성공\",\n \"data\" : {\n \"topicId\" : 1,\n \"content\" : \"가보고 싶은 해외 여행 지가 있다면 소개해 주세요!\"\n }\n}"
}
}
}
}
}
}
}
}
},
"components" : {
Expand Down Expand Up @@ -545,6 +570,33 @@
"description" : "응답 메시지"
}
}
},
"api-v2-topics-random459952356" : {
"type" : "object",
"properties" : {
"data" : {
"type" : "object",
"properties" : {
"topicId" : {
"type" : "number",
"description" : "랜덤 주제 id"
},
"content" : {
"type" : "string",
"description" : "랜덤 주제 내용"
}
},
"description" : "응답 데이터"
},
"success" : {
"type" : "boolean",
"description" : "응답 성공 여부"
},
"message" : {
"type" : "string",
"description" : "응답 메시지"
}
}
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/smeme/server/controller/TopicControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.smeme.server.controller;

import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.smeme.server.dto.topic.TopicResponseDTO;
import com.smeme.server.util.ApiResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.ResponseEntity;

import java.security.Principal;

import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static com.smeme.server.util.ApiResponse.success;
import static org.mockito.Mockito.when;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.JsonFieldType.*;
import static org.springframework.restdocs.payload.JsonFieldType.STRING;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@DisplayName("TopicController 테스트")
@WebMvcTest(TopicController.class)
class TopicControllerTest extends BaseControllerTest {
@MockBean
TopicController topicController;
@MockBean
Principal principal;

private final String DEFAULT_URL = "/api/v2/topics";
private final String TAG = "Topic";

@Test
@DisplayName("랜덤 주제 조회 테스트")
void success_get_random_topic_test() throws Exception {
// given
TopicResponseDTO response = new TopicResponseDTO(1L, "가보고 싶은 해외 여행 지가 있다면 소개해 주세요!");
ResponseEntity<ApiResponse> result = ResponseEntity.ok(success("랜덤 주제 조회 성공", response));

// when
when(topicController.getRandom()).thenReturn(result);

// then
mockMvc.perform(get(DEFAULT_URL + "/random")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.principal(principal))
.andDo(
document("랜덤 주제 조회 성공 Example",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
resource(ResourceSnippetParameters.builder()
.tag(TAG)
.description("랜덤 주제 조회")
.responseFields(
fieldWithPath("success").type(BOOLEAN).description("응답 성공 여부"),
fieldWithPath("message").type(STRING).description("응답 메시지"),
fieldWithPath("data").type(OBJECT).description("응답 데이터"),
fieldWithPath("data.topicId").type(NUMBER).description("랜덤 주제 id"),
fieldWithPath("data.content").type(STRING).description("랜덤 주제 내용")
)
.build()
)
))
.andExpect(status().isOk());
}
}

0 comments on commit 02570af

Please sign in to comment.