0273b9dbf696f977432a2a82f98e395d8e7dcf5a
[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.UUID;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.so.serviceinstancebeans.ModelInfo;
38 import org.onap.so.serviceinstancebeans.Resources;
39 import org.onap.so.serviceinstancebeans.Service;
40 import org.onap.so.serviceinstancebeans.VfModules;
41 import org.onap.so.serviceinstancebeans.Vnfs;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class ConfigureInstanceParamsForVfModuleTest {
45
46     @InjectMocks
47     private ConfigureInstanceParamsForVfModule configureInstanceParamsForVfModule;
48
49     @Mock
50     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
51
52     private static final String VNF_CUSTOMIZATION_ID = UUID.randomUUID().toString();
53     private static final String VFMODULE_1_CUSTOMIZATION_ID = UUID.randomUUID().toString();
54     private static final String VFMODULE_2_CUSTOMIZATION_ID = UUID.randomUUID().toString();
55     private static final String VFMODULE_1_INSTANCE_NAME = "vfmodule-instance-1";
56     private static final String VFMODULE_2_INSTANCE_NAME = "vfmodule-instance-2";
57     private static final List<Map<String, String>> VFMODULE_1_INSTANCE_PARAMS =
58             Arrays.asList(Map.of("param-1", "xyz", "param-2", "123"), Map.of("param-3", "CCC"));
59     private static final List<Map<String, String>> VFMODULE_2_INSTANCE_PARAMS =
60             Arrays.asList(Map.of("param-1", "abc", "param-2", "999"), Map.of("param-3", "AAA"));
61
62
63     @Test
64     public void testPopulateInstanceParamsByInstanceName() throws Exception {
65         Service service = new Service();
66         Resources resources = new Resources();
67         resources.setVnfs(createVnfs());
68         service.setResources(resources);
69
70         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(service);
71         JsonObject jsonObject = new JsonObject();
72
73         configureInstanceParamsForVfModule.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_CUSTOMIZATION_ID,
74                 VFMODULE_2_CUSTOMIZATION_ID, VFMODULE_2_INSTANCE_NAME);
75
76         assertEquals("abc", jsonObject.get("param-1").getAsString());
77         assertEquals("999", jsonObject.get("param-2").getAsString());
78         assertEquals("AAA", jsonObject.get("param-3").getAsString());
79     }
80
81     @Test
82     public void testPopulateInstanceParamsByCustomizationId() throws Exception {
83         Service service = new Service();
84         Resources resources = new Resources();
85         resources.setVnfs(createVnfs());
86         service.setResources(resources);
87
88         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(service);
89         JsonObject jsonObject = new JsonObject();
90
91         // No instance name is passed
92         configureInstanceParamsForVfModule.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_CUSTOMIZATION_ID,
93                 VFMODULE_1_CUSTOMIZATION_ID, null);
94
95         assertEquals("xyz", jsonObject.get("param-1").getAsString());
96         assertEquals("123", jsonObject.get("param-2").getAsString());
97         assertEquals("CCC", jsonObject.get("param-3").getAsString());
98     }
99
100     private List<Vnfs> createVnfs() {
101         Vnfs vnf1 = new Vnfs();
102         ModelInfo modelInfo = new ModelInfo();
103         modelInfo.setModelCustomizationId(VNF_CUSTOMIZATION_ID);
104         vnf1.setModelInfo(modelInfo);
105
106         VfModules vfModule1 = new VfModules();
107         modelInfo = new ModelInfo();
108         modelInfo.setModelCustomizationId(VFMODULE_1_CUSTOMIZATION_ID);
109         vfModule1.setModelInfo(modelInfo);
110         vfModule1.setInstanceName(VFMODULE_1_INSTANCE_NAME);
111         vfModule1.setInstanceParams(VFMODULE_1_INSTANCE_PARAMS);
112
113         VfModules vfModule2 = new VfModules();
114         modelInfo = new ModelInfo();
115         modelInfo.setModelCustomizationId(VFMODULE_2_CUSTOMIZATION_ID);
116         vfModule2.setModelInfo(modelInfo);
117         vfModule2.setInstanceName(VFMODULE_2_INSTANCE_NAME);
118         vfModule2.setInstanceParams(VFMODULE_2_INSTANCE_PARAMS);
119
120         vnf1.setVfModules(Arrays.asList(vfModule1, vfModule2));
121
122         return Arrays.asList(vnf1);
123     }
124
125 }