1
1
package io .javaoperatorsdk .operator .processing .event ;
2
2
3
- import java .util .Set ;
4
- import java .util .stream .Collectors ;
5
-
6
3
import org .junit .jupiter .api .Test ;
7
4
8
5
import io .fabric8 .kubernetes .api .model .HasMetadata ;
12
9
import io .javaoperatorsdk .operator .processing .Controller ;
13
10
import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
14
11
12
+ import static io .javaoperatorsdk .operator .processing .event .EventSources .CONTROLLER_RESOURCE_EVENT_SOURCE_NAME ;
13
+ import static io .javaoperatorsdk .operator .processing .event .EventSources .RETRY_RESCHEDULE_TIMER_EVENT_SOURCE_NAME ;
14
+ import static org .assertj .core .api .Assertions .assertThat ;
15
15
import static org .junit .jupiter .api .Assertions .*;
16
16
import static org .mockito .Mockito .mock ;
17
17
18
18
@ SuppressWarnings ({"unchecked" , "rawtypes" })
19
19
class EventSourcesTest {
20
20
21
+ public static final String EVENT_SOURCE_NAME = "foo" ;
21
22
EventSources eventSources = new EventSources ();
22
23
23
24
@ Test
@@ -31,20 +32,41 @@ void cannotAddTwoEventSourcesWithSameName() {
31
32
@ Test
32
33
void allEventSourcesShouldReturnAll () {
33
34
// initial state doesn't have ControllerResourceEventSource
34
- assertEquals (Set .of (eventSources .retryEventSource ()), eventSources .allEventSources ().collect (
35
- Collectors .toSet ()));
35
+ assertThat (eventSources .eventSources ()).containsExactly (eventSources .retryEventSource ());
36
+
37
+ initControllerEventSource ();
38
+
39
+ assertThat (eventSources .eventSources ()).containsExactly (
40
+ eventSources .controllerResourceEventSource (),
41
+ eventSources .retryEventSource ());
42
+
43
+ final var source = mock (EventSource .class );
44
+ eventSources .add (EVENT_SOURCE_NAME , source );
45
+ // order matters
46
+ assertThat (eventSources .eventSources ())
47
+ .containsExactly (eventSources .controllerResourceEventSource (),
48
+ eventSources .retryEventSource (), source );
49
+ }
50
+
51
+ @ Test
52
+ void eventSourcesIteratorShouldReturnControllerEventSourceAsFirst () {
53
+ initControllerEventSource ();
54
+ final var source = mock (EventSource .class );
55
+ eventSources .add (EVENT_SOURCE_NAME , source );
56
+
57
+ assertThat (eventSources .iterator ()).toIterable ().containsExactly (
58
+ new NamedEventSource (eventSources .controllerResourceEventSource (),
59
+ CONTROLLER_RESOURCE_EVENT_SOURCE_NAME ),
60
+ new NamedEventSource (eventSources .retryEventSource (),
61
+ RETRY_RESCHEDULE_TIMER_EVENT_SOURCE_NAME ),
62
+ new NamedEventSource (source , EVENT_SOURCE_NAME ));
63
+ }
64
+
65
+ private void initControllerEventSource () {
36
66
final var configuration = MockControllerConfiguration .forResource (HasMetadata .class );
37
67
final var controller = new Controller (mock (Reconciler .class ), configuration ,
38
68
MockKubernetesClient .client (HasMetadata .class ));
39
69
eventSources .initControllerEventSource (controller );
40
- assertEquals (
41
- Set .of (eventSources .retryEventSource (), eventSources .controllerResourceEventSource ()),
42
- eventSources .allEventSources ().collect (Collectors .toSet ()));
43
- final var source = mock (EventSource .class );
44
- eventSources .add ("foo" , source );
45
- assertEquals (Set .of (eventSources .retryEventSource (),
46
- eventSources .controllerResourceEventSource (), source ),
47
- eventSources .allEventSources ().collect (Collectors .toSet ()));
48
70
}
49
71
50
72
}
0 commit comments