Skip to content

Commit

Permalink
[#58] feat: Get Tags api 구현 및 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyoungchoi95 committed Oct 12, 2021
1 parent 6b236e0 commit a6b2ae1
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.study.realworld.tag.controller;

import com.study.realworld.tag.controller.response.TagsResponse;
import com.study.realworld.tag.service.TagService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api")
@RestController
public class TagController {

private final TagService tagService;

public TagController(TagService tagService) {
this.tagService = tagService;
}

@GetMapping("/tags")
public ResponseEntity<TagsResponse> getTags() {
return ResponseEntity.ok().body(TagsResponse.of(tagService.findAll()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.study.realworld.tag.controller.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.study.realworld.tag.domain.Tag;
import java.util.List;
import java.util.stream.Collectors;

public class TagsResponse {

@JsonProperty("tags")
private List<String> tags;

protected TagsResponse() {
}

public List<String> getTags() {
return tags;
}

private TagsResponse(List<String> tags) {
this.tags = tags;
}

public static TagsResponse of(List<Tag> tags) {
return new TagsResponse(
tags.stream()
.map(Tag::name)
.collect(Collectors.toList())
);
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/study/realworld/tag/domain/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public static Tag of(String name) {
return new Tag(name);
}

public String name() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.study.realworld.tag.controller;

import static com.study.realworld.user.controller.ApiDocumentUtils.getDocumentResponse;
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.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.study.realworld.tag.domain.Tag;
import com.study.realworld.tag.service.TagService;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@ExtendWith({MockitoExtension.class, RestDocumentationExtension.class})
class TagControllerTest {

@Mock
private TagService tagService;

@InjectMocks
private TagController tagController;

private MockMvc mockMvc;

@BeforeEach
void beforeEach(RestDocumentationContextProvider restDocumentationContextProvider) {
SecurityContextHolder.clearContext();
mockMvc = MockMvcBuilders.standaloneSetup(tagController)
.apply(documentationConfiguration(restDocumentationContextProvider))
.alwaysExpect(status().isOk())
.build();
}

@Test
void getTagsTest() throws Exception {

// setup
List<Tag> tags = Arrays.asList(Tag.of("reactjs"), Tag.of("angularjs"));
when(tagService.findAll()).thenReturn(tags);

// given
final String URL = "/api/tags";

// when
ResultActions resultActions = mockMvc.perform(get(URL)
.contentType(MediaType.APPLICATION_JSON))
.andDo(print());

// then
resultActions
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))

.andExpect(jsonPath("$.tags[0]", is("reactjs")))
.andExpect(jsonPath("$.tags[1]", is("angularjs")))
.andDo(document("tags",
getDocumentResponse(),
responseFields(
fieldWithPath("tags").type(JsonFieldType.ARRAY).description("tags array")
)
))
;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.study.realworld.tag.controller.response;

import org.junit.jupiter.api.Test;

class TagsResponseTest {

@Test
void tagsResonseTest() {
TagsResponse tagsResponse = new TagsResponse();
}

}

0 comments on commit a6b2ae1

Please sign in to comment.