Enable ControllerExecutionBB for service scope
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / cds / ConfigureInstanceParamsForVfModuleTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Bell Canada
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
26 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.UUID;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.so.serviceinstancebeans.ModelInfo;
39 import org.onap.so.serviceinstancebeans.Resources;
40 import org.onap.so.serviceinstancebeans.Service;
41 import org.onap.so.serviceinstancebeans.VfModules;
42 import org.onap.so.serviceinstancebeans.Vnfs;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class ConfigureInstanceParamsForVfModuleTest {
46
47     @InjectMocks
48     private ConfigureInstanceParamsForVfModule configureInstanceParamsForVfModule;
49
50     @Mock
51     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
52
53     private static final String VNF_CUSTOMIZATION_ID = UUID.randomUUID().toString();
54     private static final String VFMODULE_1_CUSTOMIZATION_ID = UUID.randomUUID().toString();
55     private static final String VFMODULE_2_CUSTOMIZATION_ID = UUID.randomUUID().toString();
56     private static final String VFMODULE_1_INSTANCE_NAME = "vfmodule-instance-1";
57     private static final String VFMODULE_2_INSTANCE_NAME = "vfmodule-instance-2";
58     private static final List<Map<String, String>> VFMODULE_1_INSTANCE_PARAMS =
59             Arrays.asList(Map.of("param-1", "xyz", "param-2", "123"), Map.of("param-3", "CCC"));
60     private static final List<Map<String, String>> VFMODULE_2_INSTANCE_PARAMS =
61             Arrays.asList(Map.of("param-1", "abc", "param-2", "999"), Map.of("param-3", "AAA"));
62
63
64     @Test
65     public void testPopulateInstanceParamsByInstanceName() throws Exception {
66         Service service = new Service();
67         Resources resources = new Resources();
68         resources.setVnfs(createVnfs());
69         service.setResources(resources);
70
71         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(Optional.of(service));
72         JsonObject jsonObject = new JsonObject();
73
74         configureInstanceParamsForVfModule.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_CUSTOMIZATION_ID,
75                 VFMODULE_2_CUSTOMIZATION_ID, VFMODULE_2_INSTANCE_NAME);
76
77         assertEquals("abc", jsonObject.get("param-1").getAsString());
78         assertEquals("999", jsonObject.get("param-2").getAsString());
79         assertEquals("AAA", jsonObject.get("param-3").getAsString());
80     }
81
82     @Test
83     public void testPopulateInstanceParamsByCustomizationId() throws Exception {
84         Service service = new Service();
85         Resources resources = new Resources();
86         resources.setVnfs(createVnfs());
87         service.setResources(resources);
88
89         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(Optional.of(service));
90         JsonObject jsonObject = new JsonObject();
91
92         // No instance name is passed
93         configureInstanceParamsForVfModule.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_CUSTOMIZATION_ID,
94                 VFMODULE_1_CUSTOMIZATION_ID, null);
95
96         assertEquals("xyz", jsonObject.get("param-1").getAsString());
97         assertEquals("123", jsonObject.get("param-2").getAsString());
98         assertEquals("CCC", jsonObject.get("param-3").getAsString());
99     }
100
101     private List<Vnfs> createVnfs() {
102         Vnfs vnf1 = new Vnfs();
103         ModelInfo modelInfo = new ModelInfo();
104         modelInfo.setModelCustomizationId(VNF_CUSTOMIZATION_ID);
105         vnf1.setModelInfo(modelInfo);
106
107         VfModules vfModule1 = new VfModules();
108         modelInfo = new ModelInfo();
109         modelInfo.setModelCustomizationId(VFMODULE_1_CUSTOMIZATION_ID);
110         vfModule1.setModelInfo(modelInfo);
111         vfModule1.setInstanceName(VFMODULE_1_INSTANCE_NAME);
112         vfModule1.setInstanceParams(VFMODULE_1_INSTANCE_PARAMS);
113
114         VfModules vfModule2 = new VfModules();
115         modelInfo = new ModelInfo();
116         modelInfo.setModelCustomizationId(VFMODULE_2_CUSTOMIZATION_ID);
117         vfModule2.setModelInfo(modelInfo);
118         vfModule2.setInstanceName(VFMODULE_2_INSTANCE_NAME);
119         vfModule2.setInstanceParams(VFMODULE_2_INSTANCE_PARAMS);
120
121         vnf1.setVfModules(Arrays.asList(vfModule1, vfModule2));
122
123         return Arrays.asList(vnf1);
124     }
125
126 }