8c311fbbe0984dc4c20fb8ceba12ffab53703a0c
[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-list-item.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 goodContent = readFileContent("src/test/resources/preload1-net-list-item.json");
224
225
226         // Test with no data
227         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
228                 .andReturn();
229         assertEquals(201, mvcResult.getResponse().getStatus());
230         assertEquals(1, configPreloadDataRepository.count());
231
232         // Test with existing data - should return 204
233         mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
234                 .andReturn();
235         assertEquals(204, mvcResult.getResponse().getStatus());
236         assertEquals(1, configPreloadDataRepository.count());
237
238     }
239
240
241     @Test
242     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete() throws Exception {
243         // Clean up data
244         configPreloadDataRepository.deleteAll();
245
246         // Test with data
247         loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
248         assert(configPreloadDataRepository.count() > 0);
249         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(""))
250                 .andReturn();
251         assertEquals(204, mvcResult.getResponse().getStatus());
252         assertEquals(1, configPreloadDataRepository.count());
253
254         // Test without data
255         configPreloadDataRepository.deleteAll();
256         assertEquals(0, configPreloadDataRepository.count());
257         mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(""))
258                 .andReturn();
259         assertEquals(404, mvcResult.getResponse().getStatus());
260         assertEquals(0, configPreloadDataRepository.count());
261
262     }
263
264     @Test
265     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet() throws Exception {
266         // Clean up data
267         configPreloadDataRepository.deleteAll();
268
269         // Test with data
270         loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
271         assert(configPreloadDataRepository.count() > 0);
272
273         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(""))
274                 .andReturn();
275         assertEquals(200, mvcResult.getResponse().getStatus());
276
277         // Test with no data
278         configPreloadDataRepository.deleteAll();
279         mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data").contentType(MediaType.APPLICATION_JSON).content(""))
280                 .andReturn();
281         assertEquals(404, mvcResult.getResponse().getStatus());
282     }
283
284     @Test
285     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostNoData() throws Exception {
286         // Clean up data
287         configPreloadDataRepository.deleteAll();
288
289         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
290
291
292         // Test with no data
293         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
294                 .andReturn();
295         assertEquals(404, mvcResult.getResponse().getStatus());
296         assertEquals(0, configPreloadDataRepository.count());
297
298
299     }
300
301     @Test
302     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostBadContent() throws Exception {
303         // Clean up data
304         configPreloadDataRepository.deleteAll();
305
306         String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
307
308         // Load single entry with no preloadData
309         ConfigPreloadData preloadData = new ConfigPreloadData();
310         preloadData.setPreloadId("preload1");
311         preloadData.setPreloadType("network");
312         preloadData.setPreloadData(null);
313         configPreloadDataRepository.save(preloadData);
314
315
316     }
317
318     @Test
319     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostGoodData() throws Exception {
320         // Clean up data
321         configPreloadDataRepository.deleteAll();
322
323         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
324
325         // Load single entry with no preloadData
326         ConfigPreloadData preloadData = new ConfigPreloadData();
327         preloadData.setPreloadId("preload1");
328         preloadData.setPreloadType("network");
329         preloadData.setPreloadData(null);
330         configPreloadDataRepository.save(preloadData);
331
332
333         // Test with no existing preload data
334         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
335                 .andReturn();
336
337         assertEquals(201, mvcResult.getResponse().getStatus());
338         assertEquals(1, configPreloadDataRepository.count());
339     }
340
341     @Test
342     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostExistingRecord() throws Exception {
343         // Clean up data
344         configPreloadDataRepository.deleteAll();
345
346         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
347
348
349         // Test with data
350         loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
351         assert(configPreloadDataRepository.count() > 0);
352
353
354         // Test with existing preload dat
355         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
356                 .andReturn();
357         assertEquals(409, mvcResult.getResponse().getStatus());
358         assertEquals(1, configPreloadDataRepository.count());
359     }
360
361
362     @Test
363     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutNoData() throws Exception {
364         // Clean up data
365         configPreloadDataRepository.deleteAll();
366
367         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
368
369
370         // Test with no data
371         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
372                 .andReturn();
373         assertEquals(404, mvcResult.getResponse().getStatus());
374         assertEquals(0, configPreloadDataRepository.count());
375     }
376
377     @Test
378     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutBadContent() throws Exception {
379         // Clean up data
380         configPreloadDataRepository.deleteAll();
381
382         String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
383
384         // Load single entry with no preloadData
385         ConfigPreloadData preloadData = new ConfigPreloadData();
386         preloadData.setPreloadId("preload1");
387         preloadData.setPreloadType("network");
388         preloadData.setPreloadData(null);
389         configPreloadDataRepository.save(preloadData);
390
391     }
392
393     @Test
394     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutGoodData() throws Exception {
395         // Clean up data
396         configPreloadDataRepository.deleteAll();
397
398         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
399
400         // Load single entry with no preloadData
401         ConfigPreloadData preloadData = new ConfigPreloadData();
402         preloadData.setPreloadId("preload1");
403         preloadData.setPreloadType("network");
404         preloadData.setPreloadData(null);
405         configPreloadDataRepository.save(preloadData);
406
407
408         // Test with no existing preload data
409         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
410                 .andReturn();
411
412         assertEquals(201, mvcResult.getResponse().getStatus());
413         assertEquals(1, configPreloadDataRepository.count());
414     }
415
416     @Test
417     public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutExistingRecord() throws Exception {
418         // Clean up data
419         configPreloadDataRepository.deleteAll();
420
421         String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
422
423
424         // Test with data
425         loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
426         assert(configPreloadDataRepository.count() > 0);
427
428
429         // Test with existing preload dat
430         MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
431                 .andReturn();
432         assertEquals(204, mvcResult.getResponse().getStatus());
433         assertEquals(1, configPreloadDataRepository.count());
434     }
435
436     private String readFileContent(String path) throws IOException {
437         String content = new String(Files.readAllBytes(Paths.get(path)));
438         return content;
439     }
440
441     private void deleteData(String url) throws Exception {
442         mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
443                 .andReturn();
444     }
445
446     private void loadData(String url, String path) throws Exception {
447         String content = readFileContent(path);
448         mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(content))
449                 .andReturn();
450
451
452     }
453 }