2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.cds;
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;
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.Vnfs;
43 @RunWith(MockitoJUnitRunner.class)
44 public class ConfigureInstanceParamsForVnfTest {
47 private ConfigureInstanceParamsForVnf configureInstanceParamsForVnf;
50 private ExtractServiceFromUserParameters extractServiceFromUserParameters;
52 private static final String VNF_1_CUSTOMIZATION_ID = UUID.randomUUID().toString();
53 private static final String VNF_2_CUSTOMIZATION_ID = UUID.randomUUID().toString();
54 private static final String VNF_1_INSTANCE_NAME = "vnf-instance-1";
55 private static final String VNF_2_INSTANCE_NAME = "vnf-instance-2";
56 private static final List<Map<String, String>> VNF_1_INSTANCE_PARAMS =
57 Arrays.asList(Map.of("param-1", "xyz", "param-2", "123"), Map.of("param-3", "CCC"));
58 private static final List<Map<String, String>> VNF_2_INSTANCE_PARAMS =
59 Arrays.asList(Map.of("param-1", "abc", "param-2", "999"), Map.of("param-3", "AAA"));
63 public void testPopulateInstanceParamsByInstanceName() throws Exception {
64 Service service = new Service();
65 Resources resources = new Resources();
66 resources.setVnfs(createVnfs());
67 service.setResources(resources);
68 when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(Optional.of(service));
69 JsonObject jsonObject = new JsonObject();
71 configureInstanceParamsForVnf.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_2_CUSTOMIZATION_ID,
74 assertEquals("abc", jsonObject.get("param-1").getAsString());
75 assertEquals("999", jsonObject.get("param-2").getAsString());
76 assertEquals("AAA", jsonObject.get("param-3").getAsString());
80 public void testPopulateInstanceParamsByCustomizationId() throws Exception {
81 Service service = new Service();
82 Resources resources = new Resources();
83 resources.setVnfs(createVnfs());
84 service.setResources(resources);
85 when(extractServiceFromUserParameters.getServiceFromRequestUserParams(any())).thenReturn(Optional.of(service));
86 JsonObject jsonObject = new JsonObject();
88 // No instance name is passed
89 configureInstanceParamsForVnf.populateInstanceParams(jsonObject, new ArrayList<>(), VNF_1_CUSTOMIZATION_ID,
92 assertEquals("xyz", jsonObject.get("param-1").getAsString());
93 assertEquals("123", jsonObject.get("param-2").getAsString());
94 assertEquals("CCC", jsonObject.get("param-3").getAsString());
97 private List<Vnfs> createVnfs() {
98 Vnfs vnf1 = new Vnfs();
99 vnf1.setInstanceName(VNF_1_INSTANCE_NAME);
100 ModelInfo modelInfo = new ModelInfo();
101 modelInfo.setModelCustomizationId(VNF_1_CUSTOMIZATION_ID);
102 vnf1.setModelInfo(modelInfo);
103 vnf1.setInstanceParams(VNF_1_INSTANCE_PARAMS);
105 Vnfs vnf2 = new Vnfs();
106 modelInfo = new ModelInfo();
107 modelInfo.setModelCustomizationId(VNF_2_CUSTOMIZATION_ID);
108 vnf2.setModelInfo(modelInfo);
109 vnf2.setInstanceName(VNF_2_INSTANCE_NAME);
110 vnf2.setInstanceParams(VNF_2_INSTANCE_PARAMS);
112 return Arrays.asList(vnf1, vnf2);