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