1 package org.onap.sdnc.apps.ms.gra.controllers;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotEquals;
6 import java.io.IOException;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
11 import com.fasterxml.jackson.databind.ObjectMapper;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.onap.sdnc.apps.ms.gra.GenericResourceMsApp;
17 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
18 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
19 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
22 import org.springframework.boot.test.context.SpringBootTest;
23 import org.springframework.http.MediaType;
24 import org.springframework.test.context.junit4.SpringRunner;
25 import org.springframework.test.web.servlet.MockMvc;
26 import org.springframework.test.web.servlet.MvcResult;
27 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
28 import org.springframework.transaction.annotation.Transactional;
30 @RunWith(SpringRunner.class)
31 @SpringBootTest(classes={GenericResourceMsApp.class})
34 public class ConfigApiServicesControllerTest {
36 private final static String CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/";
37 private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/service/";
43 ConfigServicesRepository configServicesRepository;
46 public static void setUp() throws Exception {
47 System.out.println("ConfigApiServicesControllerTest: Setting serviceLogicProperties, serviceLogicDirectory and sdnc.config.dir");
48 System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
49 System.setProperty("serviceLogicDirectory", "src/test/resources/svclogic");
50 System.setProperty("sdnc.config.dir", "src/test/resources");
56 public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
59 configServicesRepository.deleteAll();
62 loadServicesData( "src/test/resources/service1.json");
64 assertEquals(1, configServicesRepository.count());
65 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
67 assertEquals(204, mvcResult.getResponse().getStatus());
68 assertEquals(0, configServicesRepository.count());
71 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
73 assertEquals(204, mvcResult.getResponse().getStatus());
78 public void configGENERICRESOURCEAPIservicesGet() throws Exception {
80 configServicesRepository.deleteAll();
83 loadServicesData("src/test/resources/service1.json");
84 assert(configServicesRepository.count() > 0);
86 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
88 assertEquals(200, mvcResult.getResponse().getStatus());
91 configServicesRepository.deleteAll();
92 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
94 assertEquals(404, mvcResult.getResponse().getStatus());
98 public void configGENERICRESOURCEAPIservicesPost() throws Exception {
100 configServicesRepository.deleteAll();
102 String content = readFileContent("src/test/resources/service1.json");
105 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
107 assertEquals(201, mvcResult.getResponse().getStatus());
108 assertEquals(1, configServicesRepository.count());
110 // Test with existing data - should return 409
111 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
113 assertEquals(409, mvcResult.getResponse().getStatus());
114 assertEquals(1, configServicesRepository.count());
117 configServicesRepository.deleteAll();
122 public void configGENERICRESOURCEAPIservicesPut() throws Exception {
124 configServicesRepository.deleteAll();
126 String content = readFileContent("src/test/resources/service1.json");
129 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
131 assertEquals(201, mvcResult.getResponse().getStatus());
132 assertEquals(1, configServicesRepository.count());
134 // Test with existing data - should return 409
135 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
137 assertEquals(204, mvcResult.getResponse().getStatus());
138 assertEquals(1, configServicesRepository.count());
141 configServicesRepository.deleteAll();
146 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
148 configServicesRepository.deleteAll();
151 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
153 assertEquals(204, mvcResult.getResponse().getStatus());
154 assertEquals(0, configServicesRepository.count());
157 loadServicesData("src/test/resources/service1.json");
158 assertEquals(1, configServicesRepository.count());
161 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
163 assertEquals(204, mvcResult.getResponse().getStatus());
164 assertEquals(0, configServicesRepository.count());
169 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
171 configServicesRepository.deleteAll();
174 loadServicesData("src/test/resources/service1.json");
175 assert(configServicesRepository.count() > 0);
177 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
179 assertEquals(200, mvcResult.getResponse().getStatus());
182 configServicesRepository.deleteAll();
183 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
185 assertEquals(404, mvcResult.getResponse().getStatus());
189 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
191 configServicesRepository.deleteAll();
193 String content = readFileContent("src/test/resources/service1-serviceitem.json");
196 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
198 assertEquals(201, mvcResult.getResponse().getStatus());
199 assertEquals(1, configServicesRepository.count());
201 // Test with existing data - should return 409
202 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
204 assertEquals(409, mvcResult.getResponse().getStatus());
205 assertEquals(1, configServicesRepository.count());
208 configServicesRepository.deleteAll();
213 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
215 configServicesRepository.deleteAll();
217 String content = readFileContent("src/test/resources/service1-serviceitem.json");
220 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
222 assertEquals(201, mvcResult.getResponse().getStatus());
223 assertEquals(1, configServicesRepository.count());
225 // Test with existing data - should return 409
226 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
228 assertEquals(204, mvcResult.getResponse().getStatus());
229 assertEquals(1, configServicesRepository.count());
232 configServicesRepository.deleteAll();
236 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
238 configServicesRepository.deleteAll();
241 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
243 assertEquals(404, mvcResult.getResponse().getStatus());
244 assertEquals(0, configServicesRepository.count());
247 loadServicesData("src/test/resources/service1.json");
248 assertEquals(1, configServicesRepository.count());
251 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
253 assertEquals(204, mvcResult.getResponse().getStatus());
254 assertEquals(1, configServicesRepository.count());
255 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
256 assertEquals(1, services.size());
257 assertEquals(null, services.get(0).getSvcData());
263 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
265 configServicesRepository.deleteAll();
268 loadServicesData("src/test/resources/service1.json");
269 assert(configServicesRepository.count() > 0);
271 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
273 assertEquals(200, mvcResult.getResponse().getStatus());
276 configServicesRepository.deleteAll();
277 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
279 assertEquals(404, mvcResult.getResponse().getStatus());
283 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
285 configServicesRepository.deleteAll();
287 String content = readFileContent("src/test/resources/service1-servicedata.json");
290 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
292 assertEquals(404, mvcResult.getResponse().getStatus());
293 assertEquals(0, configServicesRepository.count());
295 // Test with empty service data
296 ConfigServices service = new ConfigServices();
297 service.setSvcInstanceId("service1");
298 configServicesRepository.save(service);
299 assertEquals(1, configServicesRepository.count());
300 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
302 assertEquals(201, mvcResult.getResponse().getStatus());
303 assertEquals(1, configServicesRepository.count());
304 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
305 assertEquals(1, updatedService.size());
306 assertNotEquals(null, updatedService.get(0).getSvcData());
308 // Test with existing data - should return 409
309 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
311 assertEquals(409, mvcResult.getResponse().getStatus());
314 configServicesRepository.deleteAll();
318 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
320 configServicesRepository.deleteAll();
322 String content = readFileContent("src/test/resources/service1-servicedata.json");
325 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
327 assertEquals(404, mvcResult.getResponse().getStatus());
328 assertEquals(0, configServicesRepository.count());
330 // Test with empty service data
331 ConfigServices service = new ConfigServices();
332 service.setSvcInstanceId("service1");
333 configServicesRepository.save(service);
334 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
336 assertEquals(201, mvcResult.getResponse().getStatus());
337 assertEquals(1, configServicesRepository.count());
338 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
339 assertEquals(1, updatedService.size());
340 assertNotEquals(null, updatedService.get(0).getSvcData());
342 // Test with existing data - should return 204
343 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
345 assertEquals(204, mvcResult.getResponse().getStatus());
348 configServicesRepository.deleteAll();
352 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
354 configServicesRepository.deleteAll();
357 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
359 assertEquals(404, mvcResult.getResponse().getStatus());
360 assertEquals(0, configServicesRepository.count());
363 loadServicesData("src/test/resources/service1.json");
364 assertEquals(1, configServicesRepository.count());
367 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
369 assertEquals(204, mvcResult.getResponse().getStatus());
370 assertEquals(1, configServicesRepository.count());
371 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
372 assertEquals(1, services.size());
373 assertEquals(null, services.get(0).getServiceStatus());
377 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
379 configServicesRepository.deleteAll();
382 loadServicesData("src/test/resources/service1.json");
383 assert(configServicesRepository.count() > 0);
385 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
387 assertEquals(200, mvcResult.getResponse().getStatus());
390 configServicesRepository.deleteAll();
391 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
393 assertEquals(404, mvcResult.getResponse().getStatus());
397 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
399 configServicesRepository.deleteAll();
401 String content = readFileContent("src/test/resources/service1-servicestatus.json");
404 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
406 assertEquals(404, mvcResult.getResponse().getStatus());
407 assertEquals(0, configServicesRepository.count());
409 // Test with empty service data
410 ConfigServices service = new ConfigServices();
411 service.setSvcInstanceId("service1");
412 configServicesRepository.save(service);
413 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
415 assertEquals(201, mvcResult.getResponse().getStatus());
416 assertEquals(1, configServicesRepository.count());
417 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
418 assertEquals(1, updatedService.size());
419 assertNotEquals(null, updatedService.get(0).getServiceStatus());
421 // Test with existing data - should return 409
422 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
424 assertEquals(409, mvcResult.getResponse().getStatus());
427 configServicesRepository.deleteAll();
431 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
433 configServicesRepository.deleteAll();
435 String content = readFileContent("src/test/resources/service1-servicestatus.json");
438 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
440 assertEquals(404, mvcResult.getResponse().getStatus());
441 assertEquals(0, configServicesRepository.count());
443 // Test with empty service status
444 ConfigServices service = new ConfigServices();
445 service.setSvcInstanceId("service1");
446 configServicesRepository.save(service);
447 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
449 assertEquals(201, mvcResult.getResponse().getStatus());
450 assertEquals(1, configServicesRepository.count());
451 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
452 assertEquals(1, updatedService.size());
453 assertNotEquals(null, updatedService.get(0).getServiceStatus());
455 // Test with existing data - should return 204
456 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
458 assertEquals(204, mvcResult.getResponse().getStatus());
461 configServicesRepository.deleteAll();
464 private String readFileContent(String path) throws IOException {
465 String content = new String(Files.readAllBytes(Paths.get(path)));
469 private void deleteData(String url) throws Exception {
470 mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
474 private void loadServicesData(String path) throws IOException {
475 ObjectMapper objectMapper = new ObjectMapper();
476 String content = readFileContent(path);
477 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
479 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
480 ConfigServices newService = new ConfigServices();
481 newService.setSvcInstanceId(service.getServiceInstanceId());
482 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
483 newService.setServiceStatus(service.getServiceStatus());
484 configServicesRepository.save(newService);