Skip to content

Commit

Permalink
test : #144 투표 마감 통합 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
PHS00 committed Nov 8, 2023
1 parent 45ba0bd commit 1e075e5
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/test/java/com/kakao/golajuma/vote/api/CloseVoteControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.kakao.golajuma.vote.api;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.kakao.golajuma.auth.domain.token.TokenProvider;
import com.kakao.golajuma.auth.infra.entity.UserEntity;
import com.kakao.golajuma.auth.infra.repository.UserRepository;
import com.kakao.golajuma.vote.infra.entity.Category;
import com.kakao.golajuma.vote.infra.entity.VoteEntity;
import com.kakao.golajuma.vote.infra.repository.VoteRepository;
import java.time.LocalDateTime;
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.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class CloseVoteControllerTest {

@Autowired private MockMvc mvc;
@Autowired private UserRepository userRepository;
@Autowired private TokenProvider tokenProvider;
@Autowired VoteRepository voteRepository;
private String jwtToken;

@BeforeEach
public void setup() throws Exception {
jwtToken = tokenProvider.createAccessToken(1L);
UserEntity user =
UserEntity.builder()
.id(1L)
.nickname("test")
.email("[email protected]")
.password("1234")
.build();
userRepository.save(user);
}

@DisplayName("투표 마감 성공")
@Test
public void closeVote_test() throws Exception {
// given
LocalDateTime date = LocalDateTime.now().plusMinutes(240);
VoteEntity vote =
VoteEntity.builder()
.voteTitle("title")
.category(Category.TOTAL)
.userId(1L)
.voteEndDate(date)
.build();

Long voteId = voteRepository.save(vote).getId();

// when
ResultActions resultActions =
mvc.perform(
patch("/votes/" + voteId + "/close").header("Authorization", "Bearer " + jwtToken));
// eye
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);

// then
resultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").hasJsonPath())
.andExpect(jsonPath("$.data").hasJsonPath());
}

@DisplayName("투표 마감 실패 - 존재하지 않는 투표")
@Test
public void closeVote_error_test_notFoundVote() throws Exception {
// given
Long voteId = -1L;

// when
ResultActions resultActions =
mvc.perform(
patch("/votes/" + voteId + "/close").header("Authorization", "Bearer " + jwtToken));
// eye
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);

// then
resultActions
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.message").hasJsonPath());
}

@DisplayName("투표 마감 실패 - 이미 마감된 투표")
@Test
public void closeVote_error_test_alreadyComplete() throws Exception {
// given
LocalDateTime date = LocalDateTime.now().minusMinutes(240);
VoteEntity vote =
VoteEntity.builder()
.voteTitle("title")
.category(Category.TOTAL)
.userId(1L)
.voteEndDate(date)
.build();

Long voteId = voteRepository.save(vote).getId();

// when
ResultActions resultActions =
mvc.perform(
patch("/votes/" + voteId + "/close").header("Authorization", "Bearer " + jwtToken));
// eye
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);

// then
resultActions
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.message").hasJsonPath());
}
}

0 comments on commit 1e075e5

Please sign in to comment.