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 {
170 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
171 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
172 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
173 System.setProperty("sdnc.config.dir", "src/test/resources");
174 // Remove any existing service data
175 configServicesRepository.deleteAll();
176 operationalServicesRepository.deleteAll();
178 // Load services data
179 loadServicesData("src/test/resources/service1.json");
181 // Add invalid content
182 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
183 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
185 assertEquals(200, mvcResult.getResponse().getStatus());
188 content = readFileContent("src/test/resources/network-assign-rpc.json");
189 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
191 assertEquals(200, mvcResult.getResponse().getStatus());
196 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
198 // Remove any existing service data
199 configServicesRepository.deleteAll();
200 operationalServicesRepository.deleteAll();
202 // Load services data
203 loadServicesData("src/test/resources/service1.json");
205 // Add invalid content
206 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
207 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
209 assertEquals(200, mvcResult.getResponse().getStatus());
212 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
213 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
215 assertEquals(200, mvcResult.getResponse().getStatus());
220 public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationAssignPost() throws Exception {
222 // Remove any existing service data
223 configServicesRepository.deleteAll();
224 operationalServicesRepository.deleteAll();
226 // Load services data
227 loadServicesData("src/test/resources/service1.json");
229 // Add invalid content
230 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
231 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
233 assertEquals(200, mvcResult.getResponse().getStatus());
236 content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
237 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
239 assertEquals(200, mvcResult.getResponse().getStatus());
244 public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
246 // Remove any existing service data
247 configPortMirrorConfigurationsRepository.deleteAll();
248 operationalPortMirrorConfigurationsRepository.deleteAll();
250 // Load port-mirror-configuration data
251 loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
253 // Load services data
254 loadServicesData("src/test/resources/service1.json");
256 // Add invalid content for request input
257 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
258 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
260 assertEquals(200, mvcResult.getResponse().getStatus());
263 content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
264 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
266 assertEquals(200, mvcResult.getResponse().getStatus());
271 public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
273 // Remove any existing service data
274 configServicesRepository.deleteAll();
275 operationalServicesRepository.deleteAll();
277 // Load services data
278 loadServicesData("src/test/resources/service9.json");
281 String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
282 String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
283 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
285 assertEquals(200, mvcResult.getResponse().getStatus());
286 assertEquals(expected, mvcResult.getResponse().getContentAsString());
290 public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
293 String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
294 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
296 assertEquals(200, mvcResult.getResponse().getStatus());
300 private void loadServicesData(String path) throws IOException {
301 ObjectMapper objectMapper = new ObjectMapper();
302 String content = readFileContent(path);
303 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
305 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
306 ConfigServices newService = new ConfigServices();
307 newService.setSvcInstanceId(service.getServiceInstanceId());
308 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
309 newService.setServiceStatus(service.getServiceStatus());
310 configServicesRepository.save(newService);
314 private void loadPortMirrorConfigurationData(String path) throws IOException {
315 ObjectMapper objectMapper = new ObjectMapper();
316 String content = readFileContent(path);
317 GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
319 for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
320 ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
321 newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
322 newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
323 newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
324 configPortMirrorConfigurationsRepository.save(newPmConfig);
328 private String readFileContent(String path) throws IOException {
329 String content = new String(Files.readAllBytes(Paths.get(path)));