From 5a907895783ba1947ef87579c2476bf5dba0177d Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Fri, 12 Jan 2024 17:17:19 +0200 Subject: [PATCH 1/6] Created common tests that will be used for most tmf apis --- .gitignore | 1 + .../etsi/osl/tmf/rcm634/api/ApiException.java | 4 + .../osl/services/api/rcm634/CommonTests.java | 168 ++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java diff --git a/.gitignore b/.gitignore index 59419d5..f91fa93 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /.project /.classpath /.settings +/org.etsi.osl.tmf.api.iml diff --git a/src/main/java/org/etsi/osl/tmf/rcm634/api/ApiException.java b/src/main/java/org/etsi/osl/tmf/rcm634/api/ApiException.java index 5371ea0..b979eef 100644 --- a/src/main/java/org/etsi/osl/tmf/rcm634/api/ApiException.java +++ b/src/main/java/org/etsi/osl/tmf/rcm634/api/ApiException.java @@ -19,6 +19,9 @@ */ package org.etsi.osl.tmf.rcm634.api; +import lombok.Getter; + +@Getter @jakarta.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2021-05-29T22:34:44.143740800+03:00[Europe/Athens]") public class ApiException extends Exception{ private int code; @@ -26,4 +29,5 @@ public class ApiException extends Exception{ super(msg); this.code = code; } + } diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java new file mode 100644 index 0000000..f2c3bee --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java @@ -0,0 +1,168 @@ +package org.etsi.osl.services.api.rcm634; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.etsi.osl.tmf.rcm634.api.ApiException; +import org.etsi.osl.tmf.rcm634.api.ApiOriginFilter; +import org.etsi.osl.tmf.rcm634.api.ApiResponseMessage; +import org.etsi.osl.tmf.rcm634.api.ImportJobApiController; +import org.etsi.osl.tmf.rcm634.api.ExportJobApiController; +import org.etsi.osl.tmf.rcm634.api.ListenerApiController; +import org.etsi.osl.tmf.rcm634.api.HubApiController; +import org.etsi.osl.tmf.rcm634.api.NotFoundException; + +import org.junit.jupiter.api.Test; + +import org.springframework.mock.web.MockHttpServletRequest; + +import java.io.IOException; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class CommonTests { + + @Test + public void testApiException() { + int errorCode = 404; + String errorMessage = "Not Found"; + + ApiException exception = new ApiException(errorCode, errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + assertEquals(errorCode, exception.getCode()); + } + + @Test + public void ApiOriginFilter() throws IOException, ServletException { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + FilterChain chain = mock(FilterChain.class); + + ApiOriginFilter filter = new ApiOriginFilter(); + filter.doFilter(request, response, chain); + + verify(response).addHeader("Access-Control-Allow-Origin", "*"); + verify(response).addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + verify(response).addHeader("Access-Control-Allow-Headers", "Content-Type"); + verify(chain).doFilter(request, response); + } + + @Test + public void testApiResponseMessage() { + int[] codes = {ApiResponseMessage.ERROR, ApiResponseMessage.WARNING, ApiResponseMessage.INFO, ApiResponseMessage.OK, ApiResponseMessage.TOO_BUSY, 6}; + + String[] types = {"error", "warning", "info", "ok", "too busy", "unknown"}; + + String[] messages = {"An error occured", "This is a warning", "Given info", "ok", "System is too busy", "unknown code"}; + + for (int i = 0; i < codes.length; i++) { + int code = codes[i]; + String type = types[i]; + String message = messages[i]; + + ApiResponseMessage responseMessage = new ApiResponseMessage(code, message); + + assertEquals(message, responseMessage.getMessage()); + assertEquals(code, responseMessage.getCode()); + assertEquals(type, responseMessage.getType()); + + } + + ApiResponseMessage responseMessage = new ApiResponseMessage(); + responseMessage.setMessage("Error"); + assertEquals("Error", responseMessage.getMessage()); + responseMessage.setType("ok"); + assertEquals("ok", responseMessage.getType()); + responseMessage.setCode(ApiResponseMessage.OK); + assertEquals(ApiResponseMessage.OK, responseMessage.getCode()); + } + + @Test + public void testExportJobApiController() { + ObjectMapper objectMapper = new ObjectMapper(); + HttpServletRequest request = new MockHttpServletRequest(); + + ExportJobApiController controller = new ExportJobApiController(objectMapper, request); + + Optional returnedObjectMapper = controller.getObjectMapper(); + Optional returnedRequest = controller.getRequest(); + + assertTrue(returnedObjectMapper.isPresent()); + assertTrue(returnedRequest.isPresent()); + + assertEquals(objectMapper, returnedObjectMapper.get()); + assertEquals(request, returnedRequest.get()); + } + + @Test + public void testHubApiController() { + ObjectMapper objectMapper = new ObjectMapper(); + HttpServletRequest request = new MockHttpServletRequest(); + + HubApiController controller = new HubApiController(objectMapper, request); + + Optional returnedObjectMapper = controller.getObjectMapper(); + Optional returnedRequest = controller.getRequest(); + + assertTrue(returnedObjectMapper.isPresent()); + assertTrue(returnedRequest.isPresent()); + + assertEquals(objectMapper, returnedObjectMapper.get()); + assertEquals(request, returnedRequest.get()); + } + + @Test + public void testImportJobApiController() { + ObjectMapper objectMapper = new ObjectMapper(); + HttpServletRequest request = new MockHttpServletRequest(); + + ImportJobApiController controller = new ImportJobApiController(objectMapper, request); + + Optional returnedObjectMapper = controller.getObjectMapper(); + Optional returnedRequest = controller.getRequest(); + + assertTrue(returnedObjectMapper.isPresent()); + assertTrue(returnedRequest.isPresent()); + + assertEquals(objectMapper, returnedObjectMapper.get()); + assertEquals(request, returnedRequest.get()); + } + + @Test + public void testListenerApiController() { + ObjectMapper objectMapper = new ObjectMapper(); + HttpServletRequest request = new MockHttpServletRequest(); + + ListenerApiController controller = new ListenerApiController(objectMapper, request); + + Optional returnedObjectMapper = controller.getObjectMapper(); + Optional returnedRequest = controller.getRequest(); + + assertTrue(returnedObjectMapper.isPresent()); + assertTrue(returnedRequest.isPresent()); + + assertEquals(objectMapper, returnedObjectMapper.get()); + assertEquals(request, returnedRequest.get()); + } + + @Test + public void testNotFoundException() { + int errorCode = 404; + String errorMessage = "Not Found"; + + NotFoundException exception = new NotFoundException(errorCode, errorMessage); + + assertEquals(errorMessage, exception.getMessage()); + assertEquals(errorCode, exception.getCode()); + } + +} + -- GitLab From 4276ae8e4c32c07147435000ee0ace2ad3648691 Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Thu, 18 Jan 2024 15:45:03 +0200 Subject: [PATCH 2/6] added test for resourceSpecificationApiController --- src/main/java/org/etsi/osl/tmf/JsonUtils.java | 11 + .../osl/services/api/rcm634/CommonTests.java | 23 +- ...esourceSpecificationApiControllerTest.java | 267 ++++++++++++++++++ 3 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java diff --git a/src/main/java/org/etsi/osl/tmf/JsonUtils.java b/src/main/java/org/etsi/osl/tmf/JsonUtils.java index ff2221e..b28edd6 100644 --- a/src/main/java/org/etsi/osl/tmf/JsonUtils.java +++ b/src/main/java/org/etsi/osl/tmf/JsonUtils.java @@ -2,6 +2,7 @@ package org.etsi.osl.tmf; import java.io.IOException; import java.io.InputStream; +import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; @@ -41,4 +42,14 @@ public class JsonUtils { return mapper.readValue(content, valueType); } + public static List toListOfJsonObj(String content, Class valueType) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + List listOfJsonObj = mapper.readValue( + content, + mapper.getTypeFactory().constructCollectionType( + List.class, valueType)); + return listOfJsonObj; + } + } diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java index f2c3bee..d77b1e0 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java @@ -1,3 +1,23 @@ +/*- + * ========================LICENSE_START================================= + * org.etsi.osl.tmf.api + * %% + * Copyright (C) 2019 - 2024 openslice.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * =========================LICENSE_END================================== + */ + package org.etsi.osl.services.api.rcm634; import com.fasterxml.jackson.databind.ObjectMapper; @@ -24,8 +44,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class CommonTests { diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java new file mode 100644 index 0000000..9cc12ef --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java @@ -0,0 +1,267 @@ +package org.etsi.osl.services.api.rcm634; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.transaction.Transactional; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.IOUtils; +import org.etsi.osl.tmf.JsonUtils; +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.common.model.Attachment; +import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; +import org.etsi.osl.tmf.pcm620.reposervices.AttachmentRepoService; +import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; +import org.etsi.osl.tmf.rcm634.model.*; +import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +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.web.servlet.MockMvc; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + + +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureTestDatabase +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ResourceSpecificationApiControllerTest { + + @Autowired + private MockMvc mvc; + + @InjectMocks + ResourceSpecificationApiController resourceSpecificationApiController; + + @Mock + ResourceSpecificationRepoService resourceSpecificationRepoService; + + @Mock + ResourceSpecification resourceSpecification; + + @BeforeEach + public void init() { + MockitoAnnotations.initMocks(this); + } + + + // Test CREATE with role ADMIN + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testCreateResourceSpecificationAdmin() throws Exception { + + // Testing a valid physical resource specification at the correct endpoint + File resourceSpecFile = new File("src/test/resources/testPhysicalResourceSpec.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String resourceSpecString = IOUtils.toString(in, "UTF-8"); + PhysicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(resourceSpecString, PhysicalResourceSpecificationCreate.class); + + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Spec"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + + LogicalResourceSpecificationCreate logicalResourceSpecificationCreate = new LogicalResourceSpecificationCreate(); + logicalResourceSpecificationCreate.setName("Test Logical Resource Spec"); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(logicalResourceSpecificationCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Logical Resource Spec"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Function Specification"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + // Testing a valid physical resource specification wrong endpoint + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecificat") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(status().isNotFound()) + .andReturn().getResponse(); + + // Testing a valid physical resource specification wrong payload correct endpoint + String wrongStringPayload = "random string as payload"; + byte[] wrongPayload = wrongStringPayload.getBytes(); + + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(wrongPayload)) + .andExpect(status().isBadRequest()) + .andReturn().getResponse(); + } + + // Test CREATE with incorrect role USER + @Test + @WithMockUser(authorities = {"ROLE_USER"}) + public void testCreateResourceSpecificationUser() throws Exception { + // Testing a valid physical resource specification + File physicalResourceSpecFile = new File("src/test/resources/testResourceSpec.json"); + InputStream in = new FileInputStream(physicalResourceSpecFile); + String physicalResourceSpecString = IOUtils.toString(in, "UTF-8"); + LogicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(physicalResourceSpecString, LogicalResourceSpecificationCreate.class); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(status().isForbidden()) + .andReturn().getResponse(); + + } + + // Test DELETE method with role ADMIN + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testDeleteResourceSpecificationAdmin() throws Exception { + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andReturn().getResponse().getContentAsString(); + System.out.println(createdSpecificationString); + + ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); + String idToBeDeleted = createdSpecification.getId(); + + mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/" + idToBeDeleted) + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + } + + // Test DELETE method with incorrect role USER + @Test + @WithMockUser(authorities = {"ROLE_USER"}) + public void testDeleteResourceSpecificationUser() throws Exception { + mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/idToBeDeleted") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(status().isForbidden()) + .andReturn().getResponse(); + + } + + // Test GET by id method with no role + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testRetrieveResourceSpecification() throws Exception { + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andReturn().getResponse().getContentAsString(); + + ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); + String id = createdSpecification.getId(); + String fields = "name"; + mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification/" + id) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null)) + .param("fields", fields)) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Function Specification"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + } + + // Test GET all method with no role + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testListResourceSpecification() throws Exception { + String listResSpecsResponseString = mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + List listResSpecsResponse = JsonUtils.toListOfJsonObj(listResSpecsResponseString, LogicalResourceSpecification.class); + assertEquals(7, listResSpecsResponse.size()); + + mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null)) + .param("fields", "lastUpdate")) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + } + + @Test + void testGetObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + MockHttpServletRequest request = new MockHttpServletRequest(); + ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); + + Optional result = controller.getObjectMapper(); + assertTrue(result.isPresent()); + assertEquals(objectMapper, result.get()); + } + + @Test + void testGetRequest() { + ObjectMapper objectMapper = new ObjectMapper(); + MockHttpServletRequest request = new MockHttpServletRequest(); + ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); + + Optional result = controller.getRequest(); + assertTrue(result.isPresent()); + assertEquals(request, result.get()); + } +} \ No newline at end of file -- GitLab From c7f2d445d73060a7266b0dc29074f54614929b4c Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Thu, 18 Jan 2024 17:44:27 +0200 Subject: [PATCH 3/6] tests for model --- .../ConnectionPointSpecificationRefTest.java | 156 ++++++++++++++++++ .../rcm634/ConnectionSpecificationTest.java | 118 +++++++++++++ .../api/rcm634/ConstraintRefTest.java | 80 +++++++++ ...esourceSpecificationApiControllerTest.java | 1 + 4 files changed, 355 insertions(+) create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ConnectionSpecificationTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ConstraintRefTest.java diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java new file mode 100644 index 0000000..3a92301 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java @@ -0,0 +1,156 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ConnectionPointSpecificationRef; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class ConnectionPointSpecificationRefTest { + + @Test + void testConnectionPointSpecificationRef() { + ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef(); + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String version = "testVersion"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.href(href); + ref.name(name); + ref.version(version); + ref._atReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(version, ref.getVersion()); + assertEquals(referredType, ref.getAtReferredType()); + + ref = new ConnectionPointSpecificationRef(); + ref.setUuid(id); + ref.setHref(href); + ref.setName(name); + ref.setVersion(version); + ref.setAtReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(version, ref.getVersion()); + assertEquals(referredType, ref.getAtReferredType()); + } + + @Test + void testEquals() { + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String version = "testVersion"; + String referredType = "testReferredType"; + + ConnectionPointSpecificationRef ref1 = new ConnectionPointSpecificationRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setName(name); + ref1.setVersion(version); + ref1.setAtReferredType(referredType); + + ConnectionPointSpecificationRef ref2 = new ConnectionPointSpecificationRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setName(name); + ref2.setVersion(version); + ref2.setAtReferredType(referredType); + + + assertTrue(ref1.equals(ref2)); + + ref1.id("differentId"); + + assertFalse(ref1.equals(ref2)); + } + + @Test + void testHashCode() { + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String version = "testVersion"; + String referredType = "testReferredType"; + + ConnectionPointSpecificationRef ref1 = new ConnectionPointSpecificationRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setName(name); + ref1.setVersion(version); + ref1.setAtReferredType(referredType); + + ConnectionPointSpecificationRef ref2 = new ConnectionPointSpecificationRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setName(name); + ref2.setVersion(version); + ref2.setAtReferredType(referredType); + + assertEquals(ref1.hashCode(), ref2.hashCode()); + + ref1.id("differentId"); + + assertNotEquals(ref1.hashCode(), ref2.hashCode()); + } + + @Test + void testToString() { + ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef(); + + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String version = "testVersion"; + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.setHref(href); + ref.setName(name); + ref.setVersion(version); + ref.setBaseType(baseType); + ref.setSchemaLocation(schemaLocation); + ref.setType(type); + ref.setAtReferredType(referredType); + + String expectedString = "class ConnectionPointSpecificationRef {\n" + + " id: " + id + "\n" + + " href: " + href + "\n" + + " name: " + name + "\n" + + " version: " + version + "\n" + + " _atBaseType: " + baseType + "\n" + + " _atSchemaLocation: " + schemaLocation + "\n" + + " _atType: " + type + "\n" + + " _atReferredType: " + referredType + "\n" + + "}"; + + assertEquals(expectedString, ref.toString()); + } + + @Test + void testToIndentedString() throws Exception { + ConnectionPointSpecificationRef ref = new ConnectionPointSpecificationRef(); + + Method method = ConnectionPointSpecificationRef.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(ref, input); + + assertEquals(expectedOutput, output); + } +} diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionSpecificationTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionSpecificationTest.java new file mode 100644 index 0000000..527d5df --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionSpecificationTest.java @@ -0,0 +1,118 @@ +package org.etsi.osl.services.api.rcm634; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import org.etsi.osl.tmf.rcm634.model.ConnectionSpecification; +import org.etsi.osl.tmf.rcm634.model.EndpointSpecificationRef; + +import java.util.HashSet; +import java.util.Set; + +public class ConnectionSpecificationTest { + + @Test + void testConnectionSpecification() { + ConnectionSpecification spec = new ConnectionSpecification(); + String id = "testId"; + String href = "testHref"; + String associationType = "testAssociationType"; + String name = "testName"; + Set endpointSpecification = new HashSet<>(); + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + + spec.id(id); + spec.setUuid(id); + spec.href(href); + spec.associationType(associationType); + spec.name(name); + spec.endpointSpecification(endpointSpecification); + spec._atBaseType(baseType); + spec._atSchemaLocation(schemaLocation); + spec._atType(type); + + assertEquals(id, spec.getId()); + assertEquals(href, spec.getHref()); + assertEquals(associationType, spec.getAssociationType()); + assertEquals(name, spec.getName()); + assertEquals(endpointSpecification, spec.getEndpointSpecification()); + assertEquals(baseType, spec.getAtBaseType()); + assertEquals(schemaLocation, spec.getAtSchemaLocation()); + assertEquals(type, spec.getAtType()); + + String expectedString = "class ConnectionSpecification {\n" + + " id: " + id + "\n" + + " href: " + href + "\n" + + " associationType: " + associationType + "\n" + + " name: " + name + "\n" + + " endpointSpecification: " + endpointSpecification.toString().replace("\n", "\n ") + "\n" + + " _atBaseType: " + baseType + "\n" + + " _atSchemaLocation: " + schemaLocation + "\n" + + " _atType: " + type + "\n" + + "}"; + + assertEquals(expectedString, spec.toString()); + + ConnectionSpecification spec2 = new ConnectionSpecification(); + spec2.id(id); + spec2.href(href); + spec2.associationType(associationType); + spec2.name(name); + spec2.endpointSpecification(endpointSpecification); + spec2._atBaseType(baseType); + spec2._atSchemaLocation(schemaLocation); + spec2._atType(type); + + assertTrue(spec.equals(spec2)); + assertEquals(spec.hashCode(), spec2.hashCode()); + + spec.id("differentId"); + + assertFalse(spec.equals(spec2)); + assertNotEquals(spec.hashCode(), spec2.hashCode()); + } + + @Test + void testSetters() { + ConnectionSpecification spec = new ConnectionSpecification(); + String id = "testId"; + String href = "testHref"; + String associationType = "testAssociationType"; + String name = "testName"; + Set endpointSpecification = new HashSet<>(); + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + + spec.setUuid(id); + spec.id(id); + spec.setHref(href); + spec.setAssociationType(associationType); + spec.setName(name); + spec.setEndpointSpecification(endpointSpecification); + spec.setAtBaseType(baseType); + spec.setAtSchemaLocation(schemaLocation); + spec.setAtType(type); + + assertEquals(id, spec.getId()); + assertEquals(href, spec.getHref()); + assertEquals(associationType, spec.getAssociationType()); + assertEquals(name, spec.getName()); + assertEquals(endpointSpecification, spec.getEndpointSpecification()); + assertEquals(baseType, spec.getAtBaseType()); + assertEquals(schemaLocation, spec.getAtSchemaLocation()); + assertEquals(type, spec.getAtType()); + } + + @Test + void testAddEndpointSpecificationItem() { + ConnectionSpecification spec = new ConnectionSpecification(); + EndpointSpecificationRef endpointSpecRef = new EndpointSpecificationRef(); + endpointSpecRef.id("testId"); + + spec.addEndpointSpecificationItem(endpointSpecRef); + + assertTrue(spec.getEndpointSpecification().contains(endpointSpecRef)); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ConstraintRefTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ConstraintRefTest.java new file mode 100644 index 0000000..17dc9e3 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ConstraintRefTest.java @@ -0,0 +1,80 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ConstraintRef; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ConstraintRefTest { + + @Test + void testConstraintRef() { + ConstraintRef ref = new ConstraintRef(); + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String version = "testVersion"; + String referredType = "testReferredType"; + + String baseType = "BaseEntity"; + String schemaLocation = "null"; + String type = "null"; + + ref.id(id); + ref.setUuid(id); + ref.href(href); + ref.name(name); + ref.version(version); + ref._atReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(version, ref.getVersion()); + assertEquals(referredType, ref.getAtReferredType()); + + String expectedString = "class ConstraintRef {\n" + + " id: " + id + "\n" + + " href: " + href + "\n" + + " name: " + name + "\n" + + " version: " + version + "\n" + + " baseType: " + baseType + "\n" + + " schemaLocation: " + schemaLocation + "\n" + + " type: " + type + "\n" + + " _atReferredType: " + referredType + "\n" + + "}"; + + assertEquals(expectedString, ref.toString()); + + ConstraintRef ref2 = new ConstraintRef(); + ref2.id(id); + ref2.setUuid(id); + ref2.href(href); + ref2.name(name); + ref2.version(version); + ref2._atReferredType(referredType); + + assertTrue(ref.equals(ref2)); + assertEquals(ref.hashCode(), ref2.hashCode()); + + ref.id("differentId"); + ref.setUuid("differentId"); + + assertFalse(ref.equals(ref2)); + assertNotEquals(ref.hashCode(), ref2.hashCode()); + + ref = new ConstraintRef(); + + ref.setId(id); + ref.setUuid(id); + ref.setHref(href); + ref.setName(name); + ref.setVersion(version); + ref.setAtReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(version, ref.getVersion()); + assertEquals(referredType, ref.getAtReferredType()); + } +} diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java index 9cc12ef..b41f281 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java @@ -12,6 +12,7 @@ import org.etsi.osl.tmf.pcm620.reposervices.AttachmentRepoService; import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; import org.etsi.osl.tmf.rcm634.model.*; import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; +import org.junit.Ignore; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; -- GitLab From f61d1651f42ef8fcb6191d68eeeaca0ba2406ae0 Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Fri, 19 Jan 2024 19:13:30 +0200 Subject: [PATCH 4/6] Added tests for model --- .../rcm634/EndpointSpecificationRefTest.java | 180 ++++++++ .../services/api/rcm634/EntityRefTest.java | 143 ++++++ .../osl/services/api/rcm634/ErrorTest.java | 179 ++++++++ .../rcm634/EventSubscriptionInputTest.java | 102 +++++ .../api/rcm634/EventSubscriptionTest.java | 116 +++++ .../ExportJobCreateEventPayloadTest.java | 87 ++++ .../api/rcm634/ExportJobCreateEventTest.java | 180 ++++++++ .../api/rcm634/ExportJobCreateTest.java | 231 ++++++++++ .../rcm634/ExportJobStateChangeEventTest.java | 196 ++++++++ .../services/api/rcm634/ExportJobTest.java | 266 +++++++++++ .../api/rcm634/ResourceSpecificationTest.java | 422 ++++++++++++++++++ 11 files changed, 2102 insertions(+) create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/EndpointSpecificationRefTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/EntityRefTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ErrorTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionInputTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventPayloadTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ExportJobStateChangeEventTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ExportJobTest.java create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationTest.java diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/EndpointSpecificationRefTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/EndpointSpecificationRefTest.java new file mode 100644 index 0000000..2618021 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/EndpointSpecificationRefTest.java @@ -0,0 +1,180 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ConnectionPointSpecificationRef; +import org.etsi.osl.tmf.rcm634.model.EndpointSpecificationRef; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class EndpointSpecificationRefTest { + @Test + void testName() { + EndpointSpecificationRef ref = new EndpointSpecificationRef(); + String name = "testName"; + ref.setName(name); + assertEquals(name, ref.getName()); + + EndpointSpecificationRef newRef = ref.name("newTestName"); + assertEquals(ref, newRef); + } + + @Test + void testConnectionPointSpecification() { + EndpointSpecificationRef ref = new EndpointSpecificationRef(); + ConnectionPointSpecificationRef connectionPointSpecificationRef = new ConnectionPointSpecificationRef(); + ref.setConnectionPointSpecification(connectionPointSpecificationRef); + assertEquals(connectionPointSpecificationRef, ref.getConnectionPointSpecification()); + + EndpointSpecificationRef newRef = ref.connectionPointSpecification(connectionPointSpecificationRef.name("newRef")); + assertEquals(ref, newRef); + } + + @Test + void testEndpointSpecificationRef() { + EndpointSpecificationRef ref = new EndpointSpecificationRef(); + String id = "testId"; + String href = "testHref"; + Boolean isRoot = true; + String role = "testRole"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.href(href); + ref.isRoot(isRoot); + ref.role(role); + ref._atReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(isRoot, ref.isIsRoot()); + assertEquals(role, ref.getRole()); + assertEquals(referredType, ref.getAtReferredType()); + + ref = new EndpointSpecificationRef(); + ref.setUuid(id); + ref.setHref(href); + ref.setIsRoot(isRoot); + ref.setRole(role); + ref.setAtReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(isRoot, ref.isIsRoot()); + assertEquals(role, ref.getRole()); + assertEquals(referredType, ref.getAtReferredType()); + } + + @Test + void testEquals() { + String id = "testId"; + String href = "testHref"; + Boolean isRoot = true; + String role = "testRole"; + String referredType = "testReferredType"; + + EndpointSpecificationRef ref1 = new EndpointSpecificationRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setIsRoot(isRoot); + ref1.setRole(role); + ref1.setAtReferredType(referredType); + + EndpointSpecificationRef ref2 = new EndpointSpecificationRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setIsRoot(isRoot); + ref2.setRole(role); + ref2.setAtReferredType(referredType); + + assertTrue(ref1.equals(ref2)); + + ref1.id("differentId"); + + assertFalse(ref1.equals(ref2)); + } + + @Test + void testHashCode() { + String id = "testId"; + String href = "testHref"; + Boolean isRoot = true; + String role = "testRole"; + String referredType = "testReferredType"; + + EndpointSpecificationRef ref1 = new EndpointSpecificationRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setIsRoot(isRoot); + ref1.setRole(role); + ref1.setAtReferredType(referredType); + + EndpointSpecificationRef ref2 = new EndpointSpecificationRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setIsRoot(isRoot); + ref2.setRole(role); + ref2.setAtReferredType(referredType); + + assertEquals(ref1.hashCode(), ref2.hashCode()); + + ref1.id("differentId"); + + assertNotEquals(ref1.hashCode(), ref2.hashCode()); + } + + @Test + void testToString() { + EndpointSpecificationRef ref = new EndpointSpecificationRef(); + + String id = "testId"; + String href = "testHref"; + Boolean isRoot = true; + String role = "testRole"; + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.setHref(href); + ref.isRoot(isRoot); + ref.role(role); + ref.setBaseType(baseType); + ref.setSchemaLocation(schemaLocation); + ref.setType(type); + ref.setAtReferredType(referredType); + + + String expectedString = "class EndpointSpecificationRef {\n" + + " id: testId\n" + + " href: testHref\n" + + " isRoot: true\n" + + " name: null\n" + + " role: testRole\n" + + " connectionPointSpecification: null\n" + + " _atBaseType: testBaseType\n" + + " _atSchemaLocation: testSchemaLocation\n" + + " _atType: testType\n" + + " _atReferredType: testReferredType\n" + + "}"; + + assertEquals(expectedString, ref.toString()); + } + + @Test + void testToIndentedString() throws Exception { + EndpointSpecificationRef ref = new EndpointSpecificationRef(); + + Method method = EndpointSpecificationRef.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(ref, input); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/EntityRefTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/EntityRefTest.java new file mode 100644 index 0000000..156d8c9 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/EntityRefTest.java @@ -0,0 +1,143 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.EntityRef; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class EntityRefTest { + + @Test + void testEntityRef() { + EntityRef ref = new EntityRef(); + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.href(href); + ref.name(name); + ref._atReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(referredType, ref.getAtReferredType()); + + ref = new EntityRef(); + ref.setId(id); + ref.setUuid(id); + ref.setHref(href); + ref.setName(name); + ref.setAtReferredType(referredType); + + assertEquals(id, ref.getId()); + assertEquals(href, ref.getHref()); + assertEquals(name, ref.getName()); + assertEquals(referredType, ref.getAtReferredType()); + } + + @Test + void testEquals() { + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String referredType = "testReferredType"; + + EntityRef ref1 = new EntityRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setName(name); + ref1.setAtReferredType(referredType); + + EntityRef ref2 = new EntityRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setName(name); + ref2.setAtReferredType(referredType); + + assertTrue(ref1.equals(ref2)); + + ref1.id("differentId"); + + assertFalse(ref1.equals(ref2)); + } + + @Test + void testHashCode() { + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String referredType = "testReferredType"; + + EntityRef ref1 = new EntityRef(); + ref1.setUuid(id); + ref1.setHref(href); + ref1.setName(name); + ref1.setAtReferredType(referredType); + + EntityRef ref2 = new EntityRef(); + ref2.setUuid(id); + ref2.setHref(href); + ref2.setName(name); + ref2.setAtReferredType(referredType); + + assertEquals(ref1.hashCode(), ref2.hashCode()); + + ref1.id("differentId"); + ref1.setUuid("differentId"); + + assertNotEquals(ref1.hashCode(), ref2.hashCode()); + } + + @Test + void testToString() { + EntityRef ref = new EntityRef(); + + String id = "testId"; + String href = "testHref"; + String name = "testName"; + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + String referredType = "testReferredType"; + + ref.id(id); + ref.setUuid(id); + ref.setHref(href); + ref.name(name); + ref.setBaseType(baseType); + ref.setSchemaLocation(schemaLocation); + ref.setType(type); + ref.setAtReferredType(referredType); + + String expectedString = "class EntityRef {\n" + + " id: testId\n" + + " href: testHref\n" + + " name: testName\n" + + " baseType: testBaseType\n" + + " schemaLocation: testSchemaLocation\n" + + " type: testType\n" + + " _atReferredType: testReferredType\n" + + "}"; + + assertEquals(expectedString, ref.toString()); + } + + @Test + void testToIndentedString() throws Exception { + EntityRef ref = new EntityRef(); + + Method method = EntityRef.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(ref, input); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ErrorTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ErrorTest.java new file mode 100644 index 0000000..d567d60 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ErrorTest.java @@ -0,0 +1,179 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.Error; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class ErrorTest { + + @Test + void testError() { + Error error = new Error(); + String code = "testCode"; + String reason = "testReason"; + String message = "testMessage"; + String status = "testStatus"; + String referenceError = "testReferenceError"; + + error.code(code); + error.reason(reason); + error.message(message); + error.status(status); + error.referenceError(referenceError); + + assertEquals(code, error.getCode()); + assertEquals(reason, error.getReason()); + assertEquals(message, error.getMessage()); + assertEquals(status, error.getStatus()); + assertEquals(referenceError, error.getReferenceError()); + + error = new Error(); + error.setCode(code); + error.setReason(reason); + error.setMessage(message); + error.setStatus(status); + error.setReferenceError(referenceError); + + assertEquals(code, error.getCode()); + assertEquals(reason, error.getReason()); + assertEquals(message, error.getMessage()); + assertEquals(status, error.getStatus()); + assertEquals(referenceError, error.getReferenceError()); + } + + @Test + void testEquals() { + String code = "testCode"; + String reason = "testReason"; + String message = "testMessage"; + String status = "testStatus"; + String referenceError = "testReferenceError"; + + Error error1 = new Error(); + error1.setReason(reason); + error1.setMessage(message); + error1.setStatus(status); + error1.setReferenceError(referenceError); + + Error error2 = new Error(); + error2.setReason(reason); + error2.setMessage(message); + error2.setStatus(status); + error2.setReferenceError(referenceError); + + assertTrue(error1.equals(error2)); + + error1.code("differentCode"); + + assertFalse(error1.equals(error2)); + } + + @Test + void testHashCode() { + String code = "testCode"; + String reason = "testReason"; + String message = "testMessage"; + String status = "testStatus"; + String referenceError = "testReferenceError"; + + Error error1 = new Error(); + error1.setReason(reason); + error1.setMessage(message); + error1.setStatus(status); + error1.setReferenceError(referenceError); + + Error error2 = new Error(); + error2.setReason(reason); + error2.setMessage(message); + error2.setStatus(status); + error2.setReferenceError(referenceError); + + assertEquals(error1.hashCode(), error2.hashCode()); + + error1.code("differentCode"); + + assertNotEquals(error1.hashCode(), error2.hashCode()); + } + + @Test + void testToString() { + Error error = new Error(); + + String code = "testCode"; + String reason = "testReason"; + String message = "testMessage"; + String status = "testStatus"; + String referenceError = "testReferenceError"; + String baseType = "testBaseType"; + String schemaLocation = "testSchemaLocation"; + String type = "testType"; + + error.code(code); + error.reason(reason); + error.message(message); + error.status(status); + error.referenceError(referenceError); + + String expectedString = "class Error {\n" + + " code: testCode\n" + + " reason: testReason\n" + + " message: testMessage\n" + + " status: testStatus\n" + + " referenceError: testReferenceError\n" + + " _atBaseType: null\n" + + " _atSchemaLocation: null\n" + + " _atType: null\n" + + "}"; + + assertEquals(expectedString, error.toString()); + } + + @Test + void testToIndentedString() throws Exception { + Error error = new Error(); + + Method method = Error.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(error, input); + + assertEquals(expectedOutput, output); + } + + @Test + void testAtBaseType() { + Error error = new Error(); + String baseType = "testBaseType"; + error.setAtBaseType(baseType); + assertEquals(baseType, error.getAtBaseType()); + + Error newError = error._atBaseType("newTestBaseType"); + assertEquals(error, newError); + } + + @Test + void testAtSchemaLocation() { + Error error = new Error(); + String schemaLocation = "testSchemaLocation"; + error.setAtSchemaLocation(schemaLocation); + assertEquals(schemaLocation, error.getAtSchemaLocation()); + + Error newError = error._atSchemaLocation("newTestSchemaLocation"); + assertEquals(error, newError); + } + + @Test + void testAtType() { + Error error = new Error(); + String type = "testType"; + error.setAtType(type); + assertEquals(type, error.getAtType()); + + Error newError = error._atType("newTestType"); + assertEquals(error, newError); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionInputTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionInputTest.java new file mode 100644 index 0000000..b585830 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionInputTest.java @@ -0,0 +1,102 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.EventSubscriptionInput; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class EventSubscriptionInputTest { + + @Test + void testEventSubscriptionInput() { + EventSubscriptionInput input = new EventSubscriptionInput(); + String callback = "testCallback"; + String query = "testQuery"; + + input.callback(callback); + input.query(query); + + assertEquals(callback, input.getCallback()); + assertEquals(query, input.getQuery()); + + input = new EventSubscriptionInput(); + input.setCallback(callback); + input.setQuery(query); + + assertEquals(callback, input.getCallback()); + assertEquals(query, input.getQuery()); + } + + @Test + void testEquals() { + String callback = "testCallback"; + String query = "testQuery"; + + EventSubscriptionInput input1 = new EventSubscriptionInput(); + input1.setCallback(callback); + input1.setQuery(query); + + EventSubscriptionInput input2 = new EventSubscriptionInput(); + input2.setCallback(callback); + input2.setQuery(query); + + assertTrue(input1.equals(input2)); + + input1.callback("differentCallback"); + + assertFalse(input1.equals(input2)); + } + + @Test + void testHashCode() { + String callback = "testCallback"; + String query = "testQuery"; + + EventSubscriptionInput input1 = new EventSubscriptionInput(); + input1.setCallback(callback); + input1.setQuery(query); + + EventSubscriptionInput input2 = new EventSubscriptionInput(); + input2.setCallback(callback); + input2.setQuery(query); + + assertEquals(input1.hashCode(), input2.hashCode()); + + input1.callback("differentCallback"); + + assertNotEquals(input1.hashCode(), input2.hashCode()); + } + + @Test + void testToString() { + EventSubscriptionInput input = new EventSubscriptionInput(); + + String callback = "testCallback"; + String query = "testQuery"; + + input.callback(callback); + input.query(query); + + String expectedString = "class EventSubscriptionInput {\n" + + " callback: testCallback\n" + + " query: testQuery\n" + + "}"; + + assertEquals(expectedString, input.toString()); + } + + @Test + void testToIndentedString() throws Exception { + EventSubscriptionInput input = new EventSubscriptionInput(); + + Method method = EventSubscriptionInput.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String inputString = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(input, inputString); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionTest.java new file mode 100644 index 0000000..b28f7a8 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/EventSubscriptionTest.java @@ -0,0 +1,116 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.EventSubscription; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Method; + +public class EventSubscriptionTest { + + @Test + void testEventSubscription() { + EventSubscription subscription = new EventSubscription(); + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + + subscription.id(id); + subscription.callback(callback); + subscription.query(query); + + assertEquals(id, subscription.getId()); + assertEquals(callback, subscription.getCallback()); + assertEquals(query, subscription.getQuery()); + + subscription = new EventSubscription(); + subscription.setId(id); + subscription.setCallback(callback); + subscription.setQuery(query); + + assertEquals(id, subscription.getId()); + assertEquals(callback, subscription.getCallback()); + assertEquals(query, subscription.getQuery()); + } + + @Test + void testEquals() { + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + + EventSubscription subscription1 = new EventSubscription(); + subscription1.setId(id); + subscription1.setCallback(callback); + subscription1.setQuery(query); + + EventSubscription subscription2 = new EventSubscription(); + subscription2.setId(id); + subscription2.setCallback(callback); + subscription2.setQuery(query); + + assertTrue(subscription1.equals(subscription2)); + + subscription1.id("differentId"); + + assertFalse(subscription1.equals(subscription2)); + } + + @Test + void testHashCode() { + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + + EventSubscription subscription1 = new EventSubscription(); + subscription1.setId(id); + subscription1.setCallback(callback); + subscription1.setQuery(query); + + EventSubscription subscription2 = new EventSubscription(); + subscription2.setId(id); + subscription2.setCallback(callback); + subscription2.setQuery(query); + + assertEquals(subscription1.hashCode(), subscription2.hashCode()); + + subscription1.id("differentId"); + + assertNotEquals(subscription1.hashCode(), subscription2.hashCode()); + } + + @Test + void testToString() { + EventSubscription subscription = new EventSubscription(); + + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + + subscription.id(id); + subscription.callback(callback); + subscription.query(query); + + String expectedString = "class EventSubscription {\n" + + " id: testId\n" + + " callback: testCallback\n" + + " query: testQuery\n" + + "}"; + + assertEquals(expectedString, subscription.toString()); + } + + @Test + void testToIndentedString() throws Exception { + EventSubscription subscription = new EventSubscription(); + + Method method = EventSubscription.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(subscription, input); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventPayloadTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventPayloadTest.java new file mode 100644 index 0000000..87641b1 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventPayloadTest.java @@ -0,0 +1,87 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ExportJob; +import org.etsi.osl.tmf.rcm634.model.ExportJobCreateEventPayload; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import static org.junit.jupiter.api.Assertions.*; + +public class ExportJobCreateEventPayloadTest { + + @Test + void testExportJobCreateEventPayload() { + ExportJobCreateEventPayload payload = new ExportJobCreateEventPayload(); + ExportJob job = new ExportJob(); + job.setId("testId"); + + payload.exportJob(job); + assertEquals(job, payload.getExportJob()); + + payload = new ExportJobCreateEventPayload(); + payload.setExportJob(job); + assertEquals(job, payload.getExportJob()); + } + + @Test + void testEquals() { + ExportJobCreateEventPayload payload1 = new ExportJobCreateEventPayload(); + ExportJob job = new ExportJob(); + job.setId("testId"); + payload1.setExportJob(job); + + ExportJobCreateEventPayload payload2 = new ExportJobCreateEventPayload(); + payload2.setExportJob(job); + + assertTrue(payload1.equals(payload2)); + + payload1.setExportJob(new ExportJob()); + + assertFalse(payload1.equals(payload2)); + } + + @Test + void testHashCode() { + ExportJobCreateEventPayload payload1 = new ExportJobCreateEventPayload(); + ExportJob job = new ExportJob(); + job.setId("testId"); + payload1.setExportJob(job); + + ExportJobCreateEventPayload payload2 = new ExportJobCreateEventPayload(); + payload2.setExportJob(job); + + assertEquals(payload1.hashCode(), payload2.hashCode()); + + payload1.setExportJob(new ExportJob()); + + assertNotEquals(payload1.hashCode(), payload2.hashCode()); + } + + @Test + void testToString() { + ExportJobCreateEventPayload payload = new ExportJobCreateEventPayload(); + ExportJob job = new ExportJob(); + job.setId("testId"); + payload.setExportJob(job); + + String expectedString = "class ExportJobCreateEventPayload {\n" + + " exportJob: class ExportJob {\n" + + " id: testId\n" + + " href: null\n" + + " completionDate: null\n" + + " contentType: null\n" + + " creationDate: null\n" + + " errorLog: null\n" + + " path: null\n" + + " query: null\n" + + " url: null\n" + + " status: null\n" + + " _atBaseType: null\n" + + " _atSchemaLocation: null\n" + + " _atType: null\n" + + " }\n" + + "}"; + + assertEquals(expectedString, payload.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventTest.java new file mode 100644 index 0000000..1c6eb60 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateEventTest.java @@ -0,0 +1,180 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ExportJobCreateEvent; +import org.etsi.osl.tmf.rcm634.model.ExportJobCreateEventPayload; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.time.OffsetDateTime; +import static org.junit.jupiter.api.Assertions.*; + +public class ExportJobCreateEventTest { + + @Test + void testExportJobCreateEvent() { + ExportJobCreateEvent event = new ExportJobCreateEvent(); + String id = "testId"; + String href = "testHref"; + String eventId = "testEventId"; + OffsetDateTime eventTime = OffsetDateTime.now(); + String eventType = "testEventType"; + String correlationId = "testCorrelationId"; + String domain = "testDomain"; + String title = "testTitle"; + String description = "testDescription"; + String priority = "testPriority"; + OffsetDateTime timeOccurred = OffsetDateTime.now(); + ExportJobCreateEventPayload eventPayload = new ExportJobCreateEventPayload(); + + event.id(id); + event.href(href); + event.eventId(eventId); + event.eventTime(eventTime); + event.eventType(eventType); + event.correlationId(correlationId); + event.domain(domain); + event.title(title); + event.description(description); + event.priority(priority); + event.timeOcurred(timeOccurred); + event.event(eventPayload); + + assertEquals(id, event.getId()); + assertEquals(href, event.getHref()); + assertEquals(eventId, event.getEventId()); + assertEquals(eventTime, event.getEventTime()); + assertEquals(eventType, event.getEventType()); + assertEquals(correlationId, event.getCorrelationId()); + assertEquals(domain, event.getDomain()); + assertEquals(title, event.getTitle()); + assertEquals(description, event.getDescription()); + assertEquals(priority, event.getPriority()); + assertEquals(timeOccurred, event.getTimeOcurred()); + assertEquals(eventPayload, event.getEvent()); + } + + @Test + void testEquals() { + ExportJobCreateEvent event1 = new ExportJobCreateEvent(); + event1.setId("testId"); + event1.setHref("testHref"); + event1.setEventId("testEventId"); + event1.setEventTime(OffsetDateTime.now()); + event1.setEventType("testEventType"); + event1.setCorrelationId("testCorrelationId"); + event1.setDomain("testDomain"); + event1.setTitle("testTitle"); + event1.setDescription("testDescription"); + event1.setPriority("testPriority"); + event1.setTimeOcurred(OffsetDateTime.now()); + event1.setEvent(new ExportJobCreateEventPayload()); + + ExportJobCreateEvent event2 = new ExportJobCreateEvent(); + event2.setId("testId"); + event2.setHref("testHref"); + event2.setEventId("testEventId"); + event2.setEventTime(event1.getEventTime()); + event2.setEventType("testEventType"); + event2.setCorrelationId("testCorrelationId"); + event2.setDomain("testDomain"); + event2.setTitle("testTitle"); + event2.setDescription("testDescription"); + event2.setPriority("testPriority"); + event2.setTimeOcurred(event1.getTimeOcurred()); + event2.setEvent(new ExportJobCreateEventPayload()); + + assertTrue(event1.equals(event2)); + + event1.setId("differentId"); + + assertFalse(event1.equals(event2)); + } + + @Test + void testHashCode() { + ExportJobCreateEvent event1 = new ExportJobCreateEvent(); + event1.setId("testId"); + event1.setHref("testHref"); + event1.setEventId("testEventId"); + event1.setEventTime(OffsetDateTime.now()); + event1.setEventType("testEventType"); + event1.setCorrelationId("testCorrelationId"); + event1.setDomain("testDomain"); + event1.setTitle("testTitle"); + event1.setDescription("testDescription"); + event1.setPriority("testPriority"); + event1.setTimeOcurred(OffsetDateTime.now()); + event1.setEvent(new ExportJobCreateEventPayload()); + + ExportJobCreateEvent event2 = new ExportJobCreateEvent(); + event2.setId("testId"); + event2.setHref("testHref"); + event2.setEventId("testEventId"); + event2.setEventTime(event1.getEventTime()); + event2.setEventType("testEventType"); + event2.setCorrelationId("testCorrelationId"); + event2.setDomain("testDomain"); + event2.setTitle("testTitle"); + event2.setDescription("testDescription"); + event2.setPriority("testPriority"); + event2.setTimeOcurred(event1.getTimeOcurred()); + event2.setEvent(new ExportJobCreateEventPayload()); + + assertEquals(event1.hashCode(), event2.hashCode()); + + event1.setId("differentId"); + + assertNotEquals(event1.hashCode(), event2.hashCode()); + } + + @Test + void testToString() { + ExportJobCreateEvent event = new ExportJobCreateEvent(); + event.setId("testId"); + event.setHref("testHref"); + event.setEventId("testEventId"); + event.setEventTime(OffsetDateTime.parse("2024-01-19T17:02:03.289504717+02:00")); + event.setEventType("testEventType"); + event.setCorrelationId("testCorrelationId"); + event.setDomain("testDomain"); + event.setTitle("testTitle"); + event.setDescription("testDescription"); + event.setPriority("testPriority"); + event.setTimeOcurred(OffsetDateTime.parse("2024-01-19T17:02:03.289527665+02:00")); + event.setEvent(new ExportJobCreateEventPayload()); + + String expectedString = "class ExportJobCreateEvent {\n" + + " id: testId\n" + + " href: testHref\n" + + " eventId: testEventId\n" + + " eventTime: 2024-01-19T17:02:03.289504717+02:00\n" + + " eventType: testEventType\n" + + " correlationId: testCorrelationId\n" + + " domain: testDomain\n" + + " title: testTitle\n" + + " description: testDescription\n" + + " priority: testPriority\n" + + " timeOcurred: 2024-01-19T17:02:03.289527665+02:00\n" + + " event: class ExportJobCreateEventPayload {\n" + + " exportJob: null\n" + + " }\n" + + "}"; + + assertEquals(expectedString, event.toString()); + } + + @Test + void testToIndentedString() throws Exception { + ExportJobCreateEvent event = new ExportJobCreateEvent(); + + Method method = ExportJobCreateEvent.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(event, input); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateTest.java new file mode 100644 index 0000000..8333fe9 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobCreateTest.java @@ -0,0 +1,231 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ExportJobCreate; +import org.etsi.osl.tmf.rcm634.model.JobStateType; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.time.OffsetDateTime; +import static org.junit.jupiter.api.Assertions.*; + +public class ExportJobCreateTest { + + @Test + void testExportJobCreate() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String query = "testQuery"; + OffsetDateTime completionDate = OffsetDateTime.now(); + OffsetDateTime creationDate = OffsetDateTime.now(); + String contentType = "testContentType"; + String errorLog = "testErrorLog"; + String path = "testPath"; + String url = "testUrl"; + + jobCreate.query(query); + jobCreate.setCompletionDate(completionDate); + jobCreate.setCreationDate(creationDate); + jobCreate.setContentType(contentType); + jobCreate.setErrorLog(errorLog); + jobCreate.setPath(path); + jobCreate.setUrl(url); + + assertEquals(query, jobCreate.getQuery()); + assertEquals(completionDate, jobCreate.getCompletionDate()); + assertEquals(creationDate, jobCreate.getCreationDate()); + assertEquals(contentType, jobCreate.getContentType()); + assertEquals(errorLog, jobCreate.getErrorLog()); + assertEquals(path, jobCreate.getPath()); + assertEquals(url, jobCreate.getUrl()); + + jobCreate = new ExportJobCreate(); + jobCreate.setQuery(query); + jobCreate.setCompletionDate(completionDate); + jobCreate.setCreationDate(creationDate); + jobCreate.setContentType(contentType); + jobCreate.setErrorLog(errorLog); + jobCreate.setPath(path); + jobCreate.setUrl(url); + + assertEquals(query, jobCreate.getQuery()); + assertEquals(completionDate, jobCreate.getCompletionDate()); + assertEquals(creationDate, jobCreate.getCreationDate()); + assertEquals(contentType, jobCreate.getContentType()); + assertEquals(errorLog, jobCreate.getErrorLog()); + assertEquals(path, jobCreate.getPath()); + assertEquals(url, jobCreate.getUrl()); + } + + @Test + void testStatus() { + ExportJobCreate jobCreate = new ExportJobCreate(); + JobStateType status = JobStateType.RUNNING; + jobCreate.setStatus(status); + assertEquals(status, jobCreate.getStatus()); + + ExportJobCreate newJobCreate = jobCreate.status(JobStateType.FAILED); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testAtBaseType() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String baseType = "testBaseType"; + jobCreate.setAtBaseType(baseType); + assertEquals(baseType, jobCreate.getAtBaseType()); + + ExportJobCreate newJobCreate = jobCreate._atBaseType("newTestBaseType"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testAtSchemaLocation() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String schemaLocation = "testSchemaLocation"; + jobCreate.setAtSchemaLocation(schemaLocation); + assertEquals(schemaLocation, jobCreate.getAtSchemaLocation()); + + ExportJobCreate newJobCreate = jobCreate._atSchemaLocation("newTestSchemaLocation"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testAtType() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String type = "testType"; + jobCreate.setAtType(type); + assertEquals(type, jobCreate.getAtType()); + + ExportJobCreate newJobCreate = jobCreate._atType("newTestType"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testErrorLog() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String errorLog = "testErrorLog"; + jobCreate.setErrorLog(errorLog); + assertEquals(errorLog, jobCreate.getErrorLog()); + + ExportJobCreate newJobCreate = jobCreate.errorLog("newTestErrorLog"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testPath() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String path = "testPath"; + jobCreate.setPath(path); + assertEquals(path, jobCreate.getPath()); + + ExportJobCreate newJobCreate = jobCreate.path("newTestPath"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testUrl() { + ExportJobCreate jobCreate = new ExportJobCreate(); + String url = "testUrl"; + jobCreate.setUrl(url); + assertEquals(url, jobCreate.getUrl()); + + ExportJobCreate newJobCreate = jobCreate.url("newTestUrl"); + assertEquals(jobCreate, newJobCreate); + } + + @Test + void testEquals() { + ExportJobCreate jobCreate1 = new ExportJobCreate(); + jobCreate1.setQuery("testQuery"); + jobCreate1.setCompletionDate(OffsetDateTime.now()); + jobCreate1.setCreationDate(OffsetDateTime.now()); + jobCreate1.setContentType("testContentType"); + jobCreate1.setErrorLog("testErrorLog"); + jobCreate1.setPath("testPath"); + jobCreate1.setUrl("testUrl"); + + ExportJobCreate jobCreate2 = new ExportJobCreate(); + jobCreate2.setQuery("testQuery"); + jobCreate2.setCompletionDate(jobCreate1.getCompletionDate()); + jobCreate2.setCreationDate(jobCreate1.getCreationDate()); + jobCreate2.setContentType("testContentType"); + jobCreate2.setErrorLog("testErrorLog"); + jobCreate2.setPath("testPath"); + jobCreate2.setUrl("testUrl"); + + assertTrue(jobCreate1.equals(jobCreate2)); + + jobCreate1.setQuery("differentQuery"); + + assertFalse(jobCreate1.equals(jobCreate2)); + } + + @Test + void testHashCode() { + ExportJobCreate jobCreate1 = new ExportJobCreate(); + jobCreate1.setQuery("testQuery"); + jobCreate1.setCompletionDate(OffsetDateTime.now()); + jobCreate1.setCreationDate(OffsetDateTime.now()); + jobCreate1.setContentType("testContentType"); + jobCreate1.setErrorLog("testErrorLog"); + jobCreate1.setPath("testPath"); + jobCreate1.setUrl("testUrl"); + + ExportJobCreate jobCreate2 = new ExportJobCreate(); + jobCreate2.setQuery("testQuery"); + jobCreate2.setCompletionDate(jobCreate1.getCompletionDate()); + jobCreate2.setCreationDate(jobCreate1.getCreationDate()); + jobCreate2.setContentType("testContentType"); + jobCreate2.setErrorLog("testErrorLog"); + jobCreate2.setPath("testPath"); + jobCreate2.setUrl("testUrl"); + + assertEquals(jobCreate1.hashCode(), jobCreate2.hashCode()); + + jobCreate1.setQuery("differentQuery"); + + assertNotEquals(jobCreate1.hashCode(), jobCreate2.hashCode()); + } + + @Test + void testToString() { + ExportJobCreate jobCreate = new ExportJobCreate(); + jobCreate.setQuery("testQuery"); + jobCreate.setCompletionDate(OffsetDateTime.parse("2024-01-19T17:02:03.289504717+02:00")); + jobCreate.setCreationDate(OffsetDateTime.parse("2024-01-19T17:02:03.289527665+02:00")); + jobCreate.setContentType("testContentType"); + jobCreate.setErrorLog("testErrorLog"); + jobCreate.setPath("testPath"); + jobCreate.setUrl("testUrl"); + + String expectedString = "class ExportJobCreate {\n" + + " completionDate: 2024-01-19T17:02:03.289504717+02:00\n" + + " contentType: testContentType\n" + + " creationDate: 2024-01-19T17:02:03.289527665+02:00\n" + + " errorLog: testErrorLog\n" + + " path: testPath\n" + + " query: testQuery\n" + + " url: testUrl\n" + + " status: null\n" + + " _atBaseType: null\n" + + " _atSchemaLocation: null\n" + + " _atType: null\n" + + "}"; + + assertEquals(expectedString, jobCreate.toString()); + } + + @Test + void testToIndentedString() throws Exception { + ExportJobCreate jobCreate = new ExportJobCreate(); + + Method method = ExportJobCreate.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(jobCreate, input); + + assertEquals(expectedOutput, output); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobStateChangeEventTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobStateChangeEventTest.java new file mode 100644 index 0000000..d8bb2bb --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobStateChangeEventTest.java @@ -0,0 +1,196 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ExportJob; +import org.etsi.osl.tmf.rcm634.model.ExportJobStateChangeEvent; +import org.etsi.osl.tmf.rcm634.model.ExportJobStateChangeEventPayload; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.time.OffsetDateTime; +import static org.junit.jupiter.api.Assertions.*; + +public class ExportJobStateChangeEventTest { + + @Test + void testExportJobStateChangeEvent() { + ExportJobStateChangeEvent event = new ExportJobStateChangeEvent(); + String id = "testId"; + String href = "testHref"; + String eventId = "testEventId"; + OffsetDateTime eventTime = OffsetDateTime.now(); + String eventType = "testEventType"; + String correlationId = "testCorrelationId"; + String domain = "testDomain"; + String title = "testTitle"; + String description = "testDescription"; + String priority = "testPriority"; + OffsetDateTime timeOccurred = OffsetDateTime.now(); + ExportJobStateChangeEventPayload eventPayload = new ExportJobStateChangeEventPayload(); + + event.id(id); + event.href(href); + event.eventId(eventId); + event.eventTime(eventTime); + event.eventType(eventType); + event.correlationId(correlationId); + event.domain(domain); + event.title(title); + event.description(description); + event.priority(priority); + event.timeOcurred(timeOccurred); + event.event(eventPayload); + + assertEquals(id, event.getId()); + assertEquals(href, event.getHref()); + assertEquals(eventId, event.getEventId()); + assertEquals(eventTime, event.getEventTime()); + assertEquals(eventType, event.getEventType()); + assertEquals(correlationId, event.getCorrelationId()); + assertEquals(domain, event.getDomain()); + assertEquals(title, event.getTitle()); + assertEquals(description, event.getDescription()); + assertEquals(priority, event.getPriority()); + assertEquals(timeOccurred, event.getTimeOcurred()); + assertEquals(eventPayload, event.getEvent()); + } + + @Test + void testEquals() { + ExportJobStateChangeEvent event1 = new ExportJobStateChangeEvent(); + event1.setId("testId"); + event1.setHref("testHref"); + event1.setEventId("testEventId"); + event1.setEventTime(OffsetDateTime.now()); + event1.setEventType("testEventType"); + event1.setCorrelationId("testCorrelationId"); + event1.setDomain("testDomain"); + event1.setTitle("testTitle"); + event1.setDescription("testDescription"); + event1.setPriority("testPriority"); + event1.setTimeOcurred(OffsetDateTime.now()); + event1.setEvent(new ExportJobStateChangeEventPayload()); + + ExportJobStateChangeEvent event2 = new ExportJobStateChangeEvent(); + event2.setId("testId"); + event2.setHref("testHref"); + event2.setEventId("testEventId"); + event2.setEventTime(event1.getEventTime()); + event2.setEventType("testEventType"); + event2.setCorrelationId("testCorrelationId"); + event2.setDomain("testDomain"); + event2.setTitle("testTitle"); + event2.setDescription("testDescription"); + event2.setPriority("testPriority"); + event2.setTimeOcurred(event1.getTimeOcurred()); + event2.setEvent(new ExportJobStateChangeEventPayload()); + + assertTrue(event1.equals(event2)); + + event1.setId("differentId"); + + assertFalse(event1.equals(event2)); + } + + @Test + void testHashCode() { + ExportJobStateChangeEvent event1 = new ExportJobStateChangeEvent(); + event1.setId("testId"); + event1.setHref("testHref"); + event1.setEventId("testEventId"); + event1.setEventTime(OffsetDateTime.now()); + event1.setEventType("testEventType"); + event1.setCorrelationId("testCorrelationId"); + event1.setDomain("testDomain"); + event1.setTitle("testTitle"); + event1.setDescription("testDescription"); + event1.setPriority("testPriority"); + event1.setTimeOcurred(OffsetDateTime.now()); + event1.setEvent(new ExportJobStateChangeEventPayload()); + + ExportJobStateChangeEvent event2 = new ExportJobStateChangeEvent(); + event2.setId("testId"); + event2.setHref("testHref"); + event2.setEventId("testEventId"); + event2.setEventTime(event1.getEventTime()); + event2.setEventType("testEventType"); + event2.setCorrelationId("testCorrelationId"); + event2.setDomain("testDomain"); + event2.setTitle("testTitle"); + event2.setDescription("testDescription"); + event2.setPriority("testPriority"); + event2.setTimeOcurred(event1.getTimeOcurred()); + event2.setEvent(new ExportJobStateChangeEventPayload()); + + assertEquals(event1.hashCode(), event2.hashCode()); + + event1.setId("differentId"); + + assertNotEquals(event1.hashCode(), event2.hashCode()); + } + + @Test + void testToString() { + ExportJobStateChangeEvent event = new ExportJobStateChangeEvent(); + event.setId("testId"); + event.setHref("testHref"); + event.setEventId("testEventId"); + event.setEventTime(OffsetDateTime.parse("2024-01-19T17:02:03.289504717+02:00")); + event.setEventType("testEventType"); + event.setCorrelationId("testCorrelationId"); + event.setDomain("testDomain"); + event.setTitle("testTitle"); + event.setDescription("testDescription"); + event.setPriority("testPriority"); + event.setTimeOcurred(OffsetDateTime.parse("2024-01-19T17:02:03.289527665+02:00")); + event.setEvent(new ExportJobStateChangeEventPayload()); + + String expectedString = "class ExportJobStateChangeEvent {\n" + + " id: testId\n" + + " href: testHref\n" + + " eventId: testEventId\n" + + " eventTime: 2024-01-19T17:02:03.289504717+02:00\n" + + " eventType: testEventType\n" + + " correlationId: testCorrelationId\n" + + " domain: testDomain\n" + + " title: testTitle\n" + + " description: testDescription\n" + + " priority: testPriority\n" + + " timeOcurred: 2024-01-19T17:02:03.289527665+02:00\n" + + " event: class ExportJobStateChangeEventPayload {\n" + + " exportJob: null\n" + + " }\n" + + "}"; + + assertEquals(expectedString, event.toString()); + } + + @Test + void testToIndentedString() throws Exception { + ExportJobStateChangeEvent event = new ExportJobStateChangeEvent(); + + Method method = ExportJobStateChangeEvent.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(event, input); + + assertEquals(expectedOutput, output); + } + + @Test + void testExportJob() { + ExportJobStateChangeEventPayload payload = new ExportJobStateChangeEventPayload(); + ExportJob job = new ExportJob(); + job.setId("testId"); + payload.setExportJob(job); + assertEquals(job, payload.getExportJob()); + + ExportJob newJob = new ExportJob(); + newJob.setId("newTestId"); + ExportJobStateChangeEventPayload newPayload = payload.exportJob(newJob); + assertEquals(payload, newPayload); + assertEquals(newJob, payload.getExportJob()); + } +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobTest.java new file mode 100644 index 0000000..07567d6 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ExportJobTest.java @@ -0,0 +1,266 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.rcm634.model.ExportJob; +import org.etsi.osl.tmf.rcm634.model.JobStateType; +import org.junit.jupiter.api.Test; +import java.time.OffsetDateTime; +import static org.junit.jupiter.api.Assertions.*; + +public class ExportJobTest { + + @Test + void testExportJob() { + ExportJob job = new ExportJob(); + String id = "testId"; + String query = "testQuery"; + OffsetDateTime completionDate = OffsetDateTime.now(); + OffsetDateTime creationDate = OffsetDateTime.now(); + String contentType = "testContentType"; + String errorLog = "testErrorLog"; + String path = "testPath"; + String url = "testUrl"; + + job.id(id); + job.query(query); + job.setCompletionDate(completionDate); + job.setCreationDate(creationDate); + job.setContentType(contentType); + job.setErrorLog(errorLog); + job.setPath(path); + job.setUrl(url); + + assertEquals(id, job.getId()); + assertEquals(query, job.getQuery()); + assertEquals(completionDate, job.getCompletionDate()); + assertEquals(creationDate, job.getCreationDate()); + assertEquals(contentType, job.getContentType()); + assertEquals(errorLog, job.getErrorLog()); + assertEquals(path, job.getPath()); + assertEquals(url, job.getUrl()); + + job = new ExportJob(); + job.setId(id); + job.setQuery(query); + job.setCompletionDate(completionDate); + job.setCreationDate(creationDate); + job.setContentType(contentType); + job.setErrorLog(errorLog); + job.setPath(path); + job.setUrl(url); + + assertEquals(id, job.getId()); + assertEquals(query, job.getQuery()); + assertEquals(completionDate, job.getCompletionDate()); + assertEquals(creationDate, job.getCreationDate()); + assertEquals(contentType, job.getContentType()); + assertEquals(errorLog, job.getErrorLog()); + assertEquals(path, job.getPath()); + assertEquals(url, job.getUrl()); + } + + @Test + void testEquals() { + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + OffsetDateTime completionDate = OffsetDateTime.now(); + OffsetDateTime creationDate = OffsetDateTime.now(); + String contentType = "testContentType"; + String errorLog = "testErrorLog"; + String path = "testPath"; + String url = "testUrl"; + + ExportJob job1 = new ExportJob(); + job1.setId(id); + job1.setQuery(query); + job1.setCompletionDate(completionDate); + job1.setCreationDate(creationDate); + job1.setContentType(contentType); + job1.setErrorLog(errorLog); + job1.setPath(path); + job1.setUrl(url); + + ExportJob job2 = new ExportJob(); + job2.setId(id); + job2.setQuery(query); + job2.setCompletionDate(completionDate); + job2.setCreationDate(creationDate); + job2.setContentType(contentType); + job2.setErrorLog(errorLog); + job2.setPath(path); + job2.setUrl(url); + + assertTrue(job1.equals(job2)); + + job1.id("differentId"); + + assertFalse(job1.equals(job2)); + } + + @Test + void testHashCode() { + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + OffsetDateTime completionDate = OffsetDateTime.now(); + OffsetDateTime creationDate = OffsetDateTime.now(); + String contentType = "testContentType"; + String errorLog = "testErrorLog"; + String path = "testPath"; + String url = "testUrl"; + + ExportJob job1 = new ExportJob(); + job1.setId(id); + job1.setQuery(query); + job1.setCompletionDate(completionDate); + job1.setCreationDate(creationDate); + job1.setContentType(contentType); + job1.setErrorLog(errorLog); + job1.setPath(path); + job1.setUrl(url); + + ExportJob job2 = new ExportJob(); + job2.setId(id); + job2.setQuery(query); + job2.setCompletionDate(completionDate); + job2.setCreationDate(creationDate); + job2.setContentType(contentType); + job2.setErrorLog(errorLog); + job2.setPath(path); + job2.setUrl(url); + + assertEquals(job1.hashCode(), job2.hashCode()); + + job1.id("differentId"); + + assertNotEquals(job1.hashCode(), job2.hashCode()); + } + + @Test + void testToString() { + ExportJob job = new ExportJob(); + + String id = "testId"; + String callback = "testCallback"; + String query = "testQuery"; + OffsetDateTime completionDate = OffsetDateTime.parse("2024-01-19T16:35:07.730526064+02:00"); + OffsetDateTime creationDate = OffsetDateTime.parse("2024-01-19T16:35:07.731493435+02:00"); + String contentType = "testContentType"; + String errorLog = "testErrorLog"; + String path = "testPath"; + String url = "testUrl"; + + job.id(id); + job.query(query); + job.setCompletionDate(completionDate); + job.setCreationDate(creationDate); + job.setContentType(contentType); + job.setErrorLog(errorLog); + job.setPath(path); + job.setUrl(url); + + String expectedString = "class ExportJob {\n" + + " id: testId\n" + + " href: null\n" + + " completionDate: 2024-01-19T16:35:07.730526064+02:00\n" + + " contentType: testContentType\n" + + " creationDate: 2024-01-19T16:35:07.731493435+02:00\n" + + " errorLog: testErrorLog\n" + + " path: testPath\n" + + " query: testQuery\n" + + " url: testUrl\n" + + " status: null\n" + + " _atBaseType: null\n" + + " _atSchemaLocation: null\n" + + " _atType: null\n" + + "}"; + + assertEquals(expectedString, job.toString()); + } + + @Test + void testHref() { + ExportJob job = new ExportJob(); + String href = "testHref"; + job.setHref(href); + assertEquals(href, job.getHref()); + + ExportJob newJob = job.href("newTestHref"); + assertEquals(job, newJob); + } + + @Test + void testCompletionDate() { + ExportJob job = new ExportJob(); + OffsetDateTime completionDate = OffsetDateTime.now(); + job.setCompletionDate(completionDate); + assertEquals(completionDate, job.getCompletionDate()); + + ExportJob newJob = job.completionDate(OffsetDateTime.now()); + assertEquals(job, newJob); + } + + @Test + void testContentType() { + ExportJob job = new ExportJob(); + String contentType = "testContentType"; + job.setContentType(contentType); + assertEquals(contentType, job.getContentType()); + + ExportJob newJob = job.contentType("newTestContentType"); + assertEquals(job, newJob); + } + + @Test + void testCreationDate() { + ExportJob job = new ExportJob(); + OffsetDateTime creationDate = OffsetDateTime.now(); + job.setCreationDate(creationDate); + assertEquals(creationDate, job.getCreationDate()); + } + + @Test + void testStatus() { + ExportJob job = new ExportJob(); + JobStateType status = JobStateType.RUNNING; + job.setStatus(status); + assertEquals(status, job.getStatus()); + + ExportJob newJob = job.status(JobStateType.FAILED); + assertEquals(job, newJob); + } + + @Test + void testAtBaseType() { + ExportJob job = new ExportJob(); + String baseType = "testBaseType"; + job.setAtBaseType(baseType); + assertEquals(baseType, job.getAtBaseType()); + + ExportJob newJob = job._atBaseType("newTestBaseType"); + assertEquals(job, newJob); + } + + @Test + void testAtSchemaLocation() { + ExportJob job = new ExportJob(); + String schemaLocation = "testSchemaLocation"; + job.setAtSchemaLocation(schemaLocation); + assertEquals(schemaLocation, job.getAtSchemaLocation()); + + ExportJob newJob = job._atSchemaLocation("newTestSchemaLocation"); + assertEquals(job, newJob); + } + + @Test + void testAtType() { + ExportJob job = new ExportJob(); + String type = "testType"; + job.setAtType(type); + assertEquals(type, job.getAtType()); + + ExportJob newJob = job._atType("newTestType"); + assertEquals(job, newJob); + } + +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationTest.java new file mode 100644 index 0000000..a767b11 --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationTest.java @@ -0,0 +1,422 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; +import org.etsi.osl.tmf.prm669.model.RelatedParty; +import org.etsi.osl.tmf.rcm634.model.*; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +public class ResourceSpecificationTest { + + static class TestResourceSpecification extends ResourceSpecification { + // You can add additional methods or override existing ones if needed + } + + @Test + void testResourceSpecification() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + + String id = "testId"; + Boolean isBundle = true; + String category = "testCategory"; + + resourceSpec.setUuid(id); + resourceSpec.setIsBundle(isBundle); + resourceSpec.setCategory(category); + + assertEquals(id, resourceSpec.getId()); + assertEquals(isBundle, resourceSpec.isIsBundle()); + assertEquals(category, resourceSpec.getCategory()); + } + + @Test + void testEquals() { + ResourceSpecification resourceSpec1 = new TestResourceSpecification(); + resourceSpec1.setUuid("testId"); + resourceSpec1.getId(); + resourceSpec1.setIsBundle(true); + resourceSpec1.setCategory("testCategory"); + + ResourceSpecification resourceSpec2 = new TestResourceSpecification(); + resourceSpec2.setUuid("testId"); + resourceSpec2.getId(); + resourceSpec2.setIsBundle(true); + resourceSpec2.setCategory("testCategory"); + + assertTrue(resourceSpec1.equals(resourceSpec2)); + + resourceSpec1.setUuid("differentId"); + resourceSpec1.getId(); + + assertFalse(resourceSpec1.equals(resourceSpec2)); + } + + @Test + void testHashCode() { + ResourceSpecification resourceSpec1 = new TestResourceSpecification(); + resourceSpec1.setUuid("testId"); + resourceSpec1.getId(); + resourceSpec1.setIsBundle(true); + resourceSpec1.setCategory("testCategory"); + + ResourceSpecification resourceSpec2 = new TestResourceSpecification(); + resourceSpec2.setUuid("testId"); + resourceSpec2.getId(); + resourceSpec2.setIsBundle(true); + resourceSpec2.setCategory("testCategory"); + + assertEquals(resourceSpec1.hashCode(), resourceSpec2.hashCode()); + + resourceSpec1.setUuid("differentId"); + resourceSpec1.getId(); + + assertNotEquals(resourceSpec1.hashCode(), resourceSpec2.hashCode()); + } + + @Test + void testHref() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String href = "testHref"; + resourceSpec.setHref(href); + assertEquals(href, resourceSpec.getHref()); + + ResourceSpecification newResourceSpec = resourceSpec.href("newTestHref"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testCategory() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String category = "testCategory"; + resourceSpec.setCategory(category); + assertEquals(category, resourceSpec.getCategory()); + + ResourceSpecification newResourceSpec = resourceSpec.category("newTestCategory"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testDescription() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String description = "testDescription"; + resourceSpec.setDescription(description); + assertEquals(description, resourceSpec.getDescription()); + + ResourceSpecification newResourceSpec = resourceSpec.description("newTestDescription"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testIsBundle() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Boolean isBundle = true; + resourceSpec.setIsBundle(isBundle); + assertEquals(isBundle, resourceSpec.isIsBundle()); + + ResourceSpecification newResourceSpec = resourceSpec.isBundle(false); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testLifecycleStatus() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String lifecycleStatus = "In study"; + resourceSpec.setLifecycleStatus(lifecycleStatus); + assertEquals(lifecycleStatus, resourceSpec.getLifecycleStatus()); + + ResourceSpecification newResourceSpec = resourceSpec.lifecycleStatus("In use"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testName() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String name = "testName"; + resourceSpec.setName(name); + assertEquals(name, resourceSpec.getName()); + + ResourceSpecification newResourceSpec = resourceSpec.name("newTestName"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testSettersAndGetters() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + + // Test id + String id = "testId"; + resourceSpec.setUuid(id); + resourceSpec.getId(); + assertEquals(id, resourceSpec.getId()); + + // Test href + String href = "testHref"; + resourceSpec.setHref(href); + assertEquals(href, resourceSpec.getHref()); + + // Test category + String category = "testCategory"; + resourceSpec.setCategory(category); + assertEquals(category, resourceSpec.getCategory()); + + // Test description + String description = "testDescription"; + resourceSpec.setDescription(description); + assertEquals(description, resourceSpec.getDescription()); + + // Test isBundle + Boolean isBundle = true; + resourceSpec.setIsBundle(isBundle); + assertEquals(isBundle, resourceSpec.isIsBundle()); + + // Test lifecycleStatus + String lifecycleStatus = "In study"; + resourceSpec.setLifecycleStatus(lifecycleStatus); + assertEquals(lifecycleStatus, resourceSpec.getLifecycleStatus()); + + // Test name + String name = "testName"; + resourceSpec.setName(name); + assertEquals(name, resourceSpec.getName()); + + // Test version + String version = "testVersion"; + resourceSpec.setVersion(version); + assertEquals(version, resourceSpec.getVersion()); + + // Test baseType + String baseType = "BaseRootEntity"; + resourceSpec.setBaseType(baseType); + assertEquals(baseType, resourceSpec.getBaseType()); + + // Test schemaLocation + String schemaLocation = "testSchemaLocation"; + resourceSpec.setAtSchemaLocation(schemaLocation); + assertEquals(schemaLocation, resourceSpec.getAtSchemaLocation()); + + // Test type + String type = "BaseEntity"; + resourceSpec.setType(type); + assertEquals(type, resourceSpec.getType()); + } + @Test + void testToString() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + resourceSpec.setUuid("testId"); + resourceSpec.getId(); + resourceSpec.setIsBundle(true); + resourceSpec.setCategory("testCategory"); + resourceSpec.setHref("testHref"); + resourceSpec.setLifecycleStatus("In study"); + resourceSpec.setName("testName"); + resourceSpec.setVersion("testVersion"); + resourceSpec.setBaseType("BaseRootEntity"); + resourceSpec.setAtSchemaLocation("testSchemaLocation"); + resourceSpec.setType("BaseEntity"); + + String expectedString = "class ResourceSpecification {\n" + + " id: testId\n" + + " href: testHref\n" + + " category: testCategory\n" + + " description: null\n" + + " isBundle: true\n" + + " lastUpdate: null\n" + + " lifecycleStatus: In study\n" + + " name: testName\n" + + " version: testVersion\n" + + " attachment: []\n" + + " featureSpecification: []\n" + + " relatedParty: []\n" + + " resourceSpecCharacteristic: []\n" + + " resourceSpecRelationship: []\n" + + " targetResourceSchema: null\n" + + " validFor: null\n" + + " baseType: BaseRootEntity\n" + + " schemaLocation: testSchemaLocation\n" + + " type: BaseEntity\n" + + "}"; + + assertEquals(expectedString, resourceSpec.toString()); + } + + @Test + void testFixResourceCharRelationhsipIDs() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + resourceSpec.setName("testResourceSpec"); + + ResourceSpecificationCharacteristic schar1 = new ResourceSpecificationCharacteristic(); + schar1.setName("schar1"); + schar1.setUuid("uuid1"); + + ResourceSpecificationCharacteristic schar2 = new ResourceSpecificationCharacteristic(); + schar2.setName("schar2"); + + ResourceSpecCharRelationship charRel1 = new ResourceSpecCharRelationship(); + charRel1.setName("schar1"); // same name as schar1 + + ResourceSpecCharRelationship charRel2 = new ResourceSpecCharRelationship(); + charRel2.setName("schar2"); // same name as schar2 + + schar1.addResourceSpecCharRelationshipItem(charRel1); + schar2.addResourceSpecCharRelationshipItem(charRel2); + + resourceSpec.addResourceSpecCharacteristicItem(schar1); + resourceSpec.addResourceSpecCharacteristicItem(schar2); + + resourceSpec.fixResourceCharRelationhsipIDs(); + + assertEquals("uuid1", charRel1.getId()); // id should be set to the uuid of schar1 + assertEquals("testResourceSpec-schar2", charRel2.getId()); // id should be set to "resourceSpecName-scharName" + } + + @Test + void testRelatedParty() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Set relatedPartySet = new HashSet<>(); + relatedPartySet.add(new RelatedParty()); + resourceSpec.setRelatedParty(relatedPartySet); + assertEquals(relatedPartySet, resourceSpec.getRelatedParty()); + + relatedPartySet.add(new RelatedParty()); + resourceSpec.setRelatedParty(relatedPartySet); + assertEquals(relatedPartySet, resourceSpec.getRelatedParty()); + } + + @Test + void testFindSpecCharacteristicByName() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + ResourceSpecificationCharacteristic characteristic1 = new ResourceSpecificationCharacteristic(); + characteristic1.setName("testName1"); + resourceSpec.getResourceSpecCharacteristic().add(characteristic1); + + ResourceSpecificationCharacteristic characteristic2 = new ResourceSpecificationCharacteristic(); + characteristic2.setName("testName2"); + resourceSpec.getResourceSpecCharacteristic().add(characteristic2); + + assertEquals(characteristic1, resourceSpec.findSpecCharacteristicByName("testName1")); + assertEquals(characteristic2, resourceSpec.findSpecCharacteristicByName("testName2")); + assertNull(resourceSpec.findSpecCharacteristicByName("nonexistentName")); + } + + + + @Test + void testResourceSpecRelationship() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Set relationships = new HashSet<>(); + relationships.add(new ResourceSpecificationRelationship()); + resourceSpec.setResourceSpecRelationship(relationships); + assertEquals(relationships, resourceSpec.getResourceSpecRelationship()); + + ResourceSpecificationRelationship newRelationship = new ResourceSpecificationRelationship(); + resourceSpec.addResourceSpecRelationshipItem(newRelationship); + assertTrue(resourceSpec.getResourceSpecRelationship().contains(newRelationship)); + } + + @Test + void testResourceCandidateObjId() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String resourceCandidateObjId = "testResourceCandidateObjId"; + resourceSpec.setResourceCandidateObjId(resourceCandidateObjId); + assertEquals(resourceCandidateObjId, resourceSpec.getResourceCandidateObjId()); + } + + + @Test + void testBaseType() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String baseType = "testBaseType"; + resourceSpec.setBaseType(baseType); + assertEquals(baseType, resourceSpec.getBaseType()); + + ResourceSpecification newResourceSpec = resourceSpec.baseType("newTestBaseType"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testSchemaLocation() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String schemaLocation = "testSchemaLocation"; + resourceSpec.setAtSchemaLocation(schemaLocation); + assertEquals(schemaLocation, resourceSpec.getAtSchemaLocation()); + + ResourceSpecification newResourceSpec = resourceSpec.schemaLocation("newTestSchemaLocation"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testVersion() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String version = "testVersion"; + resourceSpec.setVersion(version); + assertEquals(version, resourceSpec.getVersion()); + + ResourceSpecification newResourceSpec = resourceSpec.version("newTestVersion"); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testAttachment() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Set attachments = new HashSet<>(); + attachments.add(new AttachmentRefOrValue()); + resourceSpec.setAttachment(attachments); + assertEquals(attachments, resourceSpec.getAttachment()); + + AttachmentRefOrValue newAttachment = new AttachmentRefOrValue(); + resourceSpec.addAttachmentItem(newAttachment); + assertTrue(resourceSpec.getAttachment().contains(newAttachment)); + } + + @Test + void testFeatureSpecification() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Set featureSpecifications = new HashSet<>(); + featureSpecifications.add(new FeatureSpecification()); + resourceSpec.setFeatureSpecification(featureSpecifications); + assertEquals(featureSpecifications, resourceSpec.getFeatureSpecification()); + + FeatureSpecification newFeatureSpecification = new FeatureSpecification(); + resourceSpec.addFeatureSpecificationItem(newFeatureSpecification); + assertTrue(resourceSpec.getFeatureSpecification().contains(newFeatureSpecification)); + } + + @Test + void testResourceSpecCharacteristic() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + Set characteristics = new HashSet<>(); + characteristics.add(new ResourceSpecificationCharacteristic()); + resourceSpec.setResourceSpecCharacteristic(characteristics); + assertEquals(characteristics, resourceSpec.getResourceSpecCharacteristic()); + + ResourceSpecification newResourceSpec = resourceSpec.resourceSpecCharacteristic(characteristics); + assertEquals(resourceSpec, newResourceSpec); + } + + + @Test + void testTargetResourceSchema() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + TargetResourceSchema targetResourceSchema = new TargetResourceSchema(); + resourceSpec.setTargetResourceSchema(targetResourceSchema); + assertEquals(targetResourceSchema, resourceSpec.getTargetResourceSchema()); + + ResourceSpecification newResourceSpec = resourceSpec.targetResourceSchema(targetResourceSchema); + assertEquals(resourceSpec, newResourceSpec); + } + + @Test + void testType() { + ResourceSpecification resourceSpec = new TestResourceSpecification(); + String type = "testType"; + resourceSpec.setType(type); + assertEquals(type, resourceSpec.getType()); + + ResourceSpecification newResourceSpec = resourceSpec.type("newTestType"); + assertEquals(resourceSpec, newResourceSpec); + } +} \ No newline at end of file -- GitLab From fad57338203a369c6b4e0166cc750e7d2d505dc1 Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Mon, 22 Jan 2024 15:49:35 +0200 Subject: [PATCH 5/6] More tests for model --- .../osl/services/api/rcm634/CommonTests.java | 2 +- .../ConnectionPointSpecificationRefTest.java | 29 +- ...esourceSpecificationApiControllerTest.java | 535 +++++++++--------- ...sourceSpecificationCharacteristicTest.java | 351 ++++++++++++ 4 files changed, 620 insertions(+), 297 deletions(-) create mode 100644 src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationCharacteristicTest.java diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java index d77b1e0..94440f8 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/CommonTests.java @@ -60,7 +60,7 @@ public class CommonTests { } @Test - public void ApiOriginFilter() throws IOException, ServletException { + public void testApiOriginFilter() throws IOException, ServletException { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); FilterChain chain = mock(FilterChain.class); diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java index 3a92301..c899aef 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ConnectionPointSpecificationRefTest.java @@ -67,38 +67,11 @@ public class ConnectionPointSpecificationRefTest { assertTrue(ref1.equals(ref2)); - - ref1.id("differentId"); - - assertFalse(ref1.equals(ref2)); - } - - @Test - void testHashCode() { - String id = "testId"; - String href = "testHref"; - String name = "testName"; - String version = "testVersion"; - String referredType = "testReferredType"; - - ConnectionPointSpecificationRef ref1 = new ConnectionPointSpecificationRef(); - ref1.setUuid(id); - ref1.setHref(href); - ref1.setName(name); - ref1.setVersion(version); - ref1.setAtReferredType(referredType); - - ConnectionPointSpecificationRef ref2 = new ConnectionPointSpecificationRef(); - ref2.setUuid(id); - ref2.setHref(href); - ref2.setName(name); - ref2.setVersion(version); - ref2.setAtReferredType(referredType); - assertEquals(ref1.hashCode(), ref2.hashCode()); ref1.id("differentId"); + assertFalse(ref1.equals(ref2)); assertNotEquals(ref1.hashCode(), ref2.hashCode()); } diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java index b41f281..a104f2a 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java @@ -1,268 +1,267 @@ -package org.etsi.osl.services.api.rcm634; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.transaction.Transactional; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.io.IOUtils; -import org.etsi.osl.tmf.JsonUtils; -import org.etsi.osl.tmf.OpenAPISpringBoot; -import org.etsi.osl.tmf.common.model.Attachment; -import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; -import org.etsi.osl.tmf.pcm620.reposervices.AttachmentRepoService; -import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; -import org.etsi.osl.tmf.rcm634.model.*; -import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; -import org.junit.Ignore; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -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.web.servlet.MockMvc; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.BeforeEach; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - - -@Transactional -@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) -@AutoConfigureTestDatabase -@AutoConfigureMockMvc -@ActiveProfiles("testing") -public class ResourceSpecificationApiControllerTest { - - @Autowired - private MockMvc mvc; - - @InjectMocks - ResourceSpecificationApiController resourceSpecificationApiController; - - @Mock - ResourceSpecificationRepoService resourceSpecificationRepoService; - - @Mock - ResourceSpecification resourceSpecification; - - @BeforeEach - public void init() { - MockitoAnnotations.initMocks(this); - } - - - // Test CREATE with role ADMIN - @Test - @WithMockUser(authorities = {"ROLE_ADMIN"}) - public void testCreateResourceSpecificationAdmin() throws Exception { - - // Testing a valid physical resource specification at the correct endpoint - File resourceSpecFile = new File("src/test/resources/testPhysicalResourceSpec.json"); - InputStream in = new FileInputStream(resourceSpecFile); - String resourceSpecString = IOUtils.toString(in, "UTF-8"); - PhysicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(resourceSpecString, PhysicalResourceSpecificationCreate.class); - - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(physicalResourceSpecCreate))) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("name", is("Test Resource Spec"))) - .andExpect(status().isOk()) - .andReturn().getResponse(); - - - LogicalResourceSpecificationCreate logicalResourceSpecificationCreate = new LogicalResourceSpecificationCreate(); - logicalResourceSpecificationCreate.setName("Test Logical Resource Spec"); - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(logicalResourceSpecificationCreate))) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("name", is("Test Logical Resource Spec"))) - .andExpect(status().isOk()) - .andReturn().getResponse(); - - - ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); - resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); - resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("name", is("Test Resource Function Specification"))) - .andExpect(status().isOk()) - .andReturn().getResponse(); - - // Testing a valid physical resource specification wrong endpoint - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecificat") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(physicalResourceSpecCreate))) - .andExpect(status().isNotFound()) - .andReturn().getResponse(); - - // Testing a valid physical resource specification wrong payload correct endpoint - String wrongStringPayload = "random string as payload"; - byte[] wrongPayload = wrongStringPayload.getBytes(); - - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(wrongPayload)) - .andExpect(status().isBadRequest()) - .andReturn().getResponse(); - } - - // Test CREATE with incorrect role USER - @Test - @WithMockUser(authorities = {"ROLE_USER"}) - public void testCreateResourceSpecificationUser() throws Exception { - // Testing a valid physical resource specification - File physicalResourceSpecFile = new File("src/test/resources/testResourceSpec.json"); - InputStream in = new FileInputStream(physicalResourceSpecFile); - String physicalResourceSpecString = IOUtils.toString(in, "UTF-8"); - LogicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(physicalResourceSpecString, LogicalResourceSpecificationCreate.class); - mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(physicalResourceSpecCreate))) - .andExpect(status().isForbidden()) - .andReturn().getResponse(); - - } - - // Test DELETE method with role ADMIN - @Test - @WithMockUser(authorities = {"ROLE_ADMIN"}) - public void testDeleteResourceSpecificationAdmin() throws Exception { - ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); - resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); - resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); - String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) - .andReturn().getResponse().getContentAsString(); - System.out.println(createdSpecificationString); - - ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); - String idToBeDeleted = createdSpecification.getId(); - - mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/" + idToBeDeleted) - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(null))) - .andExpect(status().isOk()) - .andReturn().getResponse(); - - } - - // Test DELETE method with incorrect role USER - @Test - @WithMockUser(authorities = {"ROLE_USER"}) - public void testDeleteResourceSpecificationUser() throws Exception { - mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/idToBeDeleted") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(null))) - .andExpect(status().isForbidden()) - .andReturn().getResponse(); - - } - - // Test GET by id method with no role - @Test - @WithMockUser(authorities = {"ROLE_ADMIN"}) - public void testRetrieveResourceSpecification() throws Exception { - ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); - resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); - resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); - String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") - .with(SecurityMockMvcRequestPostProcessors.csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) - .andReturn().getResponse().getContentAsString(); - - ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); - String id = createdSpecification.getId(); - String fields = "name"; - mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification/" + id) - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(null)) - .param("fields", fields)) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("name", is("Test Resource Function Specification"))) - .andExpect(status().isOk()) - .andReturn().getResponse(); - } - - // Test GET all method with no role - @Test - @WithMockUser(authorities = {"ROLE_ADMIN"}) - public void testListResourceSpecification() throws Exception { - String listResSpecsResponseString = mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(null))) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn().getResponse().getContentAsString(); - - List listResSpecsResponse = JsonUtils.toListOfJsonObj(listResSpecsResponseString, LogicalResourceSpecification.class); - assertEquals(7, listResSpecsResponse.size()); - - mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") - .contentType(MediaType.APPLICATION_JSON) - .content(JsonUtils.toJson(null)) - .param("fields", "lastUpdate")) - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn().getResponse().getContentAsString(); - } - - @Test - void testGetObjectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - MockHttpServletRequest request = new MockHttpServletRequest(); - ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); - - Optional result = controller.getObjectMapper(); - assertTrue(result.isPresent()); - assertEquals(objectMapper, result.get()); - } - - @Test - void testGetRequest() { - ObjectMapper objectMapper = new ObjectMapper(); - MockHttpServletRequest request = new MockHttpServletRequest(); - ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); - - Optional result = controller.getRequest(); - assertTrue(result.isPresent()); - assertEquals(request, result.get()); - } -} \ No newline at end of file +//package org.etsi.osl.services.api.rcm634; +// +//import jakarta.servlet.http.HttpServletRequest; +//import jakarta.transaction.Transactional; +//import com.fasterxml.jackson.databind.ObjectMapper; +//import org.apache.commons.io.IOUtils; +//import org.etsi.osl.tmf.JsonUtils; +//import org.etsi.osl.tmf.OpenAPISpringBoot; +//import org.etsi.osl.tmf.common.model.Attachment; +//import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; +//import org.etsi.osl.tmf.pcm620.reposervices.AttachmentRepoService; +//import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; +//import org.etsi.osl.tmf.rcm634.model.*; +//import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; +//import org.junit.Ignore; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +//import org.mockito.InjectMocks; +//import org.mockito.Mock; +//import org.mockito.MockitoAnnotations; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.MediaType; +//import org.springframework.http.ResponseEntity; +//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.web.servlet.MockMvc; +//import org.springframework.mock.web.MockHttpServletRequest; +//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +//import org.junit.jupiter.api.Test; +//import org.junit.jupiter.api.BeforeEach; +// +//import java.io.File; +//import java.io.FileInputStream; +//import java.io.InputStream; +//import java.util.List; +//import java.util.Optional; +//import java.util.Set; +// +//import static org.hamcrest.CoreMatchers.is; +//import static org.junit.Assert.assertNotNull; +//import static org.junit.Assert.assertTrue; +//import static org.junit.jupiter.api.Assertions.assertEquals; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +// +// +//@Transactional +//@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +//@AutoConfigureTestDatabase +//@AutoConfigureMockMvc +//@ActiveProfiles("testing") +//public class ResourceSpecificationApiControllerTest { +// +// @Autowired +// private MockMvc mvc; +// +// @InjectMocks +// ResourceSpecificationApiController resourceSpecificationApiController; +// +// @Mock +// ResourceSpecificationRepoService resourceSpecificationRepoService; +// +// @Mock +// ResourceSpecification resourceSpecification; +// +// @BeforeEach +// public void init() { +// MockitoAnnotations.initMocks(this); +// } +// +// +// // Test CREATE with role ADMIN +// @Test +// @WithMockUser(authorities = {"ROLE_ADMIN"}) +// public void testCreateResourceSpecificationAdmin() throws Exception { +// +// // Testing a valid physical resource specification at the correct endpoint +// File resourceSpecFile = new File("src/test/resources/testPhysicalResourceSpec.json"); +// InputStream in = new FileInputStream(resourceSpecFile); +// String resourceSpecString = IOUtils.toString(in, "UTF-8"); +// PhysicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(resourceSpecString, PhysicalResourceSpecificationCreate.class); +// +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(physicalResourceSpecCreate))) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(jsonPath("name", is("Test Resource Spec"))) +// .andExpect(status().isOk()) +// .andReturn().getResponse(); +// +// +// LogicalResourceSpecificationCreate logicalResourceSpecificationCreate = new LogicalResourceSpecificationCreate(); +// logicalResourceSpecificationCreate.setName("Test Logical Resource Spec"); +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(logicalResourceSpecificationCreate))) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(jsonPath("name", is("Test Logical Resource Spec"))) +// .andExpect(status().isOk()) +// .andReturn().getResponse(); +// +// +// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); +// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); +// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(jsonPath("name", is("Test Resource Function Specification"))) +// .andExpect(status().isOk()) +// .andReturn().getResponse(); +// +// // Testing a valid physical resource specification wrong endpoint +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecificat") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(physicalResourceSpecCreate))) +// .andExpect(status().isNotFound()) +// .andReturn().getResponse(); +// +// // Testing a valid physical resource specification wrong payload correct endpoint +// String wrongStringPayload = "random string as payload"; +// byte[] wrongPayload = wrongStringPayload.getBytes(); +// +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(wrongPayload)) +// .andExpect(status().isBadRequest()) +// .andReturn().getResponse(); +// } +// +// // Test CREATE with incorrect role USER +// @Test +// @WithMockUser(authorities = {"ROLE_USER"}) +// public void testCreateResourceSpecificationUser() throws Exception { +// // Testing a valid physical resource specification +// File physicalResourceSpecFile = new File("src/test/resources/testResourceSpec.json"); +// InputStream in = new FileInputStream(physicalResourceSpecFile); +// String physicalResourceSpecString = IOUtils.toString(in, "UTF-8"); +// LogicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(physicalResourceSpecString, LogicalResourceSpecificationCreate.class); +// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(physicalResourceSpecCreate))) +// .andExpect(status().isForbidden()) +// .andReturn().getResponse(); +// +// } +// +// // Test DELETE method with role ADMIN +// @Test +// @WithMockUser(authorities = {"ROLE_ADMIN"}) +// public void testDeleteResourceSpecificationAdmin() throws Exception { +// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); +// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); +// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); +// String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) +// .andReturn().getResponse().getContentAsString(); +// System.out.println(createdSpecificationString); +// +// ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); +// String idToBeDeleted = createdSpecification.getId(); +// +// mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/" + idToBeDeleted) +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(null))) +// .andExpect(status().isOk()) +// .andReturn().getResponse(); +// +// } +// +// // Test DELETE method with incorrect role USER +// @Test +// @WithMockUser(authorities = {"ROLE_USER"}) +// public void testDeleteResourceSpecificationUser() throws Exception { +// mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/idToBeDeleted") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(null))) +// .andExpect(status().isForbidden()) +// .andReturn().getResponse(); +// +// } +// +// // Test GET by id method with no role +// @Test +// @WithMockUser(authorities = {"ROLE_ADMIN"}) +// public void testRetrieveResourceSpecification() throws Exception { +// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); +// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); +// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); +// String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") +// .with(SecurityMockMvcRequestPostProcessors.csrf()) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) +// .andReturn().getResponse().getContentAsString(); +// +// ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); +// String id = createdSpecification.getId(); +// String fields = "name"; +// mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification/" + id) +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(null)) +// .param("fields", fields)) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(jsonPath("name", is("Test Resource Function Specification"))) +// .andExpect(status().isOk()) +// .andReturn().getResponse(); +// } +// +// @Test +// @WithMockUser(authorities = {"ROLE_ADMIN"}) +// public void testListResourceSpecification() throws Exception { +// String listResSpecsResponseString = mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(null))) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(status().isOk()) +// .andReturn().getResponse().getContentAsString(); +// +// List listResSpecsResponse = JsonUtils.toListOfJsonObj(listResSpecsResponseString, LogicalResourceSpecification.class); +// assertEquals(7, listResSpecsResponse.size()); +// +// mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") +// .contentType(MediaType.APPLICATION_JSON) +// .content(JsonUtils.toJson(null)) +// .param("fields", "lastUpdate")) +// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) +// .andExpect(status().isOk()) +// .andReturn().getResponse().getContentAsString(); +// } +// +// @Test +// void testGetObjectMapper() { +// ObjectMapper objectMapper = new ObjectMapper(); +// MockHttpServletRequest request = new MockHttpServletRequest(); +// ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); +// +// Optional result = controller.getObjectMapper(); +// assertTrue(result.isPresent()); +// assertEquals(objectMapper, result.get()); +// } +// +// @Test +// void testGetRequest() { +// ObjectMapper objectMapper = new ObjectMapper(); +// MockHttpServletRequest request = new MockHttpServletRequest(); +// ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); +// +// Optional result = controller.getRequest(); +// assertTrue(result.isPresent()); +// assertEquals(request, result.get()); +// } +//} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationCharacteristicTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationCharacteristicTest.java new file mode 100644 index 0000000..4e297af --- /dev/null +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationCharacteristicTest.java @@ -0,0 +1,351 @@ +package org.etsi.osl.services.api.rcm634; + +import org.etsi.osl.tmf.common.model.TimePeriod; +import org.etsi.osl.tmf.rcm634.model.ResourceSpecCharRelationship; +import org.etsi.osl.tmf.rcm634.model.ResourceSpecificationCharacteristic; +import org.etsi.osl.tmf.rcm634.model.ResourceSpecificationCharacteristicValue; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +public class ResourceSpecificationCharacteristicTest { + @Test + void testResourceSpecificationCharacteristicConstructorAndAllProperties() { + // Create a new ResourceSpecificationCharacteristic object and set its properties + ResourceSpecificationCharacteristic characteristic1 = new ResourceSpecificationCharacteristic(); + characteristic1.setName("testName1"); + characteristic1.setDescription("testDescription1"); + characteristic1.setValueType("testValueType1"); + characteristic1.setConfigurable(true); + characteristic1.setValidFor(new TimePeriod()); + characteristic1.setSchemaLocation("testSchemaLocation1"); + characteristic1.setValueSchemaLocation("testValueSchemaLocation1"); + characteristic1.setMinCardinality(1); + characteristic1.setMaxCardinality(1); + characteristic1.setIsUnique(true); + characteristic1.setRegex("testRegex1"); + characteristic1.setExtensible(true); + characteristic1.setResourceSpecCharRelationship(new HashSet<>()); + characteristic1.setResourceSpecificationCharacteristicValue(new HashSet<>()); + + // Create another ResourceSpecificationCharacteristic object using the constructor that takes another ResourceSpecificationCharacteristic object as a parameter + ResourceSpecificationCharacteristic characteristic2 = new ResourceSpecificationCharacteristic(characteristic1); + characteristic2.setSchemaLocation("testSchemaLocation1"); + characteristic2.setValueSchemaLocation("testValueSchemaLocation1"); + + // Assert that the properties of the two objects are equal + assertEquals(characteristic1.getName(), characteristic2.getName()); + assertEquals(characteristic1.getDescription(), characteristic2.getDescription()); + assertEquals(characteristic1.getValueType(), characteristic2.getValueType()); + assertEquals(characteristic1.isConfigurable(), characteristic2.isConfigurable()); + assertEquals(characteristic1.getValidFor(), characteristic2.getValidFor()); + assertEquals(characteristic1.getSchemaLocation(), characteristic2.getSchemaLocation()); + assertEquals(characteristic1.getValueSchemaLocation(), characteristic2.getValueSchemaLocation()); + assertEquals(characteristic1.getMinCardinality(), characteristic2.getMinCardinality()); + assertEquals(characteristic1.getMaxCardinality(), characteristic2.getMaxCardinality()); + assertEquals(characteristic1.isIsUnique(), characteristic2.isIsUnique()); + assertEquals(characteristic1.getRegex(), characteristic2.getRegex()); + assertEquals(characteristic1.isExtensible(), characteristic2.isExtensible()); + assertEquals(characteristic1.getResourceSpecCharRelationship(), characteristic2.getResourceSpecCharRelationship()); + assertEquals(characteristic1.getResourceSpecCharacteristicValue(), characteristic2.getResourceSpecCharacteristicValue()); + } + + @Test + void testName() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String name = "Test Name"; + characteristic.name(name); + assertEquals(name, characteristic.getName()); + } + + @Test + void testDescription() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String description = "Test Description"; + characteristic.description(description); + assertEquals(description, characteristic.getDescription()); + } + + @Test + void testValueType() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String valueType = "Test ValueType"; + characteristic.valueType(valueType); + assertEquals(valueType, characteristic.getValueType()); + } + + @Test + void testConfigurable() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Boolean configurable = true; + characteristic.configurable(configurable); + assertEquals(configurable, characteristic.isConfigurable()); + } + + @Test + void testValidFor() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + TimePeriod validFor = new TimePeriod(); + characteristic.validFor(validFor); + assertEquals(validFor, characteristic.getValidFor()); + } + + @Test + void testType() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String type = "Test Type"; + characteristic.type(type); + assertEquals(type, characteristic.getType()); + } + + @Test + void testSchemaLocation() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String schemaLocation = "Test SchemaLocation"; + characteristic.schemaLocation(schemaLocation); + assertEquals(schemaLocation, characteristic.getSchemaLocation()); + } + + @Test + void testValueSchemaLocation() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String valueSchemaLocation = "Test ValueSchemaLocation"; + characteristic.valueSchemaLocation(valueSchemaLocation); + assertEquals(valueSchemaLocation, characteristic.getValueSchemaLocation()); + } + + @Test + void testMinCardinality() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Integer minCardinality = 1; + characteristic.minCardinality(minCardinality); + assertEquals(minCardinality, characteristic.getMinCardinality()); + } + + @Test + void testMaxCardinality() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Integer maxCardinality = 10; + characteristic.maxCardinality(maxCardinality); + assertEquals(maxCardinality, characteristic.getMaxCardinality()); + } + + @Test + void testIsUnique() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Boolean isUnique = true; + characteristic.isUnique(isUnique); + assertEquals(isUnique, characteristic.isIsUnique()); + } + + @Test + void testRegex() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + String regex = "Test Regex"; + characteristic.regex(regex); + assertEquals(regex, characteristic.getRegex()); + } + + @Test + void testExtensible() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Boolean extensible = true; + characteristic.extensible(extensible); + assertEquals(extensible, characteristic.isExtensible()); + } + + @Test + void testResourceSpecCharRelationship() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Set resourceSpecCharRelationship = new HashSet<>(); + resourceSpecCharRelationship.add(new ResourceSpecCharRelationship()); + characteristic.resourceSpecCharRelationship(resourceSpecCharRelationship); + assertEquals(resourceSpecCharRelationship, characteristic.getResourceSpecCharRelationship()); + } + + @Test + void testResourceSpecCharacteristicValue() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + Set resourceSpecCharacteristicValue = new HashSet<>(); + resourceSpecCharacteristicValue.add(new ResourceSpecificationCharacteristicValue()); + characteristic.ResourceSpecCharacteristicValue(resourceSpecCharacteristicValue); + assertEquals(resourceSpecCharacteristicValue, characteristic.getResourceSpecCharacteristicValue()); + } + + @Test + void testAddResourceSpecCharacteristicValueItem() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + ResourceSpecificationCharacteristicValue resourceSpecCharacteristicValueItem = new ResourceSpecificationCharacteristicValue(); + characteristic.addResourceSpecCharacteristicValueItem(resourceSpecCharacteristicValueItem); + assertTrue(characteristic.getResourceSpecCharacteristicValue().contains(resourceSpecCharacteristicValueItem)); + } + + @Test + void testEquals() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + characteristic.setName("Test Name"); + characteristic.setDescription("Test Description"); + characteristic.setValueType("Test ValueType"); + characteristic.setConfigurable(true); + characteristic.setType("Test Type"); + characteristic.setSchemaLocation("Test SchemaLocation"); + characteristic.setValueSchemaLocation("Test ValueSchemaLocation"); + characteristic.setMinCardinality(1); + characteristic.setMaxCardinality(10); + characteristic.setIsUnique(true); + characteristic.setRegex("Test Regex"); + characteristic.setExtensible(true); + + ResourceSpecificationCharacteristic characteristic2 = new ResourceSpecificationCharacteristic(); + characteristic2.setName("Test Name"); + characteristic2.setDescription("Test Description"); + characteristic2.setValueType("Test ValueType"); + characteristic2.setConfigurable(true); + characteristic2.setType("Test Type"); + characteristic2.setSchemaLocation("Test SchemaLocation"); + characteristic2.setValueSchemaLocation("Test ValueSchemaLocation"); + characteristic2.setMinCardinality(1); + characteristic2.setMaxCardinality(10); + characteristic2.setIsUnique(true); + characteristic2.setRegex("Test Regex"); + characteristic2.setExtensible(true); + + assertTrue(characteristic.equals(characteristic2)); + + characteristic2.name("Test Name 2"); + assertFalse(characteristic.equals(characteristic2)); + } + + @Test + void testToString() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + characteristic.setName("Test Name"); + characteristic.setDescription("Test Description"); + characteristic.setValueType("Test ValueType"); + characteristic.setConfigurable(true); + characteristic.setValidFor(new TimePeriod()); + characteristic.setType("Test Type"); + characteristic.setSchemaLocation("Test SchemaLocation"); + characteristic.setValueSchemaLocation("Test ValueSchemaLocation"); + characteristic.setMinCardinality(1); + characteristic.setMaxCardinality(10); + characteristic.setIsUnique(true); + characteristic.setRegex("Test Regex"); + characteristic.setExtensible(true); + characteristic.setResourceSpecCharRelationship(new HashSet<>()); + characteristic.setResourceSpecificationCharacteristicValue(new HashSet<>()); + + String expectedString = "class ResourceSpecificationCharacteristic {\n" + + " name: Test Name\n" + + " description: Test Description\n" + + " valueType: Test ValueType\n" + + " configurable: true\n" + + " validFor: " + characteristic.getValidFor().toString().replace("\n", "\n ") + "\n" + + " type: Test Type\n" + + " schemaLocation: Test SchemaLocation\n" + + " valueSchemaLocation: Test ValueSchemaLocation\n" + + " minCardinality: 1\n" + + " maxCardinality: 10\n" + + " isUnique: true\n" + + " regex: Test Regex\n" + + " extensible: true\n" + + " resourceSpecCharRelationship: []\n" + + " ResourceSpecificationCharacteristicValue: []\n" + + "}"; + + assertEquals(expectedString, characteristic.toString()); + } + + @Test + void testToIndentedString() throws Exception { + ResourceSpecificationCharacteristic resourceSpecificationCharacteristic = new ResourceSpecificationCharacteristic(); + + Method method = ResourceSpecificationCharacteristic.class.getDeclaredMethod("toIndentedString", Object.class); + method.setAccessible(true); + + String input = "Hello\nWorld"; + String expectedOutput = "Hello\n World"; + + String output = (String) method.invoke(resourceSpecificationCharacteristic, input); + + assertEquals(expectedOutput, output); + } + + @Test + void testUpdateWith() { + ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); + characteristic.setName("Test Name"); + characteristic.setDescription("Test Description"); + characteristic.setConfigurable(true); + characteristic.setType("org.etsi.osl.tmf.rcm634.model.ResourceSpecificationCharacteristic"); + characteristic.setMinCardinality(1); + characteristic.setMaxCardinality(10); + characteristic.setIsUnique(true); + characteristic.setRegex("Test Regex"); + characteristic.setExtensible(true); + + ResourceSpecificationCharacteristic characteristic2 = new ResourceSpecificationCharacteristic(); + characteristic2.updateWith(characteristic); + + assertEquals(characteristic, characteristic2); + } + +// @Test +// void testUpdateResourceSpecificationCharacteristicValues() throws NoSuchMethodException { +// // Create a ResourceSpecificationCharacteristic instance +// ResourceSpecificationCharacteristic characteristic = new ResourceSpecificationCharacteristic(); +// +// // Create a set of ResourceSpecificationCharacteristicValue instances +// Set values = new HashSet<>(); +// ResourceSpecificationCharacteristicValue value1 = new ResourceSpecificationCharacteristicValue(); +// value1.setValue(new Any("Test value 1")); +// +// ResourceSpecificationCharacteristicValue value2 = new ResourceSpecificationCharacteristicValue(); +// value2.setValue(new Any("Test value 2")); +// values.add(value2); +// +// Method method = ResourceSpecificationCharacteristic.class.getDeclaredMethod("updateResourceSpecificationCharacteristicValues", Object.class); +// method.setAccessible(true); +// characteristic.updateResourceSpecificationCharacteristicValues(values); +// +// // Assert that the resourceSpecCharacteristicValue set has been updated correctly +// Set updatedValues = characteristic.getResourceSpecCharacteristicValue(); +// assertEquals(2, updatedValues.size()); +// assertTrue(updatedValues.contains(value1)); +// assertTrue(updatedValues.contains(value2)); +// +// // Create a new set of ResourceSpecificationCharacteristicValue instances with one new value +// Set newValues = new HashSet<>(values); +// ResourceSpecificationCharacteristicValue value3 = new ResourceSpecificationCharacteristicValue(); +// value3.setValue(new Any("Test Value 3")); +// newValues.add(value3); +// +// // Call the updateResourceSpecificationCharacteristicValues method with the new set +// characteristic.updateResourceSpecificationCharacteristicValues(newValues); +// +// // Assert that the resourceSpecCharacteristicValue set has been updated correctly +// updatedValues = characteristic.getResourceSpecCharacteristicValue(); +// assertEquals(3, updatedValues.size()); +// assertTrue(updatedValues.contains(value1)); +// assertTrue(updatedValues.contains(value2)); +// assertTrue(updatedValues.contains(value3)); +// +// // Create a new set of ResourceSpecificationCharacteristicValue instances without the first value +// newValues = new HashSet<>(values); +// newValues.remove(value1); +// +// // Call the updateResourceSpecificationCharacteristicValues method with the new set +// characteristic.updateResourceSpecificationCharacteristicValues(newValues); +// +// // Assert that the resourceSpecCharacteristicValue set has been updated correctly +// updatedValues = characteristic.getResourceSpecCharacteristicValue(); +// assertEquals(2, updatedValues.size()); +// assertFalse(updatedValues.contains(value1)); +// assertTrue(updatedValues.contains(value2)); +// assertTrue(updatedValues.contains(value3)); +// } +} \ No newline at end of file -- GitLab From 3b24814f95c297a9f965c1447cd46784a395e1ba Mon Sep 17 00:00:00 2001 From: George Tziavas Date: Tue, 23 Jan 2024 12:07:27 +0200 Subject: [PATCH 6/6] ResourceSpecificationApiControllerTest --- ...esourceSpecificationApiControllerTest.java | 524 +++++++++--------- 1 file changed, 257 insertions(+), 267 deletions(-) diff --git a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java index a104f2a..1aa8d1a 100644 --- a/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java +++ b/src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationApiControllerTest.java @@ -1,267 +1,257 @@ -//package org.etsi.osl.services.api.rcm634; -// -//import jakarta.servlet.http.HttpServletRequest; -//import jakarta.transaction.Transactional; -//import com.fasterxml.jackson.databind.ObjectMapper; -//import org.apache.commons.io.IOUtils; -//import org.etsi.osl.tmf.JsonUtils; -//import org.etsi.osl.tmf.OpenAPISpringBoot; -//import org.etsi.osl.tmf.common.model.Attachment; -//import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; -//import org.etsi.osl.tmf.pcm620.reposervices.AttachmentRepoService; -//import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; -//import org.etsi.osl.tmf.rcm634.model.*; -//import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; -//import org.junit.Ignore; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -//import org.mockito.InjectMocks; -//import org.mockito.Mock; -//import org.mockito.MockitoAnnotations; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.http.HttpStatus; -//import org.springframework.http.MediaType; -//import org.springframework.http.ResponseEntity; -//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.web.servlet.MockMvc; -//import org.springframework.mock.web.MockHttpServletRequest; -//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.BeforeEach; -// -//import java.io.File; -//import java.io.FileInputStream; -//import java.io.InputStream; -//import java.util.List; -//import java.util.Optional; -//import java.util.Set; -// -//import static org.hamcrest.CoreMatchers.is; -//import static org.junit.Assert.assertNotNull; -//import static org.junit.Assert.assertTrue; -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -// -// -//@Transactional -//@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) -//@AutoConfigureTestDatabase -//@AutoConfigureMockMvc -//@ActiveProfiles("testing") -//public class ResourceSpecificationApiControllerTest { -// -// @Autowired -// private MockMvc mvc; -// -// @InjectMocks -// ResourceSpecificationApiController resourceSpecificationApiController; -// -// @Mock -// ResourceSpecificationRepoService resourceSpecificationRepoService; -// -// @Mock -// ResourceSpecification resourceSpecification; -// -// @BeforeEach -// public void init() { -// MockitoAnnotations.initMocks(this); -// } -// -// -// // Test CREATE with role ADMIN -// @Test -// @WithMockUser(authorities = {"ROLE_ADMIN"}) -// public void testCreateResourceSpecificationAdmin() throws Exception { -// -// // Testing a valid physical resource specification at the correct endpoint -// File resourceSpecFile = new File("src/test/resources/testPhysicalResourceSpec.json"); -// InputStream in = new FileInputStream(resourceSpecFile); -// String resourceSpecString = IOUtils.toString(in, "UTF-8"); -// PhysicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(resourceSpecString, PhysicalResourceSpecificationCreate.class); -// -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(physicalResourceSpecCreate))) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("name", is("Test Resource Spec"))) -// .andExpect(status().isOk()) -// .andReturn().getResponse(); -// -// -// LogicalResourceSpecificationCreate logicalResourceSpecificationCreate = new LogicalResourceSpecificationCreate(); -// logicalResourceSpecificationCreate.setName("Test Logical Resource Spec"); -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(logicalResourceSpecificationCreate))) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("name", is("Test Logical Resource Spec"))) -// .andExpect(status().isOk()) -// .andReturn().getResponse(); -// -// -// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); -// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); -// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("name", is("Test Resource Function Specification"))) -// .andExpect(status().isOk()) -// .andReturn().getResponse(); -// -// // Testing a valid physical resource specification wrong endpoint -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecificat") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(physicalResourceSpecCreate))) -// .andExpect(status().isNotFound()) -// .andReturn().getResponse(); -// -// // Testing a valid physical resource specification wrong payload correct endpoint -// String wrongStringPayload = "random string as payload"; -// byte[] wrongPayload = wrongStringPayload.getBytes(); -// -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(wrongPayload)) -// .andExpect(status().isBadRequest()) -// .andReturn().getResponse(); -// } -// -// // Test CREATE with incorrect role USER -// @Test -// @WithMockUser(authorities = {"ROLE_USER"}) -// public void testCreateResourceSpecificationUser() throws Exception { -// // Testing a valid physical resource specification -// File physicalResourceSpecFile = new File("src/test/resources/testResourceSpec.json"); -// InputStream in = new FileInputStream(physicalResourceSpecFile); -// String physicalResourceSpecString = IOUtils.toString(in, "UTF-8"); -// LogicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(physicalResourceSpecString, LogicalResourceSpecificationCreate.class); -// mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(physicalResourceSpecCreate))) -// .andExpect(status().isForbidden()) -// .andReturn().getResponse(); -// -// } -// -// // Test DELETE method with role ADMIN -// @Test -// @WithMockUser(authorities = {"ROLE_ADMIN"}) -// public void testDeleteResourceSpecificationAdmin() throws Exception { -// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); -// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); -// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); -// String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) -// .andReturn().getResponse().getContentAsString(); -// System.out.println(createdSpecificationString); -// -// ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); -// String idToBeDeleted = createdSpecification.getId(); -// -// mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/" + idToBeDeleted) -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(null))) -// .andExpect(status().isOk()) -// .andReturn().getResponse(); -// -// } -// -// // Test DELETE method with incorrect role USER -// @Test -// @WithMockUser(authorities = {"ROLE_USER"}) -// public void testDeleteResourceSpecificationUser() throws Exception { -// mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/idToBeDeleted") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(null))) -// .andExpect(status().isForbidden()) -// .andReturn().getResponse(); -// -// } -// -// // Test GET by id method with no role -// @Test -// @WithMockUser(authorities = {"ROLE_ADMIN"}) -// public void testRetrieveResourceSpecification() throws Exception { -// ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); -// resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); -// resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); -// String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") -// .with(SecurityMockMvcRequestPostProcessors.csrf()) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) -// .andReturn().getResponse().getContentAsString(); -// -// ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); -// String id = createdSpecification.getId(); -// String fields = "name"; -// mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification/" + id) -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(null)) -// .param("fields", fields)) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("name", is("Test Resource Function Specification"))) -// .andExpect(status().isOk()) -// .andReturn().getResponse(); -// } -// -// @Test -// @WithMockUser(authorities = {"ROLE_ADMIN"}) -// public void testListResourceSpecification() throws Exception { -// String listResSpecsResponseString = mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(null))) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// -// List listResSpecsResponse = JsonUtils.toListOfJsonObj(listResSpecsResponseString, LogicalResourceSpecification.class); -// assertEquals(7, listResSpecsResponse.size()); -// -// mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") -// .contentType(MediaType.APPLICATION_JSON) -// .content(JsonUtils.toJson(null)) -// .param("fields", "lastUpdate")) -// .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()) -// .andReturn().getResponse().getContentAsString(); -// } -// -// @Test -// void testGetObjectMapper() { -// ObjectMapper objectMapper = new ObjectMapper(); -// MockHttpServletRequest request = new MockHttpServletRequest(); -// ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); -// -// Optional result = controller.getObjectMapper(); -// assertTrue(result.isPresent()); -// assertEquals(objectMapper, result.get()); -// } -// -// @Test -// void testGetRequest() { -// ObjectMapper objectMapper = new ObjectMapper(); -// MockHttpServletRequest request = new MockHttpServletRequest(); -// ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); -// -// Optional result = controller.getRequest(); -// assertTrue(result.isPresent()); -// assertEquals(request, result.get()); -// } -//} \ No newline at end of file +package org.etsi.osl.services.api.rcm634; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.transaction.Transactional; +import org.apache.commons.io.IOUtils; +import org.etsi.osl.tmf.JsonUtils; +import org.etsi.osl.tmf.OpenAPISpringBoot; +import org.etsi.osl.tmf.rcm634.api.ResourceSpecificationApiController; +import org.etsi.osl.tmf.rcm634.model.*; +import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletRequest; +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.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.List; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + + +@Transactional +@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.MOCK , classes = OpenAPISpringBoot.class) +@AutoConfigureTestDatabase +@AutoConfigureMockMvc +@ActiveProfiles("testing") +public class ResourceSpecificationApiControllerTest { + + @Autowired + private MockMvc mvc; + + @InjectMocks + ResourceSpecificationApiController resourceSpecificationApiController; + + @Mock + ResourceSpecificationRepoService resourceSpecificationRepoService; + + @Mock + ResourceSpecification resourceSpecification; + + @BeforeEach + public void init() { + MockitoAnnotations.initMocks(this); + } + + + // Test CREATE with role ADMIN + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testCreateResourceSpecificationAdmin() throws Exception { + + // Testing a valid physical resource specification at the correct endpoint + File resourceSpecFile = new File("src/test/resources/testPhysicalResourceSpec.json"); + InputStream in = new FileInputStream(resourceSpecFile); + String resourceSpecString = IOUtils.toString(in, "UTF-8"); + PhysicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(resourceSpecString, PhysicalResourceSpecificationCreate.class); + + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Spec"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + + LogicalResourceSpecificationCreate logicalResourceSpecificationCreate = new LogicalResourceSpecificationCreate(); + logicalResourceSpecificationCreate.setName("Test Logical Resource Spec"); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(logicalResourceSpecificationCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Logical Resource Spec"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Function Specification"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + // Testing a valid physical resource specification wrong endpoint + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecificat") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(status().isNotFound()) + .andReturn().getResponse(); + + // Testing a valid physical resource specification wrong payload correct endpoint + String wrongStringPayload = "random string as payload"; + byte[] wrongPayload = wrongStringPayload.getBytes(); + + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(wrongPayload)) + .andExpect(status().isBadRequest()) + .andReturn().getResponse(); + } + + // Test CREATE with incorrect role USER + @Test + @WithMockUser(authorities = {"ROLE_USER"}) + public void testCreateResourceSpecificationUser() throws Exception { + // Testing a valid physical resource specification + File physicalResourceSpecFile = new File("src/test/resources/testResourceSpec.json"); + InputStream in = new FileInputStream(physicalResourceSpecFile); + String physicalResourceSpecString = IOUtils.toString(in, "UTF-8"); + LogicalResourceSpecificationCreate physicalResourceSpecCreate = JsonUtils.toJsonObj(physicalResourceSpecString, LogicalResourceSpecificationCreate.class); + mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(physicalResourceSpecCreate))) + .andExpect(status().isForbidden()) + .andReturn().getResponse(); + + } + + // Test DELETE method with role ADMIN + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testDeleteResourceSpecificationAdmin() throws Exception { + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andReturn().getResponse().getContentAsString(); + System.out.println(createdSpecificationString); + + ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); + String idToBeDeleted = createdSpecification.getId(); + + mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/" + idToBeDeleted) + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + + } + + // Test DELETE method with incorrect role USER + @Test + @WithMockUser(authorities = {"ROLE_USER"}) + public void testDeleteResourceSpecificationUser() throws Exception { + mvc.perform(MockMvcRequestBuilders.delete("/resourceCatalogManagement/v4/resourceSpecification/idToBeDeleted") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(status().isForbidden()) + .andReturn().getResponse(); + + } + + // Test GET by id method with no role + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testRetrieveResourceSpecification() throws Exception { + ResourceFunctionSpecificationCreate resourceFunctionSpecificationCreate = new ResourceFunctionSpecificationCreate(); + resourceFunctionSpecificationCreate.setName("Test Resource Function Specification"); + resourceFunctionSpecificationCreate.setType("ResourceFunctionSpecification"); + String createdSpecificationString = mvc.perform(MockMvcRequestBuilders.post("/resourceCatalogManagement/v4/resourceSpecification") + .with(SecurityMockMvcRequestPostProcessors.csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(resourceFunctionSpecificationCreate))) + .andReturn().getResponse().getContentAsString(); + + ResourceSpecification createdSpecification = JsonUtils.toJsonObj(createdSpecificationString, ResourceFunctionSpecification.class); + String id = createdSpecification.getId(); + String fields = "name"; + mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification/" + id) + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null)) + .param("fields", fields)) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("name", is("Test Resource Function Specification"))) + .andExpect(status().isOk()) + .andReturn().getResponse(); + } + + @Test + @WithMockUser(authorities = {"ROLE_ADMIN"}) + public void testListResourceSpecification() throws Exception { + String listResSpecsResponseString = mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null))) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + List listResSpecsResponse = JsonUtils.toListOfJsonObj(listResSpecsResponseString, LogicalResourceSpecification.class); + assertEquals(7, listResSpecsResponse.size()); + + mvc.perform(MockMvcRequestBuilders.get("/resourceCatalogManagement/v4/resourceSpecification") + .contentType(MediaType.APPLICATION_JSON) + .content(JsonUtils.toJson(null)) + .param("fields", "lastUpdate")) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + } + + @Test + void testGetObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + MockHttpServletRequest request = new MockHttpServletRequest(); + ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); + + Optional result = controller.getObjectMapper(); + assertTrue(result.isPresent()); + assertEquals(objectMapper, result.get()); + } + + @Test + void testGetRequest() { + ObjectMapper objectMapper = new ObjectMapper(); + MockHttpServletRequest request = new MockHttpServletRequest(); + ResourceSpecificationApiController controller = new ResourceSpecificationApiController(objectMapper, request); + + Optional result = controller.getRequest(); + assertTrue(result.isPresent()); + assertEquals(request, result.get()); + } +} \ No newline at end of file -- GitLab