547129e89806aa3ebf1958a42ec8075bc1d0cd94
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / cds / ConfigureInstanceParamsForVnfTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2021 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.Vnfs;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class ConfigureInstanceParamsForVnfTest {
44
45     @InjectMocks
46     private ConfigureInstanceParamsForVnf configureInstanceParamsForVnf;
47
48     @Mock
49     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
50
51     private static final String VNF_1_CUSTOMIZATION_ID = UUID.randomUUID().toString();
52     private static final String VNF_2_CUSTOMIZATION_ID = UUID.randomUUID().toString();
53     private static final String VNF_1_INSTANCE_NAME = "vnf-instance-1";
54     private static final String VNF_2_INSTANCE_NAME = "vnf-instance-2";
55     private static final List<Map<String, String>> VNF_1_INSTANCE_PARAMS =
56             Arrays.asList(Map.of("param-1", "xyz", "param-2", "123"), Map.of("param-3", "CCC"));
57     private static final List<Map<String, String>> VNF_2_INSTANCE_PARAMS =
58             Arrays.asList(Map.of("param-1", "abc", "param-2", "999"), Map.of("param-3", "AAA"));
59
60
61     @Test
62     public void testPopulateInstanceParamsByInstanceName() throws Exception {
63         Service service = new Service();
64         Resources resources = new Resources();
65         resources.setVnfs(createVnfs());
66         service.setResources(resources);
67         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(service);
68         JsonObject jsonObject = new JsonObject();
69
70         configureInstanceParamsForVnf.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_2_CUSTOMIZATION_ID,
71                 VNF_2_INSTANCE_NAME);
72
73         assertEquals("abc", jsonObject.get("param-1").getAsString());
74         assertEquals("999", jsonObject.get("param-2").getAsString());
75         assertEquals("AAA", jsonObject.get("param-3").getAsString());
76     }
77
78     @Test
79     public void testPopulateInstanceParamsByCustomizationId() throws Exception {
80         Service service = new Service();
81         Resources resources = new Resources();
82         resources.setVnfs(createVnfs());
83         service.setResources(resources);
84         when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(service);
85         JsonObject jsonObject = new JsonObject();
86
87         // No instance name is passed
88         configureInstanceParamsForVnf.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_1_CUSTOMIZATION_ID,
89                 null);
90
91         assertEquals("xyz", jsonObject.get("param-1").getAsString());
92         assertEquals("123", jsonObject.get("param-2").getAsString());
93         assertEquals("CCC", jsonObject.get("param-3").getAsString());
94     }
95
96     private List<Vnfs> createVnfs() {
97         Vnfs vnf1 = new Vnfs();
98         vnf1.setInstanceName(VNF_1_INSTANCE_NAME);
99         ModelInfo modelInfo = new ModelInfo();
100         modelInfo.setModelCustomizationId(VNF_1_CUSTOMIZATION_ID);
101         vnf1.setModelInfo(modelInfo);
102         vnf1.setInstanceParams(VNF_1_INSTANCE_PARAMS);
103
104         Vnfs vnf2 = new Vnfs();
105         modelInfo = new ModelInfo();
106         modelInfo.setModelCustomizationId(VNF_2_CUSTOMIZATION_ID);
107         vnf2.setModelInfo(modelInfo);
108         vnf2.setInstanceName(VNF_2_INSTANCE_NAME);
109         vnf2.setInstanceParams(VNF_2_INSTANCE_PARAMS);
110
111         return Arrays.asList(vnf1, vnf2);
112     }
113
114 }