1111
1212import java .time .LocalDate ;
1313import java .time .LocalDateTime ;
14+ import java .util .Arrays ;
1415
1516import static org .junit .jupiter .api .Assertions .*;
1617import static org .mockito .Mockito .*;
1718
1819class CustomResourceEventSourceTest {
1920
21+ public static final String FINALIZER = "finalizer" ;
2022 CustomResourceCache customResourceCache = new CustomResourceCache ();
2123 MixedOperation mixedOperation = mock (MixedOperation .class );
2224 EventHandler eventHandler = mock (EventHandler .class );
2325
2426 private CustomResourceEventSource customResourceEventSource =
25- CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , true );
27+ CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , true , FINALIZER );
2628
2729 @ BeforeEach
2830 public void setup () {
@@ -32,25 +34,26 @@ public void setup() {
3234 @ Test
3335 public void skipsEventHandlingIfGenerationNotIncreased () {
3436 TestCustomResource customResource1 = TestUtils .testCustomResource ();
37+ customResource1 .getMetadata ().setFinalizers (Arrays .asList (FINALIZER ));
3538
3639 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
37- verify (eventHandler ,times (1 )).handleEvent (any ());
40+ verify (eventHandler , times (1 )).handleEvent (any ());
3841
3942 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
40- verify (eventHandler ,times (1 )).handleEvent (any ());
43+ verify (eventHandler , times (1 )).handleEvent (any ());
4144 }
4245
4346 @ Test
4447 public void dontSkipEventHandlingIfMarkedForDeletion () {
4548 TestCustomResource customResource1 = TestUtils .testCustomResource ();
4649
4750 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
48- verify (eventHandler ,times (1 )).handleEvent (any ());
51+ verify (eventHandler , times (1 )).handleEvent (any ());
4952
5053 // mark for deletion
5154 customResource1 .getMetadata ().setDeletionTimestamp (LocalDateTime .now ().toString ());
5255 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
53- verify (eventHandler ,times (2 )).handleEvent (any ());
56+ verify (eventHandler , times (2 )).handleEvent (any ());
5457 }
5558
5659
@@ -59,26 +62,38 @@ public void normalExecutionIfGenerationChanges() {
5962 TestCustomResource customResource1 = TestUtils .testCustomResource ();
6063
6164 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
62- verify (eventHandler ,times (1 )).handleEvent (any ());
65+ verify (eventHandler , times (1 )).handleEvent (any ());
6366
6467 customResource1 .getMetadata ().setGeneration (2L );
6568 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
66- verify (eventHandler ,times (2 )).handleEvent (any ());
69+ verify (eventHandler , times (2 )).handleEvent (any ());
6770 }
6871
6972 @ Test
7073 public void handlesAllEventIfNotGenerationAware () {
7174 customResourceEventSource =
72- CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache , mixedOperation , false );
75+ CustomResourceEventSource .customResourceEventSourceForAllNamespaces (customResourceCache ,
76+ mixedOperation , false , FINALIZER );
7377 setup ();
7478
7579 TestCustomResource customResource1 = TestUtils .testCustomResource ();
7680
7781 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
78- verify (eventHandler ,times (1 )).handleEvent (any ());
82+ verify (eventHandler , times (1 )).handleEvent (any ());
7983
8084 customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
81- verify (eventHandler ,times (2 )).handleEvent (any ());
85+ verify (eventHandler , times (2 )).handleEvent (any ());
86+ }
87+
88+ @ Test
89+ public void eventNotMarkedForLastGenerationIfNoFinalizer () {
90+ TestCustomResource customResource1 = TestUtils .testCustomResource ();
91+
92+ customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
93+ verify (eventHandler , times (1 )).handleEvent (any ());
94+
95+ customResourceEventSource .eventReceived (Watcher .Action .MODIFIED , customResource1 );
96+ verify (eventHandler , times (2 )).handleEvent (any ());
8297 }
8398
8499}
0 commit comments