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