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