2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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.etsi.nfvo.ns.workflow.engine.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 java.time.LocalDateTime;
31 import java.util.Optional;
32 import java.util.UUID;
33 import org.camunda.bpm.engine.history.HistoricProcessInstance;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.BaseTest;
40 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
41 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.service.JobExecutorService;
42 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
43 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
44 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
45 import org.springframework.beans.factory.annotation.Autowired;
48 * @author Andrew Lamb (andrew.a.lamb@est.tech)
51 public class DeleteNsTaskTest extends BaseTest {
54 private JobExecutorService objUnderTest;
57 public ExpectedException expectedException = ExpectedException.none();
60 public void before() {
61 wireMockServer.resetAll();
66 wireMockServer.resetAll();
70 public void testRunDeleteNsJob_nsInstanceIdNotInDatabase_throwsException() {
71 final String nsInstanceId = UUID.randomUUID().toString();
72 assertTrue(databaseServiceProvider.getNfvoNsInst(nsInstanceId).isEmpty());
74 expectedException.expect(NsRequestProcessingException.class);
75 objUnderTest.runDeleteNsJob(nsInstanceId);
79 public void testRunDeleteNsJob_nsInstanceStateInstantiated_throwsException() {
80 final String nsInstanceId = UUID.randomUUID().toString();
81 addDummyNsToDatabase(nsInstanceId, State.INSTANTIATED);
83 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
84 assertTrue(optionalNfvoNsInst.isPresent());
85 assertEquals(State.INSTANTIATED, optionalNfvoNsInst.get().getStatus());
87 expectedException.expect(NsRequestProcessingException.class);
88 objUnderTest.runDeleteNsJob(nsInstanceId);
92 public void testRunDeleteNsJob_nsInstanceStateInstantiating_throwsException() {
93 final String nsInstanceId = UUID.randomUUID().toString();
94 addDummyNsToDatabase(nsInstanceId, State.INSTANTIATING);
96 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
97 assertTrue(optionalNfvoNsInst.isPresent());
98 assertEquals(State.INSTANTIATING, optionalNfvoNsInst.get().getStatus());
100 expectedException.expect(NsRequestProcessingException.class);
101 objUnderTest.runDeleteNsJob(nsInstanceId);
105 public void testRunDeleteNsJob_nsInstanceStateTerminating_throwsException() {
106 final String nsInstanceId = UUID.randomUUID().toString();
107 addDummyNsToDatabase(nsInstanceId, State.TERMINATING);
109 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
110 assertTrue(optionalNfvoNsInst.isPresent());
111 assertEquals(State.TERMINATING, optionalNfvoNsInst.get().getStatus());
113 expectedException.expect(NsRequestProcessingException.class);
114 objUnderTest.runDeleteNsJob(nsInstanceId);
118 public void testRunDeleteNsJob_nsInstanceStateFailed_throwsException() {
119 final String nsInstanceId = UUID.randomUUID().toString();
120 addDummyNsToDatabase(nsInstanceId, State.FAILED);
122 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
123 assertTrue(optionalNfvoNsInst.isPresent());
124 assertEquals(State.FAILED, optionalNfvoNsInst.get().getStatus());
126 expectedException.expect(NsRequestProcessingException.class);
127 objUnderTest.runDeleteNsJob(nsInstanceId);
131 public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
132 final String nsInstanceId = UUID.randomUUID().toString();
133 addDummyNsToDatabase(nsInstanceId, State.NOT_INSTANTIATED);
136 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
137 assertTrue(optionalNfvoNsInst.isPresent());
138 assertEquals(State.NOT_INSTANTIATED, optionalNfvoNsInst.get().getStatus());
140 objUnderTest.runDeleteNsJob(nsInstanceId);
142 final Optional<NfvoJob> optional = getJobByResourceId(nsInstanceId);
143 assertTrue(optional.isPresent());
144 final NfvoJob nfvoJob = optional.get();
146 // Confirm Process finishes in STATE_COMPLETED
147 assertTrue(waitForProcessInstanceToFinish(nfvoJob.getProcessInstanceId()));
148 final HistoricProcessInstance historicProcessInstance =
149 getHistoricProcessInstance(nfvoJob.getProcessInstanceId());
150 assertNotNull(historicProcessInstance);
151 assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
153 // Confirm NS Instance no longer in database
154 final Optional<NfvoNsInst> optionalNfvoNsInstance = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
155 assertTrue(optionalNfvoNsInstance.isEmpty());
158 private void addDummyNsToDatabase(final String nsInstanceId, final State state) {
159 final String nsPackageId = UUID.randomUUID().toString();
160 final NfvoNsInst nfvoNsInst = new NfvoNsInst().nsInstId(nsInstanceId).name("nsName").nsPackageId(nsPackageId)
161 .nsdId("nsdId").nsdInvariantId("nsdId").status(state).statusUpdatedTime(LocalDateTime.now())
162 .globalCustomerId(GLOBAL_CUSTOMER_ID).serviceType(SERVICE_TYPE);
163 databaseServiceProvider.saveNfvoNsInst(nfvoNsInst);
166 private void mockAaiEndpoints() {
167 final String modelEndpoint = getAaiServiceInstanceEndPoint();
168 final String resourceVersion = "12345";
171 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
172 wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
174 wireMockServer.stubFor(
175 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));