2424import org .junit .jupiter .api .Test ;
2525
2626import io .fabric8 .kubernetes .api .model .ObjectMeta ;
27+ import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2728import io .fabric8 .kubernetes .api .model .apps .Deployment ;
2829import io .fabric8 .kubernetes .api .model .apps .DeploymentBuilder ;
2930import io .fabric8 .kubernetes .client .KubernetesClient ;
@@ -74,14 +75,16 @@ class InformerEventSourceTest {
7475 private final EventHandler eventHandlerMock = mock (EventHandler .class );
7576 private final InformerEventSourceConfiguration <Deployment > informerEventSourceConfiguration =
7677 mock (InformerEventSourceConfiguration .class );
78+ private SecondaryToPrimaryMapper mockSecondaryToPrimaryMapper =
79+ mock (SecondaryToPrimaryMapper .class );
7780
7881 @ BeforeEach
7982 void setup () {
8083 final var informerConfig = mock (InformerConfiguration .class );
8184 when (informerEventSourceConfiguration .getInformerConfig ()).thenReturn (informerConfig );
8285 when (informerConfig .getEffectiveNamespaces (any ())).thenReturn (DEFAULT_NAMESPACES_SET );
8386 when (informerEventSourceConfiguration .getSecondaryToPrimaryMapper ())
84- .thenReturn (mock ( SecondaryToPrimaryMapper . class ) );
87+ .thenReturn (mockSecondaryToPrimaryMapper );
8588 when (informerEventSourceConfiguration .getResourceClass ()).thenReturn (Deployment .class );
8689
8790 informerEventSource =
@@ -90,6 +93,11 @@ void setup() {
9093 // mocking start
9194 @ Override
9295 public synchronized void start () {}
96+
97+ @ Override
98+ public Optional <Deployment > get (ResourceID resourceID ) {
99+ return temporaryResourceCache .getResourceFromCache (resourceID );
100+ }
93101 });
94102
95103 var mockControllerConfig = mock (ControllerConfiguration .class );
@@ -338,6 +346,30 @@ void multipleCachingFilteringUpdates_variant4() {
338346 assertNoEventProduced ();
339347 }
340348
349+ @ Test
350+ void cachingFilteringUpdateEventUpdatesPrimaryToSecondaryIndex () {
351+ withRealTemporaryResourceCache ();
352+ var td = testDeployment ();
353+ when (mockSecondaryToPrimaryMapper .toPrimaryResourceIDs (any ()))
354+ .thenReturn (Set .of (ResourceID .fromResource (td )));
355+
356+ informerEventSource .eventFilteringUpdateAndCacheResource (
357+ td ,
358+ d -> {
359+ var d1 = testDeployment ();
360+ d1 .getMetadata ().setResourceVersion ("2" );
361+ return d1 ;
362+ });
363+
364+ var cr = new TestCustomResource ();
365+ cr .setMetadata (
366+ new ObjectMetaBuilder ()
367+ .withName (td .getMetadata ().getName ())
368+ .withNamespace (td .getMetadata ().getNamespace ())
369+ .build ());
370+ assertThat (informerEventSource .getSecondaryResources (cr )).isNotEmpty ();
371+ }
372+
341373 private void assertNoEventProduced () {
342374 await ()
343375 .pollDelay (Duration .ofMillis (50 ))
0 commit comments