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;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.List;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.etsi.sol003.adapter.lcm.v1.model.ExternalVirtualLink;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
42 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
43 import org.onap.so.client.exception.BadResponseException;
44 import org.onap.so.client.exception.MapperException;
45 import org.onap.so.client.sdnc.SDNCClient;
48 * @author waqas.ikram@est.tech
50 @RunWith(MockitoJUnitRunner.class)
51 public class SdncInputParametersProviderImplTest {
53 private static final String BASE_DIR = "src/test/resources/__files/";
55 private static final Path PRE_LOAD_SDNC_RESPONSE = Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponse.json");
57 private static final Path INVALID_PRE_LOAD_SDNC_RESPONSE =
58 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidData.json");
60 private static final Path INVALID_ADDITIONAL_AND_EXT_VM_DATA =
61 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidAdditionalAndExtVmData.json");
64 private static final Path INVALID_VNF_PARAMS =
65 Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidVnfParamsTag.json");
68 private static final String MODEL_NAME = "MODEL_NAME";
70 private static final String GENERIC_VNF_NAME = "GENERIC_VNF_NAME";
72 private static final String URL = PRELOAD_VNFS_URL + GENERIC_VNF_NAME + FORWARD_SLASH + MODEL_NAME;
74 private static final String GENERIC_VNF_TYPE = MODEL_NAME;
77 private SDNCClient mockedSdncClient;
80 public void testGetInputParameter_ValidResponseFromSdnc_NotEmptyInputParameter() throws Exception {
81 assertValues(getGenericVnf());
85 public void testGetInputParameter_ValidResponseFromSdncAndVnfType_NotEmptyInputParameter() throws Exception {
86 assertValues(getGenericVnf(GENERIC_VNF_TYPE));
90 public void testGetInputParameter_ValidResponseFromSdncInvalidData_EmptyInputParameter() throws Exception {
91 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_PRE_LOAD_SDNC_RESPONSE));
92 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
93 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
94 assertNotNull(actual);
95 assertTrue(actual.getAdditionalParams().isEmpty());
96 assertTrue(actual.getExtVirtualLinks().isEmpty());
100 public void testGetInputParameter_ExceptionThrownFromSdnc_EmptyInputParameter() throws Exception {
101 when(mockedSdncClient.get(Mockito.eq(URL))).thenThrow(RuntimeException.class);
102 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
103 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
104 assertNotNull(actual);
105 assertTrue(actual instanceof NullInputParameter);
106 assertTrue(actual.getAdditionalParams().isEmpty());
107 assertTrue(actual.getExtVirtualLinks().isEmpty());
111 public void testGetInputParameter_InvalidResponseData_EmptyInputParameter() throws Exception {
112 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_ADDITIONAL_AND_EXT_VM_DATA));
113 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
114 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
115 assertNotNull(actual);
116 assertTrue(actual.getAdditionalParams().isEmpty());
117 assertTrue(actual.getExtVirtualLinks().isEmpty());
121 public void testGetInputParameter_EmptyResponseData_EmptyInputParameter() throws Exception {
122 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn("");
123 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
124 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
125 assertNotNull(actual);
126 assertTrue(actual instanceof NullInputParameter);
127 assertTrue(actual.getAdditionalParams().isEmpty());
128 assertTrue(actual.getExtVirtualLinks().isEmpty());
132 public void testGetInputParameter_InvalidVnfParamsResponseData_EmptyInputParameter() throws Exception {
133 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(INVALID_VNF_PARAMS));
134 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
135 final InputParameter actual = objUnderTest.getInputParameter(getGenericVnf());
136 assertNotNull(actual);
137 assertTrue(actual.getAdditionalParams().isEmpty());
138 assertTrue(actual.getExtVirtualLinks().isEmpty());
141 private void assertValues(final GenericVnf genericVnf) throws MapperException, BadResponseException, IOException {
142 when(mockedSdncClient.get(Mockito.eq(URL))).thenReturn(getReponseAsString(PRE_LOAD_SDNC_RESPONSE));
143 final InputParametersProvider<GenericVnf> objUnderTest = new SdncInputParametersProvider(mockedSdncClient);
144 final InputParameter actual = objUnderTest.getInputParameter(genericVnf);
145 assertNotNull(actual);
147 final Map<String, String> actualAdditionalParams = actual.getAdditionalParams();
148 assertEquals(3, actualAdditionalParams.size());
150 final String actualInstanceType = actualAdditionalParams.get("instance_type");
151 assertEquals("m1.small", actualInstanceType);
153 final List<ExternalVirtualLink> actualExtVirtualLinks = actual.getExtVirtualLinks();
154 assertEquals(1, actualExtVirtualLinks.size());
156 final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0);
157 assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId());
160 private String getReponseAsString(final Path filePath) throws IOException {
161 return new String(Files.readAllBytes(filePath));
164 private GenericVnf getGenericVnf() {
165 final GenericVnf genericVnf = getGenericVnf(GENERIC_VNF_TYPE);
166 final ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
167 modelInfoGenericVnf.setModelName(MODEL_NAME);
168 genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
172 private GenericVnf getGenericVnf(final String vnfType) {
173 final GenericVnf genericVnf = new GenericVnf();
174 genericVnf.setVnfId(DUMMY_GENERIC_VND_ID);
175 genericVnf.setVnfName(GENERIC_VNF_NAME);
176 genericVnf.setVnfType(vnfType);