Skip to content

Commit

Permalink
πŸ§ͺ [Test] κ°œμΈμΌμ • μˆ˜μ • API ν…ŒμŠ€νŠΈ μΆ”κ°€ #1057
Browse files Browse the repository at this point in the history
  • Loading branch information
taehyeon committed Dec 30, 2024
1 parent 9351a15 commit 167e383
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import gg.auth.UserDto;
Expand Down Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<Void> privateScheduleCreate(@Login @Parameter(hidden = tru
public ResponseEntity<PrivateScheduleUpdateResDto> privateScheduleUpdate(
@Login @Parameter(hidden = true) UserDto userDto,
@Valid @RequestBody PrivateScheduleUpdateReqDto privateScheduleUpdateReqDto,
@RequestParam Long id) {
@PathVariable Long id) {
PrivateScheduleUpdateResDto privateScheduleUpdateResDto = privateScheduleService.updatePrivateSchedule(userDto,
privateScheduleUpdateReqDto, id);
return ResponseEntity.status(HttpStatus.OK).body(privateScheduleUpdateResDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public class PrivateScheduleUpdateResDto {
@Builder
private PrivateScheduleUpdateResDto(Long id, DetailClassification classification, EventTag eventTag, JobTag jobTag,
TechTag techTag, String author, String title, String content, String link, ScheduleStatus status,
LocalDateTime startTime,
LocalDateTime endTime, boolean alarm, Long groupId) {
LocalDateTime startTime, LocalDateTime endTime, boolean alarm, Long groupId) {
this.id = id;
this.classification = classification;
this.eventTag = eventTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import gg.repo.calendar.ScheduleGroupRepository;
import gg.repo.user.UserRepository;
import gg.utils.exception.ErrorCode;
import gg.utils.exception.custom.ForbiddenException;
import gg.utils.exception.custom.InvalidParameterException;
import gg.utils.exception.custom.NotExistException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -54,6 +55,7 @@ public PrivateScheduleUpdateResDto updatePrivateSchedule(UserDto userDto,
validateAuthor(userDto.getIntraId(), privateSchedule.getPublicSchedule().getAuthor());
scheduleGroupRepository.findById(privateScheduleUpdateReqDto.getGroupId())
.orElseThrow(() -> new NotExistException(ErrorCode.SCHEDULE_GROUP_NOT_FOUND));

privateSchedule.update(privateScheduleUpdateReqDto.getEventTag(), privateScheduleUpdateReqDto.getJobTag(),
privateScheduleUpdateReqDto.getTechTag(), privateScheduleUpdateReqDto.getTitle(),
privateScheduleUpdateReqDto.getContent(), privateScheduleUpdateReqDto.getLink(),
Expand All @@ -70,8 +72,8 @@ public void validateTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
}

public void validateAuthor(String intraId, String author) {
if (intraId.equals(author)) {
throw new InvalidParameterException(ErrorCode.CALENDAR_AUTHOR_NOT_MATCH);
if (!intraId.equals(author)) {
throw new ForbiddenException(ErrorCode.CALENDAR_AUTHOR_NOT_MATCH);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import org.springframework.stereotype.Component;

import gg.data.calendar.PrivateSchedule;
import gg.data.calendar.PublicSchedule;
import gg.data.calendar.ScheduleGroup;
import gg.data.calendar.type.DetailClassification;
import gg.data.calendar.type.ScheduleStatus;
import gg.data.user.User;
import gg.repo.calendar.PrivateScheduleRepository;
import gg.repo.calendar.PublicScheduleRepository;
import gg.repo.calendar.ScheduleGroupRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,15 +20,16 @@
public class PrivateScheduleMockData {
private final PublicScheduleRepository publicScheduleRepository;
private final ScheduleGroupRepository scheduleGroupRepository;
private final PrivateScheduleRepository privateScheduleRepository;

public PublicSchedule createPublicSchedule() {
public PublicSchedule createPublicSchedule(String author) {
PublicSchedule publicSchedule = PublicSchedule.builder()
.classification(DetailClassification.EVENT)
.eventTag(null)
.jobTag(null)
.techTag(null)
.title("Test Schedule")
.author("author")
.author(author)
.content("Test Content")
.link("http://test.com")
.status(ScheduleStatus.ACTIVATE)
Expand All @@ -44,4 +47,10 @@ public ScheduleGroup createScheduleGroup(User user) {
.build();
return scheduleGroupRepository.save(scheduleGroup);
}

public PrivateSchedule createPrivateSchedule(PublicSchedule publicSchedule, ScheduleGroup scheduleGroup) {
PrivateSchedule privateSchedule = new PrivateSchedule(scheduleGroup.getUser(), publicSchedule, false,
scheduleGroup.getId());
return privateScheduleRepository.save(privateSchedule);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gg.calendar.api.user.schedule.privateschedule.controller;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

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

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -20,16 +22,19 @@

import gg.calendar.api.user.schedule.privateschedule.PrivateScheduleMockData;
import gg.calendar.api.user.schedule.privateschedule.controller.request.PrivateScheduleCreateReqDto;
import gg.calendar.api.user.schedule.privateschedule.controller.request.PrivateScheduleUpdateReqDto;
import gg.data.calendar.PrivateSchedule;
import gg.data.calendar.PublicSchedule;
import gg.data.calendar.ScheduleGroup;
import gg.data.calendar.type.EventTag;
import gg.data.calendar.type.ScheduleStatus;
import gg.data.user.User;
import gg.repo.calendar.PrivateScheduleRepository;
import gg.utils.TestDataUtils;
import gg.utils.annotation.IntegrationTest;
import lombok.extern.slf4j.Slf4j;

@Transactional
@Slf4j
@IntegrationTest
@AutoConfigureMockMvc
public class PrivateScheduleControllerTest {
Expand Down Expand Up @@ -61,6 +66,7 @@ void setUp() {
@DisplayName("PrivateSchedule μƒμ„±ν•˜κΈ°")
class CreatePrivateSchedule {
@Test
@Transactional
@DisplayName("성곡")
void success() throws Exception {
//given
Expand All @@ -75,7 +81,7 @@ void success() throws Exception {
.startTime(LocalDateTime.now())
.endTime(LocalDateTime.now().plusDays(1))
.alarm(true)
.groupId(1L)
.groupId(scheduleGroup.getId())
.status(ScheduleStatus.ACTIVATE)
.build();
//when
Expand All @@ -85,12 +91,16 @@ void success() throws Exception {
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isCreated());
//then
PrivateSchedule privateSchedule = privateScheduleRepository.findById(1L).orElseThrow();

List<PrivateSchedule> schedules = privateScheduleRepository.findAll();
assertThat(schedules.size()).isEqualTo(1);
PrivateSchedule privateSchedule = schedules.get(0);
Assertions.assertThat(privateSchedule.getGroupId()).isEqualTo(scheduleGroup.getId());
Assertions.assertThat(privateSchedule.isAlarm()).isEqualTo(reqDto.isAlarm());
}

@Test
@Transactional
@DisplayName("일정 그룹이 μ—†λŠ” 경우 404")
void noGroup() throws Exception {
//given
Expand All @@ -116,6 +126,7 @@ void noGroup() throws Exception {
}

@Test
@Transactional
@DisplayName("μ‹œμž‘ λ‚ μ§œλ³΄λ‹€ λλ‚˜λŠ” λ‚ μ§œκ°€ λΉ λ₯Έ 경우 400")
void endTimeBeforeStartTime() throws Exception {
//given
Expand All @@ -140,4 +151,134 @@ void endTimeBeforeStartTime() throws Exception {
.andExpect(status().isBadRequest());
}
}

@Nested
@DisplayName("PrivateSchedule μˆ˜μ •ν•˜κΈ°")
class UpdatePrivateSchedule {
@Test
@Transactional
@DisplayName("성곡")
void success() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(publicSchedule,
scheduleGroup);
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
.jobTag(null)
.alarm(false)
.title("123")
.content("")
.link(null)
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now())
.endTime(LocalDateTime.now().plusDays(1))
.groupId(scheduleGroup.getId())
.build();
//when
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isOk());
//then
PrivateSchedule updated = privateScheduleRepository.findById(privateSchedule.getId()).orElseThrow();
Assertions.assertThat(privateSchedule.getGroupId()).isEqualTo(updated.getGroupId());
Assertions.assertThat(privateSchedule.isAlarm()).isEqualTo(updated.isAlarm());
Assertions.assertThat(privateSchedule.getGroupId()).isEqualTo(updated.getGroupId());
Assertions.assertThat(privateSchedule.getPublicSchedule()).isEqualTo(updated.getPublicSchedule());
}

@Test
@Transactional
@DisplayName("μ’…λ£Œ λ‚ μ§œκ°€ μ‹œμž‘ λ‚ μ§œλ³΄λ‹€ λΉ λ₯Έ 경우 400")
void endTimeBeforeStartTime() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(publicSchedule,
scheduleGroup);
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
.jobTag(null)
.alarm(false)
.title("123")
.content("")
.link(null)
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now().plusDays(1))
.endTime(LocalDateTime.now())
.groupId(scheduleGroup.getId())
.build();
//when&then
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isBadRequest());
}

@Test
@Transactional
@DisplayName("μž‘μ„±μžκ°€ μ•„λ‹Œ μ‚¬λžŒμ΄ 일정을 μˆ˜μ • ν•˜λ €λŠ” 경우 403")
void notMatchAuthor() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule("notMatchAuthor");
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(publicSchedule,
scheduleGroup);
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
.jobTag(null)
.alarm(false)
.title("123")
.content("")
.link(null)
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now())
.endTime(LocalDateTime.now().plusDays(1))
.groupId(scheduleGroup.getId())
.build();
//when
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isForbidden());
}

@Test
@Transactional
@DisplayName("일정이 μ—†λŠ” 경우 404")
void noSchedule() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule("notMatchAuthor");
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(publicSchedule,
scheduleGroup);
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
.jobTag(null)
.alarm(false)
.title("123")
.content("")
.link(null)
.status(ScheduleStatus.ACTIVATE)
.startTime(LocalDateTime.now())
.endTime(LocalDateTime.now().plusDays(1))
.groupId(scheduleGroup.getId())
.build();
//when
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId() + 123411243)
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isNotFound());
}
}
}
2 changes: 1 addition & 1 deletion gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public enum ErrorCode {
CALENDAR_BEFORE_DATE(400, "CA201", "μ’…λ£Œ μ‹œκ°„μ΄ μ‹œμž‘ μ‹œκ°„λ³΄λ‹€ λΉ λ₯Ό 수 μ—†μŠ΅λ‹ˆλ‹€."),
CALENDAR_AFTER_DATE(400, "CA202", "μ‹œμž‘ μ‹œκ°„μ΄ μ’…λ£Œ μ‹œκ°„λ³΄λ‹€ λŠ¦μ„ 수 μ—†μŠ΅λ‹ˆλ‹€."),
CALENDAR_EQUAL_DATE(400, "CA203", "μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ΄ 같을 수 μ—†μŠ΅λ‹ˆλ‹€."),
CALENDAR_AUTHOR_NOT_MATCH(400, "CA205", "μ „μ œν•˜μ§€ μ•ŠλŠ” μ‚¬μš©μžμž…λ‹ˆλ‹€."),
CALENDAR_AUTHOR_NOT_MATCH(403, "CA205", "μ „μ œν•˜μ§€ μ•ŠλŠ” μ‚¬μš©μžμž…λ‹ˆλ‹€."),
PRIVATE_SCHEDULE_NOT_FOUND(404, "CA101", "개인 일정을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
PUBLIC_SCHEDULE_NOT_FOUND(404, "CA102", "곡유 일정을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
SCHEDULE_GROUP_NOT_FOUND(404, "CA103", "μŠ€μΊμ€„ 그룹을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.");
Expand Down

0 comments on commit 167e383

Please sign in to comment.