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