Merge "Removing so-monitoring module"
[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 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;
46
47 /**
48  * @author Andrew Lamb (andrew.a.lamb@est.tech)
49  *
50  */
51 public class DeleteNsTaskTest extends BaseTest {
52
53     @Autowired
54     private JobExecutorService objUnderTest;
55
56     @Rule
57     public ExpectedException expectedException = ExpectedException.none();
58
59     @Before
60     public void before() {
61         wireMockServer.resetAll();
62     }
63
64     @After
65     public void after() {
66         wireMockServer.resetAll();
67     }
68
69     @Test
70     public void testRunDeleteNsJob_nsInstanceIdNotInDatabase_throwsException() {
71         final String nsInstanceId = UUID.randomUUID().toString();
72         assertTrue(databaseServiceProvider.getNfvoNsInst(nsInstanceId).isEmpty());
73
74         expectedException.expect(NsRequestProcessingException.class);
75         objUnderTest.runDeleteNsJob(nsInstanceId);
76     }
77
78     @Test
79     public void testRunDeleteNsJob_nsInstanceStateInstantiated_throwsException() {
80         final String nsInstanceId = UUID.randomUUID().toString();
81         addDummyNsToDatabase(nsInstanceId, State.INSTANTIATED);
82
83         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
84         assertTrue(optionalNfvoNsInst.isPresent());
85         assertEquals(State.INSTANTIATED, optionalNfvoNsInst.get().getStatus());
86
87         expectedException.expect(NsRequestProcessingException.class);
88         objUnderTest.runDeleteNsJob(nsInstanceId);
89     }
90
91     @Test
92     public void testRunDeleteNsJob_nsInstanceStateInstantiating_throwsException() {
93         final String nsInstanceId = UUID.randomUUID().toString();
94         addDummyNsToDatabase(nsInstanceId, State.INSTANTIATING);
95
96         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
97         assertTrue(optionalNfvoNsInst.isPresent());
98         assertEquals(State.INSTANTIATING, optionalNfvoNsInst.get().getStatus());
99
100         expectedException.expect(NsRequestProcessingException.class);
101         objUnderTest.runDeleteNsJob(nsInstanceId);
102     }
103
104     @Test
105     public void testRunDeleteNsJob_nsInstanceStateTerminating_throwsException() {
106         final String nsInstanceId = UUID.randomUUID().toString();
107         addDummyNsToDatabase(nsInstanceId, State.TERMINATING);
108
109         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
110         assertTrue(optionalNfvoNsInst.isPresent());
111         assertEquals(State.TERMINATING, optionalNfvoNsInst.get().getStatus());
112
113         expectedException.expect(NsRequestProcessingException.class);
114         objUnderTest.runDeleteNsJob(nsInstanceId);
115     }
116
117     @Test
118     public void testRunDeleteNsJob_nsInstanceStateFailed_throwsException() {
119         final String nsInstanceId = UUID.randomUUID().toString();
120         addDummyNsToDatabase(nsInstanceId, State.FAILED);
121
122         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
123         assertTrue(optionalNfvoNsInst.isPresent());
124         assertEquals(State.FAILED, optionalNfvoNsInst.get().getStatus());
125
126         expectedException.expect(NsRequestProcessingException.class);
127         objUnderTest.runDeleteNsJob(nsInstanceId);
128     }
129
130     @Test
131     public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
132         final String nsInstanceId = UUID.randomUUID().toString();
133         addDummyNsToDatabase(nsInstanceId, State.NOT_INSTANTIATED);
134         mockAaiEndpoints();
135
136         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
137         assertTrue(optionalNfvoNsInst.isPresent());
138         assertEquals(State.NOT_INSTANTIATED, optionalNfvoNsInst.get().getStatus());
139
140         objUnderTest.runDeleteNsJob(nsInstanceId);
141
142         final Optional<NfvoJob> optional = getJobByResourceId(nsInstanceId);
143         assertTrue(optional.isPresent());
144         final NfvoJob nfvoJob = optional.get();
145
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());
152
153         // Confirm NS Instance no longer in database
154         final Optional<NfvoNsInst> optionalNfvoNsInstance = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
155         assertTrue(optionalNfvoNsInstance.isEmpty());
156     }
157
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);
164     }
165
166     private void mockAaiEndpoints() {
167         final String modelEndpoint = getAaiServiceInstanceEndPoint();
168         final String resourceVersion = "12345";
169
170         final String body =
171                 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
172         wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
173
174         wireMockServer.stubFor(
175                 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));
176     }
177 }