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 OperationalServicesRepository operationalServicesRepository;
58 OperationsApiController operationsApiController;
61 ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
64 OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository;
67 public static void setUp() throws Exception {
68 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
69 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
70 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
71 System.setProperty("sdnc.config.dir", "src/test/resources");
77 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
79 // Remove any existing preload data
80 configPreloadDataRepository.deleteAll();
82 // Add invalid content
83 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
84 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
86 assertEquals(403, mvcResult.getResponse().getStatus());
87 assertEquals(0, configPreloadDataRepository.count());
90 content = readFileContent("src/test/resources/preload1-rpc-network.json");
91 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
93 assertEquals(200, mvcResult.getResponse().getStatus());
94 assertEquals(1, configPreloadDataRepository.count());
99 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
100 // Remove any existing preload data
101 configPreloadDataRepository.deleteAll();
103 // Add invalid content
104 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
105 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
107 assertEquals(403, mvcResult.getResponse().getStatus());
108 assertEquals(0, configPreloadDataRepository.count());
111 content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
112 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
114 assertEquals(200, mvcResult.getResponse().getStatus());
115 assertEquals(1, configPreloadDataRepository.count());
119 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
121 // Remove any existing service data
122 configServicesRepository.deleteAll();
123 operationalServicesRepository.deleteAll();
125 // Add invalid content
126 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
127 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
129 assertEquals(200, mvcResult.getResponse().getStatus());
130 assertEquals(0, configServicesRepository.count());
131 assertEquals(0, operationalServicesRepository.count());
134 content = readFileContent("src/test/resources/service-assign-rpc.json");
135 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
137 assertEquals(200, mvcResult.getResponse().getStatus());
138 assertEquals(1, configServicesRepository.count());
139 assertEquals(0, operationalServicesRepository.count());
143 public void serviceTopologyOperationAsync() throws Exception {
144 configServicesRepository.deleteAll();
145 GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
146 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
148 loadVnfData("src/test/resources/vnf-data.json");
149 inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
150 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
151 configServicesRepository.deleteAll();
154 private void loadVnfData(String path) throws IOException {
155 ObjectMapper objectMapper = new ObjectMapper();
156 String content = readFileContent(path);
157 GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
158 GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
159 svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
160 svcData.getVnfs().setVnf(new ArrayList<>());
161 svcData.getVnfs().addVnfItem(vnfData);
162 ConfigServices newService = new ConfigServices();
163 newService.setSvcData(objectMapper.writeValueAsString(svcData));
164 newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
165 configServicesRepository.save(newService);
169 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
171 // Remove any existing service data
172 configServicesRepository.deleteAll();
173 operationalServicesRepository.deleteAll();
175 // Load services data
176 loadServicesData("src/test/resources/service1.json");
178 // Add invalid content
179 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
180 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
182 assertEquals(200, mvcResult.getResponse().getStatus());
185 content = readFileContent("src/test/resources/network-assign-rpc.json");
186 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
188 assertEquals(200, mvcResult.getResponse().getStatus());
193 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
195 // Remove any existing service data
196 configServicesRepository.deleteAll();
197 operationalServicesRepository.deleteAll();
199 // Load services data
200 loadServicesData("src/test/resources/service1.json");
202 // Add invalid content
203 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
204 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
206 assertEquals(200, mvcResult.getResponse().getStatus());
209 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
210 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
212 assertEquals(200, mvcResult.getResponse().getStatus());
217 public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationAssignPost() throws Exception {
219 // Remove any existing service data
220 configServicesRepository.deleteAll();
221 operationalServicesRepository.deleteAll();
223 // Load services data
224 loadServicesData("src/test/resources/service1.json");
226 // Add invalid content
227 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
228 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
230 assertEquals(200, mvcResult.getResponse().getStatus());
233 content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
234 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
236 assertEquals(200, mvcResult.getResponse().getStatus());
241 public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
243 // Remove any existing service data
244 configPortMirrorConfigurationsRepository.deleteAll();
245 operationalPortMirrorConfigurationsRepository.deleteAll();
247 // Load port-mirror-configuration data
248 loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
250 // Load services data
251 loadServicesData("src/test/resources/service1.json");
253 // Add invalid content for request input
254 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
255 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
257 assertEquals(200, mvcResult.getResponse().getStatus());
260 content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
261 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
263 assertEquals(200, mvcResult.getResponse().getStatus());
268 public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
270 // Remove any existing service data
271 configServicesRepository.deleteAll();
272 operationalServicesRepository.deleteAll();
274 // Load services data
275 loadServicesData("src/test/resources/service9.json");
278 String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
279 String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
280 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
282 assertEquals(200, mvcResult.getResponse().getStatus());
283 assertEquals(expected, mvcResult.getResponse().getContentAsString());
287 public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
290 String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
291 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
293 assertEquals(200, mvcResult.getResponse().getStatus());
297 private void loadServicesData(String path) throws IOException {
298 ObjectMapper objectMapper = new ObjectMapper();
299 String content = readFileContent(path);
300 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
302 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
303 ConfigServices newService = new ConfigServices();
304 newService.setSvcInstanceId(service.getServiceInstanceId());
305 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
306 newService.setServiceStatus(service.getServiceStatus());
307 configServicesRepository.save(newService);
311 private void loadPortMirrorConfigurationData(String path) throws IOException {
312 ObjectMapper objectMapper = new ObjectMapper();
313 String content = readFileContent(path);
314 GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
316 for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
317 ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
318 newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
319 newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
320 newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
321 configPortMirrorConfigurationsRepository.save(newPmConfig);
325 private String readFileContent(String path) throws IOException {
326 String content = new String(Files.readAllBytes(Paths.get(path)));