1
+ package io .javaoperatorsdk .operator .processing .event ;
2
+
3
+ import io .fabric8 .kubernetes .client .CustomResource ;
4
+ import io .javaoperatorsdk .operator .TestUtils ;
5
+ import io .javaoperatorsdk .operator .processing .DefaultEventHandler ;
6
+ import org .junit .jupiter .api .Test ;
7
+
8
+ import java .util .Map ;
9
+
10
+ import static io .javaoperatorsdk .operator .processing .ProcessingUtils .getUID ;
11
+ import static org .assertj .core .api .Assertions .assertThat ;
12
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
13
+ import static org .mockito .Mockito .*;
14
+
15
+ class DefaultEventSourceManagerTest {
16
+
17
+ public static final String CUSTOM_EVENT_SOURCE_NAME = "CustomEventSource" ;
18
+
19
+ private DefaultEventHandler defaultEventHandlerMock = mock (DefaultEventHandler .class );
20
+ private DefaultEventSourceManager defaultEventSourceManager = new DefaultEventSourceManager (defaultEventHandlerMock );
21
+
22
+
23
+ @ Test
24
+ public void registersEventSource () {
25
+ CustomResource customResource = TestUtils .testCustomResource ();
26
+ EventSource eventSource = mock (EventSource .class );
27
+
28
+ defaultEventSourceManager .registerEventSource (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource );
29
+
30
+ Map <String , EventSource > registeredSources =
31
+ defaultEventSourceManager .getRegisteredEventSources (getUID (customResource ));
32
+ assertThat (registeredSources .entrySet ()).hasSize (1 );
33
+ assertThat (registeredSources .get (CUSTOM_EVENT_SOURCE_NAME )).isEqualTo (eventSource );
34
+ verify (eventSource , times (1 )).eventSourceRegisteredForResource (eq (customResource ));
35
+ verify (eventSource , times (1 )).setEventHandler (eq (defaultEventHandlerMock ));
36
+ }
37
+
38
+ @ Test
39
+ public void throwExceptionIfRegisteringEventSourceWithSameName () {
40
+ CustomResource customResource = TestUtils .testCustomResource ();
41
+ EventSource eventSource = mock (EventSource .class );
42
+ EventSource eventSource2 = mock (EventSource .class );
43
+
44
+ defaultEventSourceManager .registerEventSource (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource );
45
+ assertThatExceptionOfType (IllegalStateException .class ).isThrownBy (() -> {
46
+ defaultEventSourceManager .registerEventSource (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource2 );
47
+ });
48
+ }
49
+
50
+ @ Test
51
+ public void registersEventSourceOnlyIfNotRegistered () {
52
+ CustomResource customResource = TestUtils .testCustomResource ();
53
+ EventSource eventSource = mock (EventSource .class );
54
+ EventSource eventSource2 = mock (EventSource .class );
55
+
56
+ defaultEventSourceManager .registerEventSourceIfNotRegistered (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource );
57
+ defaultEventSourceManager .registerEventSourceIfNotRegistered (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource2 );
58
+
59
+ Map <String , EventSource > registeredEventSources = defaultEventSourceManager
60
+ .getRegisteredEventSources (getUID (customResource ));
61
+ assertThat (registeredEventSources .get (CUSTOM_EVENT_SOURCE_NAME )).isEqualTo (eventSource );
62
+ }
63
+
64
+ @ Test
65
+ public void deRegistersEventSources () {
66
+ CustomResource customResource = TestUtils .testCustomResource ();
67
+ EventSource eventSource = mock (EventSource .class );
68
+ defaultEventSourceManager .registerEventSourceIfNotRegistered (customResource , CUSTOM_EVENT_SOURCE_NAME , eventSource );
69
+
70
+ defaultEventSourceManager .deRegisterEventSource (getUID (customResource ), CUSTOM_EVENT_SOURCE_NAME );
71
+
72
+ assertThat (defaultEventSourceManager .getRegisteredEventSources (getUID (customResource ))).isEmpty ();
73
+ }
74
+
75
+ }
0 commit comments