Fixing EtSI BB ClassCastException
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorInstantiateVnfmNodeTaskTest.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.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_NODE_STATUS;
30 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.VNF_CREATED;
31 import java.util.Optional;
32 import java.util.UUID;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
40 import org.onap.so.client.orchestration.AAIVnfResources;
41
42 /**
43  * 
44  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
45  * @author Waqas Ikram (waqas.ikram@est.tech)
46  *
47  */
48 public class MonitorInstantiateVnfmNodeTaskTest extends BaseTaskTest {
49
50     private static final String VNF_ID = UUID.randomUUID().toString();
51
52     private static final String VNF_NAME = "VNF_NAME";
53
54     private MonitorVnfmNodeTask objUnderTest;
55
56     @Mock
57     private VnfmAdapterServiceProvider mockedVnfmAdapterServiceProvider;
58
59     @Mock
60     private AAIVnfResources mockedAaiVnfResources;
61
62     private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
63
64     @Before
65     public void setUp() {
66         objUnderTest = getEtsiVnfMonitorNodeJobTask();
67     }
68
69     @Test
70     public void testGetNodeStatus_genericVnfWithOrchStatusCreated_executionVariableSetToCreate() throws Exception {
71         final org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf = getGenericVnf();
72         final GenericVnf aaiGenericVnf = getAAIGenericVnf();
73         aaiGenericVnf.setOrchestrationStatus(VNF_CREATED);
74
75         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(vnf);
76         when(mockedAaiVnfResources.getGenericVnf(eq(VNF_ID))).thenReturn(Optional.of(aaiGenericVnf));
77         objUnderTest.getNodeStatus(stubbedxecution);
78         assertTrue(stubbedxecution.getVariable(CREATE_VNF_NODE_STATUS));
79     }
80
81     @Test
82     public void testGetNodeStatus_noGenericVnfFoundInAAI_throwException() throws Exception {
83         final org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf = getGenericVnf();
84
85         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(vnf);
86         when(mockedAaiVnfResources.getGenericVnf(eq(VNF_ID))).thenReturn(Optional.empty());
87         objUnderTest.getNodeStatus(stubbedxecution);
88         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1220),
89                 any(Exception.class));
90         assertNull(stubbedxecution.getVariable(CREATE_VNF_NODE_STATUS));
91
92     }
93
94     @Test
95     public void testGetNodeStatusException() throws Exception {
96         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenThrow(RuntimeException.class);
97         objUnderTest.getNodeStatus(stubbedxecution);
98         assertNull(stubbedxecution.getVariable(CREATE_VNF_NODE_STATUS));
99         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1220),
100                 any(Exception.class));
101     }
102
103     @Test
104     public void testTimeOutLogFailue() throws Exception {
105         objUnderTest.timeOutLogFailue(stubbedxecution);
106         verify(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1221),
107                 eq("Node operation time out"));
108     }
109
110     private GenericVnf getAAIGenericVnf() {
111         final GenericVnf genericVnf = new GenericVnf();
112         genericVnf.setVnfId(VNF_ID);
113         genericVnf.setVnfName(VNF_NAME);
114         return genericVnf;
115     }
116
117     private org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf getGenericVnf() {
118         final org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf genericVnf =
119                 new org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf();
120         genericVnf.setVnfId(VNF_ID);
121         return genericVnf;
122
123     }
124
125     private MonitorInstantiateVnfmNodeTask getEtsiVnfMonitorNodeJobTask() {
126         return new MonitorInstantiateVnfmNodeTask(extractPojosForBB, exceptionUtil, mockedAaiVnfResources);
127     }
128
129 }