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.assertj.core.api.Assertions.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_REST_TEMPLATE_BEAN;
32 import static org.springframework.test.web.client.ExpectedCount.times;
33 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
34 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
35 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
36 import java.io.IOException;
37 import java.time.LocalDateTime;
38 import java.util.List;
39 import java.util.Optional;
40 import java.util.UUID;
41 import org.camunda.bpm.engine.history.HistoricProcessInstance;
42 import org.hamcrest.text.MatchesPattern;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
49 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStatusRetrievalStatusEnum;
50 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.BaseTest;
51 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
52 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.NsRequestProcessingException;
53 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.service.JobExecutorService;
54 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
55 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNfInst;
56 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
57 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NsLcmOpOcc;
58 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.OperationStateEnum;
59 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
60 import org.onap.so.etsi.nfvo.ns.lcm.model.TerminateNsRequest;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.beans.factory.annotation.Qualifier;
63 import org.springframework.http.HttpMethod;
64 import org.springframework.http.MediaType;
65 import org.springframework.http.converter.json.GsonHttpMessageConverter;
66 import org.springframework.test.web.client.MockRestServiceServer;
67 import org.springframework.web.client.RestTemplate;
68 import com.google.gson.Gson;
71 * @author Andrew Lamb (andrew.a.lamb@est.tech)
74 public class TerminateNsTaskTest extends BaseTest {
77 @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
78 private RestTemplate sol003AdapterRestTemplate;
80 private MockRestServiceServer mockSol003AdapterRestServiceServer;
83 private JobExecutorService objUnderTest;
86 private GsonProvider gsonProvider;
89 public ExpectedException expectedException = ExpectedException.none();
94 public void before() {
95 wireMockServer.resetAll();
96 gson = gsonProvider.getGson();
97 mockSol003AdapterRestServiceServer =
98 MockRestServiceServer.bindTo(sol003AdapterRestTemplate).ignoreExpectOrder(true).build();
99 sol003AdapterRestTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
103 public void after() {
104 wireMockServer.resetAll();
108 public void testRunTerminateNsJob_timeSetInTerminateRequest_throwsNsRequestProcessingException() {
109 final String nsInstanceId = UUID.randomUUID().toString();
110 final TerminateNsRequest terminateNsRequest = new TerminateNsRequest().terminationTime(LocalDateTime.now());
111 final String message = "TerminateNsRequest received with terminateTime: "
112 + terminateNsRequest.getTerminationTime()
113 + "\nOnly immediate Terminate requests are currently supported \n(i.e., terminateTime field must not be set).";
114 expectedException.expect(NsRequestProcessingException.class);
115 expectedException.expectMessage(message);
116 objUnderTest.runTerminateNsJob(nsInstanceId, terminateNsRequest);
120 public void testRunTerminateNsJob_NsInstNotInDb_throwsNsRequestProcessingException() {
121 final String nsInstanceId = UUID.randomUUID().toString();
122 final TerminateNsRequest terminateNsRequest = new TerminateNsRequest();
123 final String message = "No matching NS Instance for id: " + nsInstanceId + " found in database.";
124 assertThat(databaseServiceProvider.getNfvoNsInst(nsInstanceId)).isEmpty();
125 expectedException.expect(NsRequestProcessingException.class);
126 expectedException.expectMessage(message);
127 objUnderTest.runTerminateNsJob(nsInstanceId, terminateNsRequest);
131 public void testTerminateNsTask_SuccessfulCase() throws InterruptedException, IOException {
132 final String nsInstanceId = UUID.randomUUID().toString();
133 addDummyNsToDatabase(nsInstanceId);
134 mockSol003AdapterEndpoints();
137 final String nsLcmOpOccId = objUnderTest.runTerminateNsJob(nsInstanceId, new TerminateNsRequest());
139 final Optional<NfvoJob> optional = getJobByResourceId(nsInstanceId);
140 assertTrue(optional.isPresent());
141 final NfvoJob nfvoJob = optional.get();
143 // Confirm Process finishes in STATE_COMPLETED
144 assertTrue(waitForProcessInstanceToFinish(nfvoJob.getProcessInstanceId()));
145 final HistoricProcessInstance historicProcessInstance =
146 getHistoricProcessInstance(nfvoJob.getProcessInstanceId());
147 assertNotNull(historicProcessInstance);
148 assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
150 // Confirm NS Instance set to NOT_INSTANTIATED and related NF Instances Deleted
151 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstanceId);
152 assertTrue(optionalNfvoNsInst.isPresent());
153 final NfvoNsInst nfvoNsInst = optionalNfvoNsInst.get();
154 assertEquals(State.NOT_INSTANTIATED, nfvoNsInst.getStatus());
155 final List<NfvoNfInst> nfvoNfInsts = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstanceId);
156 assertTrue(nfvoNfInsts.isEmpty());
158 // Confirm NS LCM OP OCC Job set to Completed
159 final Optional<NsLcmOpOcc> optionalNsLcmOpOcc = databaseServiceProvider.getNsLcmOpOcc(nsLcmOpOccId);
160 assertTrue(optionalNsLcmOpOcc.isPresent());
161 final NsLcmOpOcc nsLcmOpOcc = optionalNsLcmOpOcc.get();
162 assertEquals(OperationStateEnum.COMPLETED, nsLcmOpOcc.getOperationState());
165 private void addDummyNsToDatabase(final String nsInstanceId) {
166 final String nsPackageId = UUID.randomUUID().toString();
167 final NfvoNsInst nfvoNsInst =
168 new NfvoNsInst().nsInstId(nsInstanceId).name("nsName").nsPackageId(nsPackageId).nsdId("nsdId")
169 .nsdInvariantId("nsdId").status(State.INSTANTIATED).statusUpdatedTime(LocalDateTime.now());
170 databaseServiceProvider.saveNfvoNsInst(nfvoNsInst);
171 addDummyNfToDatabase(nfvoNsInst);
172 addDummyNfToDatabase(nfvoNsInst);
173 addDummyNfToDatabase(nfvoNsInst);
176 private void addDummyNfToDatabase(final NfvoNsInst nfvoNsInst) {
177 final LocalDateTime localDateTime = LocalDateTime.now();
178 final String nfPackageId = UUID.randomUUID().toString();
179 final NfvoNfInst nfvoNfInst =
180 new NfvoNfInst().status(State.INSTANTIATED).createTime(localDateTime).lastUpdateTime(localDateTime)
181 .name("nfName").vnfdId("vnfdId").packageId(nfPackageId).nfvoNsInst(nfvoNsInst);
182 databaseServiceProvider.saveNfvoNfInst(nfvoNfInst);
185 private void mockSol003AdapterEndpoints() {
186 final int numTimes = 3;
188 mockSol003AdapterRestServiceServer
189 .expect(times(numTimes),
190 requestTo(MatchesPattern.matchesPattern(SOL003_ADAPTER_ENDPOINT_URL + "/vnfs/.*")))
191 .andExpect(method(HttpMethod.DELETE))
192 .andRespond(withSuccess(gson.toJson(new DeleteVnfResponse().jobId(UUID.randomUUID().toString())),
193 MediaType.APPLICATION_JSON));
195 mockSol003AdapterRestServiceServer
196 .expect(times(numTimes),
197 requestTo(MatchesPattern.matchesPattern(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/.*")))
198 .andExpect(method(HttpMethod.GET))
199 .andRespond(withSuccess(gson.toJson(
200 new org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse().operationState(
201 org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum.COMPLETED)
202 .operationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND)),
203 MediaType.APPLICATION_JSON));
206 private void mockAAIEndpoints() {
207 final String modelEndpoint = "/aai/v[0-9]+/network/generic-vnfs/generic-vnf/" + UUID_REGEX;
208 final String resourceVersion = "12345";
211 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
212 wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
214 wireMockServer.stubFor(
215 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));