11package com .back .domain .study .plan .controller ;
22
33import com .back .domain .study .plan .dto .StudyPlanRequest ;
4- import com .back .domain .study .plan .entity .Color ;
5- import com .back .domain .study .plan .entity .Frequency ;
6- import com .back .domain .study .plan .entity .RepeatRule ;
7- import com .back .domain .study .plan .entity .StudyPlan ;
4+ import com .back .domain .study .plan .entity .*;
5+ import com .back .domain .study .plan .repository .StudyPlanExceptionRepository ;
86import com .back .domain .study .plan .repository .StudyPlanRepository ;
97import com .back .domain .study .plan .service .StudyPlanService ;
108import com .back .domain .user .entity .User ;
3230import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
3331import org .springframework .transaction .annotation .Transactional ;
3432
33+ import java .time .LocalDate ;
3534import java .time .LocalDateTime ;
3635import java .util .List ;
3736
@@ -63,6 +62,9 @@ class StudyPlanControllerTest {
6362 @ Autowired
6463 private StudyPlanRepository studyPlanRepository ;
6564
65+ @ Autowired
66+ private StudyPlanExceptionRepository studyPlanExceptionRepository ;
67+
6668 @ Autowired
6769 private UserRepository userRepository ;
6870 private StudyPlan savedStudyPlan ;
@@ -786,4 +788,71 @@ void t17() throws Exception {
786788 .andExpect (jsonPath ("$.data.plans[0].subject" ).value ("매일 반복 계획" ));
787789 }
788790
791+ @ Test
792+ @ DisplayName ("다음 날에 이미 예외가 있을 때 중복 생성 방지 테스트" )
793+ void t18 () throws Exception {
794+ // Given: 매일 반복되는 계획 생성
795+ StudyPlan originalPlan = createDailyPlan ();
796+ Long planId = originalPlan .getId ();
797+
798+ // When 1: 10월 7일을 FROM_THIS_DATE로 수정
799+ mvc .perform (MockMvcRequestBuilders .put ("/api/plans/{planId}?applyScope=FROM_THIS_DATE" , planId )
800+ .header ("Authorization" , "Bearer faketoken" )
801+ .contentType (MediaType .APPLICATION_JSON )
802+ .content ("""
803+ {
804+ "subject": "10월 7일부터 수정",
805+ "startDate": "2025-10-07T14:00:00",
806+ "endDate": "2025-10-07T15:00:00",
807+ "color": "RED"
808+ }
809+ """ ))
810+ .andDo (print ())
811+ .andExpect (status ().isOk ());
812+
813+ // When 2: 10월 8일을 THIS_ONLY로 별도 수정 (다른 내용)
814+ mvc .perform (MockMvcRequestBuilders .put ("/api/plans/{planId}?applyScope=THIS_ONLY" , planId )
815+ .header ("Authorization" , "Bearer faketoken" )
816+ .contentType (MediaType .APPLICATION_JSON )
817+ .content ("""
818+ {
819+ "subject": "10월 8일만 특별 수정",
820+ "startDate": "2025-10-08T16:00:00",
821+ "endDate": "2025-10-08T17:00:00",
822+ "color": "GREEN"
823+ }
824+ """ ))
825+ .andDo (print ())
826+ .andExpect (status ().isOk ());
827+
828+ // When 3: 10월 7일을 THIS_ONLY로 삭제
829+ mvc .perform (MockMvcRequestBuilders .delete ("/api/plans/{planId}?selectedDate=2025-10-07&applyScope=THIS_ONLY" , planId )
830+ .header ("Authorization" , "Bearer faketoken" ))
831+ .andDo (print ())
832+ .andExpect (status ().isOk ());
833+
834+ // Then 1: 10월 7일은 삭제됨
835+ mvc .perform (get ("/api/plans/date/2025-10-07" )
836+ .header ("Authorization" , "Bearer faketoken" ))
837+ .andDo (print ())
838+ .andExpect (status ().isOk ())
839+ .andExpect (jsonPath ("$.data.totalCount" ).value (0 ));
840+
841+ // Then 2: 10월 8일은 기존에 설정한 "10월 8일만 특별 수정" 유지 (중복 생성되지 않음)
842+ mvc .perform (get ("/api/plans/date/2025-10-08" )
843+ .header ("Authorization" , "Bearer faketoken" ))
844+ .andDo (print ())
845+ .andExpect (status ().isOk ())
846+ .andExpect (jsonPath ("$.data.totalCount" ).value (1 ))
847+ .andExpect (jsonPath ("$.data.plans[0].subject" ).value ("10월 8일만 특별 수정" ))
848+ .andExpect (jsonPath ("$.data.plans[0].color" ).value ("GREEN" ))
849+ .andExpect (jsonPath ("$.data.plans[0].startDate" ).value ("2025-10-08T16:00:00" ));
850+
851+ // Then 3: DB에서 10월 8일 예외가 1개만 존재하는지 확인
852+ List <StudyPlanException > exceptions = studyPlanExceptionRepository .findAll ();
853+ long oct8Exceptions = exceptions .stream ()
854+ .filter (e -> e .getExceptionDate ().equals (LocalDate .of (2025 , 10 , 8 )))
855+ .count ();
856+ assertThat (oct8Exceptions ).isEqualTo (1 );
857+ }
789858}
0 commit comments