1 package org.onap.sdnc.apps.ms.gra.controllers;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.junit.BeforeClass;
6 import org.junit.runner.RunWith;
7 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
8 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
9 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
10 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
11 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
12 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceModelInfrastructure;
13 import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicemodelinfrastructureService;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
16 import org.springframework.boot.test.context.SpringBootTest;
17 import org.springframework.http.MediaType;
18 import org.springframework.test.context.junit4.SpringRunner;
19 import org.springframework.test.web.servlet.MockMvc;
20 import org.springframework.test.web.servlet.MvcResult;
21 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
22 import org.springframework.transaction.annotation.Transactional;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.List;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotEquals;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
33 @RunWith(SpringRunner.class)
34 @SpringBootTest(classes={GenericResourceMsApp.class})
37 public class ConfigApiServicesControllerTest {
39 private final static String CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/";
40 private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/GENERIC-RESOURCE-API:service/";
46 ConfigServicesRepository configServicesRepository;
49 public static void setUp() throws Exception {
50 System.out.println("ConfigApiServicesControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
51 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
52 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
53 System.setProperty("sdnc.config.dir", "src/test/resources");
59 public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
62 configServicesRepository.deleteAll();
65 loadServicesData( "src/test/resources/service1.json");
67 assertEquals(1, configServicesRepository.count());
68 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
70 assertEquals(204, mvcResult.getResponse().getStatus());
71 assertEquals(0, configServicesRepository.count());
74 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
76 assertEquals(204, mvcResult.getResponse().getStatus());
81 public void configGENERICRESOURCEAPIservicesGet() throws Exception {
83 configServicesRepository.deleteAll();
86 loadServicesData("src/test/resources/service1.json");
87 assert(configServicesRepository.count() > 0);
89 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
91 assertEquals(200, mvcResult.getResponse().getStatus());
94 configServicesRepository.deleteAll();
95 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
97 assertEquals(404, mvcResult.getResponse().getStatus());
101 public void configGENERICRESOURCEAPIservicesPost() throws Exception {
103 configServicesRepository.deleteAll();
105 String content = readFileContent("src/test/resources/service1.json");
108 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
110 assertEquals(201, mvcResult.getResponse().getStatus());
111 assertEquals(1, configServicesRepository.count());
113 // Test with existing data - should return 409
114 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
116 assertEquals(409, mvcResult.getResponse().getStatus());
117 assertEquals(1, configServicesRepository.count());
120 configServicesRepository.deleteAll();
125 public void configGENERICRESOURCEAPIservicesPut() throws Exception {
127 configServicesRepository.deleteAll();
129 String content = readFileContent("src/test/resources/service1.json");
132 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
134 assertEquals(201, mvcResult.getResponse().getStatus());
135 assertEquals(1, configServicesRepository.count());
137 // Test with existing data - should return 409
138 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
140 assertEquals(204, mvcResult.getResponse().getStatus());
141 assertEquals(1, configServicesRepository.count());
144 configServicesRepository.deleteAll();
149 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
151 configServicesRepository.deleteAll();
154 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
156 assertEquals(204, mvcResult.getResponse().getStatus());
157 assertEquals(0, configServicesRepository.count());
160 loadServicesData("src/test/resources/service1.json");
161 assertEquals(1, configServicesRepository.count());
164 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
166 assertEquals(204, mvcResult.getResponse().getStatus());
167 assertEquals(0, configServicesRepository.count());
172 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
174 configServicesRepository.deleteAll();
177 loadServicesData("src/test/resources/service1.json");
178 assert(configServicesRepository.count() > 0);
180 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
182 assertEquals(200, mvcResult.getResponse().getStatus());
185 configServicesRepository.deleteAll();
186 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
188 assertEquals(404, mvcResult.getResponse().getStatus());
192 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
194 configServicesRepository.deleteAll();
196 String content = readFileContent("src/test/resources/service1-serviceitem.json");
199 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
201 assertEquals(201, mvcResult.getResponse().getStatus());
202 assertEquals(1, configServicesRepository.count());
204 // Test with existing data - should return 409
205 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
207 assertEquals(409, mvcResult.getResponse().getStatus());
208 assertEquals(1, configServicesRepository.count());
211 configServicesRepository.deleteAll();
216 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
218 configServicesRepository.deleteAll();
220 String content = readFileContent("src/test/resources/service1-serviceitem.json");
223 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
225 assertEquals(201, mvcResult.getResponse().getStatus());
226 assertEquals(1, configServicesRepository.count());
228 // Test with existing data - should return 409
229 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
231 assertEquals(204, mvcResult.getResponse().getStatus());
232 assertEquals(1, configServicesRepository.count());
235 configServicesRepository.deleteAll();
239 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
241 configServicesRepository.deleteAll();
244 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
246 assertEquals(404, mvcResult.getResponse().getStatus());
247 assertEquals(0, configServicesRepository.count());
250 loadServicesData("src/test/resources/service1.json");
251 assertEquals(1, configServicesRepository.count());
254 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
256 assertEquals(204, mvcResult.getResponse().getStatus());
257 assertEquals(1, configServicesRepository.count());
258 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
259 assertEquals(1, services.size());
260 assertEquals(null, services.get(0).getSvcData());
266 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
268 configServicesRepository.deleteAll();
271 loadServicesData("src/test/resources/service1.json");
272 assert(configServicesRepository.count() > 0);
274 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
276 assertEquals(200, mvcResult.getResponse().getStatus());
279 configServicesRepository.deleteAll();
280 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
282 assertEquals(404, mvcResult.getResponse().getStatus());
286 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
288 configServicesRepository.deleteAll();
290 String content = readFileContent("src/test/resources/service1-servicedata.json");
293 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
295 assertEquals(404, mvcResult.getResponse().getStatus());
296 assertEquals(0, configServicesRepository.count());
298 // Test with empty service data
299 ConfigServices service = new ConfigServices();
300 service.setSvcInstanceId("service1");
301 configServicesRepository.save(service);
302 assertEquals(1, configServicesRepository.count());
303 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
305 assertEquals(201, mvcResult.getResponse().getStatus());
306 assertEquals(1, configServicesRepository.count());
307 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
308 assertEquals(1, updatedService.size());
309 assertNotEquals(null, updatedService.get(0).getSvcData());
311 // Test with existing data - should return 409
312 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
314 assertEquals(409, mvcResult.getResponse().getStatus());
317 configServicesRepository.deleteAll();
321 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
323 configServicesRepository.deleteAll();
325 String content = readFileContent("src/test/resources/service1-servicedata.json");
328 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
330 assertEquals(404, mvcResult.getResponse().getStatus());
331 assertEquals(0, configServicesRepository.count());
333 // Test with empty service data
334 ConfigServices service = new ConfigServices();
335 service.setSvcInstanceId("service1");
336 configServicesRepository.save(service);
337 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
339 assertEquals(201, mvcResult.getResponse().getStatus());
340 assertEquals(1, configServicesRepository.count());
341 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
342 assertEquals(1, updatedService.size());
343 assertNotEquals(null, updatedService.get(0).getSvcData());
345 // Test with existing data - should return 204
346 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
348 assertEquals(204, mvcResult.getResponse().getStatus());
351 configServicesRepository.deleteAll();
355 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
357 configServicesRepository.deleteAll();
360 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
362 assertEquals(404, mvcResult.getResponse().getStatus());
363 assertEquals(0, configServicesRepository.count());
366 loadServicesData("src/test/resources/service1.json");
367 assertEquals(1, configServicesRepository.count());
370 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
372 assertEquals(204, mvcResult.getResponse().getStatus());
373 assertEquals(1, configServicesRepository.count());
374 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
375 assertEquals(1, services.size());
376 assertEquals(null, services.get(0).getServiceStatus());
380 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
382 configServicesRepository.deleteAll();
385 loadServicesData("src/test/resources/service1.json");
386 assert(configServicesRepository.count() > 0);
388 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
390 assertEquals(200, mvcResult.getResponse().getStatus());
393 configServicesRepository.deleteAll();
394 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
396 assertEquals(404, mvcResult.getResponse().getStatus());
400 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
402 configServicesRepository.deleteAll();
404 String content = readFileContent("src/test/resources/service1-servicestatus.json");
407 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
409 assertEquals(404, mvcResult.getResponse().getStatus());
410 assertEquals(0, configServicesRepository.count());
412 // Test with empty service data
413 ConfigServices service = new ConfigServices();
414 service.setSvcInstanceId("service1");
415 configServicesRepository.save(service);
416 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
418 assertEquals(201, mvcResult.getResponse().getStatus());
419 assertEquals(1, configServicesRepository.count());
420 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
421 assertEquals(1, updatedService.size());
422 assertNotEquals(null, updatedService.get(0).getServiceStatus());
424 // Test with existing data - should return 409
425 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
427 assertEquals(409, mvcResult.getResponse().getStatus());
430 configServicesRepository.deleteAll();
434 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
436 configServicesRepository.deleteAll();
438 String content = readFileContent("src/test/resources/service1-servicestatus.json");
441 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
443 assertEquals(404, mvcResult.getResponse().getStatus());
444 assertEquals(0, configServicesRepository.count());
446 // Test with empty service status
447 ConfigServices service = new ConfigServices();
448 service.setSvcInstanceId("service1");
449 configServicesRepository.save(service);
450 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
452 assertEquals(201, mvcResult.getResponse().getStatus());
453 assertEquals(1, configServicesRepository.count());
454 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
455 assertEquals(1, updatedService.size());
456 assertNotEquals(null, updatedService.get(0).getServiceStatus());
458 // Test with existing data - should return 204
459 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
461 assertEquals(204, mvcResult.getResponse().getStatus());
464 configServicesRepository.deleteAll();
467 private String readFileContent(String path) throws IOException {
468 String content = new String(Files.readAllBytes(Paths.get(path)));
472 private void deleteData(String url) throws Exception {
473 mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
477 private void loadServicesData(String path) throws IOException {
478 ObjectMapper objectMapper = new ObjectMapper();
479 String content = readFileContent(path);
480 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
482 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
483 ConfigServices newService = new ConfigServices();
484 newService.setSvcInstanceId(service.getServiceInstanceId());
485 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
486 newService.setServiceStatus(service.getServiceStatus());
487 configServicesRepository.save(newService);