11
11
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
12
12
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
13
13
import io .javaoperatorsdk .operator .api .monitoring .Metrics ;
14
- import io .javaoperatorsdk .operator .processing .event .CustomResourceID ;
15
- import io .javaoperatorsdk .operator .processing .event .Event ;
16
- import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEvent ;
17
- import io .javaoperatorsdk .operator .processing .event .internal .ResourceAction ;
18
14
import io .javaoperatorsdk .operator .sample .observedgeneration .ObservedGenCustomResource ;
19
15
import io .javaoperatorsdk .operator .sample .simple .TestCustomResource ;
20
16
import org .junit .jupiter .api .BeforeEach ;
23
19
import org .mockito .ArgumentMatchers ;
24
20
25
21
import java .util .ArrayList ;
26
- import java .util .Arrays ;
27
- import java .util .List ;
28
22
29
- import static io .javaoperatorsdk .operator .processing .event .internal .ResourceAction .ADDED ;
30
- import static io .javaoperatorsdk .operator .processing .event .internal .ResourceAction .UPDATED ;
31
23
import static org .assertj .core .api .Assertions .assertThat ;
32
24
import static org .mockito .ArgumentMatchers .argThat ;
33
25
import static org .mockito .Mockito .any ;
@@ -74,9 +66,9 @@ void setup() {
74
66
void addFinalizerOnNewResource () {
75
67
assertFalse (testCustomResource .hasFinalizer (DEFAULT_FINALIZER ));
76
68
eventDispatcher .handleExecution (
77
- executionScopeWithCREvent (ADDED , testCustomResource ));
69
+ executionScopeWithCREvent (testCustomResource ));
78
70
verify (controller , never ())
79
- .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
71
+ .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
80
72
verify (customResourceFacade , times (1 ))
81
73
.replaceWithLock (
82
74
argThat (testCustomResource -> testCustomResource .hasFinalizer (DEFAULT_FINALIZER )));
@@ -87,9 +79,9 @@ void addFinalizerOnNewResource() {
87
79
void callCreateOrUpdateOnNewResourceIfFinalizerSet () {
88
80
testCustomResource .addFinalizer (DEFAULT_FINALIZER );
89
81
eventDispatcher .handleExecution (
90
- executionScopeWithCREvent (ADDED , testCustomResource ));
82
+ executionScopeWithCREvent (testCustomResource ));
91
83
verify (controller , times (1 ))
92
- .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
84
+ .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
93
85
}
94
86
95
87
@ Test
@@ -100,7 +92,7 @@ void updatesOnlyStatusSubResourceIfFinalizerSet() {
100
92
.thenReturn (UpdateControl .updateStatusSubResource (testCustomResource ));
101
93
102
94
eventDispatcher .handleExecution (
103
- executionScopeWithCREvent (ADDED , testCustomResource ));
95
+ executionScopeWithCREvent (testCustomResource ));
104
96
105
97
verify (customResourceFacade , times (1 )).updateStatus (testCustomResource );
106
98
verify (customResourceFacade , never ()).replaceWithLock (any ());
@@ -115,7 +107,7 @@ void updatesBothResourceAndStatusIfFinalizerSet() {
115
107
when (customResourceFacade .replaceWithLock (testCustomResource )).thenReturn (testCustomResource );
116
108
117
109
eventDispatcher .handleExecution (
118
- executionScopeWithCREvent (UPDATED , testCustomResource ));
110
+ executionScopeWithCREvent (testCustomResource ));
119
111
120
112
verify (customResourceFacade , times (1 )).replaceWithLock (testCustomResource );
121
113
verify (customResourceFacade , times (1 )).updateStatus (testCustomResource );
@@ -126,9 +118,9 @@ void callCreateOrUpdateOnModifiedResourceIfFinalizerSet() {
126
118
testCustomResource .addFinalizer (DEFAULT_FINALIZER );
127
119
128
120
eventDispatcher .handleExecution (
129
- executionScopeWithCREvent (UPDATED , testCustomResource ));
121
+ executionScopeWithCREvent (testCustomResource ));
130
122
verify (controller , times (1 ))
131
- .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
123
+ .createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
132
124
}
133
125
134
126
@ Test
@@ -138,7 +130,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
138
130
markForDeletion (testCustomResource );
139
131
140
132
eventDispatcher .handleExecution (
141
- executionScopeWithCREvent (UPDATED , testCustomResource ));
133
+ executionScopeWithCREvent (testCustomResource ));
142
134
143
135
verify (controller , times (1 )).deleteResource (eq (testCustomResource ), any ());
144
136
}
@@ -150,7 +142,7 @@ void callDeleteOnControllerIfMarkedForDeletionWhenNoFinalizerIsConfigured() {
150
142
markForDeletion (testCustomResource );
151
143
152
144
eventDispatcher .handleExecution (
153
- executionScopeWithCREvent (UPDATED , testCustomResource ));
145
+ executionScopeWithCREvent (testCustomResource ));
154
146
155
147
verify (controller ).deleteResource (eq (testCustomResource ), any ());
156
148
}
@@ -160,7 +152,7 @@ void doNotCallDeleteIfMarkedForDeletionWhenFinalizerHasAlreadyBeenRemoved() {
160
152
markForDeletion (testCustomResource );
161
153
162
154
eventDispatcher .handleExecution (
163
- executionScopeWithCREvent (UPDATED , testCustomResource ));
155
+ executionScopeWithCREvent (testCustomResource ));
164
156
165
157
verify (controller , never ()).deleteResource (eq (testCustomResource ), any ());
166
158
}
@@ -181,7 +173,7 @@ void doesNotAddFinalizerIfConfiguredNotTo() {
181
173
configureToNotUseFinalizer ();
182
174
183
175
eventDispatcher .handleExecution (
184
- executionScopeWithCREvent (UPDATED , testCustomResource ));
176
+ executionScopeWithCREvent (testCustomResource ));
185
177
186
178
assertEquals (0 , testCustomResource .getMetadata ().getFinalizers ().size ());
187
179
}
@@ -192,7 +184,7 @@ void removesDefaultFinalizerOnDeleteIfSet() {
192
184
markForDeletion (testCustomResource );
193
185
194
186
eventDispatcher .handleExecution (
195
- executionScopeWithCREvent (UPDATED , testCustomResource ));
187
+ executionScopeWithCREvent (testCustomResource ));
196
188
197
189
assertEquals (0 , testCustomResource .getMetadata ().getFinalizers ().size ());
198
190
verify (customResourceFacade , times (1 )).replaceWithLock (any ());
@@ -207,7 +199,7 @@ void doesNotRemovesTheSetFinalizerIfTheDeleteNotMethodInstructsIt() {
207
199
markForDeletion (testCustomResource );
208
200
209
201
eventDispatcher .handleExecution (
210
- executionScopeWithCREvent (UPDATED , testCustomResource ));
202
+ executionScopeWithCREvent (testCustomResource ));
211
203
212
204
assertEquals (1 , testCustomResource .getMetadata ().getFinalizers ().size ());
213
205
verify (customResourceFacade , never ()).replaceWithLock (any ());
@@ -221,7 +213,7 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet() {
221
213
.thenReturn (UpdateControl .noUpdate ());
222
214
223
215
eventDispatcher .handleExecution (
224
- executionScopeWithCREvent (UPDATED , testCustomResource ));
216
+ executionScopeWithCREvent (testCustomResource ));
225
217
verify (customResourceFacade , never ()).replaceWithLock (any ());
226
218
verify (customResourceFacade , never ()).updateStatus (testCustomResource );
227
219
}
@@ -233,7 +225,7 @@ void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
233
225
.thenReturn (UpdateControl .noUpdate ());
234
226
235
227
eventDispatcher .handleExecution (
236
- executionScopeWithCREvent (UPDATED , testCustomResource ));
228
+ executionScopeWithCREvent (testCustomResource ));
237
229
238
230
assertEquals (1 , testCustomResource .getMetadata ().getFinalizers ().size ());
239
231
verify (customResourceFacade , times (1 )).replaceWithLock (any ());
@@ -245,7 +237,7 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
245
237
markForDeletion (testCustomResource );
246
238
247
239
eventDispatcher .handleExecution (
248
- executionScopeWithCREvent (UPDATED , testCustomResource ));
240
+ executionScopeWithCREvent (testCustomResource ));
249
241
250
242
verify (customResourceFacade , never ()).replaceWithLock (any ());
251
243
verify (controller , never ()).deleteResource (eq (testCustomResource ), any ());
@@ -255,9 +247,9 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
255
247
void executeControllerRegardlessGenerationInNonGenerationAwareModeIfFinalizerSet () {
256
248
testCustomResource .addFinalizer (DEFAULT_FINALIZER );
257
249
eventDispatcher .handleExecution (
258
- executionScopeWithCREvent (UPDATED , testCustomResource ));
250
+ executionScopeWithCREvent (testCustomResource ));
259
251
eventDispatcher .handleExecution (
260
- executionScopeWithCREvent (UPDATED , testCustomResource ));
252
+ executionScopeWithCREvent (testCustomResource ));
261
253
262
254
verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
263
255
}
@@ -300,7 +292,7 @@ void setReScheduleToPostExecutionControlFromUpdateControl() {
300
292
UpdateControl .updateStatusSubResource (testCustomResource ).rescheduleAfter (1000L ));
301
293
302
294
PostExecutionControl control = eventDispatcher .handleExecution (
303
- executionScopeWithCREvent (ADDED , testCustomResource ));
295
+ executionScopeWithCREvent (testCustomResource ));
304
296
305
297
assertThat (control .getReScheduleDelay ().get ()).isEqualTo (1000L );
306
298
}
@@ -311,11 +303,11 @@ void reScheduleOnDeleteWithoutFinalizerRemoval() {
311
303
markForDeletion (testCustomResource );
312
304
313
305
when (controller .deleteResource (eq (testCustomResource ), any ()))
314
- .thenReturn (
315
- DeleteControl .noFinalizerRemoval ().rescheduleAfter (1000L ));
306
+ .thenReturn (
307
+ DeleteControl .noFinalizerRemoval ().rescheduleAfter (1000L ));
316
308
317
- PostExecutionControl control = eventDispatcher . handleExecution (
318
- executionScopeWithCREvent (UPDATED , testCustomResource ));
309
+ PostExecutionControl control =
310
+ eventDispatcher . handleExecution ( executionScopeWithCREvent (testCustomResource ));
319
311
320
312
assertThat (control .getReScheduleDelay ().get ()).isEqualTo (1000L );
321
313
}
@@ -355,13 +347,7 @@ private void removeFinalizers(CustomResource customResource) {
355
347
customResource .getMetadata ().getFinalizers ().clear ();
356
348
}
357
349
358
- public ExecutionScope executionScopeWithCREvent (
359
- ResourceAction action , CustomResource resource , Event ... otherEvents ) {
360
- CustomResourceEvent event =
361
- new CustomResourceEvent (action , CustomResourceID .fromResource (resource ));
362
- List <Event > eventList = new ArrayList <>(1 + otherEvents .length );
363
- eventList .add (event );
364
- eventList .addAll (Arrays .asList (otherEvents ));
365
- return new ExecutionScope (resource , null );
350
+ public <T extends CustomResource <?, ?>> ExecutionScope <T > executionScopeWithCREvent (T resource ) {
351
+ return new ExecutionScope <>(resource , null );
366
352
}
367
353
}
0 commit comments