Skip to content

Commit

Permalink
๐Ÿงช [Test] ๊ฐœ์ธ์ผ์ • ์‚ญ์ œ api ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€ #1058
Browse files Browse the repository at this point in the history
  • Loading branch information
taehyeon committed Dec 31, 2024
1 parent 0812ca1 commit dbe693b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public ScheduleGroup createScheduleGroup(User user) {
return scheduleGroupRepository.save(scheduleGroup);
}

public PrivateSchedule createPrivateSchedule(PublicSchedule publicSchedule, ScheduleGroup scheduleGroup) {
PrivateSchedule privateSchedule = new PrivateSchedule(scheduleGroup.getUser(), publicSchedule, false,
scheduleGroup.getId());
public PrivateSchedule createPrivateSchedule(User user, PublicSchedule publicSchedule, Long scheduleGroupId) {
PrivateSchedule privateSchedule = new PrivateSchedule(user, publicSchedule, false,
scheduleGroupId);
return privateScheduleRepository.save(privateSchedule);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@Slf4j
@IntegrationTest
@AutoConfigureMockMvc
@Transactional
public class PrivateScheduleControllerTest {
@Autowired
private MockMvc mockMvc;
Expand Down Expand Up @@ -66,8 +67,7 @@ void setUp() {
@DisplayName("PrivateSchedule ์ƒ์„ฑํ•˜๊ธฐ")
class CreatePrivateSchedule {
@Test
@Transactional
@DisplayName("์„ฑ๊ณต")
@DisplayName("์„ฑ๊ณต 201")
void success() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
Expand Down Expand Up @@ -100,7 +100,6 @@ void success() throws Exception {
}

@Test
@Transactional
@DisplayName("์ผ์ • ๊ทธ๋ฃน์ด ์—†๋Š” ๊ฒฝ์šฐ 404")
void noGroup() throws Exception {
//given
Expand All @@ -126,7 +125,6 @@ void noGroup() throws Exception {
}

@Test
@Transactional
@DisplayName("์‹œ์ž‘ ๋‚ ์งœ๋ณด๋‹ค ๋๋‚˜๋Š” ๋‚ ์งœ๊ฐ€ ๋น ๋ฅธ ๊ฒฝ์šฐ 400")
void endTimeBeforeStartTime() throws Exception {
//given
Expand Down Expand Up @@ -156,14 +154,13 @@ void endTimeBeforeStartTime() throws Exception {
@DisplayName("PrivateSchedule ์ˆ˜์ •ํ•˜๊ธฐ")
class UpdatePrivateSchedule {
@Test
@Transactional
@DisplayName("์„ฑ๊ณต")
@DisplayName("์„ฑ๊ณต 200")
void success() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(publicSchedule,
scheduleGroup);
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
Expand All @@ -185,21 +182,18 @@ void success() throws Exception {
.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());
Assertions.assertThat(reqDto.getGroupId()).isEqualTo(updated.getGroupId());
Assertions.assertThat(reqDto.isAlarm()).isEqualTo(updated.isAlarm());
}

@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);
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
Expand All @@ -222,14 +216,13 @@ void endTimeBeforeStartTime() throws Exception {
}

@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);
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
Expand All @@ -243,7 +236,7 @@ void notMatchAuthor() throws Exception {
.endTime(LocalDateTime.now().plusDays(1))
.groupId(scheduleGroup.getId())
.build();
//when
//when&then
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -252,14 +245,13 @@ void notMatchAuthor() throws Exception {
}

@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);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
PrivateScheduleUpdateReqDto reqDto = PrivateScheduleUpdateReqDto.builder()
.eventTag(null)
.techTag(null)
Expand All @@ -273,12 +265,112 @@ void noSchedule() throws Exception {
.endTime(LocalDateTime.now().plusDays(1))
.groupId(scheduleGroup.getId())
.build();
//when
//when&then
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId() + 123411243)
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isNotFound());
}

@Test
@DisplayName("์ผ์ • ๊ทธ๋ฃน์ด ์—†๋Š” ๊ฒฝ์šฐ 404")
void noScheduleGroup() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
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(0L)
.build();
//when&then
mockMvc.perform(put("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(reqDto)))
.andExpect(status().isNotFound());
}
}

@Nested
@DisplayName("PrivateSchedule ์‚ญ์ œํ•˜๊ธฐ")
class DeletePrivateSchedule {
@Test
@DisplayName("์„ฑ๊ณต 204")
void success() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
//when
mockMvc.perform(patch("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
//then
Assertions.assertThat(privateSchedule.getStatus()).isEqualTo(ScheduleStatus.DELETE);
}

@Test
@DisplayName("์ž‘์„ฑ์ž๊ฐ€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ 403")
void notMatchAuthor() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule("author");
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
//when
mockMvc.perform(patch("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isForbidden());
//then
Assertions.assertThat(privateSchedule.getStatus()).isEqualTo(ScheduleStatus.ACTIVATE);
}

@Test
@DisplayName("์—†๋Š” ์ผ์ •์ธ ๊ฒฝ์šฐ 404")
void notSchedule() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
//when
mockMvc.perform(patch("/calendar/private/" + privateSchedule.getId() + 1234)
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
//then
Assertions.assertThat(privateSchedule.getStatus()).isEqualTo(ScheduleStatus.ACTIVATE);
}

@Test
@DisplayName("์ด๋ฏธ ์‚ญ์ œ๋œ ์ผ์ •์ธ ๊ฒฝ์šฐ 409")
void alreadyDelete() throws Exception {
//given
ScheduleGroup scheduleGroup = privateScheduleMockData.createScheduleGroup(user);
PublicSchedule publicSchedule = privateScheduleMockData.createPublicSchedule(user.getIntraId());
PrivateSchedule privateSchedule = privateScheduleMockData.createPrivateSchedule(user, publicSchedule,
scheduleGroup.getId());
privateSchedule.delete();
//when&then
mockMvc.perform(patch("/calendar/private/" + privateSchedule.getId())
.header("Authorization", "Bearer " + accessToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isConflict());
}
}
}

0 comments on commit dbe693b

Please sign in to comment.