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