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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.FORWARD_SLASH;
27 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.PRELOAD_VNFS_URL;
28 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.TestConstants.DUMMY_GENERIC_VND_ID;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.List;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
43 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
44 import org.onap.so.client.exception.BadResponseException;
45 import org.onap.so.client.exception.MapperException;
46 import org.onap.so.client.sdnc.SDNCClient;
47 import org.onap.vnfmadapter.v1.model.ExternalVirtualLink;
50 * @author waqas.ikram@est.tech
52 @RunWith(MockitoJUnitRunner.class)
53 public class InputParametersProviderImplTest {
55 private static final String BASE_DIR = "src/test/resources/__files/";
57 private static final Path PRE_LOAD_SDNC_RESPONSE = Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponse.json");
59 private static final Path INVALID_PRE_LOAD_SDNC_RESPONSE =
60 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidData.json");
62 private static final Path INVALID_ADDITIONAL_AND_EXT_VM_DATA =
63 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidAdditionalAndExtVmData.json");
66 private static final Path INVALID_VNF_PARAMS =
67 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidVnfParamsTag.json");
70 private static final String MODEL_NAME = "MODEL_NAME";
72 private static final String GENERIC_VNF_NAME = "GENERIC_VNF_NAME";
74 private static final String URL = PRELOAD_VNFS_URL + GENERIC_VNF_NAME + FORWARD_SLASH + MODEL_NAME;
76 private static final String GENERIC_VNF_TYPE = MODEL_NAME;
79 private SDNCClient mockedSdncClient;
82 public void testGetInputParameter_ValidResponseFromSdnc_NotEmptyInputParameter() throws Exception {
83 assertValues(getGenericVnf());
87 public void testGetInputParameter_ValidResponseFromSdncAndVnfType_NotEmptyInputParameter() throws Exception {
88 assertValues(getGenericVnf(GENERIC_VNF_TYPE));
92 public void testGetInputParameter_ValidResponseFromSdncInvalidData_EmptyInputParameter() throws Exception {
93 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_PRE_LOAD_SDNC_RESPONSE));
94 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
95 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
96 assertNotNull(actual);
97 assertTrue(actual.getAdditionalParams().isEmpty());
98 assertTrue(actual.getExtVirtualLinks().isEmpty());
102 public void testGetInputParameter_ExceptionThrownFromSdnc_EmptyInputParameter() throws Exception {
103 when(mockedSdncClient.get(Mockito.eq(URL))).thenThrow(RuntimeException.class);
104 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
105 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
106 assertNotNull(actual);
107 assertTrue(actual instanceof NullInputParameter);
108 assertTrue(actual.getAdditionalParams().isEmpty());
109 assertTrue(actual.getExtVirtualLinks().isEmpty());
113 public void testGetInputParameter_InvalidResponseData_EmptyInputParameter() throws Exception {
114 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_ADDITIONAL_AND_EXT_VM_DATA));
115 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
116 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
117 assertNotNull(actual);
118 assertTrue(actual.getAdditionalParams().isEmpty());
119 assertTrue(actual.getExtVirtualLinks().isEmpty());
123 public void testGetInputParameter_EmptyResponseData_EmptyInputParameter() throws Exception {
124 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn("");
125 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
126 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
127 assertNotNull(actual);
128 assertTrue(actual instanceof NullInputParameter);
129 assertTrue(actual.getAdditionalParams().isEmpty());
130 assertTrue(actual.getExtVirtualLinks().isEmpty());
134 public void testGetInputParameter_InvalidVnfParamsResponseData_EmptyInputParameter() throws Exception {
135 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_VNF_PARAMS));
136 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
137 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
138 assertNotNull(actual);
139 assertTrue(actual.getAdditionalParams().isEmpty());
140 assertTrue(actual.getExtVirtualLinks().isEmpty());
143 private void assertValues(final GenericVnf genericVnf) throws MapperException, BadResponseException, IOException {
144 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(PRE_LOAD_SDNC_RESPONSE));
145 final InputParametersProvider objUnderTest = new InputParametersProviderImpl(mockedSdncClient);
146 final InputParameter actual = objUnderTest.getInputParameter(genericVnf);
147 assertNotNull(actual);
149 final Map<String, String> actualAdditionalParams = actual.getAdditionalParams();
150 assertEquals(3, actualAdditionalParams.size());
152 final String actualInstanceType = actualAdditionalParams.get("instance_type");
153 assertEquals("m1.small", actualInstanceType);
155 final List<ExternalVirtualLink> actualExtVirtualLinks = actual.getExtVirtualLinks();
156 assertEquals(1, actualExtVirtualLinks.size());
158 final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0);
159 assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId());
162 private String getReponseAsString(final Path filePath) throws IOException {
163 return new String(Files.readAllBytes(filePath));
166 private GenericVnf getGenericVnf() {
167 final GenericVnf genericVnf = getGenericVnf(GENERIC_VNF_TYPE);
168 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
169 modelInfoGenericVnf.setModelName(MODEL_NAME);
170 genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
174 private GenericVnf getGenericVnf(final String vnfType) {
175 final GenericVnf genericVnf = new GenericVnf();
176 genericVnf.setVnfId(DUMMY_GENERIC_VND_ID);
177 genericVnf.setVnfName(GENERIC_VNF_NAME);
178 genericVnf.setVnfType(vnfType);