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.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.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");
53 public void configGENERICRESOURCEAPIpreloadInformationDelete() throws Exception {
56 configPreloadDataRepository.deleteAll();
59 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
61 assertEquals(1, configPreloadDataRepository.count());
62 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
64 assertEquals(204, mvcResult.getResponse().getStatus());
65 assertEquals(0, configPreloadDataRepository.count());
68 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
70 assertEquals(204, mvcResult.getResponse().getStatus());
74 public void configGENERICRESOURCEAPIpreloadInformationGet() throws Exception {
76 configPreloadDataRepository.deleteAll();
79 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
80 assert(configPreloadDataRepository.count() > 0);
82 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
84 assertEquals(200, mvcResult.getResponse().getStatus());
87 configPreloadDataRepository.deleteAll();
88 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(""))
90 assertEquals(404, mvcResult.getResponse().getStatus());
95 public void configGENERICRESOURCEAPIpreloadInformationPost() throws Exception {
97 configPreloadDataRepository.deleteAll();
99 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
102 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
104 assertEquals(201, mvcResult.getResponse().getStatus());
105 assertEquals(1, configPreloadDataRepository.count());
107 // Test with existing data - should return 409
108 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
110 assertEquals(409, mvcResult.getResponse().getStatus());
111 assertEquals(1, configPreloadDataRepository.count());
114 configPreloadDataRepository.deleteAll();
119 public void configGENERICRESOURCEAPIpreloadInformationPut() throws Exception {
121 configPreloadDataRepository.deleteAll();
123 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
125 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content))
127 assertEquals(201, mvcResult.getResponse().getStatus());
128 assertEquals(1, configPreloadDataRepository.count());
132 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost() throws Exception {
134 configPreloadDataRepository.deleteAll();
136 String content = readFileContent("src/test/resources/preload1-net-model-info.json");
138 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL).contentType(MediaType.APPLICATION_JSON).content(content))
140 assertEquals(404, mvcResult.getResponse().getStatus());
141 assertEquals(0, configPreloadDataRepository.count());
146 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete() throws Exception {
148 configPreloadDataRepository.deleteAll();
151 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
152 assert(configPreloadDataRepository.count() > 0);
154 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
156 assertEquals(204, mvcResult.getResponse().getStatus());
157 assertEquals(0, configPreloadDataRepository.count());
160 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
162 assertEquals(204, mvcResult.getResponse().getStatus());
163 assertEquals(0, configPreloadDataRepository.count());
167 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet() throws Exception {
169 configPreloadDataRepository.deleteAll();
172 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
173 assert(configPreloadDataRepository.count() > 0);
175 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
177 assertEquals(200, mvcResult.getResponse().getStatus());
180 configPreloadDataRepository.deleteAll();
181 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(""))
183 assertEquals(404, mvcResult.getResponse().getStatus());
187 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostNoData() throws Exception {
189 configPreloadDataRepository.deleteAll();
191 String content = readFileContent("src/test/resources/preload1-net-list-item.json");
194 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(content))
196 assertEquals(201, mvcResult.getResponse().getStatus());
197 assertEquals(1, configPreloadDataRepository.count());
202 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostExistingData() throws Exception {
204 configPreloadDataRepository.deleteAll();
206 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
207 assert(configPreloadDataRepository.count() > 0);
209 String content = readFileContent("src/test/resources/preload1-net-list-item.json");
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))
214 assertEquals(409, mvcResult.getResponse().getStatus());
215 assertEquals(1, configPreloadDataRepository.count());
221 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePut() throws Exception {
223 configPreloadDataRepository.deleteAll();
225 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
226 String goodContent = readFileContent("src/test/resources/preload1-net-list-item.json");
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))
231 assertEquals(400, mvcResult.getResponse().getStatus());
232 assertEquals(0, configPreloadDataRepository.count());
235 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
237 assertEquals(201, mvcResult.getResponse().getStatus());
238 assertEquals(1, configPreloadDataRepository.count());
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))
243 assertEquals(204, mvcResult.getResponse().getStatus());
244 assertEquals(1, configPreloadDataRepository.count());
250 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete() throws Exception {
252 configPreloadDataRepository.deleteAll();
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(""))
259 assertEquals(204, mvcResult.getResponse().getStatus());
260 assertEquals(1, configPreloadDataRepository.count());
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(""))
267 assertEquals(404, mvcResult.getResponse().getStatus());
268 assertEquals(0, configPreloadDataRepository.count());
273 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet() throws Exception {
275 configPreloadDataRepository.deleteAll();
278 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
279 assert(configPreloadDataRepository.count() > 0);
281 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(""))
283 assertEquals(200, mvcResult.getResponse().getStatus());
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(""))
289 assertEquals(404, mvcResult.getResponse().getStatus());
293 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostNoData() throws Exception {
295 configPreloadDataRepository.deleteAll();
297 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
301 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
303 assertEquals(404, mvcResult.getResponse().getStatus());
304 assertEquals(0, configPreloadDataRepository.count());
310 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostBadContent() throws Exception {
312 configPreloadDataRepository.deleteAll();
314 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
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);
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))
326 assertEquals(400, mvcResult.getResponse().getStatus());
327 assertEquals(1, configPreloadDataRepository.count());
332 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostGoodData() throws Exception {
334 configPreloadDataRepository.deleteAll();
336 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
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);
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))
350 assertEquals(201, mvcResult.getResponse().getStatus());
351 assertEquals(1, configPreloadDataRepository.count());
355 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostExistingRecord() throws Exception {
357 configPreloadDataRepository.deleteAll();
359 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
363 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
364 assert(configPreloadDataRepository.count() > 0);
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))
370 assertEquals(409, mvcResult.getResponse().getStatus());
371 assertEquals(1, configPreloadDataRepository.count());
376 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutNoData() throws Exception {
378 configPreloadDataRepository.deleteAll();
380 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
384 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent))
386 assertEquals(404, mvcResult.getResponse().getStatus());
387 assertEquals(0, configPreloadDataRepository.count());
391 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutBadContent() throws Exception {
393 configPreloadDataRepository.deleteAll();
395 String badContent = readFileContent("src/test/resources/preload1-net-model-info.json");
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);
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))
407 assertEquals(400, mvcResult.getResponse().getStatus());
408 assertEquals(1, configPreloadDataRepository.count());
413 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutGoodData() throws Exception {
415 configPreloadDataRepository.deleteAll();
417 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
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);
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))
431 assertEquals(201, mvcResult.getResponse().getStatus());
432 assertEquals(1, configPreloadDataRepository.count());
436 public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutExistingRecord() throws Exception {
438 configPreloadDataRepository.deleteAll();
440 String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json");
444 loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json");
445 assert(configPreloadDataRepository.count() > 0);
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))
451 assertEquals(204, mvcResult.getResponse().getStatus());
452 assertEquals(1, configPreloadDataRepository.count());
455 private String readFileContent(String path) throws IOException {
456 String content = new String(Files.readAllBytes(Paths.get(path)));
460 private void deleteData(String url) throws Exception {
461 mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
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))