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.lcm.bpmn.flows;
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;
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;
50 * @author Waqas Ikram (waqas.ikram@est.tech)
53 @RunWith(SpringRunner.class)
54 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
55 @ActiveProfiles("test")
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();
68 protected static final Logger logger = getLogger(BaseTest.class);
70 private static final long TIME_OUT_IN_SECONDS = 60;
71 private static final int SLEEP_TIME_IN_SECONDS = 5;
74 private HistoryService historyService;
77 private RuntimeService runtimeService;
80 protected DatabaseServiceProvider databaseServiceProvider;
83 protected WireMockServer wireMockServer;
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);
92 public Optional<NfvoJob> getNfvoJob(final String jobId) {
93 return databaseServiceProvider.getJob(jobId);
96 public Optional<NfvoJob> getJobByResourceId(final String resourceId) {
97 return databaseServiceProvider.getJobByResourceId(resourceId);
100 public ProcessInstance executeWorkflow(final String processDefinitionKey, final String businessKey,
101 final Map<String, Object> variables) {
102 return runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
105 public HistoricProcessInstance getHistoricProcessInstance(final String processInstanceId) {
106 return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
109 public HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
110 return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
111 .variableName(name).singleResult();
114 public List<HistoricVariableInstance> getVariables(final String processInstanceId) {
115 return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
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()) {
123 if (isProcessEndedByProcessInstanceId(processInstanceId)) {
124 logger.info("processInstanceId: {} is finished", processInstanceId);
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);
132 logger.warn("Timeout {} process didn't finished ", processInstanceId);
137 public boolean isProcessEndedByProcessInstanceId(final String processInstanceId) {
138 return !isProcessInstanceActive(processInstanceId) && isProcessInstanceEnded(processInstanceId);
141 private boolean isProcessInstanceActive(final String processInstanceId) {
142 final HistoricProcessInstance processInstance = getHistoricProcessInstance(processInstanceId);
143 return processInstance != null && STATE_ACTIVE.equalsIgnoreCase(processInstance.getState());
146 private boolean isProcessInstanceEnded(final String processInstanceId) {
147 return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null;
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/.*";