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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
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;
47 * @author Waqas Ikram (waqas.ikram@est.tech)
50 public class DeleteAsTaskTest extends BaseTest {
53 private JobExecutorService objUnderTest;
56 public void before() {
57 wireMockServer.resetAll();
62 wireMockServer.resetAll();
66 public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
67 final String asInstanceId = UUID.randomUUID().toString();
68 addDummyAsToDatabase(asInstanceId);
69 mockAaiEndpoints(asInstanceId);
71 objUnderTest.runDeleteAsJob(asInstanceId);
73 final Optional<Job> optional = getJobByResourceId(asInstanceId);
74 assertTrue(optional.isPresent());
75 final Job job = optional.get();
77 assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
79 final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
80 assertNotNull(historicProcessInstance);
81 assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
83 final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
84 assertTrue(optionalAsInst.isEmpty());
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();
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()));
99 private void addDummyAsToDatabase(final String asInstanceId) {
100 final String asdId = UUID.randomUUID().toString();
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);