summaryrefslogtreecommitdiffstats
path: root/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTaskTest.java
blob: 3080459291a58713b9d43696ebe2dd3bc5e15e0e (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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*-
 * ============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.tasks;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.onap.aaiclient.client.aai.AAIVersion.V19;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.so.cnfm.lcm.bpmn.flows.BaseTest;
import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.AsRequestProcessingException;
import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService;
import org.onap.so.cnfm.lcm.database.beans.AsDeploymentItem;
import org.onap.so.cnfm.lcm.database.beans.AsInst;
import org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc;
import org.onap.so.cnfm.lcm.database.beans.AsLifecycleParam;
import org.onap.so.cnfm.lcm.database.beans.Job;
import org.onap.so.cnfm.lcm.database.beans.JobStatusEnum;
import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum;
import org.onap.so.cnfm.lcm.database.beans.State;
import org.onap.so.cnfm.lcm.model.AsInfoModificationRequestDeploymentItems;
import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import io.kubernetes.client.custom.IntOrString;
import io.kubernetes.client.openapi.models.V1DaemonSet;
import io.kubernetes.client.openapi.models.V1DaemonSetList;
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.V1DeploymentList;
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.V1JobList;
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.V1PodList;
import io.kubernetes.client.openapi.models.V1PodStatus;
import io.kubernetes.client.openapi.models.V1ReplicaSet;
import io.kubernetes.client.openapi.models.V1ReplicaSetList;
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.V1ServiceList;
import io.kubernetes.client.openapi.models.V1StatefulSet;
import io.kubernetes.client.openapi.models.V1StatefulSetList;
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;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 */
public class InstantiateAsTaskTest extends BaseTest {
    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 DEPLOYMENT_ITEM_1_RELEASE_NAME = "testOne";
    private static final String DEPLOYMENT_ITEM_2_RELEASE_NAME = "testTwo";
    private static final String DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_1 = ".Values.primary.service.ports.mysql";
    private static final String DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_2 = ".Values.primary.service.nodePorts.mysql";

    private static final String DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_1 = ".Values.service.ports.http";
    private static final String DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_2 = ".Values.service.ports.https";
    private static final String DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_3 = ".Values.service.nodePorts";

    private static final String DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE = "dummy";
    private static final String RANDOM_UUID = UUID.randomUUID().toString();
    private static final String SERVICE_INSTANCE_ID = UUID.randomUUID().toString();
    private static final String AS_INST_ID = SERVICE_INSTANCE_ID;

    private static final String SERVICE_INSTANCE_ID2 = UUID.randomUUID().toString();
    private static final String AS_INST_ID2 = SERVICE_INSTANCE_ID2;
    private static final String ASD_NAME = "InstantiateCnfService";
    private static final String AS_INST_NAME = ASD_NAME + "-" + System.currentTimeMillis();
    private static final String ASD_ID = AS_INST_ID;
    private static final String SRC_TEST_DIR = "src/test/resources";

    private static final String RESOURCE_ASD_PACKAGE_CSAR_PATH =
            SRC_TEST_DIR + "/resource-Generatedasdpackage-csar.csar";

    private static final String AS_DEPLOYMENT_ITEM_1_INST_ID = UUID.randomUUID().toString();
    private static final String AS_DEPLOYMENT_ITEM_2_INST_ID = UUID.randomUUID().toString();
    private static final String AS_DEPLOYMENT_ITEM_1_INST_ID2 = UUID.randomUUID().toString();
    private static final String AS_DEPLOYMENT_ITEM_2_INST_ID2 = UUID.randomUUID().toString();

    @Value("${cnfm.csar.dir}")
    private String dir;

    @Value("${cnfm.kube-configs-dir}")
    private String kubeConfigsDir;

    @Autowired
    private JobExecutorService objUnderTest;

    @Autowired
    private MockedHelmClient mockedHelmClient;

    @Autowired
    private MockedKubernetesClientProvider kubernetesClientProvider;

    @Autowired
    private GsonProvider gsonProvider;
    private Gson gson;

    @Before
    public void before() {
        wireMockServer.resetAll();
        try {
            deleteFoldersAndFiles(Paths.get(kubeConfigsDir));
            Files.createDirectory(Paths.get(kubeConfigsDir));
        } catch (final IOException ioException) {
            throw new RuntimeException(
                    "Failed to create/Delete Directory in InstantiateAsTaskTest due to: " + ioException.getMessage());
        }
        kubernetesClientProvider.setWireMockServer(wireMockServer);

        gson = gsonProvider.getGson();
        mockedHelmClient.clear();
    }

    @After
    public void after() {
        wireMockServer.resetAll();
        final Path path = Paths.get(dir, AS_INST_ID);
        try {
            deleteFoldersAndFiles(path);
            deleteFoldersAndFiles(Paths.get(kubeConfigsDir));
        } catch (final IOException ioException) {
            logger.debug("Exception occurred while deleting folder and files: {}", ioException.getMessage());
        }
    }

    @Test
    public void testInstantiateAsWorkflow_JustUpdateStatus_SuccessfullCase() throws InterruptedException, IOException {

        mockKubernetesClientEndpoint();

        mockAAIEndpoints();

        mockSdcPackageDownloadEndpoint();

        final AsInst asInst = createAsInst(AS_INST_ID, AS_DEPLOYMENT_ITEM_1_INST_ID, AS_DEPLOYMENT_ITEM_2_INST_ID);

        databaseServiceProvider.saveAsInst(asInst);

        createKubeConfigFile(asInst);

        final String asLcmOpOccId = objUnderTest.runInstantiateAsJob(asInst.getAsInstId(), getInstantiateAsRequest());

        final Optional<Job> optional = getJobByResourceId(asInst.getAsInstId());
        assertTrue(optional.isPresent());
        final Job job = optional.get();


        assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));

        final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
        assertNotNull(historicProcessInstance);
        assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());

        final Optional<AsInst> asInstOptional = databaseServiceProvider.getAsInst(asInst.getAsInstId());
        final AsInst actualAsInst = asInstOptional.get();
        assertEquals(State.INSTANTIATED, actualAsInst.getStatus());

        final Optional<AsLcmOpOcc> asLcmOpOccOptional = databaseServiceProvider.getAsLcmOpOcc(asLcmOpOccId);
        assertTrue(asLcmOpOccOptional.isPresent());
        assertEquals(OperationStateEnum.COMPLETED, asLcmOpOccOptional.get().getOperationState());

        final List<AsDeploymentItem> actualAsDeploymentItems =
                databaseServiceProvider.getAsDeploymentItemByAsInstId(actualAsInst.getAsInstId());
        assertEquals(2, actualAsDeploymentItems.size());

        actualAsDeploymentItems.forEach(asDeploymentItem -> {
            assertEquals(State.INSTANTIATED, asDeploymentItem.getStatus());
        });

        final Map<String, Integer> counter = mockedHelmClient.getCounter();
        assertEquals(2, counter.size());
        assertEquals(Integer.valueOf(3), counter.get(asInst.getAsdeploymentItems().get(0).getReleaseName()));
        assertEquals(Integer.valueOf(3), counter.get(asInst.getAsdeploymentItems().get(1).getReleaseName()));


    }

    @Test
    public void testInstantiateAsWorkflow_JobResourceFailedToStartUp() throws InterruptedException, IOException {

        final String asInstId = UUID.randomUUID().toString();
        final String asDeploymentItem1InstId = UUID.randomUUID().toString();
        final String asDeploymentItem2InstId = UUID.randomUUID().toString();
        final String release_name_3 = "testThree";
        final String release_name_4 = "testFour";

        final String jobResourceResponse = gson.toJson(new Watch.Response<V1Job>(RESPONSE_TYPE_ADDED,
                new V1Job().apiVersion(BATCH_V1).metadata(getV1ObjectMeta())
                        .status(new V1JobStatus().addConditionsItem(new V1JobCondition().type("Failed")
                                .status(Boolean.TRUE.toString()).reason("Image not found")))));

        final AsInst asInst = createAsInst(asInstId, asDeploymentItem1InstId, asDeploymentItem2InstId, release_name_3,
                release_name_4, asInstId);
        databaseServiceProvider.saveAsInst(asInst);

        wireMockServer.stubFor(get(urlMatching("/apis/batch/v1/jobs\\?labelSelector.*(" + release_name_3 + "|"
                + release_name_4 + ")&timeoutSeconds=1&watch=true")).willReturn(
                        aResponse().withBody(jobResourceResponse).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        mockAAIEndpoints(asInstId, asDeploymentItem1InstId, asDeploymentItem2InstId);
        mockSdcPackageDownloadEndpoint(asInstId);

        createKubeConfigFile(asInst);

        try {
            objUnderTest.runInstantiateAsJob(asInst.getAsInstId(), getInstantiateAsRequest());
        } catch (final Exception exception) {
            assertEquals(AsRequestProcessingException.class, exception.getClass());
        }

        final Optional<Job> optional = getJobByResourceId(asInst.getAsInstId());
        assertTrue(optional.isPresent());
        final Job job = optional.get();


        assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));

        final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
        assertNotNull(historicProcessInstance);
        assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());

        final Optional<AsInst> asInstOptional = databaseServiceProvider.getAsInst(asInst.getAsInstId());
        final AsInst actualAsInst = asInstOptional.get();
        assertEquals(State.FAILED, actualAsInst.getStatus());


    }

    @Test(expected = AsRequestProcessingException.class)
    public void testInstantiateAsWorkflow_LifecycleParametersMissing_Fail() throws InterruptedException, IOException {

        mockSdcPackageDownloadEndpoint();

        final AsLifecycleParam lcp3 = new AsLifecycleParam().asLifecycleParam(".Values.extra.missing");
        final AsInst asInst1 = createAsInst(AS_INST_ID2, AS_DEPLOYMENT_ITEM_1_INST_ID2, AS_DEPLOYMENT_ITEM_2_INST_ID2);
        asInst1.getAsdeploymentItems().get(0).asLifecycleParams(lcp3);

        databaseServiceProvider.saveAsInst(asInst1);

        createKubeConfigFile(asInst1);

        objUnderTest.runInstantiateAsJob(asInst1.getAsInstId(), getInstantiateAsRequest());

    }

    @Test
    public void testInstantiateAsWorkflow_UpdateAsInstState_ExceptionCase() {

        final AsInst asInst = new AsInst().asInstId(UUID.randomUUID().toString()).name(AS_INST_NAME).asdId(ASD_ID)
                .asdInvariantId(AS_INST_ID).status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now())
                .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion")
                .asProvider("asProvider").serviceInstanceId(SERVICE_INSTANCE_ID)
                .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
                .tenantId("tenantId");

        databaseServiceProvider.saveAsInst(asInst);

        assertThrows(AsRequestProcessingException.class,
                () -> objUnderTest.runInstantiateAsJob(asInst.getAsInstId(), getInstantiateAsRequest()));

        final Optional<Job> optional = getJobByResourceId(asInst.getAsInstId());
        final Job job = optional.get();

        final Optional<AsInst> asInstOptional = databaseServiceProvider.getAsInst(asInst.getAsInstId());

        assertEquals(JobStatusEnum.ERROR, job.getStatus());
        assertEquals(State.FAILED, asInstOptional.get().getStatus());
    }

    private void mockSdcPackageDownloadEndpoint() throws IOException {
        mockSdcPackageDownloadEndpoint(ASD_ID);
    }

    private void mockSdcPackageDownloadEndpoint(final String asdId) throws IOException {
        wireMockServer.stubFor(get("/sdc/v1/catalog/resources/" + asdId + "/toscaModel")
                .willReturn(aResponse().withBody(getFileContent(getAbsolutePath(RESOURCE_ASD_PACKAGE_CSAR_PATH)))
                        .withHeader(ACCEPT, APPLICATION_OCTET_STREAM_VALUE)));
    }

    private void mockKubernetesClientEndpoint() {
        wireMockServer.stubFor(get(urlMatching("/apis/batch/v1/jobs\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME
                + "|" + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                        .willReturn(aResponse().withBody(getJobResponse()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/apis/batch/v1/jobs\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getJobList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer.stubFor(get(urlMatching("/api/v1/pods\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME + "|"
                + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                        .willReturn(aResponse().withBody(getPodResponse()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/api/v1/pods\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getPodList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer.stubFor(get(urlMatching("/api/v1/services\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME
                + "|" + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true")).willReturn(
                        aResponse().withBody(getServiceResponse()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/api/v1/services\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getServiceList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer
                .stubFor(get(urlMatching("/apis/apps/v1/deployments\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME
                        + "|" + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                                .willReturn(aResponse().withBody(getDeploymentResponse()).withHeader(ACCEPT,
                                        APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/apis/apps/v1/deployments\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getDeploymentList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer
                .stubFor(get(urlMatching("/apis/apps/v1/replicasets\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME
                        + "|" + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                                .willReturn(aResponse().withBody(getReplicaSetResponse()).withHeader(ACCEPT,
                                        APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/apis/apps/v1/replicasets\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getReplicaSetList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer
                .stubFor(get(urlMatching("/apis/apps/v1/daemonsets\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME
                        + "|" + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                                .willReturn(aResponse().withBody(getDaemonSetResponse()).withHeader(ACCEPT,
                                        APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/apis/apps/v1/daemonsets\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getDaemonSetList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));

        wireMockServer.stubFor(
                get(urlMatching("/apis/apps/v1/statefulsets\\?labelSelector.*(" + DEPLOYMENT_ITEM_1_RELEASE_NAME + "|"
                        + DEPLOYMENT_ITEM_2_RELEASE_NAME + ")&timeoutSeconds=1&watch=true"))
                                .willReturn(aResponse().withBody(getStatefulSetResponse()).withHeader(ACCEPT,
                                        APPLICATION_JSON_VALUE)));
        wireMockServer.stubFor(get(urlMatching("/apis/apps/v1/statefulsets\\?labelSelector.*&watch=false"))
                .willReturn(aResponse().withBody(getStatefulSetList()).withHeader(ACCEPT, APPLICATION_JSON_VALUE)));
    }

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

    private String getStatefulSetList() {
        final V1StatefulSetList v1StatefulSetList = new V1StatefulSetList();
        v1StatefulSetList.addItemsItem(getStatefulSet());
        return gson.toJson(v1StatefulSetList);
    }

    private V1StatefulSet getStatefulSet() {
        return new V1StatefulSet()
                .apiVersion(
                        APPS_V1)
                .metadata(getV1ObjectMeta())
                .spec(new V1StatefulSetSpec()
                        .updateStrategy(new V1StatefulSetUpdateStrategy().type("RollingUpdate")
                                .rollingUpdate(new V1RollingUpdateStatefulSetStrategy().partition(Integer.valueOf(0))))
                        .replicas(Integer.valueOf(2)))
                .status(new V1StatefulSetStatus().updatedReplicas(Integer.valueOf(2))
                        .readyReplicas(Integer.valueOf(2)));
    }

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

    private String getDaemonSetList() {
        final V1DaemonSetList v1DaemonSetList = new V1DaemonSetList();
        v1DaemonSetList.addItemsItem(getDaemonSet());
        return gson.toJson(v1DaemonSetList);
    }

    private V1DaemonSet getDaemonSet() {
        return new V1DaemonSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta())
                .spec(new V1DaemonSetSpec().updateStrategy(new V1DaemonSetUpdateStrategy().type("RollingUpdate")
                        .rollingUpdate(new V1RollingUpdateDaemonSet().maxUnavailable(new IntOrString("50%")))))
                .status(new V1DaemonSetStatus().desiredNumberScheduled(Integer.valueOf(2))
                        .numberReady(Integer.valueOf(2)).updatedNumberScheduled(Integer.valueOf(2)));
    }

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

    private String getReplicaSetList() {
        final V1ReplicaSetList v1ReplicaSetList = new V1ReplicaSetList();
        v1ReplicaSetList.addItemsItem(getReplicaSet());
        return gson.toJson(v1ReplicaSetList);
    }

    private V1ReplicaSet getReplicaSet() {
        return new V1ReplicaSet().apiVersion(APPS_V1).metadata(getV1ObjectMeta())
                .status(new V1ReplicaSetStatus().readyReplicas(Integer.valueOf(1)))
                .spec(new V1ReplicaSetSpec().replicas(Integer.valueOf(1)));
    }

    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 String getDeploymentResponse() {
        return gson.toJson(new Watch.Response<V1Deployment>(RESPONSE_TYPE_ADDED, getDeployment()));
    }

    private String getDeploymentList() {
        final V1DeploymentList v1DeploymentList = new V1DeploymentList();
        v1DeploymentList.addItemsItem(getDeployment());
        return gson.toJson(v1DeploymentList);
    }

    private V1Deployment getDeployment() {
        return new V1Deployment().apiVersion(APPS_V1).metadata(getV1ObjectMeta())
                .status(new V1DeploymentStatus().replicas(Integer.valueOf(1)).availableReplicas(Integer.valueOf(1)))
                .spec(new V1DeploymentSpec().replicas(Integer.valueOf(1)));
    }

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

    }

    private String getServiceList() {
        final V1ServiceList v1ServiceList = new V1ServiceList();
        v1ServiceList.addItemsItem(getService());
        return gson.toJson(v1ServiceList);
    }

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

    private String getPodList() {
        final V1PodList v1Podlist = new V1PodList();
        v1Podlist.addItemsItem(getPod());
        return gson.toJson(v1Podlist);
    }

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

    private V1Pod getPod() {
        return new V1Pod().apiVersion(V1).metadata(getV1ObjectMeta()).status(new V1PodStatus()
                .addConditionsItem(new V1PodCondition().type("Ready").status(Boolean.TRUE.toString())));
    }

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

    private String getJobList() {
        final V1JobList v1JobList = new V1JobList();
        v1JobList.addItemsItem(getJob());
        return gson.toJson(v1JobList);
    }

    private V1Job getJob() {
        return new V1Job().apiVersion(BATCH_V1).metadata(getV1ObjectMeta()).status(new V1JobStatus()
                .addConditionsItem(new V1JobCondition().type("Complete").status(Boolean.TRUE.toString())));
    }

    private void mockAAIEndpoints() throws JsonProcessingException {
        mockAAIEndpoints(AS_INST_ID, AS_DEPLOYMENT_ITEM_1_INST_ID, AS_DEPLOYMENT_ITEM_2_INST_ID);
    }

    private void mockAAIEndpoints(final String as_inst_id, final String as_deployment_item_1_id,
            final String as_deployment_item_2_id) throws JsonProcessingException {
        final String vfModule1EndPoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + as_inst_id
                + "/vf-modules/vf-module/" + as_deployment_item_1_id;

        wireMockServer.stubFor(get(urlMatching(vfModule1EndPoint + "\\?resultIndex=0&resultSize=1&format=count"))
                .willReturn(notFound()));

        final String vfModule2EndPoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + as_inst_id
                + "/vf-modules/vf-module/" + as_deployment_item_2_id;

        wireMockServer.stubFor(get(urlMatching(vfModule2EndPoint + "\\?resultIndex=0&resultSize=1&format=count"))
                .willReturn(notFound()));

        wireMockServer.stubFor(put(urlMatching(vfModule1EndPoint)).willReturn(ok()));
        wireMockServer.stubFor(put(urlMatching(vfModule2EndPoint)).willReturn(ok()));

        final String k8sResourcesEndpoint = "/aai/" + V19
                + "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner/cloudRegion/tenants/tenant/tenantId/"
                + "k8s-resources/.*";
        wireMockServer.stubFor(get(urlMatching(k8sResourcesEndpoint)).willReturn(notFound()));
        wireMockServer.stubFor(put(urlMatching(k8sResourcesEndpoint)).willReturn(ok()));
        wireMockServer
                .stubFor(put(urlMatching(k8sResourcesEndpoint + "/relationship-list/relationship")).willReturn(ok()));


    }

    private AsInst createAsInst(final String as_inst_id, final String as_deployment_item_1_id,
            final String as_deployment_item_2_id) {
        return createAsInst(as_inst_id, as_deployment_item_1_id, as_deployment_item_2_id,
                DEPLOYMENT_ITEM_1_RELEASE_NAME, DEPLOYMENT_ITEM_2_RELEASE_NAME, ASD_ID);
    }

    private AsInst createAsInst(final String as_inst_id, final String as_deployment_item_1_id,
            final String as_deployment_item_2_id, final String as_deployment_item_1_release_name,
            final String as_deployment_item_2_release_name, final String asdId) {
        final AsInst asInst = new AsInst().asInstId(as_inst_id).name(AS_INST_NAME).asdId(asdId)
                .asdInvariantId(as_inst_id).status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now())
                .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion")
                .asProvider("asProvider").serviceInstanceId(as_inst_id).serviceInstanceName("serviceInstanceName")
                .cloudOwner("cloudOwner").cloudRegion("cloudRegion").tenantId("tenantId");

        final String helmFile1 = "Artifacts/Deployment/HELM/sampleapp-db-operator-helm.tgz";
        final AsLifecycleParam lcp1 = new AsLifecycleParam().asLifecycleParam(DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_1);
        final AsLifecycleParam lcp2 = new AsLifecycleParam().asLifecycleParam(DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_2);

        final AsDeploymentItem item1 = new AsDeploymentItem().asDeploymentItemInstId(as_deployment_item_1_id)
                .asInst(asInst).status(State.NOT_INSTANTIATED).name("sampleapp-db").itemId("1").deploymentOrder(1)
                .artifactFilePath(helmFile1).createTime(LocalDateTime.now()).lastUpdateTime(LocalDateTime.now())
                .releaseName(as_deployment_item_1_release_name).asLifecycleParams(lcp1).asLifecycleParams(lcp2);

        final String helmFile2 = "Artifacts/Deployment/HELM/sampleapp-services-helm.tgz";
        final AsLifecycleParam lcpitem2_1 =
                new AsLifecycleParam().asLifecycleParam(DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_1);
        final AsLifecycleParam lcpitem2_2 =
                new AsLifecycleParam().asLifecycleParam(DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_2);
        final AsLifecycleParam lcpitem2_3 =
                new AsLifecycleParam().asLifecycleParam(DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_3);

        final AsDeploymentItem item2 = new AsDeploymentItem().asDeploymentItemInstId(as_deployment_item_2_id)
                .asInst(asInst).status(State.NOT_INSTANTIATED).name("sampleapp-services").itemId("2").deploymentOrder(2)
                .artifactFilePath(helmFile2).createTime(LocalDateTime.now()).lastUpdateTime(LocalDateTime.now())
                .releaseName(as_deployment_item_2_release_name).asLifecycleParams(lcpitem2_1)
                .asLifecycleParams(lcpitem2_2).asLifecycleParams(lcpitem2_3);

        asInst.asdeploymentItems(item1);
        asInst.asdeploymentItems(item2);
        return asInst;
    }

    private InstantiateAsRequest getInstantiateAsRequest() {
        final AsInfoModificationRequestDeploymentItems lifecycleParams_1 =
                new AsInfoModificationRequestDeploymentItems().deploymentItemsId("1").lifecycleParameterKeyValues(
                        Map.of(DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_1, DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE,
                                DEPLOYMENT_ITEM_1_LIFECYCLE_PARAM_2, DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE));

        final AsInfoModificationRequestDeploymentItems lifecycleParams_2 =
                new AsInfoModificationRequestDeploymentItems().deploymentItemsId("2").lifecycleParameterKeyValues(
                        Map.of(DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_1, DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE,
                                DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_2, DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE,
                                DEPLOYMENT_ITEM_2_LIFECYCLE_PARAM_3, DEPLOYMENT_ITEM_LIFECYCLE_PARAM_VALUE));

        return new InstantiateAsRequest().addDeploymentItemsItem(lifecycleParams_1)
                .addDeploymentItemsItem(lifecycleParams_2);
    }

    private Path getAbsolutePath(final String path) {
        final File file = new File(path);
        return file.toPath();
    }

    private byte[] getFileContent(final Path path) throws IOException {
        return Files.readAllBytes(path);
    }

}