6752d8ce929f9efa449797bbaa114b63fee305c6
[sdnc/apps.git] /
1 package org.onap.sdnc.apps.ms.gra.controllers;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.IOException;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
9
10 import com.fasterxml.jackson.databind.ObjectMapper;
11
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;
27
28 @RunWith(SpringRunner.class)
29 @SpringBootTest(classes={GenericResourceMsApp.class})
30 @AutoConfigureMockMvc
31 @Transactional
32 public class OperationsApiControllerTest {
33
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/";
43
44
45     @Autowired
46     private MockMvc mvc;
47
48     @Autowired
49     ConfigPreloadDataRepository configPreloadDataRepository;
50
51     @Autowired
52     ConfigServicesRepository configServicesRepository;
53
54     @Autowired
55     ConfigNetworksRepository configNetworksRepository;
56
57     @Autowired
58     ConfigVnfsRepository configVnfsRepository;
59
60     @Autowired
61     ConfigVfModulesRepository configVfModulesRepository;
62
63     @Autowired
64     OperationalServicesRepository operationalServicesRepository;
65
66     @Autowired
67     OperationsApiController operationsApiController;
68
69     @Autowired
70     ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
71
72     @Autowired
73     OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository;
74
75     @Autowired
76     ServiceDataHelper serviceDataHelper;
77
78     @BeforeClass
79     public static void setUp() throws Exception {
80         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
81         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
82         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
83         System.setProperty("sdnc.config.dir", "src/test/resources");
84    
85     }
86
87
88     @Test
89     public void operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost() throws Exception {
90
91         // Remove any existing preload data
92         configPreloadDataRepository.deleteAll();
93
94         // Add invalid content
95         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
96         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
97                 .andReturn();
98         assertEquals(403, mvcResult.getResponse().getStatus());
99         assertEquals(0, configPreloadDataRepository.count());
100
101         // Add valid content
102         content = readFileContent("src/test/resources/preload1-rpc-network.json");
103         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_NETWORK_URL).contentType(MediaType.APPLICATION_JSON).content(content))
104                 .andReturn();
105         assertEquals(200, mvcResult.getResponse().getStatus());
106         assertEquals(1, configPreloadDataRepository.count());
107
108     }
109
110     @Test
111     public void operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost() throws Exception {
112         // Remove any existing preload data
113         configPreloadDataRepository.deleteAll();
114
115         // Add invalid content
116         String content = readFileContent("src/test/resources/preload1-rpc-network.json");
117         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
118                 .andReturn();
119         assertEquals(403, mvcResult.getResponse().getStatus());
120         assertEquals(0, configPreloadDataRepository.count());
121
122         // Add valid content
123         content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
124         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PRELOAD_VFMODULE_URL).contentType(MediaType.APPLICATION_JSON).content(content))
125                 .andReturn();
126         assertEquals(200, mvcResult.getResponse().getStatus());
127         assertEquals(1, configPreloadDataRepository.count());
128     }
129
130     @Test
131     public void operationsGENERICRESOURCEAPIserviceTopologyOperationAssignPost() throws Exception {
132
133         // Remove any existing service data
134         clearServicesData();
135         operationalServicesRepository.deleteAll();
136
137         // Add invalid content
138         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
139         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
140                 .andReturn();
141         assertEquals(200, mvcResult.getResponse().getStatus());
142         assertEquals(0, configServicesRepository.count());
143         assertEquals(0, operationalServicesRepository.count());
144
145         // Add valid content
146         content = readFileContent("src/test/resources/service-assign-rpc.json");
147         mvcResult = mvc.perform(MockMvcRequestBuilders.post(SERVICE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
148                 .andReturn();
149         assertEquals(200, mvcResult.getResponse().getStatus());
150         assertEquals(1, configServicesRepository.count());
151         assertEquals(0, operationalServicesRepository.count());
152     }
153
154     @Test
155     public void serviceTopologyOperationAsync() throws Exception {
156         clearServicesData();
157         GenericResourceApiVnfOperationInformationBodyparam inputParam = operationsApiController.getObjectMapper().get().readValue(readFileContent("src/test/resources/vnf-assign-rpc.json"), GenericResourceApiVnfOperationInformationBodyparam.class);
158         operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
159
160         loadVnfData("src/test/resources/vnf-data.json");
161         inputParam.getInput().getServiceInformation().setServiceInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
162         operationsApiController.processAsyncVnfTopologyOperation("vnf-topology-operation",inputParam);
163         clearServicesData();
164     }
165
166     private void loadVnfData(String path) throws IOException {
167         ObjectMapper objectMapper = new ObjectMapper();
168         String content = readFileContent(path);
169         GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
170         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
171         svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
172         svcData.getVnfs().setVnf(new ArrayList<>());
173         svcData.getVnfs().addVnfItem(vnfData);
174         ConfigServices newService = new ConfigServices();
175         newService.setSvcData(objectMapper.writeValueAsString(svcData));
176         newService.setSvcInstanceId("98f189dd-2971-46f5-b4f1-1a9a323f39a4");
177         configServicesRepository.save(newService);
178     }
179
180     @Test
181     public void operationsGENERICRESOURCEAPInetworkTopologyOperationAssignPost() throws Exception {
182         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
183         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
184         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
185         System.setProperty("sdnc.config.dir", "src/test/resources");
186         // Remove any existing service data
187         clearServicesData();
188         operationalServicesRepository.deleteAll();
189
190         // Load services data
191         loadServicesData("src/test/resources/service1.json");
192
193         // Add invalid content
194         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
195         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
196                 .andReturn();
197         assertEquals(200, mvcResult.getResponse().getStatus());
198
199         // Add valid content
200         content = readFileContent("src/test/resources/network-assign-rpc.json");
201         mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
202                 .andReturn();
203         assertEquals(200, mvcResult.getResponse().getStatus());
204
205     }
206
207     @Test
208     public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
209
210         // Remove any existing service data
211         clearServicesData();
212         operationalServicesRepository.deleteAll();
213
214         // Load services data
215         loadServicesData("src/test/resources/service1.json");
216
217         // Add invalid content
218         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
219         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
220                 .andReturn();
221         assertEquals(200, mvcResult.getResponse().getStatus());
222
223         // Add valid content
224         content = readFileContent("src/test/resources/vnf-assign-rpc.json");
225         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
226                 .andReturn();
227         assertEquals(200, mvcResult.getResponse().getStatus());
228
229     }
230
231     @Test
232     public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationPost() throws Exception {
233
234         // Remove any existing service data
235         clearServicesData();
236         operationalServicesRepository.deleteAll();
237         assertEquals(0, configServicesRepository.count());
238         assertEquals(0, configNetworksRepository.count());
239         assertEquals(0, configVnfsRepository.count());
240         assertEquals(0, configVfModulesRepository.count());
241
242         // Load services data
243         loadServicesData("src/test/resources/service1-service_vnf.json");
244         assertEquals(1, configServicesRepository.count());
245         assertEquals(0, configNetworksRepository.count());
246         assertEquals(1, configVnfsRepository.count());
247         assertEquals(0, configVfModulesRepository.count());
248
249         // Add invalid content
250         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
251         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
252                 .andReturn();
253         assertEquals(200, mvcResult.getResponse().getStatus());
254
255         // Add valid content
256         content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
257         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
258                 .andReturn();
259         assertEquals(200, mvcResult.getResponse().getStatus());
260         assertEquals(1, configServicesRepository.count());
261         assertEquals(0, configNetworksRepository.count());
262         assertEquals(1, configVnfsRepository.count());
263         assertEquals(1, configVfModulesRepository.count());
264
265         // Delete content
266         content = readFileContent("src/test/resources/vf-module-unassign-rpc.json");
267         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
268                         .andReturn();
269         assertEquals(200, mvcResult.getResponse().getStatus());
270         assertEquals(1, configServicesRepository.count());
271         assertEquals(0, configNetworksRepository.count());
272         assertEquals(1, configVnfsRepository.count());
273         assertEquals(0, configVfModulesRepository.count());
274
275     }
276
277
278     @Test
279     public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
280
281         // Remove any existing service data
282         configPortMirrorConfigurationsRepository.deleteAll();
283         operationalPortMirrorConfigurationsRepository.deleteAll();
284
285         // Load port-mirror-configuration data
286         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
287
288         // Load services data
289         loadServicesData("src/test/resources/service1.json");
290
291         // Add invalid content for request input
292         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
293         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
294                 .andReturn();
295         assertEquals(200, mvcResult.getResponse().getStatus());
296
297         // Add valid content
298         content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
299         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
300                 .andReturn();
301         assertEquals(200, mvcResult.getResponse().getStatus());
302
303     }
304
305     @Test
306     public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
307
308         // Remove any existing service data
309         clearServicesData();
310         operationalServicesRepository.deleteAll();
311
312         // Load services data
313         loadServicesData("src/test/resources/service9.json");
314
315         // Add valid content
316         String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
317         String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
318         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
319                 .andReturn();
320         assertEquals(200, mvcResult.getResponse().getStatus());
321         assertEquals(expected, mvcResult.getResponse().getContentAsString());
322
323     }
324     @Test
325     public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
326
327         // Add valid content
328         String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
329         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
330                 .andReturn();
331         assertEquals(200, mvcResult.getResponse().getStatus());
332     }
333
334
335     private void loadServicesData(String path) throws IOException {
336         ObjectMapper objectMapper = new ObjectMapper();
337         String content = readFileContent(path);
338         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
339
340         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
341             ConfigServices newService = new ConfigServices();
342             newService.setSvcInstanceId(service.getServiceInstanceId());
343             newService.setServiceStatus(service.getServiceStatus());
344             serviceDataHelper.saveService(newService, service.getServiceData());
345         }
346     }
347
348     private void loadPortMirrorConfigurationData(String path) throws IOException {
349         ObjectMapper objectMapper = new ObjectMapper();
350         String content = readFileContent(path);
351         GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
352
353         for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
354             ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
355             newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
356             newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
357             newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
358             configPortMirrorConfigurationsRepository.save(newPmConfig);
359         }
360     }
361
362     private void clearServicesData() {
363         configServicesRepository.deleteAll();
364         configNetworksRepository.deleteAll();
365         configVnfsRepository.deleteAll();
366         configVfModulesRepository.deleteAll();
367     }
368
369     private String readFileContent(String path) throws IOException {
370         String content = new String(Files.readAllBytes(Paths.get(path)));
371         return content;
372     }
373 }