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

๐Ÿงช [Test] ํŒจ๋„ํ‹ฐ ๊ด€๋ จ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ #767

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public ResponseEntity<PartyPenaltyListAdminResDto> penaltyList(@ModelAttribute @
*/
@PatchMapping("/{penaltyId}")
public ResponseEntity<Void> modifyAdminPenalty(@PathVariable Long penaltyId,
@RequestBody PartyPenaltyAdminReqDto reqDto) {
@RequestBody @Valid PartyPenaltyAdminReqDto reqDto) {
partyPenaltyAdminService.modifyAdminPenalty(penaltyId, reqDto);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

/**
* ํŒจ๋„ํ‹ฐ ๋ถ€์—ฌ
*/
@PostMapping()
@PostMapping
public ResponseEntity<Void> addAdminPenalty(
@RequestBody PartyPenaltyAdminReqDto reqDto) {
@RequestBody @Valid PartyPenaltyAdminReqDto reqDto) {
partyPenaltyAdminService.addAdminPenalty(reqDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.time.LocalDateTime;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import gg.data.party.PartyPenalty;
import gg.data.user.User;
import lombok.Getter;
Expand All @@ -10,13 +15,30 @@
@Getter
@NoArgsConstructor
public class PartyPenaltyAdminReqDto {
@NotBlank(message = "Penalty type์ด ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค")
@Size(max = 20, message = "Penalty type์€ ์ตœ๋Œ€ 20์ž์ž…๋‹ˆ๋‹ค")
String penaltyType;

@NotBlank(message = "message๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค")
@Size(max = 100, message = "message๋Š” ์ตœ๋Œ€ 100์ž์ž…๋‹ˆ๋‹ค")
String message;

@NotNull(message = "penaltyTime์ด ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค")
JaBeast marked this conversation as resolved.
Show resolved Hide resolved
@Min(value = 1, message = "์˜ฌ๋ฐ”๋ฅธ penaltyTime์„ ๋„ฃ์–ด์ฃผ์„ธ์š”")
int penaltyTime;

@NotBlank(message = "IntraId๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค")
String userIntraId;

public PartyPenalty toEntity(User user, String penaltyType, String message, LocalDateTime startTime,
Integer penaltyTime) {
return new PartyPenalty(user, penaltyType, message, startTime, penaltyTime);
}

public PartyPenaltyAdminReqDto(String penaltyType, String message, int penaltyTime, String userIntraId) {
this.penaltyType = penaltyType;
this.message = message;
this.penaltyTime = penaltyTime;
this.userIntraId = userIntraId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package gg.party.api.admin.penalty;

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;

import gg.auth.utils.AuthTokenProvider;
import gg.data.party.PartyPenalty;
import gg.data.user.User;
import gg.data.user.type.RacketType;
import gg.data.user.type.RoleType;
import gg.data.user.type.SnsType;
import gg.party.api.admin.penalty.controller.request.PartyPenaltyAdminReqDto;
import gg.party.api.admin.penalty.controller.response.PartyPenaltyListAdminResDto;
import gg.repo.party.PartyPenaltyRepository;
import gg.utils.TestDataUtils;
import gg.utils.annotation.IntegrationTest;
import lombok.extern.slf4j.Slf4j;

@IntegrationTest
@AutoConfigureMockMvc
@SpringBootTest
@Transactional
@Slf4j
public class PartyPenaltyControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private TestDataUtils testDataUtils;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private AuthTokenProvider tokenProvider;
@Autowired
private PartyPenaltyRepository partyPenaltyRepository;
private User userTester;
JaBeast marked this conversation as resolved.
Show resolved Hide resolved
private User reportedTester;
private User adminUser;
private String adminAccessToken;
private Long testPenaltyId;

@BeforeEach
void beforeEach() {
userTester = testDataUtils.createNewUser("user1", "emailTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER);
reportedTester = testDataUtils.createNewUser("reportedUser", "reportedTester",
RacketType.DUAL, SnsType.SLACK, RoleType.USER);
adminUser = testDataUtils.createNewUser("adminUser", "[email protected]",
RacketType.DUAL, SnsType.SLACK, RoleType.ADMIN);
PartyPenalty testPenalty = testDataUtils.createNewPenalty(reportedTester, "test_penalty",
"์ด์œ ๋Š”_ํ…Œ์ŠคํŠธ๋ผ์„œ", LocalDateTime.now(), 60);
testPenaltyId = testPenalty.getId();
adminAccessToken = tokenProvider.createToken(adminUser.getId());
}

@Nested
@DisplayName("ํŒจ๋„ํ‹ฐ ํ…Œ์ŠคํŠธ")
class PenaltyAdminTests {

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ์กฐํšŒ - 200")
void testRetrievePenaltiesList() throws Exception {
//given
for (int i = 1; i <= 9; i++) {
testDataUtils.createNewPenalty(reportedTester, "test_penalty_" + i,
"test_reason" + i, LocalDateTime.now(), 60);
}
int pageSize = 10;
int pageNumber = 1;
String url = String.format("/party/admin/penalties?page=%d&size=%d", pageNumber, pageSize);

//when
MvcResult result = mockMvc.perform(get(url)
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();

//then
String content = result.getResponse().getContentAsString();
PartyPenaltyListAdminResDto responseDto =
objectMapper.readValue(content, PartyPenaltyListAdminResDto.class);

assertEquals(pageSize, responseDto.getPenaltyList().size());
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ์กฐํšŒ(pagination) - 200")
void testPaginationPenaltiesList() throws Exception {
//given
for (int i = 1; i <= 15; i++) {
testDataUtils.createNewPenalty(reportedTester, "test_penalty_" + i,
"test_reason" + i, LocalDateTime.now(), 60);
}
int pageSize = 10;
int pageNumber = 2;
String url = String.format("/party/admin/penalties?page=%d&size=%d", pageNumber, pageSize);

//when
MvcResult result = mockMvc.perform(get(url)
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();

//then
String content = result.getResponse().getContentAsString();
PartyPenaltyListAdminResDto responseDto =
objectMapper.readValue(content, PartyPenaltyListAdminResDto.class);

int expectedPageSize = 6;
assertEquals(expectedPageSize, responseDto.getPenaltyList().size());
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ์ˆ˜์ • - 204")
void testModifyAdminPenalty() throws Exception {
//given
PartyPenaltyAdminReqDto penaltyDto = new PartyPenaltyAdminReqDto("test_penalty", "Test reason", 60,
reportedTester.getIntraId());

//when
mockMvc.perform(patch("/party/admin/penalties/{penaltyId}", testPenaltyId)
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(penaltyDto)))
.andExpect(status().isNoContent());

//then
PartyPenalty updatedPenalty = partyPenaltyRepository.findById(testPenaltyId).orElseThrow();

assertEquals(penaltyDto.getPenaltyType(), updatedPenalty.getPenaltyType());
assertEquals(penaltyDto.getMessage(), updatedPenalty.getMessage());
assertEquals(penaltyDto.getPenaltyTime(), updatedPenalty.getPenaltyTime().intValue());
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ์ˆ˜์ • - ์‹คํŒจ ์‹œ๋‚˜๋ฆฌ์˜ค(์—†๋Š” ์œ ์ €) - 404")
void testModifyAdminPenalty_NotFound() throws Exception {
//given
Long nonExistentPenaltyId = 999L;

PartyPenaltyAdminReqDto penaltyDto = new PartyPenaltyAdminReqDto("test_penalty", "Test reason", 60,
reportedTester.getIntraId());

//when
mockMvc.perform(patch("/party/admin/penalties/{penaltyId}", nonExistentPenaltyId)
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(penaltyDto)))
.andExpect(status().isNotFound());
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ๋ถ€์—ฌ (์ผ๋ฐ˜์ ์ธ ์ƒํ™ฉ) - 201")
void testGiveAdminPenalty() throws Exception {
//given
PartyPenaltyAdminReqDto penaltyDto = new PartyPenaltyAdminReqDto("test_penalty", "Test reason", 60,
userTester.getIntraId());

long penaltyCountBefore = partyPenaltyRepository.count();

//when
mockMvc.perform(post("/party/admin/penalties")
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(penaltyDto)))
.andExpect(status().isCreated());

//then
long penaltyCountAfter = partyPenaltyRepository.count();
assertEquals(penaltyCountBefore + 1, penaltyCountAfter);
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ๋ถ€์—ฌ (ํŒจ๋„ํ‹ฐ๋œ ์œ ์ €์—๊ฒŒ ์ถ”๊ฐ€ ํŒจ๋„ํ‹ฐ ๋ถ€์—ฌ) - 201")
void testAddAdminPenalty() throws Exception {
//given
PartyPenaltyAdminReqDto morePenaltyDto = new PartyPenaltyAdminReqDto("test_penalty", "Test reason", 60,
reportedTester.getIntraId());

//when
mockMvc.perform(post("/party/admin/penalties")
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(morePenaltyDto)))
.andExpect(status().isCreated());

//then
List<PartyPenalty> penalties = partyPenaltyRepository.findAllByUserId(reportedTester.getId());
int totalPenaltyTime = penalties.stream().mapToInt(PartyPenalty::getPenaltyTime).sum();

assertEquals(120, totalPenaltyTime);
}

@Test
@DisplayName("ํŒจ๋„ํ‹ฐ ๋ถ€์—ฌ - ์‹คํŒจ ์‹œ๋‚˜๋ฆฌ์˜ค(์—†๋Š” ์œ ์ €) - 404")
void testAddAdminPenalty_UserNotFound() throws Exception {
//given
PartyPenaltyAdminReqDto penaltyDto = new PartyPenaltyAdminReqDto("test_penalty", "Test reason", 60,
"nonexistentIntraId");

//when
mockMvc.perform(post("/party/admin/penalties")
.header("Authorization", "Bearer " + adminAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(penaltyDto)))
.andExpect(status().isNotFound());
}
}
}
JaBeast marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package gg.repo.party;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import gg.data.party.PartyPenalty;

public interface PartyPenaltyRepository extends JpaRepository<PartyPenalty, Long> {
public PartyPenalty findByUserId(Long id);
PartyPenalty findByUserId(Long id);

List<PartyPenalty> findAllByUserId(Long userId);
}
Loading