1 package org.onap.sdnc.apps.ms.gra.controllers;
3 import org.junit.BeforeClass;
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;
20 import java.io.IOException;
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
24 import static org.junit.Assert.assertEquals;
25 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
27 @RunWith(SpringRunner.class)
28 @SpringBootTest(classes={GenericResourceMsApp.class})
31 public class ConfigApiPreloadControllerTest {
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/";
41 ConfigPreloadDataRepository configPreloadDataRepository;
44 public static void setUp() throws Exception {
45 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
49 public void configGENERICRESOURCEAPIpreloadInformationDelete() throws Exception {
52 configPreloadDataRepository.deleteAll();
55 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
57 assertEquals(1, configPreloadDataRepository.count());
58 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
60 assertEquals(204, mvcResult.getResponse().getStatus());
61 assertEquals(0, configPreloadDataRepository.count());
64 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
66 assertEquals(204, mvcResult.getResponse().getStatus());
70 public void configGENERICRESOURCEAPIpreloadInformationGet() throws Exception {
72 configPreloadDataRepository.deleteAll();
75 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
76 assert(configPreloadDataRepository.count() > 0);
78 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
80 assertEquals(200, mvcResult.getResponse().getStatus());
83 configPreloadDataRepository.deleteAll();
84 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
86 assertEquals(404, mvcResult.getResponse().getStatus());
91 public void configGENERICRESOURCEAPIpreloadInformationPost() throws Exception {
93 configPreloadDataRepository.deleteAll();
95 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
98 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
100 assertEquals(201, mvcResult.getResponse().getStatus());
101 assertEquals(1, configPreloadDataRepository.count());
103 // Test with existing data - should return 409
104 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
106 assertEquals(409, mvcResult.getResponse().getStatus());
107 assertEquals(1, configPreloadDataRepository.count());
110 configPreloadDataRepository.deleteAll();
115 public void configGENERICRESOURCEAPIpreloadInformationPut() throws Exception {
117 configPreloadDataRepository.deleteAll();
119 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
121 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
123 assertEquals(201, mvcResult.getResponse().getStatus());
124 assertEquals(1, configPreloadDataRepository.count());
128 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost() throws Exception {
130 configPreloadDataRepository.deleteAll();
132 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
134 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
136 assertEquals(404, mvcResult.getResponse().getStatus());
137 assertEquals(0, configPreloadDataRepository.count());
142 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete() throws Exception {
144 configPreloadDataRepository.deleteAll();
147 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
148 assert(configPreloadDataRepository.count() > 0);
150 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
152 assertEquals(204, mvcResult.getResponse().getStatus());
153 assertEquals(0, configPreloadDataRepository.count());
156 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
158 assertEquals(204, mvcResult.getResponse().getStatus());
159 assertEquals(0, configPreloadDataRepository.count());
163 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet() throws Exception {
165 configPreloadDataRepository.deleteAll();
168 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
169 assert(configPreloadDataRepository.count() > 0);
171 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
173 assertEquals(200, mvcResult.getResponse().getStatus());
176 configPreloadDataRepository.deleteAll();
177 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
179 assertEquals(404, mvcResult.getResponse().getStatus());
183 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostNoData() throws Exception {
185 configPreloadDataRepository.deleteAll();
187 String content = readFileContent("src/test/resources/preload1-net-list-item.json");
190 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(content))
192 assertEquals(201, mvcResult.getResponse().getStatus());
193 assertEquals(1, configPreloadDataRepository.count());
198 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostExistingData() throws Exception {
200 configPreloadDataRepository.deleteAll();
202 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
203 assert(configPreloadDataRepository.count() > 0);
205 String content = readFileContent("src/test/resources/preload1-net-list-item.json");
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))
210 assertEquals(409, mvcResult.getResponse().getStatus());
211 assertEquals(1, configPreloadDataRepository.count());
217 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePut() throws Exception {
219 configPreloadDataRepository.deleteAll();
221 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
222 String goodContent = readFileContent("src/test/resources/preload1-net-list-item.json");
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))
227 assertEquals(400, mvcResult.getResponse().getStatus());
228 assertEquals(0, configPreloadDataRepository.count());
231 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
233 assertEquals(201, mvcResult.getResponse().getStatus());
234 assertEquals(1, configPreloadDataRepository.count());
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))
239 assertEquals(204, mvcResult.getResponse().getStatus());
240 assertEquals(1, configPreloadDataRepository.count());
246 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete() throws Exception {
248 configPreloadDataRepository.deleteAll();
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(""))
255 assertEquals(204, mvcResult.getResponse().getStatus());
256 assertEquals(1, configPreloadDataRepository.count());
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(""))
263 assertEquals(404, mvcResult.getResponse().getStatus());
264 assertEquals(0, configPreloadDataRepository.count());
269 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet() throws Exception {
271 configPreloadDataRepository.deleteAll();
274 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
275 assert(configPreloadDataRepository.count() > 0);
277 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(""))
279 assertEquals(200, mvcResult.getResponse().getStatus());
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(""))
285 assertEquals(404, mvcResult.getResponse().getStatus());
289 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostNoData() throws Exception {
291 configPreloadDataRepository.deleteAll();
293 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
297 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
299 assertEquals(404, mvcResult.getResponse().getStatus());
300 assertEquals(0, configPreloadDataRepository.count());
306 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostBadContent() throws Exception {
308 configPreloadDataRepository.deleteAll();
310 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
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);
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))
322 assertEquals(400, mvcResult.getResponse().getStatus());
323 assertEquals(1, configPreloadDataRepository.count());
328 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostGoodData() throws Exception {
330 configPreloadDataRepository.deleteAll();
332 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
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);
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))
346 assertEquals(201, mvcResult.getResponse().getStatus());
347 assertEquals(1, configPreloadDataRepository.count());
351 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostExistingRecord() throws Exception {
353 configPreloadDataRepository.deleteAll();
355 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
359 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
360 assert(configPreloadDataRepository.count() > 0);
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))
366 assertEquals(409, mvcResult.getResponse().getStatus());
367 assertEquals(1, configPreloadDataRepository.count());
372 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutNoData() throws Exception {
374 configPreloadDataRepository.deleteAll();
376 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
380 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
382 assertEquals(404, mvcResult.getResponse().getStatus());
383 assertEquals(0, configPreloadDataRepository.count());
387 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutBadContent() throws Exception {
389 configPreloadDataRepository.deleteAll();
391 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
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);
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))
403 assertEquals(400, mvcResult.getResponse().getStatus());
404 assertEquals(1, configPreloadDataRepository.count());
409 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutGoodData() throws Exception {
411 configPreloadDataRepository.deleteAll();
413 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
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);
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))
427 assertEquals(201, mvcResult.getResponse().getStatus());
428 assertEquals(1, configPreloadDataRepository.count());
432 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutExistingRecord() throws Exception {
434 configPreloadDataRepository.deleteAll();
436 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
440 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
441 assert(configPreloadDataRepository.count() > 0);
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))
447 assertEquals(204, mvcResult.getResponse().getStatus());
448 assertEquals(1, configPreloadDataRepository.count());
451 private String readFileContent(String path) throws IOException {
452 String content = new String(Files.readAllBytes(Paths.get(path)));
456 private void deleteData(String url) throws Exception {
457 mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
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))