@@ -98,7 +98,7 @@ void setUp() {
9898 @ Test
9999 void changeStatus_draftToEnrollment_shouldSucceed () {
100100 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .ENROLLMENT , 1L );
101- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
101+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
102102
103103 when (stageRepository .existsByCompetitionId (1L )).thenReturn (true );
104104 when (stageRepository .countStagesWithoutTours (1L )).thenReturn (0L );
@@ -115,7 +115,7 @@ void changeStatus_draftToEnrollment_shouldSucceed() {
115115 @ Test
116116 void changeStatus_draftToPublished_directly_shouldThrowInvalidTransition () {
117117 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .PUBLISHED , 1L );
118- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
118+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
119119
120120 doThrow (new CompetitionHierarchyValidationException ("Invalid status transition from DRAFT to PUBLISHED" ))
121121 .when (validator ).validateCompetitionStatusTransition (CompetitionStatus .DRAFT , CompetitionStatus .PUBLISHED );
@@ -133,7 +133,8 @@ void changeStatus_enrollmentToPublished_withValidHierarchy_shouldSucceed() {
133133 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .PUBLISHED , 1L );
134134
135135 competition .setCompetitionStatus (CompetitionStatus .ENROLLMENT );
136- when (competitionRepository .findById (1L )).thenReturn (Optional .of (competition ));
136+ when (validator .lockCompetitionForUpdate (1L )).thenReturn (competition );
137+
137138 when (stageRepository .existsByCompetitionId (1L )).thenReturn (true );
138139 when (stageRepository .countStagesWithoutTours (1L )).thenReturn (0L );
139140 when (competitionRepository .save (any (Competition .class ))).thenReturn (competition );
@@ -150,7 +151,7 @@ void changeStatus_enrollmentToPublished_withNoStages_shouldThrowException() {
150151 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .PUBLISHED , 1L );
151152
152153 competition .setCompetitionStatus (CompetitionStatus .ENROLLMENT );
153- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
154+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
154155 when (stageRepository .existsByCompetitionId (1L )).thenReturn (false );
155156
156157 CompetitionHierarchyValidationException exception = assertThrows (
@@ -166,7 +167,7 @@ void changeStatus_enrollmentToPublished_withEmptyStages_shouldThrowException() {
166167 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .PUBLISHED , 1L );
167168
168169 competition .setCompetitionStatus (CompetitionStatus .ENROLLMENT );
169- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
170+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
170171 when (stageRepository .existsByCompetitionId (1L )).thenReturn (true );
171172 when (stageRepository .countStagesWithoutTours (1L )).thenReturn (2L );
172173
@@ -183,7 +184,7 @@ void changeStatus_enrollmentToDraft_shouldThrowInvalidTransition() {
183184 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .DRAFT , 1L );
184185
185186 competition .setCompetitionStatus (CompetitionStatus .ENROLLMENT );
186- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
187+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
187188
188189 doThrow (new CompetitionHierarchyValidationException ("Invalid status transition from ENROLLMENT to DRAFT" ))
189190 .when (validator ).validateCompetitionStatusTransition (CompetitionStatus .ENROLLMENT , CompetitionStatus .DRAFT );
@@ -200,7 +201,7 @@ void changeStatus_invalidTransition_publishedToDraft_shouldThrowException() {
200201 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .DRAFT , 1L );
201202
202203 competition .setCompetitionStatus (CompetitionStatus .PUBLISHED );
203- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
204+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
204205
205206 doThrow (new CompetitionHierarchyValidationException ("Invalid status transition from PUBLISHED to DRAFT" ))
206207 .when (validator ).validateCompetitionStatusTransition (CompetitionStatus .PUBLISHED , CompetitionStatus .DRAFT );
@@ -219,7 +220,7 @@ void changeStatus_publishedToFinished_withAllStagesCompleted_shouldSucceed() {
219220 CompetitionStatus .FINISHED , 1L );
220221
221222 competition .setCompetitionStatus (CompetitionStatus .PUBLISHED );
222- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
223+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
223224 when (competitionRepository .save (any (Competition .class ))).thenReturn (competition );
224225 when (mapper .toResponse (any (Competition .class ))).thenReturn (getCompetitionResponse ());
225226
@@ -236,7 +237,7 @@ void changeStatus_publishedToFinished_withIncompleteStage_shouldThrowException()
236237 CompetitionStatus .FINISHED , 1L );
237238
238239 competition .setCompetitionStatus (CompetitionStatus .PUBLISHED );
239- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
240+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
240241
241242 doThrow (new CompetitionHierarchyValidationException (
242243 "Cannot finish competition: Not all stages are completed. "
@@ -283,7 +284,7 @@ void create_validRequest_shouldSetDraftStatusAndSave() {
283284 @ Test
284285 void changeStatus_versionMismatch_shouldThrowStaleEntityVersionException () {
285286 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .ENROLLMENT , 5L );
286- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
287+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
287288
288289 doThrow (new StaleEntityVersionException (Competition .class , 1L ))
289290 .when (validator ).validateEntityVersion (anyLong (), anyLong (), any (), anyLong ());
@@ -297,7 +298,7 @@ void changeStatus_versionMismatch_shouldThrowStaleEntityVersionException() {
297298 @ Test
298299 void changeStatus_versionMatches_shouldProceedToTransitionValidation () {
299300 ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .ENROLLMENT , 1L );
300- when (competitionRepository . findById (1L )).thenReturn (Optional . of ( competition ) );
301+ when (validator . lockCompetitionForUpdate (1L )).thenReturn (competition );
301302 when (stageRepository .existsByCompetitionId (1L )).thenReturn (true );
302303 when (stageRepository .countStagesWithoutTours (1L )).thenReturn (0L );
303304 when (competitionRepository .save (any (Competition .class ))).thenReturn (competition );
@@ -308,6 +309,16 @@ void changeStatus_versionMatches_shouldProceedToTransitionValidation() {
308309 verify (validator ).validateCompetitionStatusTransition (CompetitionStatus .DRAFT , CompetitionStatus .ENROLLMENT );
309310 }
310311
312+ @ Test
313+ void changeStatus_competitionNotFound_shouldPropagateFromValidator () {
314+ ChangeCompetitionStatusRequest request = new ChangeCompetitionStatusRequest (CompetitionStatus .ENROLLMENT , 1L );
315+ when (validator .lockCompetitionForUpdate (99L )).thenThrow (new CompetitionNotFoundException (99L ));
316+
317+ assertThrows (CompetitionNotFoundException .class , () -> competitionService .changeStatus (99L , request ));
318+
319+ verify (competitionRepository , never ()).save (any ());
320+ }
321+
311322 // ---- getVisibleById ----
312323
313324 @ Test
0 commit comments