Skip to content

Commit

Permalink
[DOCS] add friend rest docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Oct 9, 2023
1 parent ce01d90 commit 6fccd26
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ out/

### VS Code ###
.vscode/

/src/main/generated/
21 changes: 21 additions & 0 deletions backend/src/docs/friend.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 4

== Friend
=== 친구 신청
operation::post request friend[snippets='http-request,http-response']

=== 친구 승락/거절/차단
operation::post update friend status[snippets='http-request,http-response']

=== 친구 검색
operation::get search friend[snippets='http-request,http-response']

=== 친구 전체 조회
operation::get all friends[snippets='http-request,http-response']

=== 친구 상태별 조회
operation::get friends by status[snippets='http-request,http-response']
1 change: 1 addition & 0 deletions backend/src/docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ include::path.adoc[]
include::plan.adoc[]
include::place.adoc[]
include::member.adoc[]
include::friend.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ResponseEntity<Void> addRequest(@RequestBody final FriendRequest friendRe
}

@PostMapping("deny")
public ResponseEntity<Void> deny(@RequestBody final FriendUpdateRequest friendUpdateRequest) {
public ResponseEntity<Void> updateStatus(@RequestBody final FriendUpdateRequest friendUpdateRequest) {
friendService.updateStatus(friendUpdateRequest);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.twtw.backend.domain.friend.controller;

import com.twtw.backend.domain.friend.dto.request.FriendRequest;
import com.twtw.backend.domain.friend.dto.request.FriendUpdateRequest;
import com.twtw.backend.domain.friend.dto.response.FriendResponse;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.friend.service.FriendService;
import com.twtw.backend.support.docs.RestDocsTest;
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.MediaType;
import org.springframework.test.web.servlet.ResultActions;

import java.util.List;
import java.util.UUID;

import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentRequest;
import static com.twtw.backend.support.docs.ApiDocsUtils.getDocumentResponse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
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;

@DisplayName("FriendController의")
@WebMvcTest(FriendController.class)
class FriendControllerTest extends RestDocsTest {
@MockBean private FriendService friendService;

@Test
@DisplayName("친구 전체 조회 API가 수행되는가")
void getFriends() throws Exception {
//given
final List<FriendResponse> expected = List.of(
new FriendResponse(UUID.randomUUID(), "정해진"),
new FriendResponse(UUID.randomUUID(), "주어진"));
given(friendService.getFriends()).willReturn(expected);

//when
final ResultActions perform =
mockMvc.perform(
get("/friends/all")
.contentType(MediaType.APPLICATION_JSON)
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

//then
perform.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray());

//docs
perform.andDo(print())
.andDo(document("get all friends", getDocumentRequest(), getDocumentResponse()));
}

@Test
@DisplayName("친구 상태별 조회 API가 수행되는가")
void getFriendsByStatus() throws Exception {
//given
final List<FriendResponse> expected = List.of(
new FriendResponse(UUID.randomUUID(), "호전"),
new FriendResponse(UUID.randomUUID(), "후진"));
given(friendService.getFriendsByStatus(any())).willReturn(expected);

//when
final ResultActions perform =
mockMvc.perform(
get("/friends?friendStatus=REQUESTED")
.contentType(MediaType.APPLICATION_JSON)
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

//then
perform.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray());

//docs
perform.andDo(print())
.andDo(document("get friends by status", getDocumentRequest(), getDocumentResponse()));
}

@Test
@DisplayName("친구 신청 API가 수행되는가")
void addRequest() throws Exception {
//given
willDoNothing().given(friendService).addRequest(any());

//when
final ResultActions perform =
mockMvc.perform(
post("/friends/request")
.contentType(MediaType.APPLICATION_JSON)
.content(toRequestBody(new FriendRequest(UUID.randomUUID())))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

//then
perform.andExpect(status().isNoContent());

//docs
perform.andDo(print())
.andDo(document("post request friend", getDocumentRequest(), getDocumentResponse()));
}

@Test
@DisplayName("친구 상태 업데이트 API가 수행되는가")
void updateStatus() throws Exception {
//given
willDoNothing().given(friendService).updateStatus(any());

//when
final ResultActions perform =
mockMvc.perform(
post("/friends/request")
.contentType(MediaType.APPLICATION_JSON)
.content(toRequestBody(new FriendUpdateRequest(UUID.randomUUID(), FriendStatus.ACCEPTED)))
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

//then
perform.andExpect(status().isNoContent());

//docs
perform.andDo(print())
.andDo(document("post update friend status", getDocumentRequest(), getDocumentResponse()));
}

@Test
@DisplayName("친구 이름으로 검색 API가 수행되는가")
void getFriendByName() throws Exception {
//given
final List<FriendResponse> expected = List.of(
new FriendResponse(UUID.randomUUID(), "호진정"),
new FriendResponse(UUID.randomUUID(), "진정해"));
given(friendService.getFriendByNickname(any())).willReturn(expected);

//when
final ResultActions perform =
mockMvc.perform(
get("/friends/search?nickname=hojin")
.contentType(MediaType.APPLICATION_JSON)
.header(
"Authorization",
"Bearer wefa3fsdczf32.gaoiuergf92.gb5hsa2jgh"));

//then
perform.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray());

//docs
perform.andDo(print())
.andDo(document("get search friend", getDocumentRequest(), getDocumentResponse()));
}
}

0 comments on commit 6fccd26

Please sign in to comment.