-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b236e0
commit a6b2ae1
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/study/realworld/tag/controller/TagController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/study/realworld/tag/controller/response/TagsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/test/java/com/study/realworld/tag/controller/TagControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) | ||
)) | ||
; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/test/java/com/study/realworld/tag/controller/response/TagsResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |