1 package org.onap.sdnc.apps.ms.gra.controllers;
3 import static org.junit.Assert.assertEquals;
5 import java.io.IOException;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
10 import com.fasterxml.jackson.databind.ObjectMapper;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
16 import org.onap.sdnc.apps.ms.gra.data.*;
17 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
20 import org.springframework.boot.test.context.SpringBootTest;
21 import org.springframework.http.MediaType;
22 import org.springframework.test.context.junit4.SpringRunner;
23 import org.springframework.test.web.servlet.MockMvc;
24 import org.springframework.test.web.servlet.MvcResult;
25 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
26 import org.springframework.transaction.annotation.Transactional;
28 @RunWith(SpringRunner.class)
29 @SpringBootTest(classes={GenericResourceMsApp.class})
32 public class OperationsApiControllerTest {
33 private final static String TEST_SVC_INSTANCE_ID = "5c4f2d89-57a3-47e9-b49b-d3c63eb0b3ca";
34 private final static String TEST_VNF_ID = "fae319cc-68d6-496f-be1e-a09e133c71d4";
35 private final static String TEST_VF_MODULE_ID = "45841173-3729-4a1d-a811-a3bde399e22d";
36 private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
37 private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
38 private final static String SERVICE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:service-topology-operation/";
39 private final static String NETWORK_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:network-topology-operation/";
40 private final static String VNF_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vnf-topology-operation/";
41 private final static String VF_MODULE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vf-module-topology-operation/";
42 private final static String PORT_MIRROR_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:port-mirror-topology-operation/";
43 private final static String VNF_GET_RESOURCE_REQUEST_URL = "/operations/GENERIC-RESOURCE-API:vnf-get-resource-request/";
44 private final static String POLICY_UPDATE_NOTIFY_URL = "/operations/GENERIC-RESOURCE-API:policy-update-notify-operation/";
51 ConfigPreloadDataRepository configPreloadDataRepository;
54 ConfigServicesRepository configServicesRepository;
57 ConfigNetworksRepository configNetworksRepository;
60 ConfigVnfsRepository configVnfsRepository;
63 ConfigVfModulesRepository configVfModulesRepository;
66 OperationalServicesRepository operationalServicesRepository;
69 OperationsApiController operationsApiController;
72 ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
75 OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository;
78 ServiceDataHelper serviceDataHelper;
81 public static void setUp() throws Exception {
82 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
83 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
84 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
85 System.setProperty("sdnc.config.dir", "src/test/resources");
91 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
93 // Remove any existing preload data
94 configPreloadDataRepository.deleteAll();
96 // Add invalid content
97 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
98 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
100 assertEquals(403, mvcResult.getResponse().getStatus());
101 assertEquals(0, configPreloadDataRepository.count());
104 content = readFileContent("src/test/resources/preload1-rpc-network.json");
105 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
107 assertEquals(200, mvcResult.getResponse().getStatus());
108 assertEquals(1, configPreloadDataRepository.count());
113 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
114 // Remove any existing preload data
115 configPreloadDataRepository.deleteAll();
117 // Add invalid content
118 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
119 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
121 assertEquals(403, mvcResult.getResponse().getStatus());
122 assertEquals(0, configPreloadDataRepository.count());
125 content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
126 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
128 assertEquals(200, mvcResult.getResponse().getStatus());
129 assertEquals(1, configPreloadDataRepository.count());
133 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
135 // Remove any existing service data
137 operationalServicesRepository.deleteAll();
139 // Add invalid content
140 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
141 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
143 assertEquals(200, mvcResult.getResponse().getStatus());
144 assertEquals(0, configServicesRepository.count());
145 assertEquals(0, operationalServicesRepository.count());
148 content = readFileContent("src/test/resources/service-assign-rpc.json");
149 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
151 assertEquals(200, mvcResult.getResponse().getStatus());
152 assertEquals(1, configServicesRepository.count());
153 assertEquals(0, operationalServicesRepository.count());
157 public void serviceTopologyOperationAsync() throws Exception {
159 GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
160 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
162 loadVnfData("src/test/resources/vnf-data.json");
163 inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
164 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
168 private void loadVnfData(String path) throws IOException {
169 ObjectMapper objectMapper = new ObjectMapper();
170 String content = readFileContent(path);
171 GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
172 GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
173 svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
174 svcData.getVnfs().setVnf(new ArrayList<>());
175 svcData.getVnfs().addVnfItem(vnfData);
176 ConfigServices newService = new ConfigServices();
177 newService.setSvcData(objectMapper.writeValueAsString(svcData));
178 newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
179 configServicesRepository.save(newService);
183 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
184 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
185 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
186 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
187 System.setProperty("sdnc.config.dir", "src/test/resources");
188 // Remove any existing service data
190 operationalServicesRepository.deleteAll();
192 // Load services data
193 loadServicesData("src/test/resources/service1.json");
195 // Add invalid content
196 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
197 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
199 assertEquals(200, mvcResult.getResponse().getStatus());
202 content = readFileContent("src/test/resources/network-assign-rpc.json");
203 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
205 assertEquals(200, mvcResult.getResponse().getStatus());
210 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
212 // Remove any existing service data
214 operationalServicesRepository.deleteAll();
216 // Load services data
217 loadServicesData("src/test/resources/service1-service.json");
220 // Add invalid content
221 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
222 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
224 assertEquals(200, mvcResult.getResponse().getStatus());
227 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
228 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
230 assertEquals(200, mvcResult.getResponse().getStatus());
235 public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationPost() throws Exception {
237 // Remove any existing service data
239 operationalServicesRepository.deleteAll();
240 assertEquals(0, configServicesRepository.count());
241 assertEquals(0, configNetworksRepository.count());
242 assertEquals(0, configVnfsRepository.count());
243 assertEquals(0, configVfModulesRepository.count());
245 // Load services data
246 loadServicesData("src/test/resources/service1-service_vnf.json");
247 assertEquals(1, configServicesRepository.count());
248 assertEquals(0, configNetworksRepository.count());
249 assertEquals(1, configVnfsRepository.count());
250 assertEquals(0, configVfModulesRepository.count());
252 // Add invalid content
253 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
254 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
256 assertEquals(200, mvcResult.getResponse().getStatus());
259 content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
260 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
262 assertEquals(200, mvcResult.getResponse().getStatus());
265 content = readFileContent("src/test/resources/vf-module-unassign-rpc.json");
266 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
268 assertEquals(200, mvcResult.getResponse().getStatus());
274 public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
276 // Remove any existing service data
277 configPortMirrorConfigurationsRepository.deleteAll();
278 operationalPortMirrorConfigurationsRepository.deleteAll();
280 // Load port-mirror-configuration data
281 loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
283 // Load services data
284 loadServicesData("src/test/resources/service1.json");
286 // Add invalid content for request input
287 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
288 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
290 assertEquals(200, mvcResult.getResponse().getStatus());
293 content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
294 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
296 assertEquals(200, mvcResult.getResponse().getStatus());
301 public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
303 // Remove any existing service data
305 operationalServicesRepository.deleteAll();
307 // Load services data
308 loadServicesData("src/test/resources/service9.json");
311 String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
312 String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
313 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
315 assertEquals(200, mvcResult.getResponse().getStatus());
316 assertEquals(expected, mvcResult.getResponse().getContentAsString());
320 public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
323 String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
324 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
326 assertEquals(200, mvcResult.getResponse().getStatus());
330 private void loadServicesData(String path) throws IOException {
331 ObjectMapper objectMapper = new ObjectMapper();
332 String content = readFileContent(path);
333 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
335 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
336 ConfigServices newService = new ConfigServices();
337 newService.setSvcInstanceId(service.getServiceInstanceId());
338 newService.setServiceStatus(service.getServiceStatus());
339 serviceDataHelper.saveService(newService, service.getServiceData());
343 private void loadPortMirrorConfigurationData(String path) throws IOException {
344 ObjectMapper objectMapper = new ObjectMapper();
345 String content = readFileContent(path);
346 GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
348 for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
349 ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
350 newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
351 newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
352 newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
353 configPortMirrorConfigurationsRepository.save(newPmConfig);
357 private void clearServicesData() {
358 configServicesRepository.deleteAll();
359 configNetworksRepository.deleteAll();
360 configVnfsRepository.deleteAll();
361 configVfModulesRepository.deleteAll();
364 private String readFileContent(String path) throws IOException {
365 String content = new String(Files.readAllBytes(Paths.get(path)));