11package  io .javaoperatorsdk .operator .processing .event ;
22
3- import  java .util .Set ;
4- import  java .util .stream .Collectors ;
5- 
63import  org .junit .jupiter .api .Test ;
74
85import  io .fabric8 .kubernetes .api .model .HasMetadata ;
129import  io .javaoperatorsdk .operator .processing .Controller ;
1310import  io .javaoperatorsdk .operator .processing .event .source .EventSource ;
1411
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 ;
1515import  static  org .junit .jupiter .api .Assertions .*;
1616import  static  org .mockito .Mockito .mock ;
1717
1818@ SuppressWarnings ({"unchecked" , "rawtypes" })
1919class  EventSourcesTest  {
2020
21+   public  static  final  String  EVENT_SOURCE_NAME  = "foo" ;
2122  EventSources  eventSources  = new  EventSources ();
2223
2324  @ Test 
@@ -31,20 +32,41 @@ void cannotAddTwoEventSourcesWithSameName() {
3132  @ Test 
3233  void  allEventSourcesShouldReturnAll () {
3334    // 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 () {
3666    final  var  configuration  = MockControllerConfiguration .forResource (HasMetadata .class );
3767    final  var  controller  = new  Controller (mock (Reconciler .class ), configuration ,
3868        MockKubernetesClient .client (HasMetadata .class ));
3969    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 ()));
4870  }
4971
5072}
0 commit comments