summaryrefslogtreecommitdiffstats
path: root/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/extclients/kubernetes/KubernetesClientTest.java
blob: 8dcab508af4fc895cd8db9e20d8353f3abee03f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import org.junit.Test;
import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.KubernetesRequestProcessingException;
import com.google.gson.Gson;
import io.kubernetes.client.custom.IntOrString;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.JSON;
import io.kubernetes.client.openapi.models.V1DaemonSet;
import io.kubernetes.client.openapi.models.V1DaemonSetSpec;
import io.kubernetes.client.openapi.models.V1DaemonSetStatus;
import io.kubernetes.client.openapi.models.V1DaemonSetUpdateStrategy;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentSpec;
import io.kubernetes.client.openapi.models.V1DeploymentStatus;
import io.kubernetes.client.openapi.models.V1Job;
import io.kubernetes.client.openapi.models.V1JobCondition;
import io.kubernetes.client.openapi.models.V1JobStatus;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodCondition;
import io.kubernetes.client.openapi.models.V1PodStatus;
import io.kubernetes.client.openapi.models.V1ReplicaSet;
import io.kubernetes.client.openapi.models.V1ReplicaSetSpec;
import io.kubernetes.client.openapi.models.V1ReplicaSetStatus;
import io.kubernetes.client.openapi.models.V1RollingUpdateDaemonSet;
import io.kubernetes.client.openapi.models.V1RollingUpdateStatefulSetStrategy;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1StatefulSet;
import io.kubernetes.client.openapi.models.V1StatefulSetSpec;
import io.kubernetes.client.openapi.models.V1StatefulSetStatus;
import io.kubernetes.client.openapi.models.V1StatefulSetUpdateStrategy;
import io.kubernetes.client.util.Watch;
import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;

/**
 *
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
public class KubernetesClientTest {

    private static final String DUMMY_NAME_SPACE = "default";
    private static final String DUMMY_LABEL_SELECTOR = "app.kubernetes.io/instance=test";
    private static final String BATCH_V1 = "batch/v1";
    private static final String V1 = "v1";
    private static final String APPS_V1 = "apps/v1";
    private static final String RESPONSE_TYPE_ADDED = "ADDED";
    private static final String RANDOM_UUID = UUID.randomUUID().toString();
    private final GsonProvider gsonProvider = new GsonProvider();
    private final Gson gson = gsonProvider.getGson();
    private final JSON json = new JSON();

    private final KubernetesClient objUnderTest = new KubernetesClientImpl();

    @Test
    public void testIsJobReady_jobStatusComplete_true() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("Complete", "Running"));
        assertTrue(objUnderTest.isJobReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsJobReady_jobStatusFailed_throwException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("Failed", "Not Running"));
        objUnderTest.isJobReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsJobReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isJobReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsJobReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isJobReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test
    public void testIsJobReady_jobStatusPending_false() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(getJobResponse("pending", "pending"));
        assertFalse(objUnderTest.isJobReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsPodReady_statusTrue_true() throws ApiException, IOException {

        final V1PodCondition condition = new V1PodCondition().type("Ready").status(Boolean.TRUE.toString());
        final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(condition));
        assertTrue(objUnderTest.isPodReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsPodReady_statusFalse_false() throws ApiException, IOException {

        final V1PodCondition condition = new V1PodCondition().type("Ready").status(Boolean.FALSE.toString());
        final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(condition));
        assertFalse(objUnderTest.isPodReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsPodReady_missingCondition_false() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(getPodResponse(null));
        assertFalse(objUnderTest.isPodReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsPodReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isPodReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsPodReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isPodReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test
    public void testIsServiceReady_exists_true() throws ApiException, IOException {
        final ApiClient mockedApiClient = mockApiClientResponse(getServiceResponse());
        assertTrue(objUnderTest.isServiceReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsServiceReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isServiceReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsServiceReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isServiceReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test
    public void testIsDeploymentReady_statusAvailable_true() throws ApiException, IOException {

        final V1DeploymentStatus status =
                new V1DeploymentStatus().replicas(Integer.valueOf(2)).availableReplicas(Integer.valueOf(2));
        final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(status, spec));
        assertTrue(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsDeploymentReady_statusNotAvailable_false() throws ApiException, IOException {

        final V1DeploymentStatus status =
                new V1DeploymentStatus().replicas(Integer.valueOf(2)).availableReplicas(Integer.valueOf(3));
        final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(status, spec));
        assertFalse(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsDeploymentReady_statusIsNull_false() throws ApiException, IOException {

        final V1DeploymentSpec spec = new V1DeploymentSpec().replicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getDeploymentResponse(null, spec));
        assertFalse(objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsDeploymentReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsDeploymentReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isDeploymentReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }


    @Test
    public void testIsReplicaSetReady_statusReady_true() throws ApiException, IOException {

        final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));
        final V1ReplicaSetSpec spec = new V1ReplicaSetSpec().replicas(Integer.valueOf(1));

        final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, spec));
        assertTrue(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsReplicaSetReady_statusNotReady_false() throws ApiException, IOException {

        final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));
        final V1ReplicaSetSpec spec = new V1ReplicaSetSpec().replicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, spec));
        assertFalse(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsReplicaSetReady_specIsNull_false() throws ApiException, IOException {

        final V1ReplicaSetStatus status = new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1));

        final ApiClient mockedApiClient = mockApiClientResponse(getReplicaSetResponse(status, null));
        assertFalse(objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsReplicaSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsReplicaSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isReplicaSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test
    public void testIsDaemonSetReady_statusRollingUpdate_true() throws ApiException, IOException {

        final V1RollingUpdateDaemonSet rollingUpdate =
                new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString("50%"));
        final V1DaemonSetSpec spec = new V1DaemonSetSpec()
                .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
        final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(2))
                .numberReady(Integer.valueOf(2)).updatedNumberScheduled(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
        assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));


    }

    @Test
    public void testIsDaemonSetReady_statusRollingUpdateTypeRecreate_true() throws ApiException, IOException {

        final V1DaemonSetSpec spec =
                new V1DaemonSetSpec().updateStrategy(new V1DaemonSetUpdateStrategy().type("Recreate"));
        final V1DaemonSetStatus status = new V1DaemonSetStatus();

        final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
        assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsDaemonSetReady_updateAndDesiredScheduledNumberIsNotSame_true() throws ApiException, IOException {

        final V1DaemonSetSpec spec =
                new V1DaemonSetSpec().updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate"));
        final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(3))
                .updatedNumberScheduled(Integer.valueOf(2));


        final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
        assertFalse(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsDaemonSetReady_rollingUpdateWithMaxUnavailableIntValAndDesiredNumberReady_true()
            throws ApiException, IOException {

        final V1RollingUpdateDaemonSet rollingUpdate =
                new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString(4));
        final V1DaemonSetSpec spec = new V1DaemonSetSpec()
                .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
        final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(6))
                .numberReady(Integer.valueOf(2)).updatedNumberScheduled(Integer.valueOf(6));

        final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
        assertTrue(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));


    }

    @Test
    public void testIsDaemonSetReady_rollingUpdateWithMaxUnavailableIntValAndDesiredNumberNotReady_false()
            throws ApiException, IOException {

        final V1RollingUpdateDaemonSet rollingUpdate =
                new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString(4));
        final V1DaemonSetSpec spec = new V1DaemonSetSpec()
                .updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate").rollingUpdate(rollingUpdate));
        final V1DaemonSetStatus status = new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(6))
                .numberReady(Integer.valueOf(1)).updatedNumberScheduled(Integer.valueOf(6));

        final ApiClient mockedApiClient = mockApiClientResponse(getDaemonSetResponse(status, spec));
        assertFalse(objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsDaemonSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsDaemonSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isDaemonSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test
    public void testIsStatefulSetReady_rollingUpdateAndReplicasSameAsReadyReplicas_true()
            throws IOException, ApiException {

        final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("RollingUpdate")
                .rollingUpdate(new V1RollingUpdateStatefulSetStrategy().partition(Integer.valueOf(0)));
        final V1StatefulSetSpec spec =
                new V1StatefulSetSpec().updateStrategy(updateStrategy).replicas(Integer.valueOf(2));
        final V1StatefulSetStatus status =
                new V1StatefulSetStatus().updatedReplicas(Integer.valueOf(2)).readyReplicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
        assertTrue(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));


    }

    @Test
    public void testIsStatefulSetReady_updateStrategyRecreate_true() throws IOException, ApiException {

        final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("Recreate");
        final V1StatefulSetSpec spec = new V1StatefulSetSpec().updateStrategy(updateStrategy);
        final V1StatefulSetStatus status = new V1StatefulSetStatus();

        final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
        assertTrue(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));

    }

    @Test
    public void testIsStatefulSetReady_rollingUpdateAndUnExpectedReplicas_false() throws IOException, ApiException {

        final V1StatefulSetUpdateStrategy updateStrategy = new V1StatefulSetUpdateStrategy().type("RollingUpdate")
                .rollingUpdate(new V1RollingUpdateStatefulSetStrategy().partition(Integer.valueOf(2)));
        final V1StatefulSetSpec spec =
                new V1StatefulSetSpec().updateStrategy(updateStrategy).replicas(Integer.valueOf(6));
        final V1StatefulSetStatus status = new V1StatefulSetStatus().updatedReplicas(Integer.valueOf(2));

        final ApiClient mockedApiClient = mockApiClientResponse(getStatefulSetResponse(status, spec));
        assertFalse(objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR));
    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsStatefulSetReady_apiExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(ApiException.class);
        objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    @Test(expected = KubernetesRequestProcessingException.class)
    public void testIsStatefulSetReady_RuntimeExceptionThrown_throwsException() throws ApiException, IOException {

        final ApiClient mockedApiClient = mockApiClientResponse(RuntimeException.class);
        objUnderTest.isStatefulSetReady(mockedApiClient, DUMMY_NAME_SPACE, DUMMY_LABEL_SELECTOR);

    }

    private String getStatefulSetResponse(final V1StatefulSetStatus status, final V1StatefulSetSpec spec) {
        return gson.toJson(new Watch.Response<V1StatefulSet>(RESPONSE_TYPE_ADDED, getStatefulSet(status, spec)));
    }

    private V1StatefulSet getStatefulSet(final V1StatefulSetStatus status, final V1StatefulSetSpec spec) {
        return new V1StatefulSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).spec(spec).status(status);
    }

    private String getDaemonSetResponse(final V1DaemonSetStatus status, final V1DaemonSetSpec spec) {
        return gson.toJson(new Watch.Response<V1DaemonSet>(RESPONSE_TYPE_ADDED, getDaemonSet(status, spec)));
    }

    private V1DaemonSet getDaemonSet(final V1DaemonSetStatus status, final V1DaemonSetSpec spec) {
        return new V1DaemonSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).spec(spec).status(status);
    }

    private String getReplicaSetResponse(final V1ReplicaSetStatus status, final V1ReplicaSetSpec spec) {
        return gson.toJson(new Watch.Response<V1ReplicaSet>(RESPONSE_TYPE_ADDED, getReplicaSet(status, spec)));
    }

    private V1ReplicaSet getReplicaSet(final V1ReplicaSetStatus status, final V1ReplicaSetSpec spec) {
        return new V1ReplicaSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).status(status).spec(spec);
    }

    private String getDeploymentResponse(final V1DeploymentStatus status, final V1DeploymentSpec spec) {
        return gson.toJson(new Watch.Response<V1Deployment>(RESPONSE_TYPE_ADDED, getDeployment(status, spec)));
    }

    private V1Deployment getDeployment(final V1DeploymentStatus status, final V1DeploymentSpec spec) {
        return new V1Deployment().apiVersion(APPS_V1).metadata(getV1ObjectMeta()).status(status).spec(spec);
    }

    private String getServiceResponse() {
        return gson.toJson(new Watch.Response<V1Service>(RESPONSE_TYPE_ADDED, getService()));
    }

    private V1Service getService() {
        return new V1Service().apiVersion(V1).metadata(getV1ObjectMeta());
    }

    private String getPodResponse(final V1PodCondition condition) {
        return gson.toJson(new Watch.Response<V1Pod>(RESPONSE_TYPE_ADDED, getPod(condition)));
    }

    private V1Pod getPod(final V1PodCondition condition) {
        final V1Pod pod = new V1Pod().apiVersion(V1).metadata(getV1ObjectMeta());
        if (condition != null) {
            return pod.status(new V1PodStatus().addConditionsItem(condition));
        }
        return pod;
    }

    private String getJobResponse(final String type, final String reason) {
        return gson.toJson(new Watch.Response<V1Job>(RESPONSE_TYPE_ADDED, getJob(type, reason)));
    }

    private V1Job getJob(final String type, final String reason) {
        return new V1Job().apiVersion(BATCH_V1).metadata(getV1ObjectMeta()).status(new V1JobStatus()
                .addConditionsItem(new V1JobCondition().type(type).status(Boolean.TRUE.toString()).reason(reason)));
    }

    private V1ObjectMeta getV1ObjectMeta() {
        return new V1ObjectMeta().name("job-name").namespace("job-namespace").uid(RANDOM_UUID)
                .resourceVersion(RANDOM_UUID).labels(Map.of("label-key", "label-value"));
    }

    private ApiClient mockApiClientResponse(final String response) throws IOException, ApiException {
        final ApiClient mockedApiClient = mock(ApiClient.class);
        final Response mockedResponse = mock(Response.class);
        final ResponseBody mockedResponseBody = mock(ResponseBody.class);
        final BufferedSource mockedBufferedSource = mock(BufferedSource.class);
        final Call mockedCall = mock(Call.class);

        when(mockedApiClient.escapeString(anyString())).thenCallRealMethod();
        when(mockedApiClient.getJSON()).thenReturn(json);
        doNothing().when(mockedResponse).close();
        when(mockedResponse.body()).thenReturn(mockedResponseBody);
        when(mockedBufferedSource.readUtf8Line()).thenReturn(response);
        when(mockedBufferedSource.exhausted()).thenReturn(false).thenReturn(true);
        when(mockedResponseBody.source()).thenReturn(mockedBufferedSource);
        when(mockedResponse.isSuccessful()).thenReturn(true);
        when(mockedCall.execute()).thenReturn(mockedResponse);
        when(mockedApiClient.buildCall(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
                .thenReturn(mockedCall);

        return mockedApiClient;
    }

    private ApiClient mockApiClientResponse(Class<? extends Throwable> exception) throws ApiException {
        final ApiClient mockedApiClient = mock(ApiClient.class);
        doThrow(exception).when(mockedApiClient).buildCall(any(), any(), any(), any(), any(), any(), any(), any(),
                any(), any());
        return mockedApiClient;
    }
}