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