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