7bcfefea9b22355e0e885992cae9bccca8eaba80
[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 configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdDelete() throws Exception {
468         // Clean up data
469         configServicesRepository.deleteAll();
470
471         // Test with no data
472         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(""))
473                                       .andReturn();
474         assertEquals(404, mvcResult.getResponse().getStatus());
475         assertEquals(0, configServicesRepository.count());
476
477         // Load data
478         loadVnfData("src/test/resources/vnf-data.json");
479         assertEquals(1, configServicesRepository.count());
480
481         // Test with data
482         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(""))
483                             .andReturn();
484         assertEquals(204, mvcResult.getResponse().getStatus());
485         assertEquals(1, configServicesRepository.count());
486
487         configServicesRepository.deleteAll();
488         loadVnfData("src/test/resources/vnf-data.json");
489         assertEquals(1, configServicesRepository.count());
490         mvcResult = mvc.perform(MockMvcRequestBuilders.delete("/config/GENERIC-RESOURCE-API:services/service/test-siid/service-data/vnfs/vnf/2a3bfc93-cd4c/").contentType(MediaType.APPLICATION_JSON).content(""))
491                             .andReturn();
492         assertEquals(404, mvcResult.getResponse().getStatus());
493
494         configServicesRepository.deleteAll();
495         createBadVnfData(true, true);
496         assertEquals(1, configServicesRepository.count());
497         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(""))
498                             .andReturn();
499         assertEquals(404, mvcResult.getResponse().getStatus());
500
501         configServicesRepository.deleteAll();
502         createBadVnfData(false, false);
503         assertEquals(1, configServicesRepository.count());
504         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(""))
505                             .andReturn();
506         assertEquals(404, mvcResult.getResponse().getStatus());
507
508         configServicesRepository.deleteAll();
509         createBadVnfData(false, true);
510         assertEquals(1, configServicesRepository.count());
511         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(""))
512                             .andReturn();
513         assertEquals(404, mvcResult.getResponse().getStatus());
514     }
515
516     @Test
517     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdGet() throws Exception {
518         // Clean up data
519         configServicesRepository.deleteAll();
520
521         // Test with no data
522         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(""))
523                                       .andReturn();
524         assertEquals(404, mvcResult.getResponse().getStatus());
525         assertEquals(0, configServicesRepository.count());
526
527         // Load data
528         loadVnfData("src/test/resources/vnf-data.json");
529         assertEquals(1, configServicesRepository.count());
530
531         // Test with data
532         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(""))
533                             .andReturn();
534         assertEquals(200, mvcResult.getResponse().getStatus());
535
536         configServicesRepository.deleteAll();
537         createBadVnfData(false, false);
538         assertEquals(1, configServicesRepository.count());
539         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(""))
540                             .andReturn();
541         assertEquals(404, mvcResult.getResponse().getStatus());
542     }
543
544     @Test
545     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdPut() throws Exception {
546         // Clean up data
547         configServicesRepository.deleteAll();
548         assertEquals(0, configServicesRepository.count());
549         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")))
550                                       .andReturn();
551         assertEquals(201, mvcResult.getResponse().getStatus());
552         assertEquals(1, configServicesRepository.count());
553
554         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")))
555                             .andReturn();
556         assertEquals(204, mvcResult.getResponse().getStatus());
557         assertEquals(1, configServicesRepository.count());
558     }
559
560     @Test
561     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyGet() throws Exception {
562         // Clean up data
563         configServicesRepository.deleteAll();
564
565         // Test with no data
566         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(""))
567                                       .andReturn();
568         assertEquals(404, mvcResult.getResponse().getStatus());
569         assertEquals(0, configServicesRepository.count());
570
571         // Load data
572         loadVnfData("src/test/resources/vnf-data.json");
573         assertEquals(1, configServicesRepository.count());
574
575         // Test with data
576         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(""))
577                             .andReturn();
578         assertEquals(200, mvcResult.getResponse().getStatus());
579
580         configServicesRepository.deleteAll();
581         createBadVnfData(false, false);
582         assertEquals(1, configServicesRepository.count());
583         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(""))
584                             .andReturn();
585         assertEquals(404, mvcResult.getResponse().getStatus());
586     }
587
588     @Test
589     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfLevelOperStatusPut() throws Exception {
590         // Clean up data
591         configServicesRepository.deleteAll();
592         assertEquals(0, configServicesRepository.count());
593         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")))
594                                       .andReturn();
595         assertEquals(201, mvcResult.getResponse().getStatus());
596         assertEquals(1, configServicesRepository.count());
597
598         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")))
599                             .andReturn();
600         assertEquals(204, mvcResult.getResponse().getStatus());
601         assertEquals(1, configServicesRepository.count());
602     }
603
604     @Test
605     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyOnapModelInformationPut() throws Exception {
606         // Clean up data
607         configServicesRepository.deleteAll();
608         assertEquals(0, configServicesRepository.count());
609         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")))
610                                       .andReturn();
611         assertEquals(201, mvcResult.getResponse().getStatus());
612         assertEquals(1, configServicesRepository.count());
613
614         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")))
615                             .andReturn();
616         assertEquals(204, mvcResult.getResponse().getStatus());
617         assertEquals(1, configServicesRepository.count());
618     }
619
620     @Test
621     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksPut() throws Exception {
622         // Clean up data
623         configServicesRepository.deleteAll();
624         assertEquals(0, configServicesRepository.count());
625         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")))
626                                       .andReturn();
627         assertEquals(201, mvcResult.getResponse().getStatus());
628         assertEquals(1, configServicesRepository.count());
629
630         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")))
631                             .andReturn();
632         assertEquals(204, mvcResult.getResponse().getStatus());
633         assertEquals(1, configServicesRepository.count());
634     }
635
636     @Test
637     public void configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksVnfNetworkNetworkRolePut() throws Exception {
638         // Clean up data
639         configServicesRepository.deleteAll();
640         assertEquals(0, configServicesRepository.count());
641         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")))
642                                       .andReturn();
643         assertEquals(201, mvcResult.getResponse().getStatus());
644         assertEquals(1, configServicesRepository.count());
645
646         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")))
647                             .andReturn();
648         assertEquals(204, mvcResult.getResponse().getStatus());
649         assertEquals(1, configServicesRepository.count());
650     }
651
652     private String readFileContent(String path) throws IOException {
653         String content = new String(Files.readAllBytes(Paths.get(path)));
654         return content;
655     }
656
657     private void deleteData(String url) throws Exception {
658         mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
659                 .andReturn();
660     }
661
662     private void loadServicesData(String path) throws IOException {
663         ObjectMapper objectMapper = new ObjectMapper();
664         String content = readFileContent(path);
665         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
666
667         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
668             ConfigServices newService = new ConfigServices();
669             newService.setSvcInstanceId(service.getServiceInstanceId());
670             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
671             newService.setServiceStatus(service.getServiceStatus());
672             configServicesRepository.save(newService);
673         }
674     }
675
676     private void loadVnfData(String path) throws IOException {
677         ObjectMapper objectMapper = new ObjectMapper();
678         String content = readFileContent(path);
679         GenericResourceApiServicedataServiceData svcData = new GenericResourceApiServicedataServiceData();
680         GenericResourceApiServicedataServicedataVnfsVnf vnfData = objectMapper.readValue(content, GenericResourceApiServicedataServicedataVnfsVnf.class);
681         svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
682         svcData.getVnfs().setVnf(new ArrayList<>());
683         svcData.getVnfs().addVnfItem(vnfData);
684         ConfigServices newService = new ConfigServices();
685         newService.setSvcData(objectMapper.writeValueAsString(svcData));
686         newService.setSvcInstanceId("test-siid");
687         configServicesRepository.save(newService);
688     }
689
690     private void createBadVnfData(boolean useNullSvc, boolean useNullVnfs) throws IOException {
691         ObjectMapper objectMapper = new ObjectMapper();
692         ConfigServices newService = new ConfigServices();
693         GenericResourceApiServicedataServiceData svcData = useNullSvc ? null : new GenericResourceApiServicedataServiceData();
694         GenericResourceApiServicedataServicedataVnfs vnfs = useNullVnfs ? null : new GenericResourceApiServicedataServicedataVnfs();
695
696         // Overrides useNullSvc
697         if(!useNullVnfs) {
698             svcData = new GenericResourceApiServicedataServiceData();
699             vnfs.setVnf(new ArrayList<>());
700             svcData.setVnfs(vnfs);
701         }
702
703         newService.setSvcInstanceId("test-siid");
704         newService.setSvcData(objectMapper.writeValueAsString(svcData));
705         configServicesRepository.save(newService);
706     }
707
708 }