2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import java.util.UUID;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
40 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
41 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
42 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
43 import com.google.common.base.Optional;
47 * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
50 public class MonitorVnfmCreateJobTaskTest extends BaseTaskTest {
52 private static final String JOB_ID = UUID.randomUUID().toString();
55 private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
57 private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
60 public void testGetCurrentOperationStatus() throws Exception {
61 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
62 stubbedxecution.setVariable(Constants.CREATE_VNF_RESPONSE_PARAM_NAME, getCreateVnfResponse());
63 Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
64 queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
65 queryJobResponse.get().setOperationState(OperationStateEnum.COMPLETED);
66 when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
67 objUnderTest.getCurrentOperationStatus(stubbedxecution);
68 final Optional<OperationStateEnum> operationState =
69 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
70 assertNotNull(operationState);
71 assertEquals(OperationStateEnum.COMPLETED, operationState.get());
75 public void testGetCurrentOperationStatusFailed() throws Exception {
76 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
77 stubbedxecution.setVariable(Constants.CREATE_VNF_RESPONSE_PARAM_NAME, getCreateVnfResponse());
78 Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
79 queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.CANNOT_RETRIEVE_STATUS);
80 queryJobResponse.get().setOperationState(OperationStateEnum.FAILED);
81 when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
82 objUnderTest.getCurrentOperationStatus(stubbedxecution);
83 final Optional<OperationStateEnum> operationState =
84 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
85 assertNotNull(operationState);
86 assertEquals(OperationStateEnum.FAILED, operationState.get());
90 public void testGetCurrentOperationStatusEmpty() throws Exception {
91 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
92 stubbedxecution.setVariable(Constants.CREATE_VNF_RESPONSE_PARAM_NAME, getCreateVnfResponse());
93 Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
94 queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
95 when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
96 objUnderTest.getCurrentOperationStatus(stubbedxecution);
97 final Optional<OperationStateEnum> operationState =
98 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
99 assertFalse(operationState.isPresent());
103 public void testGetCurrentOperationStatusException() throws Exception {
104 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
105 stubbedxecution.setVariable(Constants.CREATE_VNF_RESPONSE_PARAM_NAME, getCreateVnfResponse());
106 Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
107 queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
108 when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
109 objUnderTest.getCurrentOperationStatus(stubbedxecution);
110 final Optional<OperationStateEnum> operationState =
111 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
112 assertFalse(operationState.isPresent());
116 public void testHasOperationFinished() throws Exception {
117 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
118 stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.COMPLETED));
119 assertTrue(objUnderTest.hasOperationFinished(stubbedxecution));
123 public void testHasOperationPending() throws Exception {
124 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
125 stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.absent());
126 assertFalse(objUnderTest.hasOperationFinished(stubbedxecution));
130 public void testTimeOutLogFailue() throws Exception {
131 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
132 objUnderTest.timeOutLogFailue(stubbedxecution);
133 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1205),
134 eq("Instantiation operation time out"));
138 public void testCheckIfOperationWasSuccessful() throws Exception {
139 stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.COMPLETED));
140 MonitorVnfmCreateJobTask objUnderTest = Mockito.spy(getEtsiVnfMonitorJobTask());
141 objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
142 verify(objUnderTest, times(1)).checkIfOperationWasSuccessful(stubbedxecution);
146 public void testCheckIfOperationWasSuccessfulWithPending() throws Exception {
147 final MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
148 stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.PROCESSING));
149 objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
150 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1207), anyString());
154 public void testCheckIfOperationWasSuccessfulEmpty() throws Exception {
155 MonitorVnfmCreateJobTask objUnderTest = getEtsiVnfMonitorJobTask();
156 stubbedxecution.setVariable(Constants.CREATE_VNF_RESPONSE_PARAM_NAME, getCreateVnfResponse());
157 stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.absent());
158 objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
159 verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1206), anyString());
162 private CreateVnfResponse getCreateVnfResponse() {
163 final CreateVnfResponse response = new CreateVnfResponse();
164 response.setJobId(JOB_ID);
168 private Optional<QueryJobResponse> getQueryJobResponse() {
169 final QueryJobResponse queryJobResponse = new QueryJobResponse();
170 queryJobResponse.setId(JOB_ID);
171 return Optional.of(queryJobResponse);
174 private MonitorVnfmCreateJobTask getEtsiVnfMonitorJobTask() {
175 return new MonitorVnfmCreateJobTask(mockedVnfmAdapterServiceProvider, exceptionUtil);