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 / lcm / bpmn / flows / BaseTest.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.lcm.bpmn.flows;
21
22 import static org.camunda.bpm.engine.history.HistoricProcessInstance.STATE_ACTIVE;
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.time.LocalDateTime;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.UUID;
29 import java.util.concurrent.TimeUnit;
30 import org.camunda.bpm.engine.HistoryService;
31 import org.camunda.bpm.engine.RuntimeService;
32 import org.camunda.bpm.engine.history.HistoricProcessInstance;
33 import org.camunda.bpm.engine.history.HistoricVariableInstance;
34 import org.camunda.bpm.engine.runtime.ProcessInstance;
35 import org.junit.runner.RunWith;
36 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobAction;
37 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
38 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
39 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
40 import org.slf4j.Logger;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
44 import org.springframework.test.context.ActiveProfiles;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringRunner;
47 import com.github.tomakehurst.wiremock.WireMockServer;
48
49 /**
50  * @author Waqas Ikram (waqas.ikram@est.tech)
51  *
52  */
53 @RunWith(SpringRunner.class)
54 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
55 @ActiveProfiles("test")
56 @ContextConfiguration
57 @AutoConfigureWireMock(port = 0)
58 public abstract class BaseTest {
59     protected static final String ETSI_CATALOG_URL = "http://modeling-etsicatalog.onap:8806/api";
60     protected static final String SOL003_ADAPTER_ENDPOINT_URL = "https://so-vnfm-adapter.onap:9092/so/vnfm-adapter/v1";
61     protected static final String GLOBAL_CUSTOMER_ID = UUID.randomUUID().toString();
62     protected static final String NSD_INVARIANT_ID = UUID.randomUUID().toString();
63     protected static final String SERVICE_TYPE = "NetworkService";
64     protected static final String UUID_REGEX =
65             "[0-9a-zA-Z]{8}\\-[0-9a-zA-Z]{4}\\-[0-9a-zA-Z]{4}\\-[0-9a-zA-Z]{4}\\-[0-9a-zA-Z]{12}";
66     protected static final String RANDOM_JOB_ID = UUID.randomUUID().toString();
67
68     protected static final Logger logger = getLogger(BaseTest.class);
69
70     private static final long TIME_OUT_IN_SECONDS = 120;
71     private static final int SLEEP_TIME_IN_SECONDS = 5;
72
73     @Autowired
74     private HistoryService historyService;
75
76     @Autowired
77     private RuntimeService runtimeService;
78
79     @Autowired
80     protected DatabaseServiceProvider databaseServiceProvider;
81
82     @Autowired
83     protected WireMockServer wireMockServer;
84
85     public NfvoJob createNewNfvoJob(final String jobAction, final String nsdId, final String nsName) {
86         final NfvoJob newJob = new NfvoJob().startTime(LocalDateTime.now()).jobType("NS").jobAction(JobAction.CREATE)
87                 .status(JobStatusEnum.STARTING).resourceId(nsdId).resourceName(nsName);
88         databaseServiceProvider.addJob(newJob);
89         return newJob;
90     }
91
92     public Optional<NfvoJob> getNfvoJob(final String jobId) {
93         return databaseServiceProvider.getJob(jobId);
94     }
95
96     public Optional<NfvoJob> getJobByResourceId(final String resourceId) {
97         return databaseServiceProvider.getJobByResourceId(resourceId);
98     }
99
100     public ProcessInstance executeWorkflow(final String processDefinitionKey, final String businessKey,
101             final Map<String, Object> variables) {
102         return runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
103     }
104
105     public HistoricProcessInstance getHistoricProcessInstance(final String processInstanceId) {
106         return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
107     }
108
109     public HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
110         return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
111                 .variableName(name).singleResult();
112     }
113
114     public List<HistoricVariableInstance> getVariables(final String processInstanceId) {
115         return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
116     }
117
118     public boolean waitForProcessInstanceToFinish(final String processInstanceId) throws InterruptedException {
119         final long startTimeInMillis = System.currentTimeMillis();
120         final long timeOutTime = startTimeInMillis + TimeUnit.SECONDS.toMillis(TIME_OUT_IN_SECONDS);
121         while (timeOutTime > System.currentTimeMillis()) {
122
123             if (isProcessEndedByProcessInstanceId(processInstanceId)) {
124                 logger.info("processInstanceId: {} is finished", processInstanceId);
125                 return true;
126             }
127             logger.info("processInstanceId: {} is still running", processInstanceId);
128             logger.info("Process instance {} not finished yet, will try again in {} seconds", processInstanceId,
129                     SLEEP_TIME_IN_SECONDS);
130             TimeUnit.SECONDS.sleep(SLEEP_TIME_IN_SECONDS);
131         }
132         logger.warn("Timeout {} process didn't finished ", processInstanceId);
133         return false;
134     }
135
136
137     public boolean isProcessEndedByProcessInstanceId(final String processInstanceId) {
138         return !isProcessInstanceActive(processInstanceId) && isProcessInstanceEnded(processInstanceId);
139     }
140
141     private boolean isProcessInstanceActive(final String processInstanceId) {
142         final HistoricProcessInstance processInstance = getHistoricProcessInstance(processInstanceId);
143         return processInstance != null && STATE_ACTIVE.equalsIgnoreCase(processInstance.getState());
144     }
145
146     private boolean isProcessInstanceEnded(final String processInstanceId) {
147         return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null;
148     }
149
150     protected String getAaiServiceInstanceEndPoint() {
151         return "/aai/v[0-9]+/business/customers/customer/" + GLOBAL_CUSTOMER_ID
152                 + "/service-subscriptions/service-subscription/" + SERVICE_TYPE
153                 + "/service-instances/service-instance/.*";
154     }
155 }