1
1
package io .javaoperatorsdk .operator .processing .event .source ;
2
2
3
- import java .io .Serializable ;
4
- import java .util .Objects ;
5
-
6
3
import javax .cache .Cache ;
7
4
import javax .cache .CacheManager ;
8
5
import javax .cache .configuration .MutableConfiguration ;
18
15
19
16
import com .github .benmanes .caffeine .jcache .spi .CaffeineCachingProvider ;
20
17
18
+ import static io .javaoperatorsdk .operator .processing .event .source .SampleExternalResource .*;
21
19
import static org .assertj .core .api .Assertions .assertThat ;
22
- import static org .junit .jupiter .api .Assertions .*;
23
20
import static org .mockito .Mockito .*;
24
21
25
22
class CachingFilteringEventSourceTest {
26
23
27
- public static final String DEFAULT_VALUE = "value" ;
28
24
private CachingFilteringEventSource <SampleExternalResource > cachingFilteringEventSource ;
29
25
private Cache <ResourceID , SampleExternalResource > cache ;
30
26
private EventHandler eventHandlerMock = mock (EventHandler .class );
@@ -47,137 +43,85 @@ public void tearDown() {
47
43
48
44
@ Test
49
45
public void putsNewResourceIntoCacheAndProducesEvent () {
50
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
46
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
51
47
52
- verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResourceID ())));
53
- assertThat (cachingFilteringEventSource .getCachedValue (testResourceID ())).isPresent ();
48
+ verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResource1ID ())));
49
+ assertThat (cachingFilteringEventSource .getCachedValue (testResource1ID ())).isPresent ();
54
50
}
55
51
56
52
@ Test
57
53
public void propagatesEventIfResourceChanged () {
58
- var res2 = testResource ();
54
+ var res2 = testResource1 ();
59
55
res2 .setValue ("changedValue" );
60
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
61
- cachingFilteringEventSource .handleEvent (res2 , testResourceID ());
56
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
57
+ cachingFilteringEventSource .handleEvent (res2 , testResource1ID ());
62
58
63
59
64
- verify (eventHandlerMock , times (2 )).handleEvent (eq (new Event (testResourceID ())));
65
- assertThat (cachingFilteringEventSource .getCachedValue (testResourceID ()).get ()).isEqualTo (res2 );
60
+ verify (eventHandlerMock , times (2 )).handleEvent (eq (new Event (testResource1ID ())));
61
+ assertThat (cachingFilteringEventSource .getCachedValue (testResource1ID ()).get ()).isEqualTo (res2 );
66
62
}
67
63
68
64
@ Test
69
65
public void noEventPropagatedIfTheResourceIsNotChanged () {
70
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
71
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
66
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
67
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
72
68
73
- verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResourceID ())));
74
- assertThat (cachingFilteringEventSource .getCachedValue (testResourceID ())).isPresent ();
69
+ verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResource1ID ())));
70
+ assertThat (cachingFilteringEventSource .getCachedValue (testResource1ID ())).isPresent ();
75
71
}
76
72
77
73
@ Test
78
74
public void supportFilteringEvents () {
79
75
cachingFilteringEventSource = new SimpleCachingFilteringEventSource (cache ,
80
- (newValue , oldValue , relatedResourceID ) -> !newValue .getValue ().equals (DEFAULT_VALUE ));
76
+ (newValue , oldValue , relatedResourceID ) -> !newValue .getValue ().equals (DEFAULT_VALUE_1 ));
81
77
cachingFilteringEventSource .setEventHandler (eventHandlerMock );
82
78
cachingFilteringEventSource .start ();
83
79
84
- var res2 = testResource ();
80
+ var res2 = testResource1 ();
85
81
res2 .setValue ("changedValue" );
86
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
87
- cachingFilteringEventSource .handleEvent (res2 , testResourceID ());
82
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
83
+ cachingFilteringEventSource .handleEvent (res2 , testResource1ID ());
88
84
89
85
90
- verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResourceID ())));
91
- assertThat (cachingFilteringEventSource .getCachedValue (testResourceID ()).get ()).isEqualTo (res2 );
86
+ verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResource1ID ())));
87
+ assertThat (cachingFilteringEventSource .getCachedValue (testResource1ID ()).get ()).isEqualTo (res2 );
92
88
}
93
89
94
90
@ Test
95
91
public void propagatesEventOnDeleteIfThereIsPrevResourceInCache () {
96
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
97
- cachingFilteringEventSource .handleDelete (testResourceID ());
92
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
93
+ cachingFilteringEventSource .handleDelete (testResource1ID ());
98
94
99
- verify (eventHandlerMock , times (2 )).handleEvent (eq (new Event (testResourceID ())));
100
- assertThat (cachingFilteringEventSource .getCachedValue (testResourceID ())).isNotPresent ();
95
+ verify (eventHandlerMock , times (2 )).handleEvent (eq (new Event (testResource1ID ())));
96
+ assertThat (cachingFilteringEventSource .getCachedValue (testResource1ID ())).isNotPresent ();
101
97
}
102
98
103
99
@ Test
104
100
public void noEventOnDeleteIfResourceWasNotInCacheBefore () {
105
- cachingFilteringEventSource .handleDelete (testResourceID ());
101
+ cachingFilteringEventSource .handleDelete (testResource1ID ());
106
102
107
- verify (eventHandlerMock , times (0 )).handleEvent (eq (new Event (testResourceID ())));
103
+ verify (eventHandlerMock , times (0 )).handleEvent (eq (new Event (testResource1ID ())));
108
104
}
109
105
110
106
@ Test
111
107
public void deleteSupportsFiltering () {
112
108
cachingFilteringEventSource = new SimpleCachingFilteringEventSource (cache ,
113
- (newValue , oldValue , relatedResourceID ) -> !newValue .getValue ().equals (DEFAULT_VALUE ));
109
+ (newValue , oldValue , relatedResourceID ) -> !newValue .getValue ().equals (DEFAULT_VALUE_1 ));
114
110
cachingFilteringEventSource .setEventHandler (eventHandlerMock );
115
111
cachingFilteringEventSource .start ();
116
112
117
- cachingFilteringEventSource .handleEvent (testResource (), testResourceID ());
118
- cachingFilteringEventSource .handleDelete (testResourceID ());
119
-
120
- verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResourceID ())));
121
- }
122
-
123
- private ResourceID testResourceID () {
124
- return new ResourceID ("name1" , "test-namespace" );
125
- }
113
+ cachingFilteringEventSource .handleEvent (testResource1 (), testResource1ID ());
114
+ cachingFilteringEventSource .handleDelete (testResource1ID ());
126
115
127
- private SampleExternalResource testResource () {
128
- return new SampleExternalResource ("name1" , DEFAULT_VALUE );
116
+ verify (eventHandlerMock , times (1 )).handleEvent (eq (new Event (testResource1ID ())));
129
117
}
130
-
118
+
131
119
public static class SimpleCachingFilteringEventSource
132
120
extends CachingFilteringEventSource <SampleExternalResource > {
133
121
public SimpleCachingFilteringEventSource (Cache <ResourceID , SampleExternalResource > cache ,
134
122
EventFilter <SampleExternalResource > eventFilter ) {
135
123
super (cache , eventFilter );
136
124
}
137
125
}
138
-
139
- public static class SampleExternalResource implements Serializable {
140
- private String name ;
141
- private String value ;
142
-
143
- public SampleExternalResource (String name , String value ) {
144
- this .name = name ;
145
- this .value = value ;
146
- }
147
-
148
- public String getName () {
149
- return name ;
150
- }
151
-
152
- public SampleExternalResource setName (String name ) {
153
- this .name = name ;
154
- return this ;
155
- }
156
-
157
- public String getValue () {
158
- return value ;
159
- }
160
-
161
- public SampleExternalResource setValue (String value ) {
162
- this .value = value ;
163
- return this ;
164
- }
165
-
166
- @ Override
167
- public boolean equals (Object o ) {
168
- if (this == o )
169
- return true ;
170
- if (o == null || getClass () != o .getClass ())
171
- return false ;
172
- SampleExternalResource that = (SampleExternalResource ) o ;
173
- return Objects .equals (name , that .name ) && Objects .equals (value , that .value );
174
- }
175
-
176
- @ Override
177
- public int hashCode () {
178
- return Objects .hash (name , value );
179
- }
180
-
181
- }
182
-
126
+
183
127
}
0 commit comments