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 {
34 private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
35 private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
36 private final static String SERVICE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:service-topology-operation/";
37 private final static String NETWORK_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:network-topology-operation/";
38 private final static String VNF_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vnf-topology-operation/";
39 private final static String VF_MODULE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vf-module-topology-operation/";
40 private final static String PORT_MIRROR_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:port-mirror-topology-operation/";
41 private final static String VNF_GET_RESOURCE_REQUEST_URL = "/operations/GENERIC-RESOURCE-API:vnf-get-resource-request/";
42 private final static String POLICY_UPDATE_NOTIFY_URL = "/operations/GENERIC-RESOURCE-API:policy-update-notify-operation/";
49 ConfigPreloadDataRepository configPreloadDataRepository;
52 ConfigServicesRepository configServicesRepository;
55 ConfigNetworksRepository configNetworksRepository;
58 ConfigVnfsRepository configVnfsRepository;
61 ConfigVfModulesRepository configVfModulesRepository;
64 OperationalServicesRepository operationalServicesRepository;
67 OperationsApiController operationsApiController;
70 ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
73 OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository;
76 ServiceDataHelper serviceDataHelper;
79 public static void setUp() throws Exception {
80 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
81 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
82 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
83 System.setProperty("sdnc.config.dir", "src/test/resources");
89 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
91 // Remove any existing preload data
92 configPreloadDataRepository.deleteAll();
94 // Add invalid content
95 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
96 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
98 assertEquals(403, mvcResult.getResponse().getStatus());
99 assertEquals(0, configPreloadDataRepository.count());
102 content = readFileContent("src/test/resources/preload1-rpc-network.json");
103 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
105 assertEquals(200, mvcResult.getResponse().getStatus());
106 assertEquals(1, configPreloadDataRepository.count());
111 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
112 // Remove any existing preload data
113 configPreloadDataRepository.deleteAll();
115 // Add invalid content
116 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
117 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
119 assertEquals(403, mvcResult.getResponse().getStatus());
120 assertEquals(0, configPreloadDataRepository.count());
123 content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
124 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
126 assertEquals(200, mvcResult.getResponse().getStatus());
127 assertEquals(1, configPreloadDataRepository.count());
131 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
133 // Remove any existing service data
135 operationalServicesRepository.deleteAll();
137 // Add invalid content
138 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
139 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
141 assertEquals(200, mvcResult.getResponse().getStatus());
142 assertEquals(0, configServicesRepository.count());
143 assertEquals(0, operationalServicesRepository.count());
146 content = readFileContent("src/test/resources/service-assign-rpc.json");
147 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
149 assertEquals(200, mvcResult.getResponse().getStatus());
150 assertEquals(1, configServicesRepository.count());
151 assertEquals(0, operationalServicesRepository.count());
155 public void serviceTopologyOperationAsync() throws Exception {
157 GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
158 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
160 loadVnfData("src/test/resources/vnf-data.json");
161 inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
162 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
166 private void loadVnfData(String path) throws IOException {
167 ObjectMapper objectMapper = new ObjectMapper();
168 String content = readFileContent(path);
169 GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
170 GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
171 svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
172 svcData.getVnfs().setVnf(new ArrayList<>());
173 svcData.getVnfs().addVnfItem(vnfData);
174 ConfigServices newService = new ConfigServices();
175 newService.setSvcData(objectMapper.writeValueAsString(svcData));
176 newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
177 configServicesRepository.save(newService);
181 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
182 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
183 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
184 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
185 System.setProperty("sdnc.config.dir", "src/test/resources");
186 // Remove any existing service data
188 operationalServicesRepository.deleteAll();
190 // Load services data
191 loadServicesData("src/test/resources/service1.json");
193 // Add invalid content
194 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
195 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
197 assertEquals(200, mvcResult.getResponse().getStatus());
200 content = readFileContent("src/test/resources/network-assign-rpc.json");
201 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
203 assertEquals(200, mvcResult.getResponse().getStatus());
208 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
210 // Remove any existing service data
212 operationalServicesRepository.deleteAll();
214 // Load services data
215 loadServicesData("src/test/resources/service1.json");
217 // Add invalid content
218 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
219 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
221 assertEquals(200, mvcResult.getResponse().getStatus());
224 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
225 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
227 assertEquals(200, mvcResult.getResponse().getStatus());
232 public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationPost() throws Exception {
234 // Remove any existing service data
236 operationalServicesRepository.deleteAll();
237 assertEquals(0, configServicesRepository.count());
238 assertEquals(0, configNetworksRepository.count());
239 assertEquals(0, configVnfsRepository.count());
240 assertEquals(0, configVfModulesRepository.count());
242 // Load services data
243 loadServicesData("src/test/resources/service1-service_vnf.json");
244 assertEquals(1, configServicesRepository.count());
245 assertEquals(0, configNetworksRepository.count());
246 assertEquals(1, configVnfsRepository.count());
247 assertEquals(0, configVfModulesRepository.count());
249 // Add invalid content
250 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
251 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
253 assertEquals(200, mvcResult.getResponse().getStatus());
256 content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
257 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
259 assertEquals(200, mvcResult.getResponse().getStatus());
260 assertEquals(1, configServicesRepository.count());
261 assertEquals(0, configNetworksRepository.count());
262 assertEquals(1, configVnfsRepository.count());
263 assertEquals(1, configVfModulesRepository.count());
266 content = readFileContent("src/test/resources/vf-module-unassign-rpc.json");
267 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
269 assertEquals(200, mvcResult.getResponse().getStatus());
270 assertEquals(1, configServicesRepository.count());
271 assertEquals(0, configNetworksRepository.count());
272 assertEquals(1, configVnfsRepository.count());
273 assertEquals(0, configVfModulesRepository.count());
279 public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
281 // Remove any existing service data
282 configPortMirrorConfigurationsRepository.deleteAll();
283 operationalPortMirrorConfigurationsRepository.deleteAll();
285 // Load port-mirror-configuration data
286 loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
288 // Load services data
289 loadServicesData("src/test/resources/service1.json");
291 // Add invalid content for request input
292 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
293 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
295 assertEquals(200, mvcResult.getResponse().getStatus());
298 content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
299 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
301 assertEquals(200, mvcResult.getResponse().getStatus());
306 public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
308 // Remove any existing service data
310 operationalServicesRepository.deleteAll();
312 // Load services data
313 loadServicesData("src/test/resources/service9.json");
316 String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
317 String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
318 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
320 assertEquals(200, mvcResult.getResponse().getStatus());
321 assertEquals(expected, mvcResult.getResponse().getContentAsString());
325 public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
328 String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
329 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
331 assertEquals(200, mvcResult.getResponse().getStatus());
335 private void loadServicesData(String path) throws IOException {
336 ObjectMapper objectMapper = new ObjectMapper();
337 String content = readFileContent(path);
338 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
340 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
341 ConfigServices newService = new ConfigServices();
342 newService.setSvcInstanceId(service.getServiceInstanceId());
343 newService.setServiceStatus(service.getServiceStatus());
344 serviceDataHelper.saveService(newService, service.getServiceData());
348 private void loadPortMirrorConfigurationData(String path) throws IOException {
349 ObjectMapper objectMapper = new ObjectMapper();
350 String content = readFileContent(path);
351 GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
353 for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
354 ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
355 newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
356 newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
357 newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
358 configPortMirrorConfigurationsRepository.save(newPmConfig);
362 private void clearServicesData() {
363 configServicesRepository.deleteAll();
364 configNetworksRepository.deleteAll();
365 configVnfsRepository.deleteAll();
366 configVfModulesRepository.deleteAll();
369 private String readFileContent(String path) throws IOException {
370 String content = new String(Files.readAllBytes(Paths.get(path)));