Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿงช [Unit Test] tournamentController ๋‹จ์œ„ ํ…Œ์ŠคํŠธ #522

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand All @@ -31,6 +32,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/pingpong/tournaments")
@Validated
public class TournamentController {

private final TournamentService tournamentService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package com.gg.server.domain.tournament.controller;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import javax.validation.ConstraintViolationException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;

import com.gg.server.domain.tournament.dto.TournamentFilterRequestDto;
import com.gg.server.domain.tournament.dto.TournamentGameListResponseDto;
import com.gg.server.domain.tournament.dto.TournamentListResponseDto;
import com.gg.server.domain.tournament.dto.TournamentResponseDto;
import com.gg.server.domain.tournament.dto.TournamentUserRegistrationResponseDto;
import com.gg.server.domain.tournament.service.TournamentService;
import com.gg.server.domain.user.dto.UserDto;
import com.gg.server.global.config.WebConfig;
import com.gg.server.global.security.config.SecurityConfig;
import com.gg.server.global.security.jwt.utils.TokenAuthenticationFilter;
import com.gg.server.global.utils.querytracker.LoggingInterceptor;
import com.gg.server.utils.annotation.UnitTest;

@UnitTest
@WebMvcTest(controllers = TournamentController.class, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = SecurityConfig.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebConfig.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = TokenAuthenticationFilter.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = LoggingInterceptor.class)})
class TournamentControllerMvcTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private TournamentService tournamentService;

@Autowired
private TournamentController tournamentController;

@Nested
@DisplayName("getAllTournamentList")
class GetAllTournamentList {

@DisplayName("ํŽ˜์ด์ง€๋ฒˆํ˜ธ๊ฐ€ 1๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ")
@ParameterizedTest()
@ValueSource(ints = {-1, 0})
void pageMustGreaterThanZero(Integer page) {
//Arrange
TournamentFilterRequestDto dto = new TournamentFilterRequestDto(page, 1, null, null);

//Act, Assert
assertThatThrownBy(() -> tournamentController.getAllTournamentList(dto))
.isInstanceOf(ConstraintViolationException.class);
}

@DisplayName("์‚ฌ์ด์ฆˆ๊ฐ€ 1๊ณผ 30 ์‚ฌ์ด๊ฐ€ ์•„๋‹๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ")
@ParameterizedTest()
@ValueSource(ints = {-1, 0, 31})
void sizeError(Integer size) {
//Arrange
TournamentFilterRequestDto dto = new TournamentFilterRequestDto(1, size, null, null);

//Act, Assert
assertThatThrownBy(() -> tournamentController.getAllTournamentList(dto))
.isInstanceOf(ConstraintViolationException.class);
}

@DisplayName("Success")
@Test
void success() {
//Arrange
TournamentFilterRequestDto dto = new TournamentFilterRequestDto(1, 1, null, null);
TournamentListResponseDto resultDto = Mockito.mock(TournamentListResponseDto.class);
when(tournamentService.getAllTournamentList(any(), any(), any())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentListResponseDto> response = tournamentController.getAllTournamentList(dto);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}

@Nested
@DisplayName("getUserStatusInTournament")
class GetUserStatusInTournament {
@DisplayName("Success")
@Test
void success() {
//Arrange
UserDto userDto = Mockito.mock(UserDto.class);
TournamentUserRegistrationResponseDto resultDto = Mockito.mock(TournamentUserRegistrationResponseDto.class);
when(tournamentService.getUserStatusInTournament(any(), any())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentUserRegistrationResponseDto> response = tournamentController
.getUserStatusInTournament(1L, userDto);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}

@Nested
@DisplayName("getTournnament")
class GetTournnament {
@DisplayName("id๊ฐ€ ์–‘์ˆ˜๊ฐ€ ์•„๋‹๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ")
@ParameterizedTest()
@ValueSource(longs = {-1, 0})
void idGreaterThanZero(Long id) {
//Act, Assert
assertThatThrownBy(() -> tournamentController.getTournnament(id))
.isInstanceOf(ConstraintViolationException.class);
}

@DisplayName("Success")
@Test
void success() {
//Arrange
TournamentResponseDto resultDto = Mockito.mock(TournamentResponseDto.class);
when(tournamentService.getTournament(anyLong())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentResponseDto> response = tournamentController.getTournnament(1L);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}

@Nested
@DisplayName("cancelTournamentUserRegistration")
class CancelTournamentUserRegistration {

@DisplayName("Success")
@Test
void success() {
//Arrange
UserDto userDto = Mockito.mock(UserDto.class);
TournamentUserRegistrationResponseDto resultDto = Mockito.mock(TournamentUserRegistrationResponseDto.class);
when(tournamentService.cancelTournamentUserRegistration(anyLong(), any())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentUserRegistrationResponseDto> response;
response = tournamentController.cancelTournamentUserRegistration(1L, userDto);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}

@Nested
@DisplayName("getTournamentGames")
class GetTournamentGames {
@DisplayName("id๊ฐ€ ์–‘์ˆ˜๊ฐ€ ์•„๋‹๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ")
@ParameterizedTest()
@ValueSource(longs = {-1, 0})
void idGreaterThanZero(Long id) {
//Act, Assert
assertThatThrownBy(() -> tournamentController.getTournamentGames(id))
.isInstanceOf(ConstraintViolationException.class);
}

@DisplayName("Success")
@Test
void success() {
//Arrange
TournamentGameListResponseDto resultDto = Mockito.mock(TournamentGameListResponseDto.class);
when(tournamentService.getTournamentGames(anyLong())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentGameListResponseDto> response = tournamentController.getTournamentGames(1L);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}

@Nested
@DisplayName("registerTournamentUser")
class RegisterTournamentUser {

@DisplayName("Success")
@Test
void success() {
//Arrange
UserDto userDto = Mockito.mock(UserDto.class);
TournamentUserRegistrationResponseDto resultDto = Mockito.mock(TournamentUserRegistrationResponseDto.class);
when(tournamentService.registerTournamentUser(anyLong(), any())).thenReturn(resultDto);

//Act
ResponseEntity<TournamentUserRegistrationResponseDto> response;
response = tournamentController.registerTournamentUser(1L, userDto);

//Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody()).isEqualTo(resultDto);
}
}
}
Loading