Added input parameter loading handling from SDNC
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / InputParameterRetrieverTaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Ericsson. All rights reserved.
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 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
27
28 import java.io.Serializable;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.onap.so.bpmn.BaseTaskTest;
37 import org.onap.so.bpmn.common.BuildingBlockExecution;
38 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
39 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
40 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParametersProvider;
41 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
43 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
44 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
45 import org.onap.so.client.exception.BBObjectNotFoundException;
46
47 /**
48  * @author waqas.ikram@est.tech
49  */
50 public class InputParameterRetrieverTaskTest extends BaseTaskTest {
51
52     private final BuildingBlockExecution stubbedxecution = new StubbedBuildingBlockExecution();
53
54     @Mock
55     private InputParametersProvider inputParametersProvider;
56
57     @Test
58     public void testGGetInputParameters_inputParameterStoredInExecutionContext() throws BBObjectNotFoundException {
59         final InputParameterRetrieverTask objUnderTest =
60                 new InputParameterRetrieverTask(inputParametersProvider, extractPojosForBB);
61
62         final InputParameter inputParameter = new InputParameter(Collections.emptyMap(), Collections.emptyList());
63         when(inputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class))).thenReturn(inputParameter);
64         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf());
65         objUnderTest.getInputParameters(stubbedxecution);
66
67         final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER);
68         assertNotNull(actual);
69         assertTrue(actual instanceof InputParameter);
70     }
71
72     @Test
73     public void testGGetInputParameters_ThrowExecption_NullInputParameterStoredInExecutionContext()
74             throws BBObjectNotFoundException {
75         final InputParameterRetrieverTask objUnderTest =
76                 new InputParameterRetrieverTask(inputParametersProvider, extractPojosForBB);
77
78         when(inputParametersProvider.getInputParameter(Mockito.any(GenericVnf.class)))
79                 .thenThrow(RuntimeException.class);
80         when(extractPojosForBB.extractByKey(any(), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(new GenericVnf());
81         objUnderTest.getInputParameters(stubbedxecution);
82
83         final Object actual = stubbedxecution.getVariable(Constants.INPUT_PARAMETER);
84         assertNotNull(actual);
85         assertTrue(actual instanceof NullInputParameter);
86     }
87
88
89     private class StubbedBuildingBlockExecution implements BuildingBlockExecution {
90
91         private final Map<String, Serializable> execution = new HashMap<>();
92
93         @Override
94         public GeneralBuildingBlock getGeneralBuildingBlock() {
95             return null;
96         }
97
98         @SuppressWarnings("unchecked")
99         @Override
100         public <T> T getVariable(final String key) {
101             return (T) execution.get(key);
102         }
103
104         @Override
105         public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
106             return null;
107         }
108
109         @Override
110         public void setVariable(final String key, final Serializable value) {
111             execution.put(key, value);
112         }
113
114         @Override
115         public Map<ResourceKey, String> getLookupMap() {
116             return Collections.emptyMap();
117         }
118
119         @Override
120         public String getFlowToBeCalled() {
121             return null;
122         }
123
124     }
125 }