1 package org.onap.sdnc.apps.ms.gra.controllers;
3 import org.junit.BeforeClass;
5 import org.junit.runner.RunWith;
6 import org.onap.sdnc.apps.ms.gra.core.GenericResourceMsApp;
7 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
8 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
9 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
10 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
11 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceModelInfrastructure;
12 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicemodelinfrastructureService;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.http.MediaType;
17 import org.springframework.test.context.junit4.SpringRunner;
18 import org.springframework.test.web.servlet.MockMvc;
19 import org.springframework.test.web.servlet.MvcResult;
20 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
21 import org.springframework.transaction.annotation.Transactional;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import static org.junit.Assert.*;
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 public static void setUp() throws Exception {
58 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
59 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
60 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
61 System.setProperty("sdnc.config.dir", "src/test/resources");
67 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
69 // Remove any existing preload data
70 configPreloadDataRepository.deleteAll();
72 // Add invalid content
73 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
74 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
76 assertEquals(403, mvcResult.getResponse().getStatus());
77 assertEquals(0, configPreloadDataRepository.count());
80 content = readFileContent("src/test/resources/preload1-rpc-network.json");
81 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
83 assertEquals(200, mvcResult.getResponse().getStatus());
84 assertEquals(1, configPreloadDataRepository.count());
89 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
90 // Remove any existing preload data
91 configPreloadDataRepository.deleteAll();
93 // Add invalid content
94 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
95 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
97 assertEquals(403, mvcResult.getResponse().getStatus());
98 assertEquals(0, configPreloadDataRepository.count());
101 content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
102 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
104 assertEquals(200, mvcResult.getResponse().getStatus());
105 assertEquals(1, configPreloadDataRepository.count());
109 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
111 // Remove any existing service data
112 configServicesRepository.deleteAll();
113 operationalServicesRepository.deleteAll();
115 // Add invalid content
116 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
117 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
119 assertEquals(200, mvcResult.getResponse().getStatus());
120 assertEquals(0, configServicesRepository.count());
121 assertEquals(0, operationalServicesRepository.count());
124 content = readFileContent("src/test/resources/service-assign-rpc.json");
125 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
127 assertEquals(200, mvcResult.getResponse().getStatus());
128 assertEquals(1, configServicesRepository.count());
129 assertEquals(1, operationalServicesRepository.count());
134 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
136 // Remove any existing service data
137 configServicesRepository.deleteAll();
138 operationalServicesRepository.deleteAll();
140 // Load services data
141 loadServicesData("src/test/resources/service1.json");
143 // Add invalid content
144 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
145 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
147 assertEquals(200, mvcResult.getResponse().getStatus());
150 content = readFileContent("src/test/resources/network-assign-rpc.json");
151 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
153 assertEquals(200, mvcResult.getResponse().getStatus());
158 public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
160 // Remove any existing service data
161 configServicesRepository.deleteAll();
162 operationalServicesRepository.deleteAll();
164 // Load services data
165 loadServicesData("src/test/resources/service1.json");
167 // Add invalid content
168 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
169 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
171 assertEquals(200, mvcResult.getResponse().getStatus());
174 content = readFileContent("src/test/resources/vnf-assign-rpc.json");
175 mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
177 assertEquals(200, mvcResult.getResponse().getStatus());
182 private void loadServicesData(String path) throws IOException {
183 ObjectMapper objectMapper = new ObjectMapper();
184 String content = readFileContent(path);
185 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
187 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
188 ConfigServices newService = new ConfigServices();
189 newService.setSvcInstanceId(service.getServiceInstanceId());
190 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
191 newService.setServiceStatus(service.getServiceStatus());
192 configServicesRepository.save(newService);
196 private String readFileContent(String path) throws IOException {
197 String content = new String(Files.readAllBytes(Paths.get(path)));