Skip to content

Commit

Permalink
Merge pull request #37 from depromeet/feature/#36
Browse files Browse the repository at this point in the history
[test][#36] 카드 메모 api 테스트 코드 작성
  • Loading branch information
sejoon00 authored Aug 9, 2024
2 parents 8fbe954 + c522423 commit 00c433b
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ResponseEntity<CardMemoCreateResponse> createCardMemo(
return ResponseEntity.ok(cardMemoService.createCardMemo(user, request, cardId));
}

@PutMapping("/{card-id}/card-momo/{card-memo-id}/content")
@PutMapping("/{card-id}/card-memo/{card-memo-id}/content")
public ResponseEntity<Void> updateCardMemoContent(
@AuthUser User user,
@PathVariable("card-id") Long cardId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.server.bbo_gak.domain.card.controller;

import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.epages.restdocs.apispec.Schema;
import com.server.bbo_gak.domain.card.dao.CardMemoRepository;
import com.server.bbo_gak.domain.card.dto.request.CardMemoContentUpdateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardMemoCreateRequest;
import com.server.bbo_gak.domain.card.dto.response.CardMemoCreateResponse;
import com.server.bbo_gak.global.AbstractRestDocsTests;
import com.server.bbo_gak.global.RestDocsFactory;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@ActiveProfiles("test")
@Sql("/card-test-data.sql")
public class CardMemoControllerTest extends AbstractRestDocsTests {

private static final String DEFAULT_URL = "/api/v1/cards";

private final String cardMemoTag = "CardMemo";

@Autowired
private RestDocsFactory restDocsFactory;

@Autowired
private CardMemoRepository cardMemoRepository;

@Nested
@Transactional
class 카드_메모_생성 {

@Test
public void 성공() throws Exception {

CardMemoCreateRequest request = new CardMemoCreateRequest("test content");

mockMvc.perform(
restDocsFactory.createRequest(DEFAULT_URL + "/{card-id}/card-memo", request, HttpMethod.POST,
objectMapper, 1))
.andExpect(status().isOk())
.andDo(result -> restDocsFactory.getSuccessResource("[카드_메모_생성] 성공", "카드_메모_생성",
cardMemoTag, request,
objectMapper.readValue(result.getResponse().getContentAsString(), CardMemoCreateResponse.class))
.handle(result));

assertTrue(cardMemoRepository.findById(1L).isPresent());
}


@Test
public void 카드_찾기_실패() throws Exception {

CardMemoCreateRequest request = new CardMemoCreateRequest("test content");

mockMvc.perform(
restDocsFactory.createRequest(DEFAULT_URL + "/{card-id}/card-memo", request, HttpMethod.POST,
objectMapper, 999))
.andExpect(status().isNotFound())
.andDo(restDocsFactory.getFailureResource("[카드_메모_생성] 카드_찾기_실패", cardMemoTag, request));
}
}

@Nested
@Transactional
class 카드_메모_조회 {

@Test
public void 성공() throws Exception {

mockMvc.perform(getRequest(1L))
.andExpect(status().isOk())
.andDo(document("[카드_메모_조회] 성공", resource(getSuccessResponseResource())));
}

private MockHttpServletRequestBuilder getRequest(Long givenCardId) {
return get(DEFAULT_URL + "/{card-id}/card-memo", givenCardId)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
}

private ResourceSnippetParameters getSuccessResponseResource() {
return ResourceSnippetParameters.builder()
.description("카드_메모_조회").tags(cardMemoTag)
.responseSchema(Schema.schema(CardMemoCreateResponse.class.getName()))
.pathParameters(parameterWithName("card-id").description("card-id"))
.build();
}
}

@Nested
@Transactional
class 카드_메모_내용_수정 {

@Test
public void 성공() throws Exception {

String updatedContent = "updated contents";
CardMemoContentUpdateRequest request = new CardMemoContentUpdateRequest(updatedContent);

mockMvc.perform(getRequest(1L, request))
.andExpect(status().isOk())
.andDo(document("[카드_메모_내용_수정] 성공", resource(getSuccessResponseResource())));

assertTrue(cardMemoRepository.findById(1L).get().getContent().equals(updatedContent));

}

private MockHttpServletRequestBuilder getRequest(Long givenCardMemoId, CardMemoContentUpdateRequest request)
throws Exception {

return put(DEFAULT_URL + "/{card-id}/card-memo/{card-memo-id}/content", 1L, givenCardMemoId)
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
}

private ResourceSnippetParameters getSuccessResponseResource() {
return ResourceSnippetParameters.builder()
.description("카드_메모_조회").tags(cardMemoTag)
.responseSchema(Schema.schema(CardMemoCreateResponse.class.getName()))
.pathParameters(
parameterWithName("card-id").description("card-id"),
parameterWithName("card-memo-id").description("card-memo-id"))
.build();
}
}

@Nested
@Transactional
class 카드_메모_삭제 {

@Test
public void 성공() throws Exception {

long givenCardMemoId = 1L;

mockMvc.perform(getRequest(givenCardMemoId))
.andExpect(status().isOk())
.andDo(document("[카드_메모_삭제] 성공", resource(getSuccessResponseResource())));
}

private MockHttpServletRequestBuilder getRequest(Long givenCardMemoId) {
return delete(DEFAULT_URL + "/{card-id}/card-memo/{card-memo-id}", 1L, givenCardMemoId)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
}

private ResourceSnippetParameters getSuccessResponseResource() {
return ResourceSnippetParameters.builder()
.description("카드_메모_삭제").tags(cardMemoTag)
.pathParameters(
parameterWithName("card-id").description("card-id"),
parameterWithName("card-memo-id").description("card-memo-id"))
.build();
}
}
}
15 changes: 15 additions & 0 deletions src/test/resources/card-test-data.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
delete
from card_memo;
delete
from card_tag;
delete
from tag;
Expand Down Expand Up @@ -49,3 +51,16 @@ INSERT INTO card_tag (deleted, card_id, card_tag_id, created_at, tag_id, update_
VALUES (false, 1, 2, '2024-07-24 21:26:29.000000', 2, '2024-07-24 21:26:41.000000');
INSERT INTO card_tag (deleted, card_id, card_tag_id, created_at, tag_id, update_at)
VALUES (false, 2, 3, '2024-07-24 21:26:31.000000', 2, '2024-07-24 21:26:42.000000');

INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 1, 1, '2024-07-24 21:26:31.000000', 'test contents 111', '2024-07-24 21:26:42.000000');
INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 1, 2, '2024-07-24 21:26:31.000000', 'test contents 222', '2024-07-24 21:26:42.000000');
INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 1, 3, '2024-07-24 21:26:31.000000', 'test contents 222', '2024-07-24 21:26:42.000000');
INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 2, 4, '2024-07-24 21:26:31.000000', 'test contents 111', '2024-07-24 21:26:42.000000');
INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 2, 5, '2024-07-24 21:26:31.000000', 'test contents 222', '2024-07-24 21:26:42.000000');
INSERT INTO card_memo (deleted, card_id, card_memo_id, created_at, content, update_at)
VALUES (false, 3, 6, '2024-07-24 21:26:31.000000', 'test contents 333', '2024-07-24 21:26:42.000000');

0 comments on commit 00c433b

Please sign in to comment.