diff --git a/src/test/java/org/etsi/osl/services/api/ServiceCatalogIntegrationTest.java b/src/test/java/org/etsi/osl/services/api/ServiceCatalogIntegrationTest.java index 56f3a73432b6c1d228a38b5a7a607811bf887080..511e46c84b9ab994d590da4509d15973b09554c7 100644 --- a/src/test/java/org/etsi/osl/services/api/ServiceCatalogIntegrationTest.java +++ b/src/test/java/org/etsi/osl/services/api/ServiceCatalogIntegrationTest.java @@ -521,17 +521,31 @@ public class ServiceCatalogIntegrationTest { ServiceSpecCharacteristicValue val = new ServiceSpecCharacteristicValue(); val.setValueType( EValueType.ARRAY.toString()); val.setValue( new Any("1" ,"a second value") ); - responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharacteristicValue().add(val); - ServiceSpecCharRelationship scrObj = responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[0]; - ServiceSpecCharRelationship scrObj2 = responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[1]; - ServiceSpecCharRelationship scrObj3 = responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[2]; + + // Following code related to 'notNullRelationshipIndex' is to fix random test failures due to getServiceSpecCharacteristic + // not having 3 ServiceSpecCharRelationships + int notNullRelationshipIndex = 0; + for (int i=0; i<responsesSpecUpd.getServiceSpecCharacteristic().size(); i++){ + ServiceSpecCharRelationship scrObj1 = responsesSpecUpd.getServiceSpecCharacteristic().get(i).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[0]; + ServiceSpecCharRelationship scrObj2 = responsesSpecUpd.getServiceSpecCharacteristic().get(i).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[1]; + ServiceSpecCharRelationship scrObj3 = responsesSpecUpd.getServiceSpecCharacteristic().get(i).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[2]; + + if (scrObj1 != null && scrObj2 != null && scrObj3 != null){ + notNullRelationshipIndex = i; + break; + } + } + responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharacteristicValue().add(val); + ServiceSpecCharRelationship scrObj1 = responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[0]; + ServiceSpecCharRelationship scrObj2 = responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[1]; + ServiceSpecCharRelationship scrObj3 = responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().toArray( new ServiceSpecCharRelationship[3])[2]; scrObj3.setName("FORTESTING"); String preid = scrObj3.getId(); - responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().remove(scrObj); - responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().remove(scrObj2); + responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().remove(scrObj1); + responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().remove(scrObj2); ServiceSpecCharRelationship scrObj4 = new ServiceSpecCharRelationship(); scrObj4.setName("ANEWCharRel"); - responsesSpecUpd.getServiceSpecCharacteristic().get(0).getServiceSpecCharRelationship().add(scrObj4); + responsesSpecUpd.getServiceSpecCharacteristic().get(notNullRelationshipIndex).getServiceSpecCharRelationship().add(scrObj4); response2 = mvc.perform(MockMvcRequestBuilders.patch("/serviceCatalogManagement/v4/serviceSpecification/" + responsesSpec.getId() ) .with( SecurityMockMvcRequestPostProcessors.csrf()) diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ExportJobApiController633Test.java b/src/test/java/org/etsi/osl/services/api/scm633/ExportJobApiController633Test.java new file mode 100644 index 0000000000000000000000000000000000000000..14a8a40ff6d3965ec67e6c2221fdd5fac3c114a5 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ExportJobApiController633Test.java @@ -0,0 +1,130 @@ +package org.etsi.osl.services.api.scm633; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ExportJobApiController633Test { + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testCreateExportJob() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testExportJob.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String exportJobString = IOUtils.toString(in, "UTF-8"); + ExportJobCreate exportJobCreate = JsonUtils.toJsonObj(exportJobString, ExportJobCreate.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/exportJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( exportJobCreate ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/exportJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( exportJobCreate ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testDeleteExportJob() throws Exception { + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/exportJob/testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListExportJob() throws Exception { + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/exportJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/exportJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveExportJob() throws Exception { + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/exportJob?testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/exportJob/testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/scm633/HubApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/HubApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..048ad19c20ee3c49ee427bf373885f4daa118d15 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/HubApiControllerTest.java @@ -0,0 +1,92 @@ +package org.etsi.osl.services.api.scm633; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class HubApiControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRegisterListener() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testEventSubscriptionInput.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String eventSubscriptionInputString = IOUtils.toString(in, "UTF-8"); + EventSubscriptionInput eventSubscriptionInput = JsonUtils.toJsonObj(eventSubscriptionInputString, EventSubscriptionInput.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/hub") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( eventSubscriptionInput ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/hub") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( eventSubscriptionInput ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testUnregisterListener() throws Exception { + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/hub/testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ImportJobApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ImportJobApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0d3769881e3381b6d08eeb069f1e9935aa1d23c8 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ImportJobApiControllerTest.java @@ -0,0 +1,130 @@ +package org.etsi.osl.services.api.scm633; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ImportJobApiControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testCreateImportJob() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testExportJob.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String exportJobString = IOUtils.toString(in, "UTF-8"); + ExportJobCreate exportJobCreate = JsonUtils.toJsonObj(exportJobString, ExportJobCreate.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/importJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( exportJobCreate ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/importJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( exportJobCreate ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testDeleteImportJob() throws Exception { + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/importJob/testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListImportJob() throws Exception { + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/importJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/importJob") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveImportJob() throws Exception { + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/importJob?testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/importJob/testId") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is(501)); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ListenerApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ListenerApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9ed631a15bb0999c56f5dd8bbf9f03d524a2abad --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ListenerApiControllerTest.java @@ -0,0 +1,392 @@ +package org.etsi.osl.services.api.scm633; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ListenerApiControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCandidateChangeNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String changeNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCandidateChangeNotification serviceCandidateChangeNotification = JsonUtils.toJsonObj(changeNotificationString, ServiceCandidateChangeNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateChangeNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateChangeNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCandidateCreateNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String createNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCandidateCreateNotification serviceCandidateCreateNotification = JsonUtils.toJsonObj(createNotificationString, ServiceCandidateCreateNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateCreateNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateCreateNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCandidateDeleteNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String deleteNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCandidateDeleteNotification serviceCandidateDeleteNotification = JsonUtils.toJsonObj(deleteNotificationString, ServiceCandidateDeleteNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateDeleteNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCandidateDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateDeleteNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCatalogBatchNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String batchNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCatalogBatchNotification serviceCatalogBatchNotification = JsonUtils.toJsonObj(batchNotificationString, ServiceCatalogBatchNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogBatchNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogBatchNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogBatchNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogBatchNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCatalogChangeNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String changeNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCatalogChangeNotification serviceCatalogChangeNotification = JsonUtils.toJsonObj(changeNotificationString, ServiceCatalogChangeNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogChangeNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogChangeNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCatalogCreateNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String createNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCatalogCreateNotification serviceCatalogCreateNotification = JsonUtils.toJsonObj(createNotificationString, ServiceCatalogCreateNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogCreateNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogCreateNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCatalogDeleteNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String deleteNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCatalogDeleteNotification serviceCatalogDeleteNotification = JsonUtils.toJsonObj(deleteNotificationString, ServiceCatalogDeleteNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogDeleteNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCatalogDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCatalogDeleteNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCategoryChangeNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String changeNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCategoryChangeNotification serviceCategoryChangeNotification = JsonUtils.toJsonObj(changeNotificationString, ServiceCategoryChangeNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryChangeNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryChangeNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCategoryCreateNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String createNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCategoryCreateNotification serviceCategoryCreateNotification = JsonUtils.toJsonObj(createNotificationString, ServiceCategoryCreateNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryCreateNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryCreateNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceCategoryDeleteNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String deleteNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceCategoryDeleteNotification serviceCategoryDeleteNotification = JsonUtils.toJsonObj(deleteNotificationString, ServiceCategoryDeleteNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryDeleteNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceCategoryDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCategoryDeleteNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceSpecificationChangeNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String changeNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceSpecificationChangeNotification serviceSpecificationChangeNotification = JsonUtils.toJsonObj(changeNotificationString, ServiceSpecificationChangeNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationChangeNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationChangeNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationChangeNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceSpecificationCreateNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String createNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceSpecificationCreateNotification serviceSpecificationCreateNotification = JsonUtils.toJsonObj(createNotificationString, ServiceSpecificationCreateNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationCreateNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationCreateNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationCreateNotification ) )) + .andExpect(status().is(501)); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListenToServiceSpecificationDeleteNotification() throws Exception { + + File resourceSpecFile = new File("src/test/resources/testServiceCandidateChangeNotification.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String deleteNotificationString = IOUtils.toString(in, "UTF-8"); + ServiceSpecificationDeleteNotification serviceSpecificationDeleteNotification = JsonUtils.toJsonObj(deleteNotificationString, ServiceSpecificationDeleteNotification.class); + + // Test when providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationDeleteNotification ) )) + .andExpect(status().is(501)); + + // Test when not providing an "Accept" request header + mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/listener/serviceSpecificationDeleteNotification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationDeleteNotification ) )) + .andExpect(status().is(501)); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ServiceCandidateApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCandidateApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c8ed3f474d664be8bc421378cd331111f479c8b2 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCandidateApiControllerTest.java @@ -0,0 +1,201 @@ +package org.etsi.osl.services.api.scm633; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.List; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import net.minidev.json.JSONObject; +import org.apache.commons.io.IOUtils; + + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.etsi.osl.tmf.scm633.reposervices.CandidateRepoService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ServiceCandidateApiControllerTest { + + private static final int FIXED_BOOTSTRAPS_SPECS = 1; + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + CandidateRepoService candidateRepoService; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testCreateServiceCandidate() throws Exception { + + String response = createServiceCandidate(); + + ServiceCandidate responsesServiceCandidate = JsonUtils.toJsonObj(response, ServiceCandidate.class); + assertThat( responsesServiceCandidate.getDescription() ).isEqualTo( "A Test Service Candidate" ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testDeleteServiceCandidate() throws Exception { + + String response = createServiceCandidate(); + + ServiceCandidate responsesServiceCandidate = JsonUtils.toJsonObj(response, ServiceCandidate.class); + String id = responsesServiceCandidate.getId(); + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/serviceCandidate/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + assertThat( candidateRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListServiceCandidate() throws Exception { + + String response = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceCandidate") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + List<ServiceCandidate> serviceCandidateList= objectMapper.readValue(response, new TypeReference<List<ServiceCandidate>>() {}); + + assertThat(serviceCandidateList.size()).isEqualTo(candidateRepoService.findAll().size()); + + String id = candidateRepoService.findAll().get(0).getId(); + + boolean idExists = false; + for (ServiceCandidate sc : serviceCandidateList) { + if ( sc.getId().equals( id ) ) { + idExists= true; + } + } + assertThat( idExists ).isTrue(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testPatchServiceCandidate() throws Exception { + + String response = createServiceCandidate(); + + ServiceCandidate responsesServiceCandidate = JsonUtils.toJsonObj(response, ServiceCandidate.class); + String id = responsesServiceCandidate.getId(); + + JSONObject obj = JsonUtils.toJsonObj(response, JSONObject.class); + obj.remove("uuid"); + obj.remove("id"); + obj.remove("lastUpdate"); + response = JsonUtils.toJsonString(obj); + + ServiceCandidateUpdate serviceCandidateUpdate = JsonUtils.toJsonObj(response, ServiceCandidateUpdate.class); + serviceCandidateUpdate.setDescription("Test Service Candidate new description"); + + String response2 = mvc.perform(MockMvcRequestBuilders.patch("/serviceCatalogManagement/v4/serviceCandidate/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidateUpdate ) )) + .andExpect(status().isOk() ) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("description", is("Test Service Candidate new description"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + ServiceCandidate responsesServiceCandidate2 = JsonUtils.toJsonObj(response2, ServiceCandidate.class); + assertThat( responsesServiceCandidate2.getDescription() ).isEqualTo( "Test Service Candidate new description" ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveServiceCandidate() throws Exception { + + String response = createServiceCandidate(); + + ServiceCandidate responsesServiceCandidate = JsonUtils.toJsonObj(response, ServiceCandidate.class); + String id = responsesServiceCandidate.getId(); + + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceCandidate/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + ServiceCandidate responsesServiceCandidate2 = JsonUtils.toJsonObj(response2, ServiceCandidate.class); + assertThat( responsesServiceCandidate2.getDescription() ).isEqualTo( "A Test Service Candidate" ); + } + + private String createServiceCandidate() throws Exception{ + File resourceSpecFile = new File("src/test/resources/testServiceCandidate.txt"); + InputStream in = new FileInputStream(resourceSpecFile); + String serviceCandidateString = IOUtils.toString(in, "UTF-8"); + ServiceCandidateCreate serviceCandidate = JsonUtils.toJsonObj(serviceCandidateString, ServiceCandidateCreate.class); + + assertThat( candidateRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS); + + String response = mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/serviceCandidate") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceCandidate ) )) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("description", is("A Test Service Candidate"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( candidateRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1 ); + return response; + } +} diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ServiceCatalogApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCatalogApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..92b48d54028defe00acaa5f26c1f1f55f3f38219 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCatalogApiControllerTest.java @@ -0,0 +1,186 @@ +package org.etsi.osl.services.api.scm633; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import net.minidev.json.JSONObject; +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.rcm634.model.ResourceCatalog; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.etsi.osl.tmf.scm633.reposervices.CatalogRepoService; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ServiceCatalogApiControllerTest { + + private static final int FIXED_BOOTSTRAPS_CATALOGS = 1; + + @Autowired + private MockMvc mvc; + + @Autowired + private WebApplicationContext context; + + @Autowired + CatalogRepoService catalogRepoService; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testDeleteServiceCatalog() throws Exception { + + String response = createServiceCatalog(); + + ResourceCatalog responsesCatalog = JsonUtils.toJsonObj(response, ResourceCatalog.class); + assertThat( responsesCatalog.getName() ).isEqualTo( "Test Catalog" ); + + String id = responsesCatalog.getId(); + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/serviceCatalog/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + assertThat( catalogRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_CATALOGS ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListServiceCatalog() throws Exception { + + createServiceCatalog(); + + String response = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceCatalog") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + List<ServiceCatalog> serviceCatalogList = JsonUtils.toJsonObj(response, ArrayList.class); + + assertThat(serviceCatalogList.size()).isEqualTo(FIXED_BOOTSTRAPS_CATALOGS + 1); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testPatchServiceCatalog() throws Exception { + + String response = createServiceCatalog(); + + ServiceCatalog responsesServiceCatalog = JsonUtils.toJsonObj(response, ServiceCatalog.class); + String id = responsesServiceCatalog.getId(); + + JSONObject obj = JsonUtils.toJsonObj(response, JSONObject.class); + obj.remove("uuid"); + obj.remove("id"); + obj.remove("lastUpdate"); + response = JsonUtils.toJsonString(obj); + + ServiceCatalogUpdate ServiceCatalogUpdate = JsonUtils.toJsonObj(response, ServiceCatalogUpdate.class); + ServiceCatalogUpdate.setName( "Test Service Catalog new name" ); + ServiceCatalogUpdate.setVersion("2.x"); + + String response2 = mvc.perform(MockMvcRequestBuilders.patch("/serviceCatalogManagement/v4/serviceCatalog/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( ServiceCatalogUpdate ) )) + .andExpect(status().isOk() ) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Service Catalog new name"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + ServiceCatalog responsesServiceCatalog2 = JsonUtils.toJsonObj(response2, ServiceCatalog.class); + assertThat( responsesServiceCatalog2.getName() ).isEqualTo( "Test Service Catalog new name" ); + assertThat( responsesServiceCatalog2.getVersion() ).isEqualTo( "2.x" ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveServiceCatalog() throws Exception { + + String response = createServiceCatalog(); + + ServiceCatalog responsesServiceCatalog = JsonUtils.toJsonObj(response, ServiceCatalog.class); + String id = responsesServiceCatalog.getId(); + + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceCatalog/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + ServiceCatalog responsesServiceCatalog2 = JsonUtils.toJsonObj(response2, ServiceCatalog.class); + assertThat( responsesServiceCatalog2.getName() ).isEqualTo( "Test Catalog" ); + assertThat( responsesServiceCatalog2.getVersion() ).isEqualTo( "1.8" ); + } + + private String createServiceCatalog() throws Exception{ + assertThat( catalogRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_CATALOGS ); + + File scatalog = new File( "src/test/resources/testResourceCatalog.txt" ); + InputStream in = new FileInputStream( scatalog ); + String resvxf = IOUtils.toString(in, "UTF-8"); + + ServiceCatalogCreate scc = JsonUtils.toJsonObj( resvxf, ServiceCatalogCreate.class); + + String response = mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/serviceCatalog") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( scc ) )) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Catalog"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( catalogRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_CATALOGS + 1 ); + + return response; + } +} diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ServiceCategoryApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCategoryApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..865ca45fc3cb0b2f30ac468a7ea1fbbe92f92606 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ServiceCategoryApiControllerTest.java @@ -0,0 +1,85 @@ +package org.etsi.osl.services.api.scm633; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.List; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.scm633.model.*; + +import org.etsi.osl.tmf.scm633.reposervices.CategoryRepoService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") + +public class ServiceCategoryApiControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + CategoryRepoService categoryRepoService; + + @Autowired + private WebApplicationContext context; + + @Autowired + private ObjectMapper objectMapper; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListServiceCategory() throws Exception { + + String response = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceCategory") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + List<ServiceCategory> serviceCategoryList = objectMapper.readValue(response, new TypeReference<List<ServiceCategory>>() {}); + + assertThat(serviceCategoryList.size()).isEqualTo(categoryRepoService.findAll().size()); + String id = categoryRepoService.findAll().get(0).getId(); + + boolean idExists = false; + for (ServiceCategory ss : serviceCategoryList) { + if ( ss.getId().equals( id ) ) { + idExists= true; + } + } + assertThat( idExists ).isTrue(); + } +} diff --git a/src/test/java/org/etsi/osl/services/api/scm633/ServiceSpecificationApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/scm633/ServiceSpecificationApiControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e5aa51e2679c9804c6b0b34e2a7703b5a0d39abb --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/scm633/ServiceSpecificationApiControllerTest.java @@ -0,0 +1,425 @@ +package org.etsi.osl.services.api.scm633; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.io.*; +import java.util.List; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import net.minidev.json.JSONObject; +import org.apache.commons.io.IOUtils; + +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.common.model.Attachment; +import org.etsi.osl.tmf.rcm634.model.*; +import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; +import org.etsi.osl.tmf.scm633.model.*; +import org.etsi.osl.tmf.JsonUtils; + +import org.etsi.osl.tmf.scm633.reposervices.ServiceSpecificationRepoService; +import org.etsi.osl.tmf.stm653.model.ServiceTestSpecification; +import org.etsi.osl.tmf.stm653.model.ServiceTestSpecificationCreate; +import org.etsi.osl.tmf.stm653.reposervices.ServiceTestSpecificationRepoService; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.context.WebApplicationContext; + + +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ServiceSpecificationApiControllerTest { + + private static final int FIXED_BOOTSTRAPS_SPECS = 1; + + @Autowired + private MockMvc mvc; + + @Autowired + ServiceTestSpecificationRepoService aServiceTestSpecRpoService; + + @Autowired + ResourceSpecificationRepoService resourceSpecificationRepoService; + + @Autowired + private WebApplicationContext context; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + ServiceSpecificationRepoService specRepoService; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + } + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testDeleteServiceSpecification() throws Exception { + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS ); + String response = createServiceSpecification(); + + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String id = responsesSpec.getId(); + + mvc.perform(MockMvcRequestBuilders.delete("/serviceCatalogManagement/v4/serviceSpecification/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testListServiceSpecification() throws Exception { + + String response = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification") + + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + List<ServiceSpecification> serviceSpecificationList = objectMapper.readValue(response, new TypeReference<List<ServiceSpecification>>() {}); + + assertThat(serviceSpecificationList.size()).isEqualTo(specRepoService.findAll().size()); + String id = specRepoService.findAll().get(0).getId(); + + boolean idExists = false; + for (ServiceSpecification ss : serviceSpecificationList) { + if ( ss.getId().equals( id ) ) { + idExists= true; + } + } + assertThat( idExists ).isTrue(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testPatchServiceSpecification() throws Exception { + + String response = createServiceSpecification(); + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1); + + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String id = responsesSpec.getId(); + + JSONObject obj = JsonUtils.toJsonObj(response, JSONObject.class); + obj.remove("uuid"); + obj.remove("id"); + obj.remove("lastUpdate"); + response = JsonUtils.toJsonString(obj); + + ServiceSpecificationUpdate ServiceSpecUpdate = JsonUtils.toJsonObj(response, ServiceSpecificationUpdate.class); + ServiceSpecUpdate.setName( "Test Spec new name" ); + ServiceSpecUpdate.setVersion("2.x"); + + String response2 = mvc.perform(MockMvcRequestBuilders.patch("/serviceCatalogManagement/v4/serviceSpecification/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( ServiceSpecUpdate ) )) + .andExpect(status().isOk() ) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Spec new name"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + ServiceSpecification responsesServiceSpec2 = JsonUtils.toJsonObj(response2, ServiceSpecification.class); + assertThat( responsesServiceSpec2.getName() ).isEqualTo( "Test Spec new name" ); + assertThat( responsesServiceSpec2.getVersion() ).isEqualTo( "2.x" ); + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveServiceSpecification() throws Exception { + + String response = createServiceSpecification(); + + ServiceSpecification responsesServiceSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String id = responsesServiceSpec.getId(); + + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + id ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk() ) + .andReturn().getResponse().getContentAsString(); + + ServiceSpecification responsesServiceSpec2 = JsonUtils.toJsonObj(response2, ServiceSpecification.class); + assertThat( responsesServiceSpec2.getName() ).isEqualTo( "Test Spec" ); + assertThat( responsesServiceSpec2.getVersion() ).isEqualTo( "1.8.0" ); + } + + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testGetAttachment() throws Exception { + + // Create a Service Specification + String response = createServiceSpecification(); + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String specId = responsesSpec.getId(); + + // Test method for non-existing logo attachment + String attId = "logo"; + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + specId + "/attachment/" + attId) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound() ) + .andReturn().getResponse().getContentAsString(); + + // Test method for non-existing non-logo attachment + attId = "random"; + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + specId + "/attachment/" + attId) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound() ) + .andReturn().getResponse().getContentAsString(); + + + // Test method for existing attachment + + // Add a new attachment to the Service Specification + String response2 = createAttachmentAndAddToServiceSpecification(specId); + Attachment attachment = JsonUtils.toJsonObj(response2, Attachment.class); + attId = attachment.getId(); + String attName = attachment.getName(); + String attMimeType = attachment.getMimeType(); + + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + specId + "/attachment/" + attId) + ) + .andExpect(content().contentTypeCompatibleWith(MediaType.ALL_VALUE)) + .andExpect(status().isOk() ) + .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache")) + .andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + attName)) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, attMimeType)) + .andReturn().getResponse().getContentAsString(); + } + + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testGetAttachmentWithFilename() throws Exception { + + // Create a Service Specification + String response = createServiceSpecification(); + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String specId = responsesSpec.getId(); + + String response2 = createAttachmentAndAddToServiceSpecification(specId); + Attachment attachment = JsonUtils.toJsonObj(response2, Attachment.class); + String attId = attachment.getId(); + String attName = attachment.getName(); + String attMimeType = attachment.getMimeType(); + + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + specId + "/attachment/" + attId + "/" + attName) + ) + .andExpect(content().contentTypeCompatibleWith(MediaType.ALL_VALUE)) + .andExpect(status().isOk() ) + .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache")) + .andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + attName)) + .andExpect(header().string(HttpHeaders.CONTENT_TYPE, attMimeType)) + .andReturn().getResponse().getContentAsString(); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testRetrieveServiceSpecificationDescriptor() throws Exception { + + // Test a non-existing spec + mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + "fakeId" + "/sd") + ) + .andExpect(status().isNotFound()) + .andReturn().getResponse().getContentAsString(); + } + + + @WithMockUser(username = "osadmin", roles = { "ADMIN","USER" }) + @Test + public void testSpecFromTestSpec() throws Exception { + + // Creeate a Test Spec + File sspec = new File( "src/test/resources/testServiceTestSpec.json" ); + InputStream in = new FileInputStream( sspec ); + String sspectext = IOUtils.toString(in, "UTF-8"); + ServiceTestSpecificationCreate spec = JsonUtils.toJsonObj( sspectext, ServiceTestSpecificationCreate.class); + + int originalSize = aServiceTestSpecRpoService.findAll().size(); + + String response = mvc.perform(MockMvcRequestBuilders.post("/serviceTestManagement/v4/serviceTestSpecification") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( spec ) )) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("A test name"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + + assertThat( aServiceTestSpecRpoService.findAll().size() ).isEqualTo( originalSize + 1 ); + ServiceTestSpecification sts = JsonUtils.toJsonObj(response, ServiceTestSpecification.class); + assertThat(sts.getName()).isEqualTo("A test name"); + String stsId = sts.getId(); + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS); + + // Create a Service Spec from the Test Spec + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/specFromTestSpec/" + stsId) + .with( SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1 ); + + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response2, ServiceSpecification.class); + assertThat( responsesSpec.getName() ).isEqualTo( "A test name" ); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testGetImageSpecificationRelationshipGraph() throws Exception { + + // Create a Service Specification + String response = createServiceSpecification(); + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + String specId = responsesSpec.getId(); + + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/" + specId + "/relationship_graph") + ) + .andExpect(status().is(302) ) + .andReturn().getResponse().getRedirectedUrl(); + + assertThat( response2 ).contains("/blockdiag/svg/"); + } + + + @WithMockUser(username="osadmin", roles = {"ADMIN"}) + @Test + public void testSpecFromResourceSpec() throws Exception { + + File rspec = new File( "src/test/resources/testResourceSpec.json" ); + InputStream in = new FileInputStream( rspec ); + String rspectext = IOUtils.toString(in, "UTF-8"); + ResourceSpecificationCreate rspeccr = JsonUtils.toJsonObj( rspectext, ResourceSpecificationCreate.class); + + int originalSize = resourceSpecificationRepoService.findAll().size(); + + String responseSpec = mvc.perform(MockMvcRequestBuilders.post( "/resourceCatalogManagement/v4/resourceSpecification" ) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( rspeccr ) )) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( resourceSpecificationRepoService.findAll().size() ).isEqualTo( originalSize + 1 ); + ResourceSpecification responsesSpec1 = JsonUtils.toJsonObj(responseSpec, PhysicalResourceSpecification.class); + assertThat(responsesSpec1.getName()).isEqualTo("Test Resource Spec"); + String rSpecId = responsesSpec1.getId(); + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS); + + String response2 = mvc.perform(MockMvcRequestBuilders.get("/serviceCatalogManagement/v4/serviceSpecification/specFromResourceSpec/" + rSpecId) + .with( SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1 ); + + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response2, ServiceSpecification.class); + assertThat( responsesSpec.getName() ).isEqualTo( "Test Resource Spec" ); + } + + + private String createServiceSpecification() throws Exception{ + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS ); + + File sspec = new File( "src/test/resources/testServiceSpec.json" ); + InputStream in = new FileInputStream( sspec ); + String sspectext = IOUtils.toString(in, "UTF-8"); + ServiceSpecificationCreate serviceSpecificationCreate = JsonUtils.toJsonObj( sspectext, ServiceSpecificationCreate.class); + + String response = mvc.perform(MockMvcRequestBuilders.post("/serviceCatalogManagement/v4/serviceSpecification") + .with( SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content( JsonUtils.toJson( serviceSpecificationCreate ) )) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertThat( specRepoService.findAll().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS + 1 ); + + ServiceSpecification responsesSpec = JsonUtils.toJsonObj(response, ServiceSpecification.class); + assertThat( responsesSpec.getName() ).isEqualTo( "Test Spec" ); + + return response; + } + + private String createAttachmentAndAddToServiceSpecification(String serviceSpecId) throws Exception { + + Attachment att = new Attachment(); + + File gz = new File( "src/test/resources/cirros_vnf.tar.gz" ); + InputStream ing = new FileInputStream( gz ); + MockMultipartFile prodFile = new MockMultipartFile("afile", "cirros_vnf.tar.gz", "application/x-gzip", IOUtils.toByteArray(ing)); + + String response = mvc.perform(MockMvcRequestBuilders + .multipart("/serviceCatalogManagement/v4/serviceSpecification/" + serviceSpecId + "/attachment" ) + .file(prodFile) + .param("attachment", JsonUtils.toJsonString(att)) + .with( SecurityMockMvcRequestPostProcessors.csrf()) + ) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("cirros_vnf.tar.gz"))) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + Attachment attachment = JsonUtils.toJsonObj( response, Attachment.class); + + assertThat(attachment.getName()).isEqualTo("cirros_vnf.tar.gz"); + assertThat(attachment.getUrl()).contains(serviceSpecId); + + return response; + } +} diff --git a/src/test/resources/testEventSubscriptionInput.json b/src/test/resources/testEventSubscriptionInput.json new file mode 100644 index 0000000000000000000000000000000000000000..bccdb0cdb7f50e92a7ac57abcd18cbe28d5be28e --- /dev/null +++ b/src/test/resources/testEventSubscriptionInput.json @@ -0,0 +1,3 @@ +{ + "query": "test" +} \ No newline at end of file diff --git a/src/test/resources/testExportJob.json b/src/test/resources/testExportJob.json new file mode 100644 index 0000000000000000000000000000000000000000..cb006da88796f2874e9c144291833106c0c747ba --- /dev/null +++ b/src/test/resources/testExportJob.json @@ -0,0 +1,4 @@ +{ + "contentType": "application/json", + "url": "https://my/daily/job/NHCFD6" +} \ No newline at end of file diff --git a/src/test/resources/testServiceCandidate.txt b/src/test/resources/testServiceCandidate.txt new file mode 100644 index 0000000000000000000000000000000000000000..e499cc0699beaec54c04d95e62e7126b83df0599 --- /dev/null +++ b/src/test/resources/testServiceCandidate.txt @@ -0,0 +1,5 @@ +{ + "description": "A Test Service Candidate", + "version": "1.8", + "name": "Test Service Candidate" +} \ No newline at end of file diff --git a/src/test/resources/testServiceCandidateChangeNotification.json b/src/test/resources/testServiceCandidateChangeNotification.json new file mode 100644 index 0000000000000000000000000000000000000000..83442e8e267ea97db23f53bd0557c0845e6ce818 --- /dev/null +++ b/src/test/resources/testServiceCandidateChangeNotification.json @@ -0,0 +1,3 @@ +{ + "eventId": "testId" +} \ No newline at end of file