7d69436aed73ec75686bfed2765b17dd68a24e0e
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes;
21
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.Test;
33 import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
34 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.KubernetesRequestProcessingException;
35 import com.google.gson.Gson;
36 import io.kubernetes.client.custom.IntOrString;
37 import io.kubernetes.client.openapi.ApiClient;
38 import io.kubernetes.client.openapi.ApiException;
39 import io.kubernetes.client.openapi.JSON;
40 import io.kubernetes.client.openapi.models.V1DaemonSet;
41 import io.kubernetes.client.openapi.models.V1DaemonSetSpec;
42 import io.kubernetes.client.openapi.models.V1DaemonSetStatus;
43 import io.kubernetes.client.openapi.models.V1DaemonSetUpdateStrategy;
44 import io.kubernetes.client.openapi.models.V1Deployment;
45 import io.kubernetes.client.openapi.models.V1DeploymentSpec;
46 import io.kubernetes.client.openapi.models.V1DeploymentStatus;
47 import io.kubernetes.client.openapi.models.V1Job;
48 import io.kubernetes.client.openapi.models.V1JobCondition;
49 import io.kubernetes.client.openapi.models.V1JobStatus;
50 import io.kubernetes.client.openapi.models.V1ObjectMeta;
51 import io.kubernetes.client.openapi.models.V1Pod;
52 import io.kubernetes.client.openapi.models.V1PodCondition;
53 import io.kubernetes.client.openapi.models.V1PodStatus;
54 import io.kubernetes.client.openapi.models.V1ReplicaSet;
55 import io.kubernetes.client.openapi.models.V1ReplicaSetSpec;
56 import io.kubernetes.client.openapi.models.V1ReplicaSetStatus;
57 import io.kubernetes.client.openapi.models.V1RollingUpdateDaemonSet;
58 import io.kubernetes.client.openapi.models.V1RollingUpdateStatefulSetStrategy;
59 import io.kubernetes.client.openapi.models.V1Service;
60 import io.kubernetes.client.openapi.models.V1StatefulSet;
61 import io.kubernetes.client.openapi.models.V1StatefulSetSpec;
62 import io.kubernetes.client.openapi.models.V1StatefulSetStatus;
63 import io.kubernetes.client.openapi.models.V1StatefulSetUpdateStrategy;
64 import io.kubernetes.client.util.Watch;
65 import okhttp3.Call;
66 import okhttp3.Response;
67 import okhttp3.ResponseBody;
68 import okio.BufferedSource;
69
70 /**
71  *
72  * @author Waqas Ikram (waqas.ikram@est.tech)
73  *
74  */
75 public class KubernetesClientTest {
76
77     private static final String DUMMY_LABEL_SELECTOR = "app.kubernetes.io/instance=test";
78     private static final String BATCH_V1 = "batch/v1";
79     private static final String V1 = "v1";
80     private static final String APPS_V1 = "apps/v1";
81     private static final String RESPONSE_TYPE_ADDED = "ADDED";
82     private static final String RANDOM_UUID = UUID.randomUUID().toString();
83     private final GsonProvider gsonProvider = new GsonProvider();
84     private final Gson gson = gsonProvider.getGson();
85     private final JSON json = new JSON();
86
87     private final KubernetesClient objUnderTest = new KubernetesClientImpl();
88
89     @Test
90     public void testIsJobReady_jobStatusComplete_true() throws ApiException, IOException {
91
92         final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("Complete", "Running"));
93         assertTrue(objUnderTest.isJobReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
94
95     }
96
97     @Test(expected = KubernetesRequestProcessingException.class)
98     public void testIsJobReady_jobStatusFailed_throwException() throws ApiException, IOException {
99
100         final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("Failed", "Not Running"));
101         objUnderTest.isJobReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
102
103     }
104
105     @Test(expected = KubernetesRequestProcessingException.class)
106     public void testIsJobReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
107
108         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
109         objUnderTest.isJobReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
110
111     }
112
113     @Test(expected = KubernetesRequestProcessingException.class)
114     public void testIsJobReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
115
116         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
117         objUnderTest.isJobReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
118
119     }
120
121     @Test
122     public void testIsJobReady_jobStatusPending_false() throws ApiException, IOException {
123
124         final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("pending", "pending"));
125         assertFalse(objUnderTest.isJobReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
126
127     }
128
129     @Test
130     public void testIsPodReady_statusTrue_true() throws ApiException, IOException {
131
132         final V1PodCondition condition = new V1PodCondition().type("Ready").status(Boolean.TRUE.toString());
133         final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(condition));
134         assertTrue(objUnderTest.isPodReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
135
136     }
137
138     @Test
139     public void testIsPodReady_statusFalse_false() throws ApiException, IOException {
140
141         final V1PodCondition condition = new V1PodCondition().type("Ready").status(Boolean.FALSE.toString());
142         final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(condition));
143         assertFalse(objUnderTest.isPodReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
144
145     }
146
147     @Test
148     public void testIsPodReady_missingCondition_false() throws ApiException, IOException {
149
150         final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(null));
151         assertFalse(objUnderTest.isPodReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
152
153     }
154
155     @Test(expected = KubernetesRequestProcessingException.class)
156     public void testIsPodReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
157
158         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
159         objUnderTest.isPodReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
160
161     }
162
163     @Test(expected = KubernetesRequestProcessingException.class)
164     public void testIsPodReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
165
166         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
167         objUnderTest.isPodReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
168
169     }
170
171     @Test
172     public void testIsServiceReady_exists_true() throws ApiException, IOException {
173         final ApiClient mockedApiClient = mockApiClientResponse(getServiceResponse());
174         assertTrue(objUnderTest.isServiceReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
175
176     }
177
178     @Test(expected = KubernetesRequestProcessingException.class)
179     public void testIsServiceReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
180
181         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
182         objUnderTest.isServiceReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
183
184     }
185
186     @Test(expected = KubernetesRequestProcessingException.class)
187     public void testIsServiceReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
188
189         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
190         objUnderTest.isServiceReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
191
192     }
193
194     @Test
195     public void testIsDeploymentReady_statusAvailable_true() throws ApiException, IOException {
196
197         final V1DeploymentStatus status =
198                 new V1DeploymentStatus().replicas(Integer.valueOf(2)).availableReplicas(Integer.valueOf(2));
199         final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));
200
201         final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(status, spec));
202         assertTrue(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
203
204     }
205
206     @Test
207     public void testIsDeploymentReady_statusNotAvailable_false() throws ApiException, IOException {
208
209         final V1DeploymentStatus status =
210                 new V1DeploymentStatus().replicas(Integer.valueOf(2)).availableReplicas(Integer.valueOf(3));
211         final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));
212
213         final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(status, spec));
214         assertFalse(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
215
216     }
217
218     @Test
219     public void testIsDeploymentReady_statusIsNull_false() throws ApiException, IOException {
220
221         final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));
222
223         final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(null, spec));
224         assertFalse(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
225
226     }
227
228     @Test(expected = KubernetesRequestProcessingException.class)
229     public void testIsDeploymentReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
230
231         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
232         objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
233
234     }
235
236     @Test(expected = KubernetesRequestProcessingException.class)
237     public void testIsDeploymentReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
238
239         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
240         objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
241
242     }
243
244
245     @Test
246     public void testIsReplicaSetReady_statusReady_true() throws ApiException, IOException {
247
248         final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));
249         final V1ReplicaSetSpec spec = new V1ReplicaSetSpec().replicas(Integer.valueOf(1));
250
251         final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, spec));
252         assertTrue(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
253
254     }
255
256     @Test
257     public void testIsReplicaSetReady_statusNotReady_false() throws ApiException, IOException {
258
259         final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));
260         final V1ReplicaSetSpec spec = new V1ReplicaSetSpec().replicas(Integer.valueOf(2));
261
262         final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, spec));
263         assertFalse(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
264
265     }
266
267     @Test
268     public void testIsReplicaSetReady_specIsNull_false() throws ApiException, IOException {
269
270         final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));
271
272         final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, null));
273         assertFalse(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
274
275     }
276
277     @Test(expected = KubernetesRequestProcessingException.class)
278     public void testIsReplicaSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
279
280         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
281         objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
282
283     }
284
285     @Test(expected = KubernetesRequestProcessingException.class)
286     public void testIsReplicaSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
287
288         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
289         objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
290
291     }
292
293     @Test
294     public void testIsDaemonSetReady_statusRollingUpdate_true() throws ApiException, IOException {
295
296         final V1RollingUpdateDaemonSet rollingUpdate =
297                 new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString("50%"));
298         final V1DaemonSetSpec spec = new V1DaemonSetSpec()
299                 .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
300         final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(2))
301                 .numberReady(Integer.valueOf(2)).updatedNumberScheduled(Integer.valueOf(2));
302
303         final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
304         assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
305
306
307     }
308
309     @Test
310     public void testIsDaemonSetReady_statusRollingUpdateTypeRecreate_true() throws ApiException, IOException {
311
312         final V1DaemonSetSpec spec =
313                 new V1DaemonSetSpec().updateStrategy(new V1DaemonSetUpdateStrategy().type("Recreate"));
314         final V1DaemonSetStatus status = new V1DaemonSetStatus();
315
316         final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
317         assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
318
319     }
320
321     @Test
322     public void testIsDaemonSetReady_updateAndDesiredScheduledNumberIsNotSame_true() throws ApiException, IOException {
323
324         final V1DaemonSetSpec spec =
325                 new V1DaemonSetSpec().updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate"));
326         final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(3))
327                 .updatedNumberScheduled(Integer.valueOf(2));
328
329
330         final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
331         assertFalse(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
332
333     }
334
335     @Test
336     public void testIsDaemonSetReady_rollingUpdateWithMaxUnavailableIntValAndDesiredNumberReady_true()
337             throws ApiException, IOException {
338
339         final V1RollingUpdateDaemonSet rollingUpdate =
340                 new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString(4));
341         final V1DaemonSetSpec spec = new V1DaemonSetSpec()
342                 .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
343         final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(6))
344                 .numberReady(Integer.valueOf(2)).updatedNumberScheduled(Integer.valueOf(6));
345
346         final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
347         assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
348
349
350     }
351
352     @Test
353     public void testIsDaemonSetReady_rollingUpdateWithMaxUnavailableIntValAndDesiredNumberNotReady_false()
354             throws ApiException, IOException {
355
356         final V1RollingUpdateDaemonSet rollingUpdate =
357                 new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString(4));
358         final V1DaemonSetSpec spec = new V1DaemonSetSpec()
359                 .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
360         final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(6))
361                 .numberReady(Integer.valueOf(1)).updatedNumberScheduled(Integer.valueOf(6));
362
363         final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
364         assertFalse(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
365
366     }
367
368     @Test(expected = KubernetesRequestProcessingException.class)
369     public void testIsDaemonSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
370
371         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
372         objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
373
374     }
375
376     @Test(expected = KubernetesRequestProcessingException.class)
377     public void testIsDaemonSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
378
379         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
380         objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
381
382     }
383
384     @Test
385     public void testIsStatefulSetReady_rollingUpdateAndReplicasSameAsReadyReplicas_true()
386             throws IOException, ApiException {
387
388         final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("RollingUpdate")
389                 .rollingUpdate(new V1RollingUpdateStatefulSetStrategy().partition(Integer.valueOf(0)));
390         final V1StatefulSetSpec spec =
391                 new V1StatefulSetSpec().updateStrategy(updateStrategy).replicas(Integer.valueOf(2));
392         final V1StatefulSetStatus status =
393                 new V1StatefulSetStatus().updatedReplicas(Integer.valueOf(2)).readyReplicas(Integer.valueOf(2));
394
395         final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
396         assertTrue(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
397
398
399     }
400
401     @Test
402     public void testIsStatefulSetReady_updateStrategyRecreate_true() throws IOException, ApiException {
403
404         final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("Recreate");
405         final V1StatefulSetSpec spec = new V1StatefulSetSpec().updateStrategy(updateStrategy);
406         final V1StatefulSetStatus status = new V1StatefulSetStatus();
407
408         final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
409         assertTrue(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
410
411     }
412
413     @Test
414     public void testIsStatefulSetReady_rollingUpdateAndUnExpectedReplicas_false() throws IOException, ApiException {
415
416         final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("RollingUpdate")
417                 .rollingUpdate(new V1RollingUpdateStatefulSetStrategy().partition(Integer.valueOf(2)));
418         final V1StatefulSetSpec spec =
419                 new V1StatefulSetSpec().updateStrategy(updateStrategy).replicas(Integer.valueOf(6));
420         final V1StatefulSetStatus status = new V1StatefulSetStatus().updatedReplicas(Integer.valueOf(2));
421
422         final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
423         assertFalse(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR));
424     }
425
426     @Test(expected = KubernetesRequestProcessingException.class)
427     public void testIsStatefulSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {
428
429         final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
430         objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
431
432     }
433
434     @Test(expected = KubernetesRequestProcessingException.class)
435     public void testIsStatefulSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {
436
437         final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
438         objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_LABEL_SELECTOR);
439
440     }
441
442     private String getStatefulSetResponse(final V1StatefulSetStatus status, final V1StatefulSetSpec spec) {
443         return gson.toJson(new Watch.Response<V1StatefulSet>(RESPONSE_TYPE_ADDED, getStatefulSet(status, spec)));
444     }
445
446     private V1StatefulSet getStatefulSet(final V1StatefulSetStatus status, final V1StatefulSetSpec spec) {
447         return new V1StatefulSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).spec(spec).status(status);
448     }
449
450     private String getDaemonSetResponse(final V1DaemonSetStatus status, final V1DaemonSetSpec spec) {
451         return gson.toJson(new Watch.Response<V1DaemonSet>(RESPONSE_TYPE_ADDED, getDaemonSet(status, spec)));
452     }
453
454     private V1DaemonSet getDaemonSet(final V1DaemonSetStatus status, final V1DaemonSetSpec spec) {
455         return new V1DaemonSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).spec(spec).status(status);
456     }
457
458     private String getReplicaSetResponse(final V1ReplicaSetStatus status, final V1ReplicaSetSpec spec) {
459         return gson.toJson(new Watch.Response<V1ReplicaSet>(RESPONSE_TYPE_ADDED, getReplicaSet(status, spec)));
460     }
461
462     private V1ReplicaSet getReplicaSet(final V1ReplicaSetStatus status, final V1ReplicaSetSpec spec) {
463         return new V1ReplicaSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).status(status).spec(spec);
464     }
465
466     private String getDeploymentResponse(final V1DeploymentStatus status, final V1DeploymentSpec spec) {
467         return gson.toJson(new Watch.Response<V1Deployment>(RESPONSE_TYPE_ADDED, getDeployment(status, spec)));
468     }
469
470     private V1Deployment getDeployment(final V1DeploymentStatus status, final V1DeploymentSpec spec) {
471         return new V1Deployment().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).status(status).spec(spec);
472     }
473
474     private String getServiceResponse() {
475         return gson.toJson(new Watch.Response<V1Service>(RESPONSE_TYPE_ADDED, getService()));
476     }
477
478     private V1Service getService() {
479         return new V1Service().apiVersion(V1).metadata(getV1ObjectMeta());
480     }
481
482     private String getPodResponse(final V1PodCondition condition) {
483         return gson.toJson(new Watch.Response<V1Pod>(RESPONSE_TYPE_ADDED, getPod(condition)));
484     }
485
486     private V1Pod getPod(final V1PodCondition condition) {
487         final V1Pod pod = new V1Pod().apiVersion(V1).metadata(getV1ObjectMeta());
488         if (condition != null) {
489             return pod.status(new V1PodStatus().addConditionsItem(condition));
490         }
491         return pod;
492     }
493
494     private String getJobResponse(final String type, final String reason) {
495         return gson.toJson(new Watch.Response<V1Job>(RESPONSE_TYPE_ADDED, getJob(type, reason)));
496     }
497
498     private V1Job getJob(final String type, final String reason) {
499         return new V1Job().apiVersion(BATCH_V1).metadata(getV1ObjectMeta()).status(new V1JobStatus()
500                 .addConditionsItem(new V1JobCondition().type(type).status(Boolean.TRUE.toString()).reason(reason)));
501     }
502
503     private V1ObjectMeta getV1ObjectMeta() {
504         return new V1ObjectMeta().name("job-name").namespace("job-namespace").uid(RANDOM_UUID)
505                 .resourceVersion(RANDOM_UUID).labels(Map.of("label-key", "label-value"));
506     }
507
508     private ApiClient mockApiClientResponse(final String response) throws IOException, ApiException {
509         final ApiClient mockedApiClient = mock(ApiClient.class);
510         final Response mockedResponse = mock(Response.class);
511         final ResponseBody mockedResponseBody = mock(ResponseBody.class);
512         final BufferedSource mockedBufferedSource = mock(BufferedSource.class);
513         final Call mockedCall = mock(Call.class);
514
515         when(mockedApiClient.getJSON()).thenReturn(json);
516         doNothing().when(mockedResponse).close();
517         when(mockedResponse.body()).thenReturn(mockedResponseBody);
518         when(mockedBufferedSource.readUtf8Line()).thenReturn(response);
519         when(mockedBufferedSource.exhausted()).thenReturn(false).thenReturn(true);
520         when(mockedResponseBody.source()).thenReturn(mockedBufferedSource);
521         when(mockedResponse.isSuccessful()).thenReturn(true);
522         when(mockedCall.execute()).thenReturn(mockedResponse);
523         when(mockedApiClient.buildCall(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
524                 .thenReturn(mockedCall);
525
526         return mockedApiClient;
527     }
528
529     private ApiClient mockApiClientResponse(Class<? extends Throwable> exception) throws ApiException {
530         final ApiClient mockedApiClient = mock(ApiClient.class);
531         doThrow(exception).when(mockedApiClient).buildCall(any(), any(), any(), any(), any(), any(), any(), any(),
532                 any(), any());
533         return mockedApiClient;
534     }
535 }