b9ec3dea1aa296f2489ba5f8db0efad203912ede
[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.data.ConfigServices;
20 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
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 CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/";
39     private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/service/";
40
41     @Autowired
42     private MockMvc mvc;
43
44     @Autowired
45     ConfigServicesRepository configServicesRepository;
46
47     @BeforeClass
48     public static void setUp() throws Exception {
49         System.out.println("ConfigApiServicesControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
50         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
51         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
52         System.setProperty("sdnc.config.dir", "src/test/resources");
53    
54     }
55
56
57     @Test
58     public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
59
60         // Clean up data
61         configServicesRepository.deleteAll();
62
63         // Load test data
64         loadServicesData( "src/test/resources/service1.json");
65
66         assertEquals(1, configServicesRepository.count());
67         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
68                 .andReturn();
69         assertEquals(204, mvcResult.getResponse().getStatus());
70         assertEquals(0, configServicesRepository.count());
71
72         // Test with no data
73         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
74                 .andReturn();
75         assertEquals(204, mvcResult.getResponse().getStatus());
76
77     }
78
79     @Test
80     public void configGENERICRESOURCEAPIservicesGet() throws Exception {
81         // Clean up data
82         configServicesRepository.deleteAll();
83
84         // Test with data
85         loadServicesData("src/test/resources/service1.json");
86         assert(configServicesRepository.count() > 0);
87
88         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
89                 .andReturn();
90         assertEquals(200, mvcResult.getResponse().getStatus());
91
92         // Test with no data
93         configServicesRepository.deleteAll();
94         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
95                 .andReturn();
96         assertEquals(404, mvcResult.getResponse().getStatus());
97     }
98
99     @Test
100     public void configGENERICRESOURCEAPIservicesPost() throws Exception {
101         // Clean up data
102         configServicesRepository.deleteAll();
103
104         String content = readFileContent("src/test/resources/service1.json");
105
106         // Test with no data
107         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
108                 .andReturn();
109         assertEquals(201, mvcResult.getResponse().getStatus());
110         assertEquals(1, configServicesRepository.count());
111
112         // Test with existing data - should return 409
113         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
114                 .andReturn();
115         assertEquals(409, mvcResult.getResponse().getStatus());
116         assertEquals(1, configServicesRepository.count());
117
118         // Clean up data
119         configServicesRepository.deleteAll();
120
121     }
122
123     @Test
124     public void configGENERICRESOURCEAPIservicesPut() throws Exception {
125         // Clean up data
126         configServicesRepository.deleteAll();
127
128         String content = readFileContent("src/test/resources/service1.json");
129
130         // Test with no data
131         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
132                 .andReturn();
133         assertEquals(201, mvcResult.getResponse().getStatus());
134         assertEquals(1, configServicesRepository.count());
135
136         // Test with existing data - should return 409
137         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
138                 .andReturn();
139         assertEquals(204, mvcResult.getResponse().getStatus());
140         assertEquals(1, configServicesRepository.count());
141
142         // Clean up data
143         configServicesRepository.deleteAll();
144
145     }
146
147     @Test
148     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
149         // Clean up data
150         configServicesRepository.deleteAll();
151
152         // Test with no data
153         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
154                 .andReturn();
155         assertEquals(204, mvcResult.getResponse().getStatus());
156         assertEquals(0, configServicesRepository.count());
157
158         // Load data
159         loadServicesData("src/test/resources/service1.json");
160         assertEquals(1, configServicesRepository.count());
161
162         // Test with data
163         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
164                 .andReturn();
165         assertEquals(204, mvcResult.getResponse().getStatus());
166         assertEquals(0, configServicesRepository.count());
167
168     }
169
170     @Test
171     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
172         // Clean up data
173         configServicesRepository.deleteAll();
174
175         // Test with data
176         loadServicesData("src/test/resources/service1.json");
177         assert(configServicesRepository.count() > 0);
178
179         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
180                 .andReturn();
181         assertEquals(200, mvcResult.getResponse().getStatus());
182
183         // Test with no data
184         configServicesRepository.deleteAll();
185         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
186                 .andReturn();
187         assertEquals(404, mvcResult.getResponse().getStatus());
188     }
189
190     @Test
191     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
192         // Clean up data
193         configServicesRepository.deleteAll();
194
195         String content = readFileContent("src/test/resources/service1-serviceitem.json");
196
197         // Test with no data
198         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
199                 .andReturn();
200         assertEquals(201, mvcResult.getResponse().getStatus());
201         assertEquals(1, configServicesRepository.count());
202
203         // Test with existing data - should return 409
204         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
205                 .andReturn();
206         assertEquals(409, mvcResult.getResponse().getStatus());
207         assertEquals(1, configServicesRepository.count());
208
209         // Clean up data
210         configServicesRepository.deleteAll();
211
212     }
213
214     @Test
215     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
216         // Clean up data
217         configServicesRepository.deleteAll();
218
219         String content = readFileContent("src/test/resources/service1-serviceitem.json");
220
221         // Test with no data
222         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
223                 .andReturn();
224         assertEquals(201, mvcResult.getResponse().getStatus());
225         assertEquals(1, configServicesRepository.count());
226
227         // Test with existing data - should return 409
228         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
229                 .andReturn();
230         assertEquals(204, mvcResult.getResponse().getStatus());
231         assertEquals(1, configServicesRepository.count());
232
233         // Clean up data
234         configServicesRepository.deleteAll();
235     }
236
237     @Test
238     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
239         // Clean up data
240         configServicesRepository.deleteAll();
241
242         // Test with no data
243         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
244                 .andReturn();
245         assertEquals(404, mvcResult.getResponse().getStatus());
246         assertEquals(0, configServicesRepository.count());
247
248         // Load data
249         loadServicesData("src/test/resources/service1.json");
250         assertEquals(1, configServicesRepository.count());
251
252         // Test with data
253         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
254                 .andReturn();
255         assertEquals(204, mvcResult.getResponse().getStatus());
256         assertEquals(1, configServicesRepository.count());
257         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
258         assertEquals(1, services.size());
259         assertEquals(null, services.get(0).getSvcData());
260
261
262     }
263
264     @Test
265     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
266         // Clean up data
267         configServicesRepository.deleteAll();
268
269         // Test with data
270         loadServicesData("src/test/resources/service1.json");
271         assert(configServicesRepository.count() > 0);
272
273         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
274                 .andReturn();
275         assertEquals(200, mvcResult.getResponse().getStatus());
276
277         // Test with no data
278         configServicesRepository.deleteAll();
279         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
280                 .andReturn();
281         assertEquals(404, mvcResult.getResponse().getStatus());
282     }
283
284     @Test
285     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
286         // Clean up data
287         configServicesRepository.deleteAll();
288
289         String content = readFileContent("src/test/resources/service1-servicedata.json");
290
291         // Test with no data
292         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
293                 .andReturn();
294         assertEquals(404, mvcResult.getResponse().getStatus());
295         assertEquals(0, configServicesRepository.count());
296
297         // Test with empty service data
298         ConfigServices service = new ConfigServices();
299         service.setSvcInstanceId("service1");
300         configServicesRepository.save(service);
301         assertEquals(1, configServicesRepository.count());
302         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
303                 .andReturn();
304         assertEquals(201, mvcResult.getResponse().getStatus());
305         assertEquals(1, configServicesRepository.count());
306         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
307         assertEquals(1, updatedService.size());
308         assertNotEquals(null, updatedService.get(0).getSvcData());
309
310         // Test with existing data - should return 409
311         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
312                 .andReturn();
313         assertEquals(409, mvcResult.getResponse().getStatus());
314
315         // Clean up data
316         configServicesRepository.deleteAll();
317     }
318
319     @Test
320     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
321         // Clean up data
322         configServicesRepository.deleteAll();
323
324         String content = readFileContent("src/test/resources/service1-servicedata.json");
325
326         // Test with no data
327         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
328                 .andReturn();
329         assertEquals(404, mvcResult.getResponse().getStatus());
330         assertEquals(0, configServicesRepository.count());
331
332         // Test with empty service data
333         ConfigServices service = new ConfigServices();
334         service.setSvcInstanceId("service1");
335         configServicesRepository.save(service);
336         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
337                 .andReturn();
338         assertEquals(201, mvcResult.getResponse().getStatus());
339         assertEquals(1, configServicesRepository.count());
340         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
341         assertEquals(1, updatedService.size());
342         assertNotEquals(null, updatedService.get(0).getSvcData());
343
344         // Test with existing data - should return 204
345         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
346                 .andReturn();
347         assertEquals(204, mvcResult.getResponse().getStatus());
348
349         // Clean up data
350         configServicesRepository.deleteAll();
351     }
352
353     @Test
354     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
355         // Clean up data
356         configServicesRepository.deleteAll();
357
358         // Test with no data
359         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
360                 .andReturn();
361         assertEquals(404, mvcResult.getResponse().getStatus());
362         assertEquals(0, configServicesRepository.count());
363
364         // Load data
365         loadServicesData("src/test/resources/service1.json");
366         assertEquals(1, configServicesRepository.count());
367
368         // Test with data
369         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
370                 .andReturn();
371         assertEquals(204, mvcResult.getResponse().getStatus());
372         assertEquals(1, configServicesRepository.count());
373         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
374         assertEquals(1, services.size());
375         assertEquals(null, services.get(0).getServiceStatus());
376     }
377
378     @Test
379     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
380         // Clean up data
381         configServicesRepository.deleteAll();
382
383         // Test with data
384         loadServicesData("src/test/resources/service1.json");
385         assert(configServicesRepository.count() > 0);
386
387         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
388                 .andReturn();
389         assertEquals(200, mvcResult.getResponse().getStatus());
390
391         // Test with no data
392         configServicesRepository.deleteAll();
393         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
394                 .andReturn();
395         assertEquals(404, mvcResult.getResponse().getStatus());
396     }
397
398     @Test
399     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
400         // Clean up data
401         configServicesRepository.deleteAll();
402
403         String content = readFileContent("src/test/resources/service1-servicestatus.json");
404
405         // Test with no data
406         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
407                 .andReturn();
408         assertEquals(404, mvcResult.getResponse().getStatus());
409         assertEquals(0, configServicesRepository.count());
410
411         // Test with empty service data
412         ConfigServices service = new ConfigServices();
413         service.setSvcInstanceId("service1");
414         configServicesRepository.save(service);
415         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
416                 .andReturn();
417         assertEquals(201, mvcResult.getResponse().getStatus());
418         assertEquals(1, configServicesRepository.count());
419         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
420         assertEquals(1, updatedService.size());
421         assertNotEquals(null, updatedService.get(0).getServiceStatus());
422
423         // Test with existing data - should return 409
424         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
425                 .andReturn();
426         assertEquals(409, mvcResult.getResponse().getStatus());
427
428         // Clean up data
429         configServicesRepository.deleteAll();
430     }
431
432     @Test
433     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
434         // Clean up data
435         configServicesRepository.deleteAll();
436
437         String content = readFileContent("src/test/resources/service1-servicestatus.json");
438
439         // Test with no data
440         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
441                 .andReturn();
442         assertEquals(404, mvcResult.getResponse().getStatus());
443         assertEquals(0, configServicesRepository.count());
444
445         // Test with empty service status
446         ConfigServices service = new ConfigServices();
447         service.setSvcInstanceId("service1");
448         configServicesRepository.save(service);
449         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
450                 .andReturn();
451         assertEquals(201, mvcResult.getResponse().getStatus());
452         assertEquals(1, configServicesRepository.count());
453         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
454         assertEquals(1, updatedService.size());
455         assertNotEquals(null, updatedService.get(0).getServiceStatus());
456
457         // Test with existing data - should return 204
458         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
459                 .andReturn();
460         assertEquals(204, mvcResult.getResponse().getStatus());
461
462         // Clean up data
463         configServicesRepository.deleteAll();
464     }
465
466     @Test
467     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataServiceTopologyGet() throws Exception {
468         // Clean up data
469         configServicesRepository.deleteAll();
470
471         // Test with data
472         loadServicesData("src/test/resources/service1.json");
473         assert(configServicesRepository.count() > 0);
474
475         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/service-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
476                 .andReturn();
477         assertEquals(200, mvcResult.getResponse().getStatus());
478
479         // Test with no data
480         configServicesRepository.deleteAll();
481         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/service-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
482                 .andReturn();
483         assertEquals(404, mvcResult.getResponse().getStatus());
484     }
485
486     @Test
487     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdDelete() throws Exception {
488         // Clean up data
489         configServicesRepository.deleteAll();
490
491         // Test with no data
492         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
493                                       .andReturn();
494         assertEquals(404, mvcResult.getResponse().getStatus());
495         assertEquals(0, configServicesRepository.count());
496
497         // Load data
498         loadVnfData("src/test/resources/vnf-data.json");
499         assertEquals(1, configServicesRepository.count());
500
501         // Test with data
502         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
503                             .andReturn();
504         assertEquals(204, mvcResult.getResponse().getStatus());
505         assertEquals(1, configServicesRepository.count());
506
507         configServicesRepository.deleteAll();
508         loadVnfData("src/test/resources/vnf-data.json");
509         assertEquals(1, configServicesRepository.count());
510         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c/").contentType(MediaType.APPLICATION_JSON).content(""))
511                             .andReturn();
512         assertEquals(404, mvcResult.getResponse().getStatus());
513
514         configServicesRepository.deleteAll();
515         createBadVnfData(true, true);
516         assertEquals(1, configServicesRepository.count());
517         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
518                             .andReturn();
519         assertEquals(404, mvcResult.getResponse().getStatus());
520
521         configServicesRepository.deleteAll();
522         createBadVnfData(false, false);
523         assertEquals(1, configServicesRepository.count());
524         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
525                             .andReturn();
526         assertEquals(404, mvcResult.getResponse().getStatus());
527
528         configServicesRepository.deleteAll();
529         createBadVnfData(false, true);
530         assertEquals(1, configServicesRepository.count());
531         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
532                             .andReturn();
533         assertEquals(404, mvcResult.getResponse().getStatus());
534     }
535
536     @Test
537     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdGet() throws Exception {
538         // Clean up data
539         configServicesRepository.deleteAll();
540
541         // Test with no data
542         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
543                                       .andReturn();
544         assertEquals(404, mvcResult.getResponse().getStatus());
545         assertEquals(0, configServicesRepository.count());
546
547         // Load data
548         loadVnfData("src/test/resources/vnf-data.json");
549         assertEquals(1, configServicesRepository.count());
550
551         // Test with data
552         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
553                             .andReturn();
554         assertEquals(200, mvcResult.getResponse().getStatus());
555
556         configServicesRepository.deleteAll();
557         createBadVnfData(false, false);
558         assertEquals(1, configServicesRepository.count());
559         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(""))
560                             .andReturn();
561         assertEquals(404, mvcResult.getResponse().getStatus());
562     }
563
564     @Test
565     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdPut() throws Exception {
566         // Clean up data
567         configServicesRepository.deleteAll();
568         assertEquals(0, configServicesRepository.count());
569         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-data.json")))
570                                       .andReturn();
571         assertEquals(201, mvcResult.getResponse().getStatus());
572         assertEquals(1, configServicesRepository.count());
573
574         mvcResult = mvc.perform(MockMvcRequestBuilders.put("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/").contentType(MediaType.APPLICATION_JSON).content(readFileContent("src/test/resources/vnf-data.json")))
575                             .andReturn();
576         assertEquals(204, mvcResult.getResponse().getStatus());
577         assertEquals(1, configServicesRepository.count());
578     }
579
580     @Test
581     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyGet() throws Exception {
582         // Clean up data
583         configServicesRepository.deleteAll();
584
585         // Test with no data
586         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
587                                       .andReturn();
588         assertEquals(404, mvcResult.getResponse().getStatus());
589         assertEquals(0, configServicesRepository.count());
590
591         // Load data
592         loadVnfData("src/test/resources/vnf-data.json");
593         assertEquals(1, configServicesRepository.count());
594
595         // Test with data
596         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
597                             .andReturn();
598         assertEquals(200, mvcResult.getResponse().getStatus());
599
600         configServicesRepository.deleteAll();
601         createBadVnfData(false, false);
602         assertEquals(1, configServicesRepository.count());
603         mvcResult = mvc.perform(MockMvcRequestBuilders.get("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c-4845-8919-434b2d999ada/vnf-data/vnf-topology/").contentType(MediaType.APPLICATION_JSON).content(""))
604                             .andReturn();
605         assertEquals(404, mvcResult.getResponse().getStatus());
606     }
607
608     @Test
609     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfLevelOperStatusPut() throws Exception {
610         // Clean up data
611         configServicesRepository.deleteAll();
612         assertEquals(0, configServicesRepository.count());
613         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")))
614                                       .andReturn();
615         assertEquals(201, mvcResult.getResponse().getStatus());
616         assertEquals(1, configServicesRepository.count());
617
618         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")))
619                             .andReturn();
620         assertEquals(204, mvcResult.getResponse().getStatus());
621         assertEquals(1, configServicesRepository.count());
622     }
623
624     @Test
625     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyOnapModelInformationPut() throws Exception {
626         // Clean up data
627         configServicesRepository.deleteAll();
628         assertEquals(0, configServicesRepository.count());
629         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")))
630                                       .andReturn();
631         assertEquals(201, mvcResult.getResponse().getStatus());
632         assertEquals(1, configServicesRepository.count());
633
634         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")))
635                             .andReturn();
636         assertEquals(204, mvcResult.getResponse().getStatus());
637         assertEquals(1, configServicesRepository.count());
638     }
639
640     @Test
641     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksPut() throws Exception {
642         // Clean up data
643         configServicesRepository.deleteAll();
644         assertEquals(0, configServicesRepository.count());
645         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")))
646                                       .andReturn();
647         assertEquals(201, mvcResult.getResponse().getStatus());
648         assertEquals(1, configServicesRepository.count());
649
650         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")))
651                             .andReturn();
652         assertEquals(204, mvcResult.getResponse().getStatus());
653         assertEquals(1, configServicesRepository.count());
654     }
655
656     @Test
657     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksVnfNetworkNetworkRolePut() throws Exception {
658         // Clean up data
659         configServicesRepository.deleteAll();
660         assertEquals(0, configServicesRepository.count());
661         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")))
662                                       .andReturn();
663         assertEquals(201, mvcResult.getResponse().getStatus());
664         assertEquals(1, configServicesRepository.count());
665
666         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")))
667                             .andReturn();
668         assertEquals(204, mvcResult.getResponse().getStatus());
669         assertEquals(1, configServicesRepository.count());
670     }
671
672     private String readFileContent(String path) throws IOException {
673         String content = new String(Files.readAllBytes(Paths.get(path)));
674         return content;
675     }
676
677     private void deleteData(String url) throws Exception {
678         mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
679                 .andReturn();
680     }
681
682     private void loadServicesData(String path) throws IOException {
683         ObjectMapper objectMapper = new ObjectMapper();
684         String content = readFileContent(path);
685         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
686
687         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
688             ConfigServices newService = new ConfigServices();
689             newService.setSvcInstanceId(service.getServiceInstanceId());
690             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
691             newService.setServiceStatus(service.getServiceStatus());
692             configServicesRepository.save(newService);
693         }
694     }
695
696     private void loadVnfData(String path) throws IOException {
697         ObjectMapper objectMapper = new ObjectMapper();
698         String content = readFileContent(path);
699         GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
700         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
701         svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
702         svcData.getVnfs().setVnf(new ArrayList<>());
703         svcData.getVnfs().addVnfItem(vnfData);
704         ConfigServices newService = new ConfigServices();
705         newService.setSvcData(objectMapper.writeValueAsString(svcData));
706         newService.setSvcInstanceId("test-siid");
707         configServicesRepository.save(newService);
708     }
709
710     private void createBadVnfData(boolean useNullSvc, boolean useNullVnfs) throws IOException {
711         ObjectMapper objectMapper = new ObjectMapper();
712         ConfigServices newService = new ConfigServices();
713         GenericResourceApiServicedataServiceData svcData = useNullSvc ? null : new GenericResourceApiServicedataServiceData();
714         GenericResourceApiServicedataServicedataVnfs vnfs = useNullVnfs ? null : new GenericResourceApiServicedataServicedataVnfs();
715
716         // Overrides useNullSvc
717         if(!useNullVnfs) {
718             svcData = new GenericResourceApiServicedataServiceData();
719             vnfs.setVnf(new ArrayList<>());
720             svcData.setVnfs(vnfs);
721         }
722
723         newService.setSvcInstanceId("test-siid");
724         newService.setSvcData(objectMapper.writeValueAsString(svcData));
725         configServicesRepository.save(newService);
726     }
727
728 }