Updating etsi-sol003-lcm-api module
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmDeleteJobTaskTest.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.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.onap.etsi.sol003.adapter.lcm.v1.model.DeleteVnfResponse;
39 import org.onap.etsi.sol003.adapter.lcm.v1.model.OperationStateEnum;
40 import org.onap.etsi.sol003.adapter.lcm.v1.model.OperationStatusRetrievalStatusEnum;
41 import org.onap.etsi.sol003.adapter.lcm.v1.model.QueryJobResponse;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.common.BuildingBlockExecution;
44 import com.google.common.base.Optional;
45
46 /**
47  * 
48  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
49  *
50  */
51 public class MonitorVnfmDeleteJobTaskTest extends BaseTaskTest {
52
53     private static final String JOB_ID = UUID.randomUUID().toString();
54
55     private MonitorVnfmDeleteJobTask objUnderTest;
56
57     @Mock
58     private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
59
60     private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
61
62     @Before
63     public void setUp() {
64         objUnderTest = getEtsiVnfMonitorJobTask();
65         stubbedxecution.setVariable(Constants.DELETE_VNF_RESPONSE_PARAM_NAME, getDeleteVnfResponse());
66     }
67
68     @Test
69     public void testGetCurrentOperationStatus() throws Exception {
70         Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
71         queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
72         queryJobResponse.get().setOperationState(OperationStateEnum.COMPLETED);
73         when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
74         objUnderTest.getCurrentOperationStatus(stubbedxecution);
75         final Optional<OperationStateEnum> operationState =
76                 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
77         assertNotNull(operationState);
78         assertEquals(OperationStateEnum.COMPLETED, operationState.get());
79     }
80
81     @Test
82     public void testGetCurrentOperationStatusFailed() throws Exception {
83         Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
84         queryJobResponse.get()
85                 .setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.CANNOT_RETRIEVE_STATUS);
86         queryJobResponse.get().setOperationState(OperationStateEnum.FAILED);
87         when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
88         objUnderTest.getCurrentOperationStatus(stubbedxecution);
89         final Optional<OperationStateEnum> operationState =
90                 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
91         assertNotNull(operationState);
92         assertEquals(OperationStateEnum.FAILED, operationState.get());
93     }
94
95     @Test
96     public void testGetCurrentOperationStatusEmpty() throws Exception {
97         Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
98         queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
99         when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
100         objUnderTest.getCurrentOperationStatus(stubbedxecution);
101         final Optional<OperationStateEnum> operationState =
102                 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
103         assertFalse(operationState.isPresent());
104     }
105
106     @Test
107     public void testGetCurrentOperationStatusException() throws Exception {
108         Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
109         queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
110         when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
111         objUnderTest.getCurrentOperationStatus(stubbedxecution);
112         final Optional<OperationStateEnum> operationState =
113                 stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
114         assertFalse(operationState.isPresent());
115     }
116
117     @Test
118     public void testHasOperationFinished() throws Exception {
119         stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.COMPLETED));
120         assertTrue(objUnderTest.hasOperationFinished(stubbedxecution));
121     }
122
123     @Test
124     public void testHasOperationPending() throws Exception {
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         objUnderTest.timeOutLogFailue(stubbedxecution);
132         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1213),
133                 eq("Delete operation time out"));
134     }
135
136     @Test
137     public void testCheckIfOperationWasSuccessful() throws Exception {
138         stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.COMPLETED));
139         MonitorVnfmDeleteJobTask spyObject = Mockito.spy(objUnderTest);
140         spyObject.checkIfOperationWasSuccessful(stubbedxecution);
141         verify(spyObject, times(1)).checkIfOperationWasSuccessful(stubbedxecution);
142     }
143
144     @Test
145     public void testCheckIfOperationWasSuccessfulWithPending() throws Exception {
146         stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.PROCESSING));
147         objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
148         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1215), anyString());
149     }
150
151     @Test
152     public void testCheckIfOperationWasSuccessfulEmpty() throws Exception {
153         stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.absent());
154         objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
155         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1214), anyString());
156     }
157
158     private DeleteVnfResponse getDeleteVnfResponse() {
159         final DeleteVnfResponse response = new DeleteVnfResponse();
160         response.setJobId(JOB_ID);
161         return response;
162     }
163
164     private Optional<QueryJobResponse> getQueryJobResponse() {
165         final QueryJobResponse queryJobResponse = new QueryJobResponse();
166         queryJobResponse.setId(JOB_ID);
167         return Optional.of(queryJobResponse);
168     }
169
170     private MonitorVnfmDeleteJobTask getEtsiVnfMonitorJobTask() {
171         return new MonitorVnfmDeleteJobTask(mockedVnfmAdapterServiceProvider, exceptionUtil);
172     }
173
174 }