e90e64b09f9a2cfdea4eaf924c5154d75d0f9440
[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.tasks;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
23 import static com.github.tomakehurst.wiremock.client.WireMock.get;
24 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
25 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.onap.aaiclient.client.aai.AAIVersion.V19;
31 import java.time.LocalDateTime;
32 import java.util.Optional;
33 import java.util.UUID;
34 import org.camunda.bpm.engine.history.HistoricProcessInstance;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.so.cnfm.lcm.bpmn.flows.BaseTest;
39 import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService;
40 import org.onap.so.cnfm.lcm.database.beans.AsInst;
41 import org.onap.so.cnfm.lcm.database.beans.Job;
42 import org.onap.so.cnfm.lcm.database.beans.State;
43 import org.springframework.beans.factory.annotation.Autowired;
44
45 /**
46  *
47  * @author Waqas Ikram (waqas.ikram@est.tech)
48  *
49  */
50 public class DeleteAsTaskTest extends BaseTest {
51
52     @Autowired
53     private JobExecutorService objUnderTest;
54
55     @Before
56     public void before() {
57         wireMockServer.resetAll();
58     }
59
60     @After
61     public void after() {
62         wireMockServer.resetAll();
63     }
64
65     @Test
66     public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
67         final String asInstanceId = UUID.randomUUID().toString();
68         addDummyAsToDatabase(asInstanceId);
69         mockAaiEndpoints(asInstanceId);
70
71         objUnderTest.runDeleteAsJob(asInstanceId);
72
73         final Optional<Job> optional = getJobByResourceId(asInstanceId);
74         assertTrue(optional.isPresent());
75         final Job job = optional.get();
76
77         assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
78
79         final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
80         assertNotNull(historicProcessInstance);
81         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
82
83         final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
84         assertTrue(optionalAsInst.isEmpty());
85
86     }
87
88     private void mockAaiEndpoints(final String asInstanceId) {
89         final String modelEndpoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + asInstanceId;
90         final String resourceVersion = UUID.randomUUID().toString();
91
92         final String body =
93                 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
94         wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
95         wireMockServer.stubFor(
96                 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));
97     }
98
99     private void addDummyAsToDatabase(final String asInstanceId) {
100         final String asdId = UUID.randomUUID().toString();
101
102         final AsInst asInst = new AsInst().asInstId(asInstanceId).name("asName").asdId(asdId)
103                 .asdInvariantId(asInstanceId).status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now())
104                 .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion")
105                 .asProvider("asProvider").serviceInstanceId(SERVICE_INSTANCE_ID)
106                 .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
107                 .tenantId("tenantId");
108         databaseServiceProvider.saveAsInst(asInst);
109     }
110
111 }