adb8a9e69bd3f0439403bd4cef6dce423abc7ff2
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / test / java / org / onap / so / etsi / nfvo / ns / workflow / engine / tasks / DeleteNsTaskTest.java
1 /*-
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
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.etsi.nfvo.ns.workflow.engine.tasks;
21
22 import com.google.gson.Gson;
23 import org.camunda.bpm.engine.history.HistoricProcessInstance;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.BaseTest;
30 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
31 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
32 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.service.JobExecutorService;
33 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
34 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
35 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import java.time.LocalDateTime;
38 import java.util.Optional;
39 import java.util.UUID;
40 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
41 import static com.github.tomakehurst.wiremock.client.WireMock.get;
42 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
43 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
44 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48
49 /**
50  * @author Andrew Lamb (andrew.a.lamb@est.tech)
51  *
52  */
53 public class DeleteNsTaskTest extends BaseTest {
54
55     @Autowired
56     private JobExecutorService objUnderTest;
57
58     @Autowired
59     private GsonProvider gsonProvider;
60
61     @Rule
62     public ExpectedException expectedException = ExpectedException.none();
63
64     private Gson gson;
65
66     @Before
67     public void before() {
68         wireMockServer.resetAll();
69         gson = gsonProvider.getGson();
70     }
71
72     @After
73     public void after() {
74         wireMockServer.resetAll();
75     }
76
77     @Test
78     public void testRunDeleteNsJob_nsInstanceIdNotInDatabase_throwsException() {
79         final String nsInstanceId = UUID.randomUUID().toString();
80         assertTrue(databaseServiceProvider.getNfvoNsInst(nsInstanceId).isEmpty());
81
82         expectedException.expect(NsRequestProcessingException.class);
83         objUnderTest.runDeleteNsJob(nsInstanceId);
84     }
85
86     @Test
87     public void testRunDeleteNsJob_nsInstanceStateInstantiated_throwsException() {
88         final String nsInstanceId = UUID.randomUUID().toString();
89         addDummyNsToDatabase(nsInstanceId, State.INSTANTIATED);
90
91         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
92         assertTrue(optionalNfvoNsInst.isPresent());
93         assertEquals(State.INSTANTIATED, optionalNfvoNsInst.get().getStatus());
94
95         expectedException.expect(NsRequestProcessingException.class);
96         objUnderTest.runDeleteNsJob(nsInstanceId);
97     }
98
99     @Test
100     public void testRunDeleteNsJob_nsInstanceStateInstantiating_throwsException() {
101         final String nsInstanceId = UUID.randomUUID().toString();
102         addDummyNsToDatabase(nsInstanceId, State.INSTANTIATING);
103
104         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
105         assertTrue(optionalNfvoNsInst.isPresent());
106         assertEquals(State.INSTANTIATING, optionalNfvoNsInst.get().getStatus());
107
108         expectedException.expect(NsRequestProcessingException.class);
109         objUnderTest.runDeleteNsJob(nsInstanceId);
110     }
111
112     @Test
113     public void testRunDeleteNsJob_nsInstanceStateTerminating_throwsException() {
114         final String nsInstanceId = UUID.randomUUID().toString();
115         addDummyNsToDatabase(nsInstanceId, State.TERMINATING);
116
117         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
118         assertTrue(optionalNfvoNsInst.isPresent());
119         assertEquals(State.TERMINATING, optionalNfvoNsInst.get().getStatus());
120
121         expectedException.expect(NsRequestProcessingException.class);
122         objUnderTest.runDeleteNsJob(nsInstanceId);
123     }
124
125     @Test
126     public void testRunDeleteNsJob_nsInstanceStateFailed_throwsException() {
127         final String nsInstanceId = UUID.randomUUID().toString();
128         addDummyNsToDatabase(nsInstanceId, State.FAILED);
129
130         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
131         assertTrue(optionalNfvoNsInst.isPresent());
132         assertEquals(State.FAILED, optionalNfvoNsInst.get().getStatus());
133
134         expectedException.expect(NsRequestProcessingException.class);
135         objUnderTest.runDeleteNsJob(nsInstanceId);
136     }
137
138     @Test
139     public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
140         final String nsInstanceId = UUID.randomUUID().toString();
141         addDummyNsToDatabase(nsInstanceId, State.NOT_INSTANTIATED);
142         mockAaiEndpoints();
143
144         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
145         assertTrue(optionalNfvoNsInst.isPresent());
146         assertEquals(State.NOT_INSTANTIATED, optionalNfvoNsInst.get().getStatus());
147
148         objUnderTest.runDeleteNsJob(nsInstanceId);
149
150         final Optional<NfvoJob> optional = getJobByResourceId(nsInstanceId);
151         assertTrue(optional.isPresent());
152         final NfvoJob nfvoJob = optional.get();
153
154         // Confirm Process finishes in STATE_COMPLETED
155         assertTrue(waitForProcessInstanceToFinish(nfvoJob.getProcessInstanceId()));
156         final HistoricProcessInstance historicProcessInstance =
157                 getHistoricProcessInstance(nfvoJob.getProcessInstanceId());
158         assertNotNull(historicProcessInstance);
159         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
160
161         // Confirm NS Instance no longer in database
162         final Optional<NfvoNsInst> optionalNfvoNsInstance = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
163         assertTrue(optionalNfvoNsInstance.isEmpty());
164     }
165
166     private void addDummyNsToDatabase(final String nsInstanceId, final State state) {
167         final String nsPackageId = UUID.randomUUID().toString();
168         final NfvoNsInst nfvoNsInst = new NfvoNsInst().nsInstId(nsInstanceId).name("nsName").nsPackageId(nsPackageId)
169                 .nsdId("nsdId").nsdInvariantId("nsdId").status(state).statusUpdatedTime(LocalDateTime.now())
170                 .globalCustomerId(GLOBAL_CUSTOMER_ID).serviceType(SERVICE_TYPE);
171         databaseServiceProvider.saveNfvoNsInst(nfvoNsInst);
172     }
173
174     private void mockAaiEndpoints() {
175         final String modelEndpoint = getAaiServiceInstanceEndPoint();
176         final String resourceVersion = "12345";
177
178         final String body =
179                 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
180         wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
181
182         wireMockServer.stubFor(
183                 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));
184     }
185 }