609e7804dfdaf0fd2a6485faf39a66862a4a3e11
[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
6 import java.io.IOException;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.util.List;
10
11 import com.fasterxml.jackson.databind.ObjectMapper;
12
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
17 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
18 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
19 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
22 import org.springframework.boot.test.context.SpringBootTest;
23 import org.springframework.http.MediaType;
24 import org.springframework.test.context.junit4.SpringRunner;
25 import org.springframework.test.web.servlet.MockMvc;
26 import org.springframework.test.web.servlet.MvcResult;
27 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
28 import org.springframework.transaction.annotation.Transactional;
29
30 @RunWith(SpringRunner.class)
31 @SpringBootTest(classes={GenericResourceMsApp.class})
32 @AutoConfigureMockMvc
33 @Transactional
34 public class ConfigApiServicesControllerTest {
35
36     private final static String CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/";
37     private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/service/";
38
39     @Autowired
40     private MockMvc mvc;
41
42     @Autowired
43     ConfigServicesRepository configServicesRepository;
44
45     @BeforeClass
46     public static void setUp() throws Exception {
47         System.out.println("ConfigApiServicesControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
48         System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
49         System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
50         System.setProperty("sdnc.config.dir", "src/test/resources");
51    
52     }
53
54
55     @Test
56     public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
57
58         // Clean up data
59         configServicesRepository.deleteAll();
60
61         // Load test data
62         loadServicesData( "src/test/resources/service1.json");
63
64         assertEquals(1, configServicesRepository.count());
65         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
66                 .andReturn();
67         assertEquals(204, mvcResult.getResponse().getStatus());
68         assertEquals(0, configServicesRepository.count());
69
70         // Test with no data
71         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
72                 .andReturn();
73         assertEquals(204, mvcResult.getResponse().getStatus());
74
75     }
76
77     @Test
78     public void configGENERICRESOURCEAPIservicesGet() throws Exception {
79         // Clean up data
80         configServicesRepository.deleteAll();
81
82         // Test with data
83         loadServicesData("src/test/resources/service1.json");
84         assert(configServicesRepository.count() > 0);
85
86         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
87                 .andReturn();
88         assertEquals(200, mvcResult.getResponse().getStatus());
89
90         // Test with no data
91         configServicesRepository.deleteAll();
92         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
93                 .andReturn();
94         assertEquals(404, mvcResult.getResponse().getStatus());
95     }
96
97     @Test
98     public void configGENERICRESOURCEAPIservicesPost() throws Exception {
99         // Clean up data
100         configServicesRepository.deleteAll();
101
102         String content = readFileContent("src/test/resources/service1.json");
103
104         // Test with no data
105         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
106                 .andReturn();
107         assertEquals(201, mvcResult.getResponse().getStatus());
108         assertEquals(1, configServicesRepository.count());
109
110         // Test with existing data - should return 409
111         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
112                 .andReturn();
113         assertEquals(409, mvcResult.getResponse().getStatus());
114         assertEquals(1, configServicesRepository.count());
115
116         // Clean up data
117         configServicesRepository.deleteAll();
118
119     }
120
121     @Test
122     public void configGENERICRESOURCEAPIservicesPut() throws Exception {
123         // Clean up data
124         configServicesRepository.deleteAll();
125
126         String content = readFileContent("src/test/resources/service1.json");
127
128         // Test with no data
129         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
130                 .andReturn();
131         assertEquals(201, mvcResult.getResponse().getStatus());
132         assertEquals(1, configServicesRepository.count());
133
134         // Test with existing data - should return 409
135         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
136                 .andReturn();
137         assertEquals(204, mvcResult.getResponse().getStatus());
138         assertEquals(1, configServicesRepository.count());
139
140         // Clean up data
141         configServicesRepository.deleteAll();
142
143     }
144
145     @Test
146     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
147         // Clean up data
148         configServicesRepository.deleteAll();
149
150         // Test with no data
151         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
152                 .andReturn();
153         assertEquals(204, mvcResult.getResponse().getStatus());
154         assertEquals(0, configServicesRepository.count());
155
156         // Load data
157         loadServicesData("src/test/resources/service1.json");
158         assertEquals(1, configServicesRepository.count());
159
160         // Test with data
161         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
162                 .andReturn();
163         assertEquals(204, mvcResult.getResponse().getStatus());
164         assertEquals(0, configServicesRepository.count());
165
166     }
167
168     @Test
169     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
170         // Clean up data
171         configServicesRepository.deleteAll();
172
173         // Test with data
174         loadServicesData("src/test/resources/service1.json");
175         assert(configServicesRepository.count() > 0);
176
177         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
178                 .andReturn();
179         assertEquals(200, mvcResult.getResponse().getStatus());
180
181         // Test with no data
182         configServicesRepository.deleteAll();
183         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
184                 .andReturn();
185         assertEquals(404, mvcResult.getResponse().getStatus());
186     }
187
188     @Test
189     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
190         // Clean up data
191         configServicesRepository.deleteAll();
192
193         String content = readFileContent("src/test/resources/service1-serviceitem.json");
194
195         // Test with no data
196         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
197                 .andReturn();
198         assertEquals(201, mvcResult.getResponse().getStatus());
199         assertEquals(1, configServicesRepository.count());
200
201         // Test with existing data - should return 409
202         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
203                 .andReturn();
204         assertEquals(409, mvcResult.getResponse().getStatus());
205         assertEquals(1, configServicesRepository.count());
206
207         // Clean up data
208         configServicesRepository.deleteAll();
209
210     }
211
212     @Test
213     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
214         // Clean up data
215         configServicesRepository.deleteAll();
216
217         String content = readFileContent("src/test/resources/service1-serviceitem.json");
218
219         // Test with no data
220         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
221                 .andReturn();
222         assertEquals(201, mvcResult.getResponse().getStatus());
223         assertEquals(1, configServicesRepository.count());
224
225         // Test with existing data - should return 409
226         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
227                 .andReturn();
228         assertEquals(204, mvcResult.getResponse().getStatus());
229         assertEquals(1, configServicesRepository.count());
230
231         // Clean up data
232         configServicesRepository.deleteAll();
233     }
234
235     @Test
236     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
237         // Clean up data
238         configServicesRepository.deleteAll();
239
240         // Test with no data
241         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
242                 .andReturn();
243         assertEquals(404, mvcResult.getResponse().getStatus());
244         assertEquals(0, configServicesRepository.count());
245
246         // Load data
247         loadServicesData("src/test/resources/service1.json");
248         assertEquals(1, configServicesRepository.count());
249
250         // Test with data
251         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
252                 .andReturn();
253         assertEquals(204, mvcResult.getResponse().getStatus());
254         assertEquals(1, configServicesRepository.count());
255         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
256         assertEquals(1, services.size());
257         assertEquals(null, services.get(0).getSvcData());
258
259
260     }
261
262     @Test
263     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
264         // Clean up data
265         configServicesRepository.deleteAll();
266
267         // Test with data
268         loadServicesData("src/test/resources/service1.json");
269         assert(configServicesRepository.count() > 0);
270
271         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
272                 .andReturn();
273         assertEquals(200, mvcResult.getResponse().getStatus());
274
275         // Test with no data
276         configServicesRepository.deleteAll();
277         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
278                 .andReturn();
279         assertEquals(404, mvcResult.getResponse().getStatus());
280     }
281
282     @Test
283     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
284         // Clean up data
285         configServicesRepository.deleteAll();
286
287         String content = readFileContent("src/test/resources/service1-servicedata.json");
288
289         // Test with no data
290         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
291                 .andReturn();
292         assertEquals(404, mvcResult.getResponse().getStatus());
293         assertEquals(0, configServicesRepository.count());
294
295         // Test with empty service data
296         ConfigServices service = new ConfigServices();
297         service.setSvcInstanceId("service1");
298         configServicesRepository.save(service);
299         assertEquals(1, configServicesRepository.count());
300         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
301                 .andReturn();
302         assertEquals(201, mvcResult.getResponse().getStatus());
303         assertEquals(1, configServicesRepository.count());
304         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
305         assertEquals(1, updatedService.size());
306         assertNotEquals(null, updatedService.get(0).getSvcData());
307
308         // Test with existing data - should return 409
309         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
310                 .andReturn();
311         assertEquals(409, mvcResult.getResponse().getStatus());
312
313         // Clean up data
314         configServicesRepository.deleteAll();
315     }
316
317     @Test
318     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
319         // Clean up data
320         configServicesRepository.deleteAll();
321
322         String content = readFileContent("src/test/resources/service1-servicedata.json");
323
324         // Test with no data
325         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
326                 .andReturn();
327         assertEquals(404, mvcResult.getResponse().getStatus());
328         assertEquals(0, configServicesRepository.count());
329
330         // Test with empty service data
331         ConfigServices service = new ConfigServices();
332         service.setSvcInstanceId("service1");
333         configServicesRepository.save(service);
334         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
335                 .andReturn();
336         assertEquals(201, mvcResult.getResponse().getStatus());
337         assertEquals(1, configServicesRepository.count());
338         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
339         assertEquals(1, updatedService.size());
340         assertNotEquals(null, updatedService.get(0).getSvcData());
341
342         // Test with existing data - should return 204
343         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
344                 .andReturn();
345         assertEquals(204, mvcResult.getResponse().getStatus());
346
347         // Clean up data
348         configServicesRepository.deleteAll();
349     }
350
351     @Test
352     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
353         // Clean up data
354         configServicesRepository.deleteAll();
355
356         // Test with no data
357         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
358                 .andReturn();
359         assertEquals(404, mvcResult.getResponse().getStatus());
360         assertEquals(0, configServicesRepository.count());
361
362         // Load data
363         loadServicesData("src/test/resources/service1.json");
364         assertEquals(1, configServicesRepository.count());
365
366         // Test with data
367         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
368                 .andReturn();
369         assertEquals(204, mvcResult.getResponse().getStatus());
370         assertEquals(1, configServicesRepository.count());
371         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
372         assertEquals(1, services.size());
373         assertEquals(null, services.get(0).getServiceStatus());
374     }
375
376     @Test
377     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
378         // Clean up data
379         configServicesRepository.deleteAll();
380
381         // Test with data
382         loadServicesData("src/test/resources/service1.json");
383         assert(configServicesRepository.count() > 0);
384
385         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
386                 .andReturn();
387         assertEquals(200, mvcResult.getResponse().getStatus());
388
389         // Test with no data
390         configServicesRepository.deleteAll();
391         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
392                 .andReturn();
393         assertEquals(404, mvcResult.getResponse().getStatus());
394     }
395
396     @Test
397     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
398         // Clean up data
399         configServicesRepository.deleteAll();
400
401         String content = readFileContent("src/test/resources/service1-servicestatus.json");
402
403         // Test with no data
404         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
405                 .andReturn();
406         assertEquals(404, mvcResult.getResponse().getStatus());
407         assertEquals(0, configServicesRepository.count());
408
409         // Test with empty service data
410         ConfigServices service = new ConfigServices();
411         service.setSvcInstanceId("service1");
412         configServicesRepository.save(service);
413         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
414                 .andReturn();
415         assertEquals(201, mvcResult.getResponse().getStatus());
416         assertEquals(1, configServicesRepository.count());
417         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
418         assertEquals(1, updatedService.size());
419         assertNotEquals(null, updatedService.get(0).getServiceStatus());
420
421         // Test with existing data - should return 409
422         mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
423                 .andReturn();
424         assertEquals(409, mvcResult.getResponse().getStatus());
425
426         // Clean up data
427         configServicesRepository.deleteAll();
428     }
429
430     @Test
431     public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
432         // Clean up data
433         configServicesRepository.deleteAll();
434
435         String content = readFileContent("src/test/resources/service1-servicestatus.json");
436
437         // Test with no data
438         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
439                 .andReturn();
440         assertEquals(404, mvcResult.getResponse().getStatus());
441         assertEquals(0, configServicesRepository.count());
442
443         // Test with empty service status
444         ConfigServices service = new ConfigServices();
445         service.setSvcInstanceId("service1");
446         configServicesRepository.save(service);
447         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
448                 .andReturn();
449         assertEquals(201, mvcResult.getResponse().getStatus());
450         assertEquals(1, configServicesRepository.count());
451         List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
452         assertEquals(1, updatedService.size());
453         assertNotEquals(null, updatedService.get(0).getServiceStatus());
454
455         // Test with existing data - should return 204
456         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
457                 .andReturn();
458         assertEquals(204, mvcResult.getResponse().getStatus());
459
460         // Clean up data
461         configServicesRepository.deleteAll();
462     }
463
464     private String readFileContent(String path) throws IOException {
465         String content = new String(Files.readAllBytes(Paths.get(path)));
466         return content;
467     }
468
469     private void deleteData(String url) throws Exception {
470         mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
471                 .andReturn();
472     }
473
474     private void loadServicesData(String path) throws IOException {
475         ObjectMapper objectMapper = new ObjectMapper();
476         String content = readFileContent(path);
477         GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
478
479         for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
480             ConfigServices newService = new ConfigServices();
481             newService.setSvcInstanceId(service.getServiceInstanceId());
482             newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
483             newService.setServiceStatus(service.getServiceStatus());
484             configServicesRepository.save(newService);
485         }
486     }
487
488 }