e41c571765596ccd3e72899459e9907440c2b03f
[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.so.bpmn.BaseTaskTest;
39 import org.onap.so.bpmn.common.BuildingBlockExecution;
40 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
41 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
42 import org.onap.vnfmadapter.v1.model.OperationStatusRetrievalStatusEnum;
43 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
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().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.CANNOT_RETRIEVE_STATUS);
85     queryJobResponse.get().setOperationState(OperationStateEnum.FAILED);
86     when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
87     objUnderTest.getCurrentOperationStatus(stubbedxecution);
88     final Optional<OperationStateEnum> operationState =
89         stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
90     assertNotNull(operationState);
91     assertEquals(OperationStateEnum.FAILED, operationState.get());
92   }
93
94   @Test
95   public void testGetCurrentOperationStatusEmpty() throws Exception {
96     Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
97     queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
98     when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
99     objUnderTest.getCurrentOperationStatus(stubbedxecution);
100     final Optional<OperationStateEnum> operationState =
101         stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
102     assertFalse(operationState.isPresent());
103   }
104
105   @Test
106   public void testGetCurrentOperationStatusException() throws Exception {
107     Optional<QueryJobResponse> queryJobResponse = getQueryJobResponse();
108     queryJobResponse.get().setOperationStatusRetrievalStatus(OperationStatusRetrievalStatusEnum.STATUS_FOUND);
109     when(mockedVnfmAdapterServiceProvider.getInstantiateOperationJobStatus(JOB_ID)).thenReturn(queryJobResponse);
110     objUnderTest.getCurrentOperationStatus(stubbedxecution);
111     final Optional<OperationStateEnum> operationState =
112         stubbedxecution.getVariable(Constants.OPERATION_STATUS_PARAM_NAME);
113     assertFalse(operationState.isPresent());
114   }
115
116   @Test
117   public void testHasOperationFinished() throws Exception {
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     stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.absent());
125     assertFalse(objUnderTest.hasOperationFinished(stubbedxecution));
126   }
127
128   @Test
129   public void testTimeOutLogFailue() throws Exception {
130     objUnderTest.timeOutLogFailue(stubbedxecution);
131     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1213),
132         eq("Delete operation time out"));
133   }
134
135   @Test
136   public void testCheckIfOperationWasSuccessful() throws Exception {
137     stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.COMPLETED));
138     MonitorVnfmDeleteJobTask spyObject = Mockito.spy(objUnderTest);
139     spyObject.checkIfOperationWasSuccessful(stubbedxecution);
140     verify(spyObject, times(1)).checkIfOperationWasSuccessful(stubbedxecution);
141   }
142
143   @Test
144   public void testCheckIfOperationWasSuccessfulWithPending() throws Exception {
145     stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.of(OperationStateEnum.PROCESSING));
146     objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
147     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1215), anyString());
148   }
149
150   @Test
151   public void testCheckIfOperationWasSuccessfulEmpty() throws Exception {
152     stubbedxecution.setVariable(Constants.OPERATION_STATUS_PARAM_NAME, Optional.absent());
153     objUnderTest.checkIfOperationWasSuccessful(stubbedxecution);
154     verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1214), anyString());
155   }
156
157   private DeleteVnfResponse getDeleteVnfResponse() {
158     final DeleteVnfResponse response = new DeleteVnfResponse();
159     response.setJobId(JOB_ID);
160     return response;
161   }
162
163   private Optional<QueryJobResponse> getQueryJobResponse() {
164     final QueryJobResponse queryJobResponse = new QueryJobResponse();
165     queryJobResponse.setId(JOB_ID);
166     return Optional.of(queryJobResponse);
167   }
168
169   private MonitorVnfmDeleteJobTask getEtsiVnfMonitorJobTask() {
170     return new MonitorVnfmDeleteJobTask(mockedVnfmAdapterServiceProvider, exceptionUtil);
171   }
172
173 }