diff options
Diffstat (limited to 'ms/generic-resource-api/src/test/java')
3 files changed, 997 insertions, 0 deletions
diff --git a/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiPreloadControllerTest.java b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiPreloadControllerTest.java new file mode 100644 index 0000000..7e7a3ac --- /dev/null +++ b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiPreloadControllerTest.java @@ -0,0 +1,468 @@ +package org.onap.sdnc.apps.ms.gra.controllers; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdnc.apps.ms.gra.core.GenericResourceMsApp; +import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData; +import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.transaction.annotation.Transactional; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes={GenericResourceMsApp.class}) +@AutoConfigureMockMvc +@Transactional +public class ConfigApiPreloadControllerTest { + + private final static String CONFIG_PRELOAD_URL = "/config/GENERIC-RESOURCE-API:preload-information/"; + private final static String CONFIG_PRELOAD_LIST_URL = "/config/GENERIC-RESOURCE-API:preload-information/GENERIC-RESOURCE-API:preload-list/"; + + + @Autowired + private MockMvc mvc; + + @Autowired + ConfigPreloadDataRepository configPreloadDataRepository; + + @BeforeClass + public static void setUp() throws Exception { + System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties"); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationDelete() throws Exception { + + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Load test data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + + assertEquals(1, configPreloadDataRepository.count()); + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + // Test with no data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGet() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configPreloadDataRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationPost() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String content = readFileContent("src/test/resources/preload1-net-model-info.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + // Clean up data + configPreloadDataRepository.deleteAll(); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationPut() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String content = readFileContent("src/test/resources/preload1-net-model-info.json"); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String content = readFileContent("src/test/resources/preload1-net-model-info.json"); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + // Test without data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configPreloadDataRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostNoData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String content = readFileContent("src/test/resources/preload1-net-list-item.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePostExistingData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + String content = readFileContent("src/test/resources/preload1-net-list-item.json"); + + // Test with existing data - should return 409 + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + } + + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePut() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String badContent = readFileContent("src/test/resources/preload1-net-model-info.json"); + String goodContent = readFileContent("src/test/resources/preload1-net-list-item.json"); + + // Test with bad file content + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(badContent)) + .andReturn(); + assertEquals(400, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + // Test with no data + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + // Test with existing data - should return 204 + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + } + + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + // Test without data + configPreloadDataRepository.deleteAll(); + assertEquals(0, configPreloadDataRepository.count()); + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configPreloadDataRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostNoData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostBadContent() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String badContent = readFileContent("src/test/resources/preload1-net-model-info.json"); + + // Load single entry with no preloadData + ConfigPreloadData preloadData = new ConfigPreloadData(); + preloadData.setPreloadId("preload1"); + preloadData.setPreloadType("network"); + preloadData.setPreloadData(null); + configPreloadDataRepository.save(preloadData); + + // Test with bad file content + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(badContent)) + .andReturn(); + assertEquals(400, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostGoodData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + // Load single entry with no preloadData + ConfigPreloadData preloadData = new ConfigPreloadData(); + preloadData.setPreloadId("preload1"); + preloadData.setPreloadType("network"); + preloadData.setPreloadData(null); + configPreloadDataRepository.save(preloadData); + + + // Test with no existing preload data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPostExistingRecord() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + + // Test with existing preload dat + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + } + + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutNoData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configPreloadDataRepository.count()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutBadContent() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String badContent = readFileContent("src/test/resources/preload1-net-model-info.json"); + + // Load single entry with no preloadData + ConfigPreloadData preloadData = new ConfigPreloadData(); + preloadData.setPreloadId("preload1"); + preloadData.setPreloadType("network"); + preloadData.setPreloadData(null); + configPreloadDataRepository.save(preloadData); + + // Test with bad file content + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(badContent)) + .andReturn(); + assertEquals(400, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutGoodData() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + // Load single entry with no preloadData + ConfigPreloadData preloadData = new ConfigPreloadData(); + preloadData.setPreloadId("preload1"); + preloadData.setPreloadType("network"); + preloadData.setPreloadData(null); + configPreloadDataRepository.save(preloadData); + + + // Test with no existing preload data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + } + + @Test + public void configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPutExistingRecord() throws Exception { + // Clean up data + configPreloadDataRepository.deleteAll(); + + String goodContent = readFileContent("src/test/resources/preload1-net-preload-data.json"); + + + // Test with data + loadData(CONFIG_PRELOAD_URL, "src/test/resources/preload1-net-model-info.json"); + assert(configPreloadDataRepository.count() > 0); + + + // Test with existing preload dat + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_PRELOAD_LIST_URL+"preload1/network/GENERIC-RESOURCE-API:preload-data/").contentType(MediaType.APPLICATION_JSON).content(goodContent)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configPreloadDataRepository.count()); + } + + private String readFileContent(String path) throws IOException { + String content = new String(Files.readAllBytes(Paths.get(path))); + return content; + } + + private void deleteData(String url) throws Exception { + mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + } + + private void loadData(String url, String path) throws Exception { + String content = readFileContent(path); + mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + + + } +}
\ No newline at end of file diff --git a/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiServicesControllerTest.java b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiServicesControllerTest.java new file mode 100644 index 0000000..5653b3b --- /dev/null +++ b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/ConfigApiServicesControllerTest.java @@ -0,0 +1,487 @@ +package org.onap.sdnc.apps.ms.gra.controllers; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.sdnc.apps.ms.gra.core.GenericResourceMsApp; +import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData; +import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository; +import org.onap.sdnc.apps.ms.gra.data.ConfigServices; +import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServiceModelInfrastructure; +import org.onap.sdnc.apps.ms.gra.swagger.model.GenericResourceApiServicemodelinfrastructureService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.transaction.annotation.Transactional; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes={GenericResourceMsApp.class}) +@AutoConfigureMockMvc +@Transactional +public class ConfigApiServicesControllerTest { + + private final static String CONFIG_SERVICES_URL = "/config/GENERIC-RESOURCE-API:services/"; + private final static String CONFIG_SERVICES_SERVICE_URL = "/config/GENERIC-RESOURCE-API:services/GENERIC-RESOURCE-API:service/"; + + @Autowired + private MockMvc mvc; + + @Autowired + ConfigServicesRepository configServicesRepository; + + @BeforeClass + public static void setUp() throws Exception { + System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties"); + } + + + @Test + public void configGENERICRESOURCEAPIservicesDelete() throws Exception { + + // Clean up data + configServicesRepository.deleteAll(); + + // Load test data + loadServicesData( "src/test/resources/service1.json"); + + assertEquals(1, configServicesRepository.count()); + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Test with no data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + + } + + @Test + public void configGENERICRESOURCEAPIservicesGet() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with data + loadServicesData("src/test/resources/service1.json"); + assert(configServicesRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configServicesRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIservicesPost() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Clean up data + configServicesRepository.deleteAll(); + + } + + @Test + public void configGENERICRESOURCEAPIservicesPut() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_URL).contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Clean up data + configServicesRepository.deleteAll(); + + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Load data + loadServicesData("src/test/resources/service1.json"); + assertEquals(1, configServicesRepository.count()); + + // Test with data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with data + loadServicesData("src/test/resources/service1.json"); + assert(configServicesRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configServicesRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-serviceitem.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Clean up data + configServicesRepository.deleteAll(); + + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-serviceitem.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + + // Clean up data + configServicesRepository.deleteAll(); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Load data + loadServicesData("src/test/resources/service1.json"); + assertEquals(1, configServicesRepository.count()); + + // Test with data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, services.size()); + assertEquals(null, services.get(0).getSvcData()); + + + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with data + loadServicesData("src/test/resources/service1.json"); + assert(configServicesRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configServicesRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-servicedata.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Test with empty service data + ConfigServices service = new ConfigServices(); + service.setSvcInstanceId("service1"); + configServicesRepository.save(service); + assertEquals(1, configServicesRepository.count()); + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, updatedService.size()); + assertNotEquals(null, updatedService.get(0).getSvcData()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + + // Clean up data + configServicesRepository.deleteAll(); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-servicedata.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Test with empty service data + ConfigServices service = new ConfigServices(); + service.setSvcInstanceId("service1"); + configServicesRepository.save(service); + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, updatedService.size()); + assertNotEquals(null, updatedService.get(0).getSvcData()); + + // Test with existing data - should return 204 + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-data/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + + // Clean up data + configServicesRepository.deleteAll(); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Load data + loadServicesData("src/test/resources/service1.json"); + assertEquals(1, configServicesRepository.count()); + + // Test with data + mvcResult = mvc.perform(MockMvcRequestBuilders.delete(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> services = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, services.size()); + assertEquals(null, services.get(0).getServiceStatus()); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + // Test with data + loadServicesData("src/test/resources/service1.json"); + assert(configServicesRepository.count() > 0); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(200, mvcResult.getResponse().getStatus()); + + // Test with no data + configServicesRepository.deleteAll(); + mvcResult = mvc.perform(MockMvcRequestBuilders.get(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-servicestatus.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Test with empty service data + ConfigServices service = new ConfigServices(); + service.setSvcInstanceId("service1"); + configServicesRepository.save(service); + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, updatedService.size()); + assertNotEquals(null, updatedService.get(0).getServiceStatus()); + + // Test with existing data - should return 409 + mvcResult = mvc.perform(MockMvcRequestBuilders.post(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(409, mvcResult.getResponse().getStatus()); + + // Clean up data + configServicesRepository.deleteAll(); + } + + @Test + public void configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut() throws Exception { + // Clean up data + configServicesRepository.deleteAll(); + + String content = readFileContent("src/test/resources/service1-servicestatus.json"); + + // Test with no data + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(404, mvcResult.getResponse().getStatus()); + assertEquals(0, configServicesRepository.count()); + + // Test with empty service status + ConfigServices service = new ConfigServices(); + service.setSvcInstanceId("service1"); + configServicesRepository.save(service); + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(201, mvcResult.getResponse().getStatus()); + assertEquals(1, configServicesRepository.count()); + List<ConfigServices> updatedService = configServicesRepository.findBySvcInstanceId("service1"); + assertEquals(1, updatedService.size()); + assertNotEquals(null, updatedService.get(0).getServiceStatus()); + + // Test with existing data - should return 204 + mvcResult = mvc.perform(MockMvcRequestBuilders.put(CONFIG_SERVICES_SERVICE_URL+"service1/GENERIC-RESOURCE-API:service-status/").contentType(MediaType.APPLICATION_JSON).content(content)) + .andReturn(); + assertEquals(204, mvcResult.getResponse().getStatus()); + + // Clean up data + configServicesRepository.deleteAll(); + } + + private String readFileContent(String path) throws IOException { + String content = new String(Files.readAllBytes(Paths.get(path))); + return content; + } + + private void deleteData(String url) throws Exception { + mvc.perform(MockMvcRequestBuilders.delete(url).contentType(MediaType.APPLICATION_JSON).content("")) + .andReturn(); + } + + private void loadServicesData(String path) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + String content = readFileContent(path); + GenericResourceApiServiceModelInfrastructure services = objectMapper.readValue(content, GenericResourceApiServiceModelInfrastructure.class); + + for (GenericResourceApiServicemodelinfrastructureService service : services.getService()) { + ConfigServices newService = new ConfigServices(); + newService.setSvcInstanceId(service.getServiceInstanceId()); + newService.setSvcData(objectMapper.writeValueAsString(service.getServiceData())); + newService.setServiceStatus(service.getServiceStatus()); + configServicesRepository.save(newService); + } + } + +}
\ No newline at end of file diff --git a/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/GenericResourceMsAppTest.java b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/GenericResourceMsAppTest.java new file mode 100644 index 0000000..0a19f39 --- /dev/null +++ b/ms/generic-resource-api/src/test/java/org/onap/sdnc/apps/ms/gra/controllers/GenericResourceMsAppTest.java @@ -0,0 +1,42 @@ +package org.onap.sdnc.apps.ms.gra.controllers; + +import org.apache.shiro.realm.Realm; +import org.apache.shiro.realm.text.PropertiesRealm; +import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.sdnc.apps.ms.gra.core.GenericResourceMsApp; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class GenericResourceMsAppTest { + + GenericResourceMsApp app; + + @Before + public void setUp() throws Exception { + app = new GenericResourceMsApp(); + System.setProperty("serviceLogicProperties", "src/test/resources/svclogic.properties"); + } + + @Test + public void realm() { + Realm realm = app.realm(); + assertTrue(realm instanceof PropertiesRealm); + + + } + + @Test + public void shiroFilterChainDefinition() { + ShiroFilterChainDefinition chainDefinition = app.shiroFilterChainDefinition(); + Map<String, String> chainMap = chainDefinition.getFilterChainMap(); + assertEquals("anon", chainMap.get("/**")); + + + } +}
\ No newline at end of file |