3c79e1d5d373040f36f286d3527e6a1d828cfd87
[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         System.out.println("OperationsApiControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
171         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
172         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
173         System.setProperty("sdnc.config.dir", "src/test/resources");
174         // Remove any existing service data
175         configServicesRepository.deleteAll();
176         operationalServicesRepository.deleteAll();
177
178         // Load services data
179         loadServicesData("src/test/resources/service1.json");
180
181         // Add invalid content
182         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
183         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
184                 .andReturn();
185         assertEquals(200, mvcResult.getResponse().getStatus());
186
187         // Add valid content
188         content = readFileContent("src/test/resources/network-assign-rpc.json");
189         mvcResult = mvc.perform(MockMvcRequestBuilders.post(NETWORK_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
190                 .andReturn();
191         assertEquals(200, mvcResult.getResponse().getStatus());
192
193     }
194
195     @Test
196     public void operationsGENERICRESOURCEAPIvnfTopologyOperationAssignPost() throws Exception {
197
198         // Remove any existing service data
199         configServicesRepository.deleteAll();
200         operationalServicesRepository.deleteAll();
201
202         // Load services data
203         loadServicesData("src/test/resources/service1.json");
204
205         // Add invalid content
206         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
207         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
208                 .andReturn();
209         assertEquals(200, mvcResult.getResponse().getStatus());
210
211         // Add valid content
212         content = readFileContent("src/test/resources/vnf-assign-rpc.json");
213         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
214                 .andReturn();
215         assertEquals(200, mvcResult.getResponse().getStatus());
216
217     }
218
219     @Test
220     public void operationsGENERICRESOURCEAPIvfModuleTopologyOperationAssignPost() throws Exception {
221
222         // Remove any existing service data
223         configServicesRepository.deleteAll();
224         operationalServicesRepository.deleteAll();
225
226         // Load services data
227         loadServicesData("src/test/resources/service1.json");
228
229         // Add invalid content
230         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
231         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
232                 .andReturn();
233         assertEquals(200, mvcResult.getResponse().getStatus());
234
235         // Add valid content
236         content = readFileContent("src/test/resources/vf-module-assign-rpc.json");
237         mvcResult = mvc.perform(MockMvcRequestBuilders.post(VF_MODULE_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
238                 .andReturn();
239         assertEquals(200, mvcResult.getResponse().getStatus());
240
241     }
242
243     @Test
244     public void operationsGENERICRESOURCEAPIportMirrorConfigurationTopologyOperationAssignPost() throws Exception {
245
246         // Remove any existing service data
247         configPortMirrorConfigurationsRepository.deleteAll();
248         operationalPortMirrorConfigurationsRepository.deleteAll();
249
250         // Load port-mirror-configuration data
251         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
252
253         // Load services data
254         loadServicesData("src/test/resources/service1.json");
255
256         // Add invalid content for request input
257         String content = readFileContent("src/test/resources/preload1-rpc-vfmodule.json");
258         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
259                 .andReturn();
260         assertEquals(200, mvcResult.getResponse().getStatus());
261
262         // Add valid content
263         content = readFileContent("src/test/resources/port-mirror-assign-rpc.json");
264         mvcResult = mvc.perform(MockMvcRequestBuilders.post(PORT_MIRROR_TOPOLOGY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
265                 .andReturn();
266         assertEquals(200, mvcResult.getResponse().getStatus());
267
268     }
269
270     @Test
271     public void operationsGENERICRESOURCEAPIvnfGetResourceRequestPost() throws Exception {
272
273         // Remove any existing service data
274         configServicesRepository.deleteAll();
275         operationalServicesRepository.deleteAll();
276
277         // Load services data
278         loadServicesData("src/test/resources/service9.json");
279
280         // Add valid content
281         String content = readFileContent("src/test/resources/vnf-get-resource-request-rpc.json");
282         String expected = readFileContent("src/test/resources/vnf-get-resource-request-expected.json");
283         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(VNF_GET_RESOURCE_REQUEST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
284                 .andReturn();
285         assertEquals(200, mvcResult.getResponse().getStatus());
286         assertEquals(expected, mvcResult.getResponse().getContentAsString());
287
288     }
289     @Test
290     public void operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost() throws Exception {
291
292         // Add valid content
293         String content = readFileContent("src/test/resources/policy-update-notify-rpc.json");
294         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(POLICY_UPDATE_NOTIFY_URL).contentType(MediaType.APPLICATION_JSON).content(content))
295                 .andReturn();
296         assertEquals(200, mvcResult.getResponse().getStatus());
297     }
298
299
300     private void loadServicesData(String path) throws IOException {
301         ObjectMapper objectMapper = new ObjectMapper();
302         String content = readFileContent(path);
303         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
304
305         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
306             ConfigServices newService = new ConfigServices();
307             newService.setSvcInstanceId(service.getServiceInstanceId());
308             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
309             newService.setServiceStatus(service.getServiceStatus());
310             configServicesRepository.save(newService);
311         }
312     }
313
314     private void loadPortMirrorConfigurationData(String path) throws IOException {
315         ObjectMapper objectMapper = new ObjectMapper();
316         String content = readFileContent(path);
317         GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
318
319         for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
320             ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
321             newPmConfig.setConfigureationId(pmConfig.getConfigurationId());
322             newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
323             newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
324             configPortMirrorConfigurationsRepository.save(newPmConfig);
325         }
326     }
327
328     private String readFileContent(String path) throws IOException {
329         String content = new String(Files.readAllBytes(Paths.get(path)));
330         return content;
331     }
332 }