dd5a6a45fa21af05dc55a98e457306e66bf647d0
[sdnc/apps.git] /
1 package org.onap.sdnc.apps.ms.gra.controllers;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
5 import static org.junit.Assert.assertNull;
6
7 import java.io.IOException;
8 import java.nio.file.Files;
9 import java.nio.file.Paths;
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import com.fasterxml.jackson.databind.ObjectMapper;
14
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
19 import org.onap.sdnc.apps.ms.gra.controllers.ServiceDataHelper.ServiceDataTransaction;
20 import org.onap.sdnc.apps.ms.gra.data.*;
21 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
24 import org.springframework.boot.test.context.SpringBootTest;
25 import org.springframework.http.MediaType;
26 import org.springframework.test.context.junit4.SpringRunner;
27 import org.springframework.test.web.servlet.MockMvc;
28 import org.springframework.test.web.servlet.MvcResult;
29 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
30 import org.springframework.transaction.annotation.Transactional;
31
32 @RunWith(SpringRunner.class)
33 @SpringBootTest(classes={GenericResourceMsApp.class})
34 @AutoConfigureMockMvc
35 @Transactional
36 public class ConfigApiServicesControllerTest {
37
38     private final static String TEST_SVC_INSTANCE_ID = "5c4f2d89-57a3-47e9-b49b-d3c63eb0b3ca";
39     private final static String TEST_VNF_ID = "fae319cc-68d6-496f-be1e-a09e133c71d4";
40     private final static String TEST_VF_MODULE_ID = "45841173-3729-4a1d-a811-a3bde399e22d";
41     private final static String CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/";
42     private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/service/";
43     private final static String CONFIG_CR_ARS_CR_AR_URL = "/config/GENERIC-RESOURCE-API:contrail-route-allotted-resources/contrail-route-allotted-resource/";
44     private final static String CONFIG_PM_CONFIGS_PM_CONFIG_URL = "/config/GENERIC-RESOURCE-API:port-mirror-configurations/port-mirror-configuration/";
45
46     @Autowired
47     private MockMvc mvc;
48
49     @Autowired
50     ConfigServicesRepository configServicesRepository;
51
52     @Autowired
53     ConfigNetworksRepository configNetworksRepository;
54
55     @Autowired
56     ConfigVnfsRepository configVnfsRepository;
57
58     @Autowired
59     ConfigVfModulesRepository configVfModulesRepository;
60
61     @Autowired
62     ConfigContrailRouteAllottedResourcesRepository configContrailRouteAllottedResourcesRepository;
63
64     @Autowired
65     ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
66
67     @Autowired
68     ServiceDataHelper serviceDataHelper;
69
70     @BeforeClass
71     public static void setUp() throws Exception {
72         System.out.println("ConfigApiServicesControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
73         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
74         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
75         System.setProperty("sdnc.config.dir", "src/test/resources");
76    
77     }
78
79     @Test
80     public void serviceDataLoadTest() throws Exception {
81         // Clear service data
82         clearServicesData();
83         assertEquals(0, configServicesRepository.count());
84         assertEquals(0, configNetworksRepository.count());
85         assertEquals(0, configVnfsRepository.count());
86         assertEquals(0, configVfModulesRepository.count());
87
88         // Add service data - just service
89         loadServicesData("src/test/resources/service1-service.json");
90         assertEquals(1, configServicesRepository.count());
91         assertEquals(0, configNetworksRepository.count());
92         assertEquals(0, configVnfsRepository.count());
93         assertEquals(0, configVfModulesRepository.count());
94
95         // Clear service data
96         clearServicesData();
97         assertEquals(0, configServicesRepository.count());
98         assertEquals(0, configNetworksRepository.count());
99         assertEquals(0, configVnfsRepository.count());
100         assertEquals(0, configVfModulesRepository.count());
101
102        // Add service data - service, vnf, vf-module
103        loadServicesData("src/test/resources/service1.json");
104        assertEquals(1, configServicesRepository.count());
105        assertEquals(0, configNetworksRepository.count());
106        assertEquals(1, configVnfsRepository.count());
107        assertEquals(1, configVfModulesRepository.count());
108
109    
110
111     }
112
113     @Test
114     public void VnfDataLoadTest() throws Exception {
115         // Clear service data
116         clearServicesData();
117         assertEquals(0, configServicesRepository.count());
118         assertEquals(0, configNetworksRepository.count());
119         assertEquals(0, configVnfsRepository.count());
120         assertEquals(0, configVfModulesRepository.count());
121
122         // Add vnf data
123         loadVnfData("src/test/resources/vnf-data.json");
124         assertEquals(1, configServicesRepository.count());
125         assertEquals(0, configNetworksRepository.count());
126         assertEquals(1, configVnfsRepository.count());
127         assertEquals(1, configVfModulesRepository.count());
128
129         // Clear service data
130         clearServicesData();
131         assertEquals(0, configServicesRepository.count());
132         assertEquals(0, configNetworksRepository.count());
133         assertEquals(0, configVnfsRepository.count());
134         assertEquals(0, configVfModulesRepository.count());
135
136     }
137
138     @Test
139     public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
140
141         // Clean up data
142         clearServicesData();
143
144         // Load test data
145         loadServicesData( "src/test/resources/service1.json");
146
147         assertEquals(1, configServicesRepository.count());
148         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
149                 .andReturn();
150         assertEquals(204, mvcResult.getResponse().getStatus());
151         assertEquals(0, configServicesRepository.count());
152
153         // Test with no data
154         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
155                 .andReturn();
156         assertEquals(204, mvcResult.getResponse().getStatus());
157
158     }
159
160     @Test
161     public void configGENERICRESOURCEAPIservicesGet() throws Exception {
162         // Clean up data
163         clearServicesData();
164
165         // Test with data
166         loadServicesData("src/test/resources/service1.json");
167         assert(configServicesRepository.count() > 0);
168
169         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
170                 .andReturn();
171         assertEquals(200, mvcResult.getResponse().getStatus());
172
173         // Test with no data
174         clearServicesData();
175         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
176                 .andReturn();
177         assertEquals(404, mvcResult.getResponse().getStatus());
178     }
179
180     @Test
181     public void configGENERICRESOURCEAPIservicesPost() throws Exception {
182         // Clean up data
183         clearServicesData();
184
185         String content = readFileContent("src/test/resources/service1-services.json");
186
187         // Test with no data
188         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
189                 .andReturn();
190         assertEquals(201, mvcResult.getResponse().getStatus());
191         assertEquals(1, configServicesRepository.count());
192
193         // Test with existing data - should return 409
194         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
195                 .andReturn();
196         assertEquals(409, mvcResult.getResponse().getStatus());
197         assertEquals(1, configServicesRepository.count());
198
199         // Clean up data
200         clearServicesData();
201
202     }
203
204     @Test
205     public void configGENERICRESOURCEAPIservicesPut() throws Exception {
206         // Clean up data
207         clearServicesData();
208
209         String content = readFileContent("src/test/resources/service1-services.json");
210
211         // Test with no data
212         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
213                 .andReturn();
214         assertEquals(201, mvcResult.getResponse().getStatus());
215         assertEquals(1, configServicesRepository.count());
216
217         // Test with existing data - should return 409
218         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
219                 .andReturn();
220         assertEquals(204, mvcResult.getResponse().getStatus());
221         assertEquals(1, configServicesRepository.count());
222
223         // Clean up data
224         clearServicesData();
225
226     }
227
228     @Test
229     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
230         // Clean up data
231         clearServicesData();
232
233         // Test with no data
234         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
235                 .andReturn();
236         assertEquals(204, mvcResult.getResponse().getStatus());
237         assertEquals(0, configServicesRepository.count());
238
239         // Load data
240         loadServicesData("src/test/resources/service1.json");
241         assertEquals(1, configServicesRepository.count());
242
243         // Test with data
244         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
245                 .andReturn();
246         assertEquals(204, mvcResult.getResponse().getStatus());
247         assertEquals(0, configServicesRepository.count());
248
249     }
250
251     @Test
252     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
253         // Clean up data
254         clearServicesData();
255
256         // Test with data
257         loadServicesData("src/test/resources/service1.json");
258         assert(configServicesRepository.count() > 0);
259
260         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
261                 .andReturn();
262         assertEquals(200, mvcResult.getResponse().getStatus());
263
264         // Test with no data
265         clearServicesData();
266         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
267                 .andReturn();
268         assertEquals(404, mvcResult.getResponse().getStatus());
269     }
270
271     @Test
272     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
273         // Clean up data
274         clearServicesData();
275
276         String content = readFileContent("src/test/resources/service1-serviceitem.json");
277
278         // Test with no data
279         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(content))
280                 .andReturn();
281         assertEquals(201, mvcResult.getResponse().getStatus());
282         assertEquals(1, configServicesRepository.count());
283
284         // Test with existing data - should return 409
285         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(content))
286                 .andReturn();
287         assertEquals(409, mvcResult.getResponse().getStatus());
288         assertEquals(1, configServicesRepository.count());
289
290         // Clean up data
291         clearServicesData();
292
293     }
294
295     @Test
296     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
297         // Clean up data
298         clearServicesData();
299
300         String content = readFileContent("src/test/resources/service1-serviceitem.json");
301
302         // Test with no data
303         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(content))
304                 .andReturn();
305         assertEquals(201, mvcResult.getResponse().getStatus());
306         assertEquals(1, configServicesRepository.count());
307
308         // Test with existing data - should return 409
309         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/").contentType(MediaType.APPLICATION_JSON).content(content))
310                 .andReturn();
311         assertEquals(204, mvcResult.getResponse().getStatus());
312         assertEquals(1, configServicesRepository.count());
313
314         // Clean up data
315         clearServicesData();
316     }
317
318     @Test
319     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
320         // Clean up data
321         clearServicesData();
322
323         // Test with no data
324         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
325                 .andReturn();
326         assertEquals(404, mvcResult.getResponse().getStatus());
327         assertEquals(0, configServicesRepository.count());
328
329         // Load data
330         loadServicesData("src/test/resources/service1.json");
331         assertEquals(1, configServicesRepository.count());
332
333         // Test with data
334         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
335                 .andReturn();
336         assertEquals(204, mvcResult.getResponse().getStatus());
337         assertEquals(1, configServicesRepository.count());
338         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
339         assertEquals(1, services.size());
340         assertEquals(null, services.get(0).getSvcData());
341
342
343     }
344
345     @Test
346     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
347         // Clean up data
348         clearServicesData();
349
350         // Test with data
351         loadServicesData("src/test/resources/service1.json");
352         assert(configServicesRepository.count() > 0);
353
354         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
355                 .andReturn();
356         assertEquals(200, mvcResult.getResponse().getStatus());
357
358         // Test with no data
359         clearServicesData();
360         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
361                 .andReturn();
362         assertEquals(404, mvcResult.getResponse().getStatus());
363     }
364
365     @Test
366     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
367         // Clean up data
368         clearServicesData();
369
370         String content = readFileContent("src/test/resources/service1-servicedata.json");
371
372         // Test with no data
373         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
374                 .andReturn();
375         assertEquals(404, mvcResult.getResponse().getStatus());
376         assertEquals(0, configServicesRepository.count());
377
378         // Test with empty service data
379         ConfigServices service = new ConfigServices();
380         service.setSvcInstanceId(TEST_SVC_INSTANCE_ID);
381         configServicesRepository.save(service);
382         assertEquals(1, configServicesRepository.count());
383         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
384                 .andReturn();
385         assertEquals(201, mvcResult.getResponse().getStatus());
386         assertEquals(1, configServicesRepository.count());
387         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
388         assertEquals(1, updatedService.size());
389         assertNotEquals(null, updatedService.get(0).getSvcData());
390
391         // Test with existing data - should return 409
392         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
393                 .andReturn();
394         assertEquals(409, mvcResult.getResponse().getStatus());
395
396         // Clean up data
397         clearServicesData();
398     }
399
400     @Test
401     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
402         // Clean up data
403         clearServicesData();
404
405         String content = readFileContent("src/test/resources/service1-servicedata.json");
406
407         // Test with no data
408         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
409                 .andReturn();
410         assertEquals(404, mvcResult.getResponse().getStatus());
411         assertEquals(0, configServicesRepository.count());
412
413         // Test with empty service data
414         ConfigServices service = new ConfigServices();
415         service.setSvcInstanceId(TEST_SVC_INSTANCE_ID);
416         configServicesRepository.save(service);
417         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
418                 .andReturn();
419         assertEquals(201, mvcResult.getResponse().getStatus());
420         assertEquals(1, configServicesRepository.count());
421         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
422         assertEquals(1, updatedService.size());
423         assertNotEquals(null, updatedService.get(0).getSvcData());
424
425         // Test with existing data - should return 204
426         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
427                 .andReturn();
428         assertEquals(204, mvcResult.getResponse().getStatus());
429
430         // Clean up data
431         clearServicesData();
432     }
433
434     @Test
435     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
436         // Clean up data
437         clearServicesData();
438
439         // Test with no data
440         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
441                 .andReturn();
442         assertEquals(404, mvcResult.getResponse().getStatus());
443         assertEquals(0, configServicesRepository.count());
444
445         // Load data
446         loadServicesData("src/test/resources/service1.json");
447         assertEquals(1, configServicesRepository.count());
448
449         // Test with data
450         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
451                 .andReturn();
452         assertEquals(204, mvcResult.getResponse().getStatus());
453         assertEquals(1, configServicesRepository.count());
454         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
455         assertEquals(1, services.size());
456         assertEquals(null, services.get(0).getServiceStatus());
457     }
458
459     @Test
460     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
461         // Clean up data
462         clearServicesData();
463
464         // Test with data
465         loadServicesData("src/test/resources/service1.json");
466         assert(configServicesRepository.count() > 0);
467
468         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
469                 .andReturn();
470         assertEquals(200, mvcResult.getResponse().getStatus());
471
472         // Test with no data
473         clearServicesData();
474         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
475                 .andReturn();
476         assertEquals(404, mvcResult.getResponse().getStatus());
477     }
478
479     @Test
480     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
481         // Clean up data
482         clearServicesData();
483
484         String content = readFileContent("src/test/resources/service1-servicestatus.json");
485
486         // Test with no data
487         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
488                 .andReturn();
489         assertEquals(404, mvcResult.getResponse().getStatus());
490         assertEquals(0, configServicesRepository.count());
491
492         // Test with empty service data
493         ConfigServices service = new ConfigServices();
494         service.setSvcInstanceId(TEST_SVC_INSTANCE_ID);
495         configServicesRepository.save(service);
496         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
497                 .andReturn();
498         assertEquals(201, mvcResult.getResponse().getStatus());
499         assertEquals(1, configServicesRepository.count());
500         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
501         assertEquals(1, updatedService.size());
502         assertNotEquals(null, updatedService.get(0).getServiceStatus());
503
504         // Test with existing data - should return 409
505         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
506                 .andReturn();
507         assertEquals(409, mvcResult.getResponse().getStatus());
508
509         // Clean up data
510         clearServicesData();
511     }
512
513     @Test
514     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
515         // Clean up data
516         clearServicesData();
517
518         String content = readFileContent("src/test/resources/service1-servicestatus.json");
519
520         // Test with no data
521         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
522                 .andReturn();
523         assertEquals(404, mvcResult.getResponse().getStatus());
524         assertEquals(0, configServicesRepository.count());
525
526         // Test with empty service status
527         ConfigServices service = new ConfigServices();
528         service.setSvcInstanceId(TEST_SVC_INSTANCE_ID);
529         configServicesRepository.save(service);
530         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
531                 .andReturn();
532         assertEquals(201, mvcResult.getResponse().getStatus());
533         assertEquals(1, configServicesRepository.count());
534         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId(TEST_SVC_INSTANCE_ID);
535         assertEquals(1, updatedService.size());
536         assertNotEquals(null, updatedService.get(0).getServiceStatus());
537
538         // Test with existing data - should return 204
539         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
540                 .andReturn();
541         assertEquals(204, mvcResult.getResponse().getStatus());
542
543         // Clean up data
544         clearServicesData();
545     }
546
547     @Test
548     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataServiceTopologyGet() throws Exception {
549         // Clean up data
550         clearServicesData();
551
552         // Test with data
553         loadServicesData("src/test/resources/service1.json");
554         assert(configServicesRepository.count() > 0);
555
556         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/service-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
557                 .andReturn();
558         assertEquals(200, mvcResult.getResponse().getStatus());
559
560         // Test with no data
561         clearServicesData();
562         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/service-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
563                 .andReturn();
564         assertEquals(404, mvcResult.getResponse().getStatus());
565     }
566
567     @Test
568     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdDelete() throws Exception {
569         // Clean up data
570         clearServicesData();
571
572         // Test with no data
573         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
574                                       .andReturn();
575         assertEquals(404, mvcResult.getResponse().getStatus());
576         assertEquals(0, configServicesRepository.count());
577
578         // Load data
579         loadVnfData("src/test/resources/vnf-data.json");
580         assertEquals(1, configServicesRepository.count());
581
582         // Test with data
583         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
584                             .andReturn();
585         assertEquals(204, mvcResult.getResponse().getStatus());
586         assertEquals(1, configServicesRepository.count());
587
588         // Test with bad data
589
590         clearServicesData();
591         createBadVnfData(true, true);
592         assertEquals(1, configServicesRepository.count());
593         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
594                             .andReturn();
595         assertEquals(404, mvcResult.getResponse().getStatus());
596
597         clearServicesData();
598         createBadVnfData(false, false);
599         assertEquals(1, configServicesRepository.count());
600         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
601                             .andReturn();
602         assertEquals(404, mvcResult.getResponse().getStatus());
603
604         clearServicesData();
605         createBadVnfData(false, true);
606         assertEquals(1, configServicesRepository.count());
607         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
608                             .andReturn();
609         assertEquals(404, mvcResult.getResponse().getStatus());
610     }
611
612     @Test
613     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdGet() throws Exception {
614         // Clean up data
615         clearServicesData();
616
617         // Test with no data
618         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
619                                       .andReturn();
620         assertEquals(404, mvcResult.getResponse().getStatus());
621         assertEquals(0, configServicesRepository.count());
622
623         // Load data
624         loadVnfData("src/test/resources/vnf-data.json");
625         assertEquals(1, configServicesRepository.count());
626
627         // Test with data
628         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
629                             .andReturn();
630         assertEquals(200, mvcResult.getResponse().getStatus());
631
632         clearServicesData();
633         createBadVnfData(false, false);
634         assertEquals(1, configServicesRepository.count());
635         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(""))
636                             .andReturn();
637         assertEquals(404, mvcResult.getResponse().getStatus());
638     }
639
640     @Test
641     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdPut() throws Exception {
642         // Clean up data
643         clearServicesData();
644         assertEquals(0, configServicesRepository.count());
645         assertEquals(0, configVnfsRepository.count());
646         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf.json")))
647                                       .andReturn();
648         assertEquals(201, mvcResult.getResponse().getStatus());
649         assertEquals(1, configVnfsRepository.count());
650
651         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf.json")))
652                             .andReturn();
653         assertEquals(204, mvcResult.getResponse().getStatus());
654         assertEquals(1, configVnfsRepository.count());
655     }
656
657     @Test
658     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyGet() throws Exception {
659         // Clean up data
660         clearServicesData();
661
662         // Test with no data
663         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
664                                       .andReturn();
665         assertEquals(404, mvcResult.getResponse().getStatus());
666         assertEquals(0, configServicesRepository.count());
667
668         // Load data
669         loadVnfData("src/test/resources/vnf-data.json");
670         assertEquals(1, configServicesRepository.count());
671
672         // Test with data
673         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
674                             .andReturn();
675         assertEquals(200, mvcResult.getResponse().getStatus());
676
677         clearServicesData();
678         createBadVnfData(false, false);
679         assertEquals(1, configServicesRepository.count());
680         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/"+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
681                             .andReturn();
682         assertEquals(404, mvcResult.getResponse().getStatus());
683     }
684
685     @Test
686     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfLevelOperStatusPut() throws Exception {
687         // Clean up data
688         clearServicesData();
689         assertEquals(0, configServicesRepository.count());
690         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-level-oper-status/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-level-oper-status.json")))
691                                       .andReturn();
692         assertEquals(201, mvcResult.getResponse().getStatus());
693         assertEquals(1, configServicesRepository.count());
694
695         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-level-oper-status/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-level-oper-status.json")))
696                             .andReturn();
697         assertEquals(204, mvcResult.getResponse().getStatus());
698         assertEquals(1, configServicesRepository.count());
699     }
700
701     @Test
702     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyOnapModelInformationPut() throws Exception {
703         // Clean up data
704         clearServicesData();
705         assertEquals(0, configServicesRepository.count());
706         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/onap-model-information/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-onap-model-info.json")))
707                                       .andReturn();
708         assertEquals(201, mvcResult.getResponse().getStatus());
709         assertEquals(1, configServicesRepository.count());
710
711         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/onap-model-information/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-onap-model-info.json")))
712                             .andReturn();
713         assertEquals(204, mvcResult.getResponse().getStatus());
714         assertEquals(1, configServicesRepository.count());
715     }
716
717     @Test
718     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksPut() throws Exception {
719         // Clean up data
720         clearServicesData();
721         assertEquals(0, configServicesRepository.count());
722         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-vnf-networks.json")))
723                                       .andReturn();
724         assertEquals(201, mvcResult.getResponse().getStatus());
725         assertEquals(1, configServicesRepository.count());
726
727         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-vnf-networks.json")))
728                             .andReturn();
729         assertEquals(204, mvcResult.getResponse().getStatus());
730         assertEquals(1, configServicesRepository.count());
731     }
732
733     @Test
734     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksVnfNetworkNetworkRolePut() throws Exception {
735         // Clean up data
736         clearServicesData();
737         assertEquals(0, configServicesRepository.count());
738         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/vnf-network/test-network-role/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-vnf-networks-network-role.json")))
739                                       .andReturn();
740         assertEquals(201, mvcResult.getResponse().getStatus());
741         assertEquals(1, configServicesRepository.count());
742
743         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/vnf-network/test-network-role/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-vnf-networks-network-role.json")))
744                             .andReturn();
745         assertEquals(204, mvcResult.getResponse().getStatus());
746         assertEquals(1, configServicesRepository.count());
747     }
748
749     @Test
750     public void configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdPut() throws Exception {
751         // Clean up data
752         configPortMirrorConfigurationsRepository.deleteAll();
753
754         String content = readFileContent("src/test/resources/port-mirror-configuration-item.json");
755
756         // Test with no data
757         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-2/")
758                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
759         assertEquals(201, mvcResult.getResponse().getStatus());
760
761         // Test with existing port-mirror-configuration
762         // Load data
763         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
764         assertEquals(2, configPortMirrorConfigurationsRepository.count());
765
766         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-2/")
767                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
768         assertEquals(204, mvcResult.getResponse().getStatus());
769         assertEquals(2, configPortMirrorConfigurationsRepository.count());
770
771         // Clean up data
772         configPortMirrorConfigurationsRepository.deleteAll();
773     }
774
775     @Test
776     public void configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdDelete() throws Exception {
777         // Clean up data
778         configPortMirrorConfigurationsRepository.deleteAll();
779
780         // Test with no data
781         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/")
782                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
783
784         assertEquals(204, mvcResult.getResponse().getStatus());
785         assertEquals(0, configPortMirrorConfigurationsRepository.count());
786
787         // Load data
788         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
789         assertEquals(1, configPortMirrorConfigurationsRepository.count());
790
791         // Test with data
792         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/")
793                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
794
795         assertEquals(204, mvcResult.getResponse().getStatus());
796         assertEquals(0, configPortMirrorConfigurationsRepository.count());
797     }
798
799     @Test
800     public void configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdGet() throws Exception {
801         // Clean up data
802         configPortMirrorConfigurationsRepository.deleteAll();
803
804         // Test with data
805         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
806         assert(configPortMirrorConfigurationsRepository.count() > 0);
807
808         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/")
809                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
810         assertEquals(200, mvcResult.getResponse().getStatus());
811
812         // Test with bad allotted-resource-id in input
813         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"dummy/")
814                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
815
816         assertEquals(404, mvcResult.getResponse().getStatus());
817
818         // Test with no data
819         configPortMirrorConfigurationsRepository.deleteAll();
820         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/")
821                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
822
823         assertEquals(404, mvcResult.getResponse().getStatus());
824     }
825
826     @Test
827     public void configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdConfigurationDataPortMirrorConfigurationTopologyGet() throws Exception {
828         // Clean up data
829         configPortMirrorConfigurationsRepository.deleteAll();
830
831         // Test with data
832         loadPortMirrorConfigurationData("src/test/resources/port-mirror-configuration-1.json");
833         assert(configPortMirrorConfigurationsRepository.count() > 0);
834
835         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/configuration-data/port-mirror-configuration-topology/")
836                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
837         assertEquals(200, mvcResult.getResponse().getStatus());
838
839         // Test with dummy allotted-resource-id
840         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"dummy/configuration-data/port-mirror-configuration-topology/")
841                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
842         assertEquals(404, mvcResult.getResponse().getStatus());
843
844         // Test with no data
845         configPortMirrorConfigurationsRepository.deleteAll();
846         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PM_CONFIGS_PM_CONFIG_URL+"pm-config-1/configuration-data/port-mirror-configuration-topology/")
847                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
848         assertEquals(404, mvcResult.getResponse().getStatus());
849     }
850
851     @Test
852     public void configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdPut() throws Exception {
853         // Clean up data
854         configContrailRouteAllottedResourcesRepository.deleteAll();
855
856         String content = readFileContent("src/test/resources/allotted-resource-item.json");
857
858         // Test with no data
859         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_CR_ARS_CR_AR_URL+"ar2/")
860                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
861         assertEquals(201, mvcResult.getResponse().getStatus());
862
863         // Test with existing allotted-resource
864         // Load data
865         configContrailRouteAllottedResourcesRepository.deleteAll();
866         loadContrailRouteAllottedResourceData("src/test/resources/contrail-route-allotted-resource-1.json");
867         assertEquals(1, configContrailRouteAllottedResourcesRepository.count());
868
869         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_CR_ARS_CR_AR_URL+"ar2/")
870                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
871         assertEquals(201, mvcResult.getResponse().getStatus());
872         assertEquals(2, configContrailRouteAllottedResourcesRepository.count());
873
874         // Clean up data
875         configContrailRouteAllottedResourcesRepository.deleteAll();
876     }
877
878     @Test
879     public void configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdDelete() throws Exception {
880         // Clean up data
881         configContrailRouteAllottedResourcesRepository.deleteAll();
882
883         // Test with no data
884         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_CR_ARS_CR_AR_URL+"ar1/")
885                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
886
887         assertEquals(204, mvcResult.getResponse().getStatus());
888         assertEquals(0, configContrailRouteAllottedResourcesRepository.count());
889
890         // Load data
891         loadContrailRouteAllottedResourceData("src/test/resources/contrail-route-allotted-resource-1.json");
892         assertEquals(1, configContrailRouteAllottedResourcesRepository.count());
893
894         // Test with data
895         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_CR_ARS_CR_AR_URL+"ar1/")
896                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
897
898         assertEquals(204, mvcResult.getResponse().getStatus());
899         assertEquals(0, configContrailRouteAllottedResourcesRepository.count());
900     }
901
902     @Test
903     public void configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdGet() throws Exception {
904         // Clean up data
905         configContrailRouteAllottedResourcesRepository.deleteAll();
906
907         // Test with data
908         loadContrailRouteAllottedResourceData("src/test/resources/contrail-route-allotted-resource-1.json");
909         assert(configContrailRouteAllottedResourcesRepository.count() > 0);
910
911         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"ar1/")
912                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
913         assertEquals(200, mvcResult.getResponse().getStatus());
914
915         // Test with bad allotted-resource-id in input
916         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"dummy/")
917                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
918
919         assertEquals(404, mvcResult.getResponse().getStatus());
920
921         // Test with no data
922         configContrailRouteAllottedResourcesRepository.deleteAll();
923         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"ar1/")
924                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
925
926         assertEquals(404, mvcResult.getResponse().getStatus());
927     }
928
929     @Test
930     public void configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdAllottedResourceDataContrailRouteTopologyGet() throws Exception {
931         // Clean up data
932         configContrailRouteAllottedResourcesRepository.deleteAll();
933
934         // Test with data
935         loadContrailRouteAllottedResourceData("src/test/resources/contrail-route-allotted-resource-1.json");
936         assert(configContrailRouteAllottedResourcesRepository.count() > 0);
937
938         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"ar1/allotted-resource-data/contrail-route-topology/")
939                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
940         assertEquals(200, mvcResult.getResponse().getStatus());
941
942         // Test with dummy allotted-resource-id
943         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"dummy/allotted-resource-data/contrail-route-topology/").contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
944         assertEquals(404, mvcResult.getResponse().getStatus());
945
946         // Test with no data
947         configContrailRouteAllottedResourcesRepository.deleteAll();
948         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_CR_ARS_CR_AR_URL+"ar1/allotted-resource-data/contrail-route-topology/").contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
949         assertEquals(404, mvcResult.getResponse().getStatus());
950     }
951
952     @Test
953     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdPut() throws Exception {
954         // Clean up data
955         clearServicesData();
956
957         String content = readFileContent("src/test/resources/service1-vfmodule-item.json");
958
959         // Test with no data
960         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/"+TEST_VF_MODULE_ID+"/")
961                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
962         assertEquals(400, mvcResult.getResponse().getStatus());
963
964         // Test with existing service and vnf
965         // Load data
966         loadServicesData("src/test/resources/service1.json");
967         assertEquals(1, configServicesRepository.count());
968
969         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/"+TEST_VF_MODULE_ID+"/")
970                 .contentType(MediaType.APPLICATION_JSON).content(content)).andReturn();
971         assertEquals(204, mvcResult.getResponse().getStatus());
972         assertEquals(1, configServicesRepository.count());
973
974         // Clean up data
975         clearServicesData();
976     }
977
978     @Test
979     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdDelete() throws Exception {
980         // Clean up data
981         clearServicesData();
982
983         // Test with no data
984         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/")
985                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
986
987         assertEquals(400, mvcResult.getResponse().getStatus());
988         assertEquals(0, configServicesRepository.count());
989
990         // Load data
991         loadServicesData("src/test/resources/service1.json");
992         assertEquals(1, configServicesRepository.count());
993
994         // Test with data
995         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/")
996                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
997
998         assertEquals(200, mvcResult.getResponse().getStatus());
999     }
1000
1001     @Test
1002     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdGet() throws Exception {
1003         // Clean up data
1004         clearServicesData();
1005
1006         // Test with data
1007         loadServicesData("src/test/resources/service1.json");
1008         assert(configServicesRepository.count() > 0);
1009
1010         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/")
1011                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
1012
1013         assertEquals(200, mvcResult.getResponse().getStatus());
1014
1015         // Test with bad vf-module-id in input
1016         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/dummyid/")
1017                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
1018
1019         assertEquals(404, mvcResult.getResponse().getStatus());
1020
1021         // Test with no data
1022         clearServicesData();
1023         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/")
1024                 .contentType(MediaType.APPLICATION_JSON).content("")).andReturn();
1025
1026         assertEquals(404, mvcResult.getResponse().getStatus());
1027     }
1028
1029     @Test
1030     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdVfModuleDataVfModuleTopologyGet() throws Exception {
1031         // Clean up data
1032         clearServicesData();
1033
1034         // Test with data
1035         loadServicesData("src/test/resources/service1.json");
1036         assert(configServicesRepository.count() > 0);
1037
1038         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/vf-module-data/vf-module-topology/").contentType(MediaType.APPLICATION_JSON)
1039                 .content("")).andReturn();
1040         assertEquals(200, mvcResult.getResponse().getStatus());
1041
1042         // Test with existing service and vnf but with dummy vf-module-id in input
1043         clearServicesData();
1044         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/dummy/vf-module-data/vf-module-topology/").contentType(MediaType.APPLICATION_JSON)
1045                 .content("")).andReturn();
1046         assertEquals(404, mvcResult.getResponse().getStatus());
1047
1048         // Test with no data
1049         clearServicesData();
1050         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+TEST_SVC_INSTANCE_ID+"/service-data/vnfs/vnf/"+TEST_VNF_ID+"/vnf-data/vf-modules/vf-module/269bda16-f40c-41a9-baef-e8905ab2b70e/vf-module-data/vf-module-topology/").contentType(MediaType.APPLICATION_JSON)
1051                 .content("")).andReturn();
1052         assertEquals(404, mvcResult.getResponse().getStatus());
1053     }
1054
1055     private String readFileContent(String path) throws IOException {
1056         String content = new String(Files.readAllBytes(Paths.get(path)));
1057         return content;
1058     }
1059
1060     private void deleteData(String url) throws Exception {
1061         mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
1062                 .andReturn();
1063     }
1064
1065     private void clearServicesData() {
1066         configServicesRepository.deleteAll();
1067         configNetworksRepository.deleteAll();
1068         configVnfsRepository.deleteAll();
1069         configVfModulesRepository.deleteAll();
1070     }
1071
1072     private void loadServicesData(String path) throws IOException {
1073         ObjectMapper objectMapper = new ObjectMapper();
1074         String content = readFileContent(path);
1075         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
1076
1077         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
1078             ConfigServices newService = new ConfigServices();
1079             newService.setSvcInstanceId(service.getServiceInstanceId());
1080             newService.setServiceStatus(service.getServiceStatus());
1081             serviceDataHelper.saveService(newService, service.getServiceData());
1082         }
1083     }
1084
1085     private void loadServicesData(String path, ServiceDataTransaction transaction) throws IOException {
1086         ObjectMapper objectMapper = new ObjectMapper();
1087         String content = readFileContent(path);
1088         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
1089
1090         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
1091             ConfigServices newService = new ConfigServices();
1092             newService.setSvcInstanceId(service.getServiceInstanceId());
1093             newService.setServiceStatus(service.getServiceStatus());
1094             serviceDataHelper.saveService(newService, service.getServiceData(), transaction);
1095         }
1096         transaction.commit();
1097     }
1098
1099     private void loadVnfData(String path) throws IOException {
1100         ObjectMapper objectMapper = new ObjectMapper();
1101         String content = readFileContent(path);
1102         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
1103         String svcInstanceId = TEST_SVC_INSTANCE_ID;
1104         ConfigServices newService = new ConfigServices(svcInstanceId);
1105         configServicesRepository.save(newService);
1106         serviceDataHelper.saveVnf(svcInstanceId, vnfData, null);
1107     }
1108
1109     private void loadVnfData(String path, ServiceDataTransaction transaction) throws IOException {
1110         ObjectMapper objectMapper = new ObjectMapper();
1111         String content = readFileContent(path);
1112         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
1113         String svcInstanceId = TEST_SVC_INSTANCE_ID;
1114         ConfigServices newService = new ConfigServices(svcInstanceId);
1115         configServicesRepository.save(newService);
1116         serviceDataHelper.saveVnf(svcInstanceId, vnfData, transaction);
1117         if (transaction != null) {
1118             transaction.commit();
1119         }
1120     }
1121
1122     private void createBadVnfData(boolean useNullSvc, boolean useNullVnfs) throws IOException {
1123         ObjectMapper objectMapper = new ObjectMapper();
1124         ConfigServices newService = new ConfigServices();
1125         GenericResourceApiServicedataServiceData svcData = useNullSvc ? null : new GenericResourceApiServicedataServiceData();
1126         GenericResourceApiServicedataServicedataVnfs vnfs = useNullVnfs ? null : new GenericResourceApiServicedataServicedataVnfs();
1127
1128         // Overrides useNullSvc
1129         if(!useNullVnfs) {
1130             svcData = new GenericResourceApiServicedataServiceData();
1131             vnfs.setVnf(new ArrayList<>());
1132             svcData.setVnfs(vnfs);
1133         }
1134
1135         newService.setSvcInstanceId("test-siid");
1136         newService.setSvcData(objectMapper.writeValueAsString(svcData));
1137         configServicesRepository.save(newService);
1138     }
1139
1140     private void loadContrailRouteAllottedResourceData(String path) throws IOException {
1141         ObjectMapper objectMapper = new ObjectMapper();
1142         String content = readFileContent(path);
1143         GenericResourceApiContrailRouteAllottedResources allottedResources = objectMapper.readValue(content, GenericResourceApiContrailRouteAllottedResources.class);
1144
1145         for (GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource allottedResource : allottedResources.getContrailRouteAllottedResource()) {
1146             ConfigContrailRouteAllottedResources newContrailRouteAllottedResource = new ConfigContrailRouteAllottedResources();
1147             newContrailRouteAllottedResource.setAllottedResourceId(allottedResource.getAllottedResourceId());
1148             newContrailRouteAllottedResource.setArData(objectMapper.writeValueAsString(allottedResource.getAllottedResourceData()));
1149             newContrailRouteAllottedResource.setAllottedResourceStatus(allottedResource.getAllottedResourceStatus());
1150             configContrailRouteAllottedResourcesRepository.save(newContrailRouteAllottedResource);
1151         }
1152     }
1153
1154     private void loadPortMirrorConfigurationData(String path) throws IOException {
1155         ObjectMapper objectMapper = new ObjectMapper();
1156         String content = readFileContent(path);
1157         GenericResourceApiPortMirrorConfigurations pmConfigurations = objectMapper.readValue(content, GenericResourceApiPortMirrorConfigurations.class);
1158
1159         for (GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration pmConfig : pmConfigurations.getPortMirrorConfiguration()) {
1160             ConfigPortMirrorConfigurations newPmConfig = new ConfigPortMirrorConfigurations();
1161             newPmConfig.setConfigurationId(pmConfig.getConfigurationId());
1162             newPmConfig.setPmcData(objectMapper.writeValueAsString(pmConfig.getConfigurationData()));
1163             newPmConfig.setPortMirrorConfigurationStatus(pmConfig.getConfigurationStatus());
1164             configPortMirrorConfigurationsRepository.save(newPmConfig);
1165         }
1166     }
1167
1168 }