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/";
47 ConfigPreloadDataRepository configPreloadDataRepository;
50 ConfigServicesRepository configServicesRepository;
53 OperationalServicesRepository operationalServicesRepository;
56 public static void setUp() throws Exception {
57 System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
58 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
59 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
60 System.setProperty("sdnc.config.dir", "src/test/resources");
66 public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
68 // Remove any existing preload data
69 configPreloadDataRepository.deleteAll();
71 // Add invalid content
72 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
73 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
75 assertEquals(403, mvcResult.getResponse().getStatus());
76 assertEquals(0, configPreloadDataRepository.count());
79 content = readFileContent("src/test/resources/preload1-rpc-network.json");
80 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
82 assertEquals(200, mvcResult.getResponse().getStatus());
83 assertEquals(1, configPreloadDataRepository.count());
88 public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
89 // Remove any existing preload data
90 configPreloadDataRepository.deleteAll();
92 // Add invalid content
93 String content = readFileContent("src/test/resources/preload1-rpc-network.json");
94 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
96 assertEquals(403, mvcResult.getResponse().getStatus());
97 assertEquals(0, configPreloadDataRepository.count());
100 content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
101 mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
103 assertEquals(200, mvcResult.getResponse().getStatus());
104 assertEquals(1, configPreloadDataRepository.count());
108 public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
110 // Remove any existing service data
111 configServicesRepository.deleteAll();
112 operationalServicesRepository.deleteAll();
114 // Add invalid content
115 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
116 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
118 assertEquals(200, mvcResult.getResponse().getStatus());
119 assertEquals(0, configServicesRepository.count());
120 assertEquals(0, operationalServicesRepository.count());
123 content = readFileContent("src/test/resources/service-assign-rpc.json");
124 mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
126 assertEquals(200, mvcResult.getResponse().getStatus());
127 assertEquals(1, configServicesRepository.count());
128 assertEquals(1, operationalServicesRepository.count());
133 public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
135 // Remove any existing service data
136 configServicesRepository.deleteAll();
137 operationalServicesRepository.deleteAll();
139 // Load services data
140 loadServicesData("src/test/resources/service1.json");
142 // Add invalid content
143 String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
144 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
146 assertEquals(200, mvcResult.getResponse().getStatus());
149 content = readFileContent("src/test/resources/network-assign-rpc.json");
150 mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
152 assertEquals(200, mvcResult.getResponse().getStatus());
157 private void loadServicesData(String path) throws IOException {
158 ObjectMapper objectMapper = new ObjectMapper();
159 String content = readFileContent(path);
160 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
162 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
163 ConfigServices newService = new ConfigServices();
164 newService.setSvcInstanceId(service.getServiceInstanceId());
165 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
166 newService.setServiceStatus(service.getServiceStatus());
167 configServicesRepository.save(newService);
171 private String readFileContent(String path) throws IOException {
172 String content = new String(Files.readAllBytes(Paths.get(path)));