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.ConfigPreloadDataRepository;
17 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
18 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
19 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
20 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
23 import org.springframework.boot.test.context.SpringBootTest;
24 import org.springframework.http.MediaType;
25 import org.springframework.test.context.junit4.SpringRunner;
26 import org.springframework.test.web.servlet.MockMvc;
27 import org.springframework.test.web.servlet.MvcResult;
28 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
29 import org.springframework.transaction.annotation.Transactional;
31 @RunWith(SpringRunner.class)
32 @SpringBootTest(classes={GenericResourceMsApp.class})
35 public class OperationsApiControllerTest {
37 private final static String PRELOAD_NETWORK_URL = "/operations/GENERIC-RESOURCE-API:preload-network-topology-operation/";
38 private final static String PRELOAD_VFMODULE_URL = "/operations/GENERIC-RESOURCE-API:preload-vf-module-topology-operation/";
39 private final static String SERVICE_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:service-topology-operation/";
40 private final static String NETWORK_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:network-topology-operation/";
41 private final static String VNF_TOPOLOGY_URL = "/operations/GENERIC-RESOURCE-API:vnf-topology-operation/";
48 ConfigPreloadDataRepository configPreloadDataRepository;
51 ConfigServicesRepository configServicesRepository;
54 OperationalServicesRepository operationalServicesRepository;
57 OperationsApiController operationsApiController;
60 public static void setUp() throws Exception {
61 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
62 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
63 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
64 System.setProperty("sdnc.config.dir", "src/test/resources");
70 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
72 // Remove any existing preload data
73 configPreloadDataRepository.deleteAll();
75 // Add invalid content
76 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
77 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
79 assertEquals(403, mvcResult.getResponse().getStatus());
80 assertEquals(0, configPreloadDataRepository.count());
83 content = readFileContent("src/test/resources/preload1-rpc-network.json");
84 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
86 assertEquals(200, mvcResult.getResponse().getStatus());
87 assertEquals(1, configPreloadDataRepository.count());
92 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
93 // Remove any existing preload data
94 configPreloadDataRepository.deleteAll();
96 // Add invalid content
97 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
98 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_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-vfmodule.json");
105 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
107 assertEquals(200, mvcResult.getResponse().getStatus());
108 assertEquals(1, configPreloadDataRepository.count());
112 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
114 // Remove any existing service data
115 configServicesRepository.deleteAll();
116 operationalServicesRepository.deleteAll();
118 // Add invalid content
119 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
120 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
122 assertEquals(200, mvcResult.getResponse().getStatus());
123 assertEquals(0, configServicesRepository.count());
124 assertEquals(0, operationalServicesRepository.count());
127 content = readFileContent("src/test/resources/service-assign-rpc.json");
128 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
130 assertEquals(200, mvcResult.getResponse().getStatus());
131 assertEquals(1, configServicesRepository.count());
132 assertEquals(0, operationalServicesRepository.count());
136 public void serviceTopologyOperationAsync() throws Exception {
137 configServicesRepository.deleteAll();
138 GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
139 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
141 loadVnfData("src/test/resources/vnf-data.json");
142 inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
143 operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
144 configServicesRepository.deleteAll();
147 private void loadVnfData(String path) throws IOException {
148 ObjectMapper objectMapper = new ObjectMapper();
149 String content = readFileContent(path);
150 GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
151 GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
152 svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
153 svcData.getVnfs().setVnf(new ArrayList<>());
154 svcData.getVnfs().addVnfItem(vnfData);
155 ConfigServices newService = new ConfigServices();
156 newService.setSvcData(objectMapper.writeValueAsString(svcData));
157 newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
158 configServicesRepository.save(newService);
162 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
164 // Remove any existing service data
165 configServicesRepository.deleteAll();
166 operationalServicesRepository.deleteAll();
168 // Load services data
169 loadServicesData("src/test/resources/service1.json");
171 // Add invalid content
172 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
173 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
175 assertEquals(200, mvcResult.getResponse().getStatus());
178 content = readFileContent("src/test/resources/network-assign-rpc.json");
179 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
181 assertEquals(200, mvcResult.getResponse().getStatus());
186 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
188 // Remove any existing service data
189 configServicesRepository.deleteAll();
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(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
199 assertEquals(200, mvcResult.getResponse().getStatus());
202 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
203 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
205 assertEquals(200, mvcResult.getResponse().getStatus());
210 private void loadServicesData(String path) throws IOException {
211 ObjectMapper objectMapper = new ObjectMapper();
212 String content = readFileContent(path);
213 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
215 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
216 ConfigServices newService = new ConfigServices();
217 newService.setSvcInstanceId(service.getServiceInstanceId());
218 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
219 newService.setServiceStatus(service.getServiceStatus());
220 configServicesRepository.save(newService);
224 private String readFileContent(String path) throws IOException {
225 String content = new String(Files.readAllBytes(Paths.get(path)));