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