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.core.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.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties");
55 public void configGENERICRESOURCEAPIservicesDelete() throws Exception {
58 configServicesRepository.deleteAll();
61 loadServicesData( "src/test/resources/service1.json");
63 assertEquals(1, configServicesRepository.count());
64 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
66 assertEquals(204, mvcResult.getResponse().getStatus());
67 assertEquals(0, configServicesRepository.count());
70 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
72 assertEquals(204, mvcResult.getResponse().getStatus());
77 public void configGENERICRESOURCEAPIservicesGet() throws Exception {
79 configServicesRepository.deleteAll();
82 loadServicesData("src/test/resources/service1.json");
83 assert(configServicesRepository.count() > 0);
85 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
87 assertEquals(200, mvcResult.getResponse().getStatus());
90 configServicesRepository.deleteAll();
91 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(""))
93 assertEquals(404, mvcResult.getResponse().getStatus());
97 public void configGENERICRESOURCEAPIservicesPost() throws Exception {
99 configServicesRepository.deleteAll();
101 String content = readFileContent("src/test/resources/service1.json");
104 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
106 assertEquals(201, mvcResult.getResponse().getStatus());
107 assertEquals(1, configServicesRepository.count());
109 // Test with existing data - should return 409
110 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
112 assertEquals(409, mvcResult.getResponse().getStatus());
113 assertEquals(1, configServicesRepository.count());
116 configServicesRepository.deleteAll();
121 public void configGENERICRESOURCEAPIservicesPut() throws Exception {
123 configServicesRepository.deleteAll();
125 String content = readFileContent("src/test/resources/service1.json");
128 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
130 assertEquals(201, mvcResult.getResponse().getStatus());
131 assertEquals(1, configServicesRepository.count());
133 // Test with existing data - should return 409
134 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content))
136 assertEquals(204, mvcResult.getResponse().getStatus());
137 assertEquals(1, configServicesRepository.count());
140 configServicesRepository.deleteAll();
145 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception {
147 configServicesRepository.deleteAll();
150 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
152 assertEquals(204, mvcResult.getResponse().getStatus());
153 assertEquals(0, configServicesRepository.count());
156 loadServicesData("src/test/resources/service1.json");
157 assertEquals(1, configServicesRepository.count());
160 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
162 assertEquals(204, mvcResult.getResponse().getStatus());
163 assertEquals(0, configServicesRepository.count());
168 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception {
170 configServicesRepository.deleteAll();
173 loadServicesData("src/test/resources/service1.json");
174 assert(configServicesRepository.count() > 0);
176 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
178 assertEquals(200, mvcResult.getResponse().getStatus());
181 configServicesRepository.deleteAll();
182 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(""))
184 assertEquals(404, mvcResult.getResponse().getStatus());
188 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception {
190 configServicesRepository.deleteAll();
192 String content = readFileContent("src/test/resources/service1-serviceitem.json");
195 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
197 assertEquals(201, mvcResult.getResponse().getStatus());
198 assertEquals(1, configServicesRepository.count());
200 // Test with existing data - should return 409
201 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
203 assertEquals(409, mvcResult.getResponse().getStatus());
204 assertEquals(1, configServicesRepository.count());
207 configServicesRepository.deleteAll();
212 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception {
214 configServicesRepository.deleteAll();
216 String content = readFileContent("src/test/resources/service1-serviceitem.json");
219 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
221 assertEquals(201, mvcResult.getResponse().getStatus());
222 assertEquals(1, configServicesRepository.count());
224 // Test with existing data - should return 409
225 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content))
227 assertEquals(204, mvcResult.getResponse().getStatus());
228 assertEquals(1, configServicesRepository.count());
231 configServicesRepository.deleteAll();
235 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception {
237 configServicesRepository.deleteAll();
240 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
242 assertEquals(404, mvcResult.getResponse().getStatus());
243 assertEquals(0, configServicesRepository.count());
246 loadServicesData("src/test/resources/service1.json");
247 assertEquals(1, configServicesRepository.count());
250 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
252 assertEquals(204, mvcResult.getResponse().getStatus());
253 assertEquals(1, configServicesRepository.count());
254 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
255 assertEquals(1, services.size());
256 assertEquals(null, services.get(0).getSvcData());
262 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception {
264 configServicesRepository.deleteAll();
267 loadServicesData("src/test/resources/service1.json");
268 assert(configServicesRepository.count() > 0);
270 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
272 assertEquals(200, mvcResult.getResponse().getStatus());
275 configServicesRepository.deleteAll();
276 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(""))
278 assertEquals(404, mvcResult.getResponse().getStatus());
282 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception {
284 configServicesRepository.deleteAll();
286 String content = readFileContent("src/test/resources/service1-servicedata.json");
289 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
291 assertEquals(404, mvcResult.getResponse().getStatus());
292 assertEquals(0, configServicesRepository.count());
294 // Test with empty service data
295 ConfigServices service = new ConfigServices();
296 service.setSvcInstanceId("service1");
297 configServicesRepository.save(service);
298 assertEquals(1, configServicesRepository.count());
299 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
301 assertEquals(201, mvcResult.getResponse().getStatus());
302 assertEquals(1, configServicesRepository.count());
303 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
304 assertEquals(1, updatedService.size());
305 assertNotEquals(null, updatedService.get(0).getSvcData());
307 // Test with existing data - should return 409
308 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
310 assertEquals(409, mvcResult.getResponse().getStatus());
313 configServicesRepository.deleteAll();
317 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception {
319 configServicesRepository.deleteAll();
321 String content = readFileContent("src/test/resources/service1-servicedata.json");
324 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
326 assertEquals(404, mvcResult.getResponse().getStatus());
327 assertEquals(0, configServicesRepository.count());
329 // Test with empty service data
330 ConfigServices service = new ConfigServices();
331 service.setSvcInstanceId("service1");
332 configServicesRepository.save(service);
333 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
335 assertEquals(201, mvcResult.getResponse().getStatus());
336 assertEquals(1, configServicesRepository.count());
337 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
338 assertEquals(1, updatedService.size());
339 assertNotEquals(null, updatedService.get(0).getSvcData());
341 // Test with existing data - should return 204
342 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content))
344 assertEquals(204, mvcResult.getResponse().getStatus());
347 configServicesRepository.deleteAll();
351 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception {
353 configServicesRepository.deleteAll();
356 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
358 assertEquals(404, mvcResult.getResponse().getStatus());
359 assertEquals(0, configServicesRepository.count());
362 loadServicesData("src/test/resources/service1.json");
363 assertEquals(1, configServicesRepository.count());
366 mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
368 assertEquals(204, mvcResult.getResponse().getStatus());
369 assertEquals(1, configServicesRepository.count());
370 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1");
371 assertEquals(1, services.size());
372 assertEquals(null, services.get(0).getServiceStatus());
376 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception {
378 configServicesRepository.deleteAll();
381 loadServicesData("src/test/resources/service1.json");
382 assert(configServicesRepository.count() > 0);
384 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
386 assertEquals(200, mvcResult.getResponse().getStatus());
389 configServicesRepository.deleteAll();
390 mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(""))
392 assertEquals(404, mvcResult.getResponse().getStatus());
396 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception {
398 configServicesRepository.deleteAll();
400 String content = readFileContent("src/test/resources/service1-servicestatus.json");
403 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
405 assertEquals(404, mvcResult.getResponse().getStatus());
406 assertEquals(0, configServicesRepository.count());
408 // Test with empty service data
409 ConfigServices service = new ConfigServices();
410 service.setSvcInstanceId("service1");
411 configServicesRepository.save(service);
412 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
414 assertEquals(201, mvcResult.getResponse().getStatus());
415 assertEquals(1, configServicesRepository.count());
416 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
417 assertEquals(1, updatedService.size());
418 assertNotEquals(null, updatedService.get(0).getServiceStatus());
420 // Test with existing data - should return 409
421 mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
423 assertEquals(409, mvcResult.getResponse().getStatus());
426 configServicesRepository.deleteAll();
430 public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception {
432 configServicesRepository.deleteAll();
434 String content = readFileContent("src/test/resources/service1-servicestatus.json");
437 MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
439 assertEquals(404, mvcResult.getResponse().getStatus());
440 assertEquals(0, configServicesRepository.count());
442 // Test with empty service status
443 ConfigServices service = new ConfigServices();
444 service.setSvcInstanceId("service1");
445 configServicesRepository.save(service);
446 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
448 assertEquals(201, mvcResult.getResponse().getStatus());
449 assertEquals(1, configServicesRepository.count());
450 List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1");
451 assertEquals(1, updatedService.size());
452 assertNotEquals(null, updatedService.get(0).getServiceStatus());
454 // Test with existing data - should return 204
455 mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content))
457 assertEquals(204, mvcResult.getResponse().getStatus());
460 configServicesRepository.deleteAll();
463 private String readFileContent(String path) throws IOException {
464 String content = new String(Files.readAllBytes(Paths.get(path)));
468 private void deleteData(String url) throws Exception {
469 mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content(""))
473 private void loadServicesData(String path) throws IOException {
474 ObjectMapper objectMapper = new ObjectMapper();
475 String content = readFileContent(path);
476 GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class);
478 for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) {
479 ConfigServices newService = new ConfigServices();
480 newService.setSvcInstanceId(service.getServiceInstanceId());
481 newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData()));
482 newService.setServiceStatus(service.getServiceStatus());
483 configServicesRepository.save(newService);