aboutsummaryrefslogtreecommitdiffstats
path: root/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
blob: fc9f2a2195c240aa2c82f15a17889e61e8f1fce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows;

import static org.slf4j.LoggerFactory.getLogger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.junit.runner.RunWith;
import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobAction;
import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.github.tomakehurst.wiremock.WireMockServer;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@ContextConfiguration
@AutoConfigureWireMock(port = 0)
public abstract class BaseTest {
    protected static final String ETSI_CATALOG_URL = "http://modeling-etsicatalog.onap:8806/api";
    protected static final String SOL003_ADAPTER_ENDPOINT_URL = "https://so-vnfm-adapter.onap:9092/so/vnfm-adapter/v1";
    protected static final String GLOBAL_CUSTOMER_ID = UUID.randomUUID().toString();
    protected static final String NSD_INVARIANT_ID = UUID.randomUUID().toString();
    protected static final String SERVICE_TYPE = "NetworkService";
    protected static final String UUID_REGEX =
            "[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}";
    protected static final String RANDOM_JOB_ID = UUID.randomUUID().toString();

    protected static final Logger logger = getLogger(BaseTest.class);

    private static final long TIME_OUT_IN_SECONDS = 60;
    private static final int SLEEP_TIME_IN_SECONDS = 5;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    protected DatabaseServiceProvider databaseServiceProvider;

    @Autowired
    protected WireMockServer wireMockServer;

    public NfvoJob createNewNfvoJob(final String jobAction, final String nsdId, final String nsName) {
        final NfvoJob newJob = new NfvoJob().startTime(LocalDateTime.now()).jobType("NS").jobAction(JobAction.CREATE)
                .status(JobStatusEnum.STARTING).resourceId(nsdId).resourceName(nsName);
        databaseServiceProvider.addJob(newJob);
        return newJob;
    }

    public Optional<NfvoJob> getNfvoJob(final String jobId) {
        return databaseServiceProvider.getJob(jobId);
    }

    public Optional<NfvoJob> getJobByResourceId(final String resourceId) {
        return databaseServiceProvider.getJobByResourceId(resourceId);
    }

    public ProcessInstance executeWorkflow(final String processDefinitionKey, final String businessKey,
            final Map<String, Object> variables) {
        return runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
    }

    public HistoricProcessInstance getHistoricProcessInstance(final String processInstanceId) {
        return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    }

    public HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
        return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
                .variableName(name).singleResult();
    }

    public List<HistoricVariableInstance> getVariables(final String processInstanceId) {
        return historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
    }

    public boolean waitForProcessInstanceToFinish(final String processInstanceId) throws InterruptedException {
        final long startTimeInMillis = System.currentTimeMillis();
        final long timeOutTime = startTimeInMillis + TimeUnit.SECONDS.toMillis(TIME_OUT_IN_SECONDS);
        while (timeOutTime > System.currentTimeMillis()) {

            if (isProcessEndedByProcessInstanceId(processInstanceId)) {
                logger.info("processInstanceId: {} is finished", processInstanceId);
                return true;
            }
            logger.info("processInstanceId: {} is still running", processInstanceId);
            logger.info("Process instance {} not finished yet, will try again in {} seconds", processInstanceId,
                    SLEEP_TIME_IN_SECONDS);
            TimeUnit.SECONDS.sleep(SLEEP_TIME_IN_SECONDS);
        }
        logger.warn("Timeout {} process didn't finished ", processInstanceId);
        return false;
    }


    public boolean isProcessEndedByProcessInstanceId(final String processInstanceId) {
        final HistoricProcessInstance processInstance = getHistoricProcessInstance(processInstanceId);
        return processInstance != null
                && !HistoricProcessInstance.STATE_ACTIVE.equalsIgnoreCase(processInstance.getState());
    }

}