Skip to content

Commit

Permalink
Merge pull request #123 from codesquad-member-2020/feature/BE-#112
Browse files Browse the repository at this point in the history
[BE-#112] Label 목록 API Test 및 REST Docs를 추가합니다.
  • Loading branch information
ksundong authored Jul 7, 2020
2 parents 8f66c29 + f8a8149 commit 8a053e0
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 7 deletions.
36 changes: 31 additions & 5 deletions BE/src/docs/asciidoc/api-docs.adoc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
= Issue Tracker API Docs
Dion[[email protected]]
1.0.0, 2020/09/06
1.0.9, 2020/09/06
:toc: left // 왼쪽 테이블
:sectnums: // 숫자매기기

== Issue 목록 API
== Issue 관련 API

=== GET issue List 열려있는 모든 이슈 가져오기

Expand Down Expand Up @@ -108,15 +108,15 @@ include::{snippets}/login-controller-test/j-w-t_없이_로그인_테스트/http-

==== CURL

include::{snippets}/login-controller-test/j-w-t_있을_떄_로그인_테스트/curl-request.adoc[]
include::{snippets}/login-controller-test/j-w-t_있을_때_로그인_테스트/curl-request.adoc[]

==== HTTP Request

include::{snippets}/login-controller-test/j-w-t_있을_떄_로그인_테스트/http-request.adoc[]
include::{snippets}/login-controller-test/j-w-t_있을_때_로그인_테스트/http-request.adoc[]

==== HTTP RESPONSE

include::{snippets}/login-controller-test/j-w-t_있을_떄_로그인_테스트/http-response.adoc[]
include::{snippets}/login-controller-test/j-w-t_있을_때_로그인_테스트/http-response.adoc[]

=== GET OAuth 요청이 들어오는 경우(Github에서만 사용)

Expand All @@ -131,3 +131,29 @@ include::{snippets}/login-controller-test/o-auth-redirect-test/http-request.adoc
==== HTTP RESPONSE

include::{snippets}/login-controller-test/o-auth-redirect-test/http-response.adoc[]

== Label 관련 API

=== GET label List 검색조건에 해당하는 label 목록가져오기

==== CURL

include::{snippets}/label-controller-test/label_목록_조회_테스트/curl-request.adoc[]

==== HTTP Request

include::{snippets}/label-controller-test/label_목록_조회_테스트/http-request.adoc[]

==== HTTP Query Parameter

?title='검색어' 와 같은 형식으로 요청을 보내면 됩니다.

include::{snippets}/label-controller-test/label_목록_조회_테스트/request-parameters.adoc[]

==== HTTP RESPONSE

include::{snippets}/label-controller-test/label_목록_조회_테스트/http-response.adoc[]

==== Response Fields

include::{snippets}/label-controller-test/label_목록_조회_테스트/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import kr.codesquad.issuetracker.domain.issue.Issue;
import kr.codesquad.issuetracker.domain.relation.IssueLabel;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -15,7 +16,7 @@ public class LabelOfLabelList {
private String title;
private String color;
private String description;
private long openedIssueCount; // 최적화 필요. Label마다 쿼리가 날아감.
private Long openedIssueCount; // 최적화 필요. Label마다 쿼리가 날아감.

public LabelOfLabelList(Label label) {
this.id = label.getId();
Expand All @@ -28,4 +29,14 @@ public LabelOfLabelList(Label label) {
.filter(Issue::isOpened)
.count();
}

@Builder
public LabelOfLabelList(Long id, String title, String color, String description,
long openedIssueCount) {
this.id = id;
this.title = title;
this.color = color;
this.description = description;
this.openedIssueCount = openedIssueCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package kr.codesquad.issuetracker.controller;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.Cookie;
import kr.codesquad.issuetracker.domain.label.LabelOfLabelList;
import kr.codesquad.issuetracker.domain.user.User;
import kr.codesquad.issuetracker.domain.user.UserDTO;
import kr.codesquad.issuetracker.service.JwtService;
import kr.codesquad.issuetracker.service.LabelService;
import kr.codesquad.issuetracker.service.UserService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.web.servlet.MockMvc;

@AutoConfigureRestDocs(uriHost = "13.124.148.192/api", uriPort = 80)
@WebMvcTest(controllers = {LabelController.class})
class LabelControllerTest {

@Autowired
MockMvc mockMvc;

@MockBean
LabelService labelService;

@MockBean
UserService userService;

@MockBean
JwtService jwtService;

String jwt = "jwt";
UserDTO userDTO = UserDTO
.of(User.builder().id(1L).userId("jwtUser").email("[email protected]").nickname("jwt").build());

public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@BeforeEach
void setUp() {
when(jwtService.getUserFromJws(jwt)).thenReturn(userDTO);
}

@Test
@DisplayName("Label 목록 조회 테스트")
void label_목록_조회_테스트() throws Exception {
// given
LabelOfLabelList labelDTO1 = LabelOfLabelList.builder().id(1L).title("Help").color("#123456")
.description("도와주세요.").openedIssueCount(3).build();
LabelOfLabelList labelDTO2 = LabelOfLabelList.builder().id(2L).title("Test").color("#443322")
.description("테스트코드").openedIssueCount(4).build();

List<LabelOfLabelList> labels = Arrays.asList(labelDTO1, labelDTO2);
when(labelService.findLabelsByQueryString(null)).thenReturn(labels);
// then
mockMvc.perform(
get("/labels").contentType(MediaType.APPLICATION_JSON).cookie(new Cookie("jwt", jwt)))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.labels", hasSize(labels.size())))
.andExpect(jsonPath("$.labels[0].id").value(Matchers.anyOf(
Matchers.equalTo((Number) labels.get(0).getId()),
Matchers.equalTo(labels.get(0).getId().intValue()))))
.andExpect(jsonPath("$.labels[0].title", is(labels.get(0).getTitle())))
.andExpect(jsonPath("$.labels[0].color", is(labels.get(0).getColor())))
.andExpect(jsonPath("$.labels[0].description", is(labels.get(0).getDescription())))
.andExpect(jsonPath("$.labels[0].openedIssueCount").value(Matchers.anyOf(
Matchers.equalTo(labels.get(0).getOpenedIssueCount()),
Matchers.equalTo(labels.get(0).getOpenedIssueCount().intValue()))))
.andExpect(jsonPath("$.labels[1].id").value(Matchers.anyOf(
Matchers.equalTo((Number) labels.get(1).getId()),
Matchers.equalTo(labels.get(1).getId().intValue()))))
.andExpect(jsonPath("$.labels[1].title", is(labels.get(1).getTitle())))
.andExpect(jsonPath("$.labels[1].color", is(labels.get(1).getColor())))
.andExpect(jsonPath("$.labels[1].description", is(labels.get(1).getDescription())))
.andExpect(jsonPath("$.labels[1].openedIssueCount").value(Matchers.anyOf(
Matchers.equalTo(labels.get(1).getOpenedIssueCount()),
Matchers.equalTo(labels.get(1).getOpenedIssueCount().intValue()))))
.andDo(document("{class-name}/{method-name}",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestParameters(parameterWithName("title").optional().description("검색할 라벨의 title")),
responseFields(
fieldWithPath("labels").description("라벨의 목록").type(JsonFieldType.ARRAY),
fieldWithPath("labels[].id").description("라벨의 고유 id").type(JsonFieldType.NUMBER),
fieldWithPath("labels[].title").description("라벨의 표기명").type(JsonFieldType.STRING),
fieldWithPath("labels[].color").description("라벨의 배경 컬러(Hex Code)")
.type(JsonFieldType.STRING),
fieldWithPath("labels[].description").description("라벨 설명")
.type(JsonFieldType.STRING),
fieldWithPath("labels[].openedIssueCount")
.description("라벨에 속한 Issue중 열려있는 Issue의 개수").type(JsonFieldType.NUMBER)
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LoginControllerTest {

@Test
@DisplayName("JWT 있을 때 로그인 테스트")
void JWT_있을__로그인_테스트() throws Exception {
void JWT_있을__로그인_테스트() throws Exception {
// given
String jwt = "jwt";
UserDTO userDTO = UserDTO.of("1", "test", "test", "[email protected]");
Expand Down

0 comments on commit 8a053e0

Please sign in to comment.