Added input parameter loading handling from SDNC
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / utils / InputParametersProviderImplTest.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils;
21
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
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;
35 import java.util.Map;
36
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;
48
49 /**
50  * @author waqas.ikram@est.tech
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class InputParametersProviderImplTest {
54
55     private static final String BASE_DIR = "src/test/resources/__files/";
56
57     private static final Path PRE_LOAD_SDNC_RESPONSE = Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponse.json");
58
59     private static final Path INVALID_PRE_LOAD_SDNC_RESPONSE =
60             Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidData.json");
61
62     private static final Path INVALID_ADDITIONAL_AND_EXT_VM_DATA =
63             Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidAdditionalAndExtVmData.json");
64
65
66     private static final Path INVALID_VNF_PARAMS =
67             Paths.get(BASE_DIR + "SDNCClientPrelaodDataResponseWithInvalidVnfParamsTag.json");
68
69
70     private static final String MODEL_NAME = "MODEL_NAME";
71
72     private static final String GENERIC_VNF_NAME = "GENERIC_VNF_NAME";
73
74     private static final String URL = PRELOAD_VNFS_URL + GENERIC_VNF_NAME + FORWARD_SLASH + MODEL_NAME;
75
76     private static final String GENERIC_VNF_TYPE = MODEL_NAME;
77
78     @Mock
79     private SDNCClient mockedSdncClient;
80
81     @Test
82     public void testGetInputParameter_ValidResponseFromSdnc_NotEmptyInputParameter() throws Exception {
83         assertValues(getGenericVnf());
84     }
85
86     @Test
87     public void testGetInputParameter_ValidResponseFromSdncAndVnfType_NotEmptyInputParameter() throws Exception {
88         assertValues(getGenericVnf(GENERIC_VNF_TYPE));
89     }
90
91     @Test
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());
99     }
100
101     @Test
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());
110     }
111
112     @Test
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());
120     }
121
122     @Test
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());
131     }
132
133     @Test
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());
141     }
142
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);
148
149         final Map<String, String> actualAdditionalParams = actual.getAdditionalParams();
150         assertEquals(3, actualAdditionalParams.size());
151
152         final String actualInstanceType = actualAdditionalParams.get("instance_type");
153         assertEquals("m1.small", actualInstanceType);
154
155         final List<ExternalVirtualLink> actualExtVirtualLinks = actual.getExtVirtualLinks();
156         assertEquals(1, actualExtVirtualLinks.size());
157
158         final ExternalVirtualLink actualExternalVirtualLink = actualExtVirtualLinks.get(0);
159         assertEquals("ac1ed33d-8dc1-4800-8ce8-309b99c38eec", actualExternalVirtualLink.getId());
160     }
161
162     private String getReponseAsString(final Path filePath) throws IOException {
163         return new String(Files.readAllBytes(filePath));
164     }
165
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);
171         return genericVnf;
172     }
173
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);
179         return genericVnf;
180     }
181 }