1515 */
1616package io .flamingock .template .mongodb .operations ;
1717
18+ import io .flamingock .template .mongodb .MongoTemplateExecutionException ;
1819import io .flamingock .template .mongodb .model .MongoOperation ;
1920import io .flamingock .template .mongodb .model .operator .RenameCollectionOperator ;
2021import org .junit .jupiter .api .BeforeEach ;
2627import java .util .List ;
2728import java .util .Map ;
2829
29- import io .flamingock .template .mongodb .MongoTemplateExecutionException ;
30-
31- import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
3230import static org .junit .jupiter .api .Assertions .assertFalse ;
3331import static org .junit .jupiter .api .Assertions .assertThrows ;
3432import static org .junit .jupiter .api .Assertions .assertTrue ;
@@ -50,12 +48,7 @@ void renameCollectionTest() {
5048 mongoDatabase .createCollection (ORIGINAL_NAME );
5149 assertTrue (collectionExists (ORIGINAL_NAME ), "Original collection should exist before rename" );
5250
53- MongoOperation operation = new MongoOperation ();
54- operation .setType ("renameCollection" );
55- operation .setCollection (ORIGINAL_NAME );
56- Map <String , Object > params = new HashMap <>();
57- params .put ("target" , RENAMED_NAME );
58- operation .setParameters (params );
51+ MongoOperation operation = buildRenameOperation ();
5952
6053 RenameCollectionOperator operator = new RenameCollectionOperator (mongoDatabase , operation );
6154 operator .apply (null );
@@ -65,8 +58,8 @@ void renameCollectionTest() {
6558 }
6659
6760 @ Test
68- @ DisplayName ("WHEN renameCollection operator is applied twice THEN second call succeeds silently " )
69- void renameCollectionIdempotentTest () {
61+ @ DisplayName ("WHEN renameCollection is applied twice THEN second call fails because source no longer exists " )
62+ void renameCollectionFailsOnSecondAttemptTest () {
7063 mongoDatabase .createCollection (ORIGINAL_NAME );
7164 assertTrue (collectionExists (ORIGINAL_NAME ), "Original collection should exist before rename" );
7265
@@ -77,27 +70,28 @@ void renameCollectionIdempotentTest() {
7770 assertFalse (collectionExists (ORIGINAL_NAME ), "Original should not exist after rename" );
7871 assertTrue (collectionExists (RENAMED_NAME ), "Renamed should exist after rename" );
7972
80- // Second apply should not throw (source gone, target exists → already renamed)
81- assertDoesNotThrow (() -> operator .apply (null ));
82- assertTrue (collectionExists (RENAMED_NAME ), "Renamed should still exist after second apply" );
73+ // Second apply MUST fail: source is gone. Rename is a transformation, not an end-state —
74+ // re-running it is not a safe no-op.
75+ assertThrows (MongoTemplateExecutionException .class , () -> operator .apply (null ));
76+ assertTrue (collectionExists (RENAMED_NAME ), "Renamed should still exist after failed second apply" );
8377 }
8478
8579 @ Test
86- @ DisplayName ("WHEN source is gone and target exists THEN operation is skipped as already renamed " )
87- void renameCollectionAlreadyRenamedTest () {
80+ @ DisplayName ("WHEN source is gone and target exists THEN operation fails (cannot assume it was this rename) " )
81+ void renameCollectionFailsWhenSourceMissingButTargetExistsTest () {
8882 mongoDatabase .createCollection (RENAMED_NAME );
8983 assertFalse (collectionExists (ORIGINAL_NAME ), "Original should not exist" );
9084 assertTrue (collectionExists (RENAMED_NAME ), "Target should already exist" );
9185
9286 MongoOperation operation = buildRenameOperation ();
9387 RenameCollectionOperator operator = new RenameCollectionOperator (mongoDatabase , operation );
94- assertDoesNotThrow ( () -> operator .apply (null ));
95- assertTrue (collectionExists (RENAMED_NAME ), "Target should still exist after skipped rename" );
88+ assertThrows ( MongoTemplateExecutionException . class , () -> operator .apply (null ));
89+ assertTrue (collectionExists (RENAMED_NAME ), "Target should still exist after failed rename" );
9690 }
9791
9892 @ Test
99- @ DisplayName ("WHEN both source and target exist THEN operation throws MongoTemplateExecutionException" )
100- void renameCollectionBothExistTest () {
93+ @ DisplayName ("WHEN both source and target exist THEN operation fails with MongoTemplateExecutionException" )
94+ void renameCollectionFailsWhenBothExistTest () {
10195 mongoDatabase .createCollection (ORIGINAL_NAME );
10296 mongoDatabase .createCollection (RENAMED_NAME );
10397 assertTrue (collectionExists (ORIGINAL_NAME ), "Source should exist" );
@@ -109,15 +103,14 @@ void renameCollectionBothExistTest() {
109103 }
110104
111105 @ Test
112- @ DisplayName ("WHEN neither source nor target exists THEN operation is skipped gracefully " )
113- void renameCollectionNeitherExistsTest () {
106+ @ DisplayName ("WHEN neither source nor target exists THEN operation fails because source is missing " )
107+ void renameCollectionFailsWhenNeitherExistsTest () {
114108 assertFalse (collectionExists (ORIGINAL_NAME ), "Source should not exist" );
115109 assertFalse (collectionExists (RENAMED_NAME ), "Target should not exist" );
116110
117111 MongoOperation operation = buildRenameOperation ();
118112 RenameCollectionOperator operator = new RenameCollectionOperator (mongoDatabase , operation );
119- // Should skip silently — both absent means nothing to rename and nothing was renamed
120- assertDoesNotThrow (() -> operator .apply (null ));
113+ assertThrows (MongoTemplateExecutionException .class , () -> operator .apply (null ));
121114 assertFalse (collectionExists (ORIGINAL_NAME ), "Source should still not exist" );
122115 assertFalse (collectionExists (RENAMED_NAME ), "Target should still not exist" );
123116 }
0 commit comments