|
15 | 15 | */ |
16 | 16 | package io.javaoperatorsdk.operator.api.reconciler; |
17 | 17 |
|
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
18 | 20 | import java.util.function.UnaryOperator; |
19 | 21 |
|
20 | 22 | import org.junit.jupiter.api.BeforeEach; |
|
33 | 35 | import io.javaoperatorsdk.operator.TestUtils; |
34 | 36 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; |
35 | 37 | import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever; |
| 38 | +import io.javaoperatorsdk.operator.processing.event.source.EventSource; |
36 | 39 | import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerEventSource; |
| 40 | +import io.javaoperatorsdk.operator.processing.event.source.informer.ManagedInformerEventSource; |
37 | 41 | import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
38 | 42 |
|
39 | 43 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -112,7 +116,7 @@ void validateAndCompareResourceVersionsTest() { |
112 | 116 | } |
113 | 117 |
|
114 | 118 | @Test |
115 | | - public void compareResourceVersionsWithStrings() { |
| 119 | + void compareResourceVersionsWithStrings() { |
116 | 120 | // Test equal versions |
117 | 121 | assertThat(ReconcileUtils.compareResourceVersions("1", "1")).isZero(); |
118 | 122 | assertThat(ReconcileUtils.compareResourceVersions("123", "123")).isZero(); |
@@ -355,6 +359,87 @@ void retriesFinalizerRemovalWithFreshResource() { |
355 | 359 | verify(resourceOp, times(1)).get(); |
356 | 360 | } |
357 | 361 |
|
| 362 | + @Test |
| 363 | + void resourcePatchWithSingleEventSource() { |
| 364 | + var resource = TestUtils.testCustomResource1(); |
| 365 | + resource.getMetadata().setResourceVersion("1"); |
| 366 | + |
| 367 | + var updatedResource = TestUtils.testCustomResource1(); |
| 368 | + updatedResource.getMetadata().setResourceVersion("2"); |
| 369 | + |
| 370 | + var eventSourceRetriever = mock(EventSourceRetriever.class); |
| 371 | + var managedEventSource = mock(ManagedInformerEventSource.class); |
| 372 | + |
| 373 | + when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever); |
| 374 | + when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class)) |
| 375 | + .thenReturn(List.of(managedEventSource)); |
| 376 | + when(managedEventSource.eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class))) |
| 377 | + .thenReturn(updatedResource); |
| 378 | + |
| 379 | + var result = ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity()); |
| 380 | + |
| 381 | + assertThat(result).isNotNull(); |
| 382 | + assertThat(result.getMetadata().getResourceVersion()).isEqualTo("2"); |
| 383 | + verify(managedEventSource, times(1)) |
| 384 | + .eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class)); |
| 385 | + } |
| 386 | + |
| 387 | + @Test |
| 388 | + void resourcePatchThrowsWhenNoEventSourceFound() { |
| 389 | + var resource = TestUtils.testCustomResource1(); |
| 390 | + var eventSourceRetriever = mock(EventSourceRetriever.class); |
| 391 | + |
| 392 | + when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever); |
| 393 | + when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class)) |
| 394 | + .thenReturn(Collections.emptyList()); |
| 395 | + |
| 396 | + var exception = |
| 397 | + assertThrows( |
| 398 | + IllegalStateException.class, |
| 399 | + () -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity())); |
| 400 | + |
| 401 | + assertThat(exception.getMessage()).contains("No event source found for type"); |
| 402 | + } |
| 403 | + |
| 404 | + @Test |
| 405 | + void resourcePatchThrowsWhenMultipleEventSourcesFound() { |
| 406 | + var resource = TestUtils.testCustomResource1(); |
| 407 | + var eventSourceRetriever = mock(EventSourceRetriever.class); |
| 408 | + var eventSource1 = mock(ManagedInformerEventSource.class); |
| 409 | + var eventSource2 = mock(ManagedInformerEventSource.class); |
| 410 | + |
| 411 | + when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever); |
| 412 | + when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class)) |
| 413 | + .thenReturn(List.of(eventSource1, eventSource2)); |
| 414 | + |
| 415 | + var exception = |
| 416 | + assertThrows( |
| 417 | + IllegalStateException.class, |
| 418 | + () -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity())); |
| 419 | + |
| 420 | + assertThat(exception.getMessage()).contains("Multiple event sources found for"); |
| 421 | + assertThat(exception.getMessage()).contains("please provide the target event source"); |
| 422 | + } |
| 423 | + |
| 424 | + @Test |
| 425 | + void resourcePatchThrowsWhenEventSourceIsNotManagedInformer() { |
| 426 | + var resource = TestUtils.testCustomResource1(); |
| 427 | + var eventSourceRetriever = mock(EventSourceRetriever.class); |
| 428 | + var nonManagedEventSource = mock(EventSource.class); |
| 429 | + |
| 430 | + when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever); |
| 431 | + when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class)) |
| 432 | + .thenReturn(List.of(nonManagedEventSource)); |
| 433 | + |
| 434 | + var exception = |
| 435 | + assertThrows( |
| 436 | + IllegalStateException.class, |
| 437 | + () -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity())); |
| 438 | + |
| 439 | + assertThat(exception.getMessage()).contains("Target event source must be a subclass off"); |
| 440 | + assertThat(exception.getMessage()).contains("ManagedInformerEventSource"); |
| 441 | + } |
| 442 | + |
358 | 443 | // naive performance test that compares the work case scenario for the parsing and non-parsing |
359 | 444 | // variants |
360 | 445 | @Test |
|
0 commit comments