ed5fa94100ca5e81a0a299aeb5cfde3429c6ab6c
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmCreateJobTaskTest.java
1 /*-
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
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
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
22
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;
44
45 /**
46  * 
47  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
48  *
49  */
50 public class MonitorVnfmCreateJobTaskTest extends BaseTaskTest {
51
52   private static final String JOB_ID = UUID.randomUUID().toString();
53
54   @Mock
55   private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
56
57   private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
58
59   @Test
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());
72   }
73
74   @Test
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());
87   }
88
89   @Test
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());
100   }
101
102   @Test
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());
113   }
114
115   @Test
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));
120   }
121
122   @Test
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));
127   }
128
129   @Test
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"));
135   }
136
137   @Test
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);
143   }
144
145   @Test
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());
151   }
152
153   @Test
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());
160   }
161
162   private CreateVnfResponse getCreateVnfResponse() {
163     final CreateVnfResponse response = new CreateVnfResponse();
164     response.setJobId(JOB_ID);
165     return response;
166   }
167
168   private Optional<QueryJobResponse> getQueryJobResponse() {
169     final QueryJobResponse queryJobResponse = new QueryJobResponse();
170     queryJobResponse.setId(JOB_ID);
171     return Optional.of(queryJobResponse);
172   }
173
174   private MonitorVnfmCreateJobTask getEtsiVnfMonitorJobTask() {
175     return new MonitorVnfmCreateJobTask(mockedVnfmAdapterServiceProvider, exceptionUtil);
176   }
177
178 }