9ad993bc318647a6b4fdba242c853fe31c96c57e
[so/adapters/so-cnf-adapter.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.cnfm.lcm.bpmn.flows.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.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.onap.aaiclient.client.aai.AAIVersion.V19;
32 import java.time.LocalDateTime;
33 import java.util.Optional;
34 import java.util.UUID;
35 import org.camunda.bpm.engine.history.HistoricProcessInstance;
36 import org.camunda.bpm.engine.history.HistoricVariableInstance;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.so.cnfm.lcm.bpmn.flows.BaseTest;
41 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.AsRequestProcessingException;
42 import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService;
43 import org.onap.so.cnfm.lcm.bpmn.flows.service.WorkflowQueryService;
44 import org.onap.so.cnfm.lcm.database.beans.AsInst;
45 import org.onap.so.cnfm.lcm.database.beans.Job;
46 import org.onap.so.cnfm.lcm.database.beans.State;
47 import org.onap.so.cnfm.lcm.model.ErrorDetails;
48 import org.springframework.beans.factory.annotation.Autowired;
49
50 /**
51  *
52  * @author Waqas Ikram (waqas.ikram@est.tech)
53  *
54  */
55 public class DeleteAsTaskTest extends BaseTest {
56
57     @Autowired
58     private JobExecutorService objUnderTest;
59
60     @Autowired
61     private WorkflowQueryService workflowQueryService;
62
63     @Before
64     public void before() {
65         wireMockServer.resetAll();
66     }
67
68     @After
69     public void after() {
70         wireMockServer.resetAll();
71     }
72
73     @Test
74     public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException {
75         final String asInstanceId = UUID.randomUUID().toString();
76         addDummyAsToDatabase(asInstanceId, State.NOT_INSTANTIATED);
77         mockAaiEndpoints(asInstanceId);
78
79         objUnderTest.runDeleteAsJob(asInstanceId);
80
81         final Optional<Job> optional = getJobByResourceId(asInstanceId);
82         assertTrue(optional.isPresent());
83         final Job job = optional.get();
84
85         assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
86
87         final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
88         assertNotNull(historicProcessInstance);
89         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
90
91         final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
92         assertTrue(optionalAsInst.isEmpty());
93
94     }
95
96     @Test
97     public void testRunDeleteNsJob_AsInstanceDoesNotExistsInDb() throws InterruptedException {
98         final String asInstanceId = UUID.randomUUID().toString();
99
100         try {
101             objUnderTest.runDeleteAsJob(asInstanceId);
102         } catch (final Exception exception) {
103             assertEquals(AsRequestProcessingException.class, exception.getClass());
104         }
105
106         final Optional<Job> optional = getJobByResourceId(asInstanceId);
107         assertTrue(optional.isPresent());
108         final Job job = optional.get();
109
110         assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
111
112         final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
113         assertNotNull(historicProcessInstance);
114         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
115
116         final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
117         assertTrue(optionalAsInst.isEmpty());
118
119         final Optional<ErrorDetails> errorDetailsOptional =
120                 workflowQueryService.getErrorDetails(job.getProcessInstanceId());
121         assertTrue(errorDetailsOptional.isPresent());
122
123         final ErrorDetails errorDetails = errorDetailsOptional.get();
124         assertNotNull(errorDetails);
125         assertNotNull(errorDetails.getDetail());
126
127         final HistoricVariableInstance doesAsInstanceExistsVar =
128                 getVariable(job.getProcessInstanceId(), "asInstanceExists");
129         assertNotNull(doesAsInstanceExistsVar);
130         assertFalse((boolean) doesAsInstanceExistsVar.getValue());
131     }
132
133
134     @Test
135     public void testRunDeleteNsJob_AsInstanceDoesExistsInDbWithInstantiatedState() throws InterruptedException {
136         final String asInstanceId = UUID.randomUUID().toString();
137
138         addDummyAsToDatabase(asInstanceId, State.INSTANTIATED);
139
140         try {
141             objUnderTest.runDeleteAsJob(asInstanceId);
142         } catch (final Exception exception) {
143             assertEquals(AsRequestProcessingException.class, exception.getClass());
144         }
145
146         final Optional<Job> optional = getJobByResourceId(asInstanceId);
147         assertTrue(optional.isPresent());
148         final Job job = optional.get();
149
150         assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId()));
151
152         final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId());
153         assertNotNull(historicProcessInstance);
154         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
155
156         final Optional<AsInst> optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId);
157         assertFalse(optionalAsInst.isEmpty());
158
159         final Optional<ErrorDetails> errorDetailsOptional =
160                 workflowQueryService.getErrorDetails(job.getProcessInstanceId());
161         assertTrue(errorDetailsOptional.isPresent());
162
163         final ErrorDetails errorDetails = errorDetailsOptional.get();
164         assertNotNull(errorDetails);
165         assertNotNull(errorDetails.getDetail());
166
167         final HistoricVariableInstance isInNotInstantiatedStateVar =
168                 getVariable(job.getProcessInstanceId(), "isInNotInstantiatedState");
169         assertNotNull(isInNotInstantiatedStateVar);
170         assertFalse((boolean) isInNotInstantiatedStateVar.getValue());
171     }
172
173     private void mockAaiEndpoints(final String asInstanceId) {
174         final String modelEndpoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + asInstanceId;
175         final String resourceVersion = UUID.randomUUID().toString();
176
177         final String body =
178                 "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}";
179         wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body)));
180         wireMockServer.stubFor(
181                 delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok()));
182     }
183
184     private void addDummyAsToDatabase(final String asInstanceId, final State state) {
185         final String asdId = UUID.randomUUID().toString();
186
187         final AsInst asInst =
188                 new AsInst().asInstId(asInstanceId).name("asName").asdId(asdId).asdInvariantId(asInstanceId)
189                         .status(state).statusUpdatedTime(LocalDateTime.now()).asApplicationName("asApplicationName")
190                         .asApplicationVersion("asApplicationVersion").asProvider("asProvider")
191                         .serviceInstanceId(SERVICE_INSTANCE_ID).serviceInstanceName("serviceInstanceName")
192                         .cloudOwner("cloudOwner").cloudRegion("cloudRegion").tenantId("tenantId");
193         databaseServiceProvider.saveAsInst(asInst);
194     }
195
196 }