From 484fdc360289fb17e24a0bc4f4c443c5931a94af Mon Sep 17 00:00:00 2001 From: Sachin Argade Date: Fri, 10 May 2024 23:06:04 +0530 Subject: [PATCH 1/2] Added test cases for pcf and policy --- modules/sde-core/pom.xml | 23 ++ .../eclipse/tractusx/sde/SdeApplication.java | 2 + .../executor/step/EDCUsecaseHandler.java | 2 +- .../step/SubmoduleResponseHandler.java | 2 +- .../tractusx/sde/EnablePostgreSQL.java | 34 ++ .../tractusx/sde/PostgreSQLInitializer.java | 48 +++ .../PcfExchangeControllerTest.java | 290 ++++++++++++++++++ .../sde/controllers/PolicyControllerTest.java | 126 ++++++++ .../test/resource/application-test.properties | 123 ++++++++ .../handler/BPNDiscoveryUseCaseHandler.java | 2 +- pom.xml | 40 ++- 11 files changed, 684 insertions(+), 8 deletions(-) create mode 100644 modules/sde-core/src/test/java/org/eclipse/tractusx/sde/EnablePostgreSQL.java create mode 100644 modules/sde-core/src/test/java/org/eclipse/tractusx/sde/PostgreSQLInitializer.java create mode 100644 modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PcfExchangeControllerTest.java create mode 100644 modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PolicyControllerTest.java create mode 100644 modules/sde-core/src/test/resource/application-test.properties diff --git a/modules/sde-core/pom.xml b/modules/sde-core/pom.xml index 66b9e3798..ff41b86ef 100644 --- a/modules/sde-core/pom.xml +++ b/modules/sde-core/pom.xml @@ -199,6 +199,29 @@ opencsv 5.9 + + org.testcontainers + testcontainers + 1.19.1 + test + + + org.testcontainers + junit-jupiter + 1.19.1 + test + + + org.testcontainers + postgresql + 1.19.1 + test + + + org.springframework.security + spring-security-test + test + diff --git a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/SdeApplication.java b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/SdeApplication.java index c54df2777..513e1891e 100644 --- a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/SdeApplication.java +++ b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/SdeApplication.java @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.FeignAutoConfiguration; @@ -38,6 +39,7 @@ @ImportAutoConfiguration({FeignAutoConfiguration.class}) @EnableCaching @EnableScheduling +@EnableConfigurationProperties() public class SdeApplication { public static void main(String[] args) { diff --git a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/EDCUsecaseHandler.java b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/EDCUsecaseHandler.java index 82c5212fc..f3e275c9c 100644 --- a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/EDCUsecaseHandler.java +++ b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/EDCUsecaseHandler.java @@ -41,7 +41,7 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; -@Service("EDCUsecaseHandler") +@Service("eDCUsecaseHandler") @RequiredArgsConstructor public class EDCUsecaseHandler extends Step implements EDCUsecaseStep { diff --git a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/SubmoduleResponseHandler.java b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/SubmoduleResponseHandler.java index 643a1d59d..c058c6264 100644 --- a/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/SubmoduleResponseHandler.java +++ b/modules/sde-core/src/main/java/org/eclipse/tractusx/sde/core/submodel/executor/step/SubmoduleResponseHandler.java @@ -39,7 +39,7 @@ import lombok.RequiredArgsConstructor; -@Service("SubmoduleResponseHandler") +@Service("submoduleResponseHandler") @RequiredArgsConstructor public class SubmoduleResponseHandler extends Step implements SubmoduleMapperUsecaseStep { diff --git a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/EnablePostgreSQL.java b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/EnablePostgreSQL.java new file mode 100644 index 000000000..95614a580 --- /dev/null +++ b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/EnablePostgreSQL.java @@ -0,0 +1,34 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.sde; + +import org.springframework.test.context.ContextConfiguration; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ContextConfiguration(initializers = PostgreSQLInitializer.class) +public @interface EnablePostgreSQL { +} \ No newline at end of file diff --git a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/PostgreSQLInitializer.java b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/PostgreSQLInitializer.java new file mode 100644 index 000000000..0dd060a7a --- /dev/null +++ b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/PostgreSQLInitializer.java @@ -0,0 +1,48 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.sde; + + +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.testcontainers.containers.PostgreSQLContainer; + +public class PostgreSQLInitializer implements ApplicationContextInitializer { + static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:15.4") + .withAccessToHost(true) + .withDatabaseName("sde") + .withUsername("root") + .withPassword("P@ssword21"); + + static { + postgres.start(); + } + + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + TestPropertyValues.of( + "spring.datasource.url=" + postgres.getJdbcUrl(), + "spring.datasource.username="+ postgres.getUsername(), + "spring.datasource.password="+postgres.getPassword() + ).applyTo(applicationContext); + } +} \ No newline at end of file diff --git a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PcfExchangeControllerTest.java b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PcfExchangeControllerTest.java new file mode 100644 index 000000000..6f5972bc9 --- /dev/null +++ b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PcfExchangeControllerTest.java @@ -0,0 +1,290 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.sde.controllers; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.UUID; + +import org.eclipse.tractusx.sde.core.controller.PcfExchangeController; +import org.eclipse.tractusx.sde.edc.model.request.ConsumerRequest; +import org.eclipse.tractusx.sde.pcfexchange.request.PcfRequestModel; +import org.eclipse.tractusx.sde.pcfexchange.service.IPCFExchangeService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ContextConfiguration(classes = { PcfExchangeController.class }) +@ExtendWith(SpringExtension.class) +class PcfExchangeControllerTest { + + @MockBean + private IPCFExchangeService pcfExchangeService; + + @Autowired + private PcfExchangeController pcfExchangeController; + + + @Test + void testGetPcfConsumerDataSuccess() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/pcf/{type}/requests","CONSUMER") + .param("status", "") + .param("page", String.valueOf(0)) + .param("pageSize", String.valueOf(10)); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(200)); + } + + @Test + void testGetPcfProviderDataSuccess() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/pcf/{type}/requests","PROVIDER") + .param("status", "REQUESTED") + .param("page", String.valueOf(0)) + .param("pageSize", String.valueOf(10)); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(200)); + } + + @Test + void testGetPcfProviderDataFailure() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/pcf/{type}/requests","") + .param("status", "REQUESTED") + .param("page", String.valueOf(0)) + .param("pageSize", String.valueOf(10)); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(404)); + } + + @Test + void testGetPcfByProductSuccess() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/pcf/productIds/{productId}","test_product") + .param("BPN", "BPNL001000TS0100") + .param("requestId", UUID.randomUUID().toString()) + .param("message", "This is test request"); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().isAccepted()); + } + + @Test + void testUploadPcfSubmodelSuccess() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.put("/pcf/productIds/{productId}","test_product") + .param("BPN", "BPNL001000TS0100") + .param("requestId", UUID.randomUUID().toString()) + .param("message", "This is test request") + .contentType("application/json") + .content(getPCFJsonResponse()); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk()); + } + + @Test + void testUploadPcfSubmodelFailure() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.put("/pcf/productIds/{productId}","test_product") + .param("BPN", "") + .param("requestId", UUID.randomUUID().toString()) + .param("message", "This is test request"); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(400)); + } + + @Test + void testRequestForPcfDataOffer() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/pcf/request/{productId}","test_product") + .contentType("application/json") + .content(new ConsumerRequest().toString()); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(400)); + } + + @Test + void testViewForPcfDataOfferSuccess() throws Exception { + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/pcf/request/{requestId}",UUID.randomUUID().toString()); + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(200)); + } + + @Test + void testActionOnPcfRequestAndSendNotificationToConsumerSuccess() throws Exception { + + when(pcfExchangeService.actionOnPcfRequestAndSendNotificationToConsumer(any())) + .thenReturn(new String()); + + MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/pcf/actionsonrequest") + .contentType("application/json") + .content(new PcfRequestModel().toString());; + ResultActions actualPerformResult = MockMvcBuilders.standaloneSetup(pcfExchangeController).build() + .perform(requestBuilder); + actualPerformResult.andExpect(MockMvcResultMatchers.status().is(400)); + } + + + + private String getPCFJsonResponse() { + String bodyRequest = "{\n" + + " \"row_data\": [\n" + + " {\n" + + " \"id\": \"3893bb5d-da16-4dc1-9185-11d97476c7a7\",\n" + + " \"specVersion\": \"2.0.1-20230314\",\n" + + " \"partialFullPcf\": \"Cradle-to-gate\",\n" + + " \"precedingPfId\": \"3893bb5d-da16-4dc1-9185-11d97476c7b7\",\n" + + " \"version\": 0,\n" + + " \"created\": \"2022-05-22T21:47:32Z\",\n" + + " \"extWBCSD_pfStatus\": \"Active\",\n" + + " \"validityPeriodStart\": \"\",\n" + + " \"validityPeriodEnd\": \"\",\n" + + " \"comment\": \"Comment for version 42.\",\n" + + " \"pcfLegalStatement\": \"This PCF (Product Carbon Footprint) is for information purposes only. It is based upon the standards mentioned above.\",\n" + + " \"companyName\": \"My Corp\",\n" + + " \"companyId\": \"urn:uuid:51131FB5-42A2-4267-A402-0ECFEFAD16A9\",\n" + + " \"productDescription\": \"Ethanol, 95% solution\",\n" + + " \"productId\": \"urn:gtin:47123450605077\",\n" + + " \"extWBCSD_productCodeCpc\": \"011-99000\",\n" + + " \"productName\": \"My Product Name\",\n" + + " \"declaredUnit\": \"liter\",\n" + + " \"unitaryProductAmount\": 1000.0,\n" + + " \"productMassPerDeclaredUnit\": 0.456,\n" + + " \"exemptedEmissionsPercent\": 0.0,\n" + + " \"exemptedEmissionsDescription\": \"No exemption\",\n" + + " \"extWBCSD_packagingEmissionsIncluded\": \"true\",\n" + + " \"boundaryProcessesDescription\": \"Electricity consumption included as an input in the production phase\",\n" + + " \"geographyCountrySubdivision\": \"US-NY\",\n" + + " \"geographyCountry\": \"DE\",\n" + + " \"geographyRegionOrSubregion\": \"Africa\",\n" + + " \"referencePeriodStart\": \"2022-01-01T00:00:01Z\",\n" + + " \"referencePeriodEnd\": \"2022-12-31T23:59:59Z\",\n" + + " \"crossSectoralStandard\": \"GHG Protocol Product standard\",\n" + + " \"extWBCSD_operator\": \"PEF\",\n" + + " \"ruleName\": \"urn:tfs-initiative.com:PCR:The Product Carbon Footprint Guideline for the Chemical Industry:version:v2.0\",\n" + + " \"extWBCSD_otherOperatorName\": \"NSF\",\n" + + " \"extWBCSD_characterizationFactors\": \"AR5\",\n" + + " \"extWBCSD_allocationRulesDescription\": \"In accordance with Catena-X PCF Rulebook\",\n" + + " \"extTFS_allocationWasteIncineration\": \"cut-off\",\n" + + " \"primaryDataShare\": 56.12,\n" + + " \"secondaryEmissionFactorSource\": \"ecoinvent 3.8\",\n" + + " \"coveragePercent\": 100,\n" + + " \"technologicalDQR\": 2.0,\n" + + " \"temporalDQR\": 2.0,\n" + + " \"geographicalDQR\": 2.0,\n" + + " \"completenessDQR\": 2.0,\n" + + " \"reliabilityDQR\": 2.0,\n" + + " \"pcfExcludingBiogenic\": 2.0,\n" + + " \"pcfIncludingBiogenic\": 1.0,\n" + + " \"fossilGhgEmissions\": 0.5,\n" + + " \"biogenicCarbonEmissionsOtherThanCO2\": 1.0,\n" + + " \"biogenicCarbonWithdrawal\": 0.0,\n" + + " \"dlucGhgEmissions\": 0.4,\n" + + " \"extTFS_luGhgEmissions\": 0.3,\n" + + " \"aircraftGhgEmissions\": 0.0,\n" + + " \"extWBCSD_packagingGhgEmissions\": 0,\n" + + " \"distributionStagePcfExcludingBiogenic\": 1.5,\n" + + " \"distributionStagePcfIncludingBiogenic\": 0.0,\n" + + " \"distributionStageFossilGhgEmissions\": 0.5,\n" + + " \"distributionStageBiogenicCarbonEmissionsOtherThanCO2\": 1.0,\n" + + " \"distributionStageBiogenicCarbonWithdrawal\": 0.5,\n" + + " \"extTFS_distributionStageDlucGhgEmissions\": 1.0,\n" + + " \"extTFS_distributionStageLuGhgEmissions\": 1.1,\n" + + " \"carbonContentTotal\": 2.5,\n" + + " \"extWBCSD_fossilCarbonContent\": 0.1,\n" + + " \"carbonContentBiogenic\": 0.0,\n" + + " \"assetLifeCyclePhase\": \"AsPlanned\"\n" + + " }\n" + + " ],\n" + + " \"access_policies\": [\n" + + " {\n" + + " \"technicalKey\": \"BusinessPartnerNumber\",\n" + + " \"value\": [\n" + + " \"BPNL001000TS0100\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"Membership\",\n" + + " \"value\": [\n" + + " \"active\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"companyRole.dismantler\",\n" + + " \"value\": [\n" + + " \"active\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + " \"usage_policies\": [\n" + + " {\n" + + " \"technicalKey\": \"Membership\",\n" + + " \"value\": [\n" + + " \"active\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"companyRole.dismantler\",\n" + + " \"value\": [\n" + + " \"active\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"FrameworkAgreement.pcf\",\n" + + " \"value\": [\n" + + " \"active:v1.0.0\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"FrameworkAgreement.sustainability\",\n" + + " \"value\": [\n" + + " \"active:v1.0.0\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"PURPOSE\",\n" + + " \"value\": []\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"PURPOSE\",\n" + + " \"value\": []\n" + + " },\n" + + " {\n" + + " \"technicalKey\": \"CUSTOM\",\n" + + " \"value\": []\n" + + " }\n" + + " ]\n" + + "}"; + + return bodyRequest; + } + +} \ No newline at end of file diff --git a/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PolicyControllerTest.java b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PolicyControllerTest.java new file mode 100644 index 000000000..bc022bbcd --- /dev/null +++ b/modules/sde-core/src/test/java/org/eclipse/tractusx/sde/controllers/PolicyControllerTest.java @@ -0,0 +1,126 @@ +/******************************************************************************** + * Copyright (c) 2024 T-Systems International GmbH + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.sde.controllers; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.List; + +import org.eclipse.tractusx.sde.EnablePostgreSQL; +import org.eclipse.tractusx.sde.common.entities.Policies; +import org.eclipse.tractusx.sde.common.entities.PolicyModel; +import org.eclipse.tractusx.sde.core.policy.entity.PolicyEntity; +import org.eclipse.tractusx.sde.core.policy.repository.PolicyRepository; +import org.eclipse.tractusx.sde.core.policy.service.PolicyService; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.fasterxml.jackson.databind.ObjectMapper; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +@ActiveProfiles("test") +@WithMockUser(username = "Admin", authorities = { "Admin" }) +@EnablePostgreSQL +class PolicyControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private PolicyRepository policyRepository; + + @Autowired + private PolicyService policyService; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @BeforeEach + public void init() { + policyRepository.deleteAll(); + } + + //@Test + void testSaveAndRetrievePolicy() throws Exception { + mvc.perform(MockMvcRequestBuilders.post("/policy").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(getPolicy("new_policy")))).andExpect(status().isOk()); + + Assertions.assertEquals(1, policyRepository.findAll().size()); + List entities = policyRepository.findAll(); + String uuid = entities.get(0).getUuid(); + + mvc.perform(MockMvcRequestBuilders.get("/policy/{uuid}", uuid).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + } + + //@Test + void testUniquePolicies() throws Exception { + mvc.perform(MockMvcRequestBuilders.post("/policy").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(getPolicy("new_policy")))).andExpect(status().isOk()); + + Assertions.assertEquals(1, policyRepository.findAll().size()); + + mvc.perform(MockMvcRequestBuilders.post("/policy").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(getPolicy("new_policy")))).andExpect(status().isBadRequest()); + + Assertions.assertEquals(1, policyRepository.findAll().size()); + + } + + //@Test + void findMatchingPolicyBasedOnFileName() throws Exception { + + String fileName = "Mysubmodel_new_policy.csv"; + mvc.perform(MockMvcRequestBuilders.post("/policy").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(getPolicy("new_policy")))).andExpect(status().isOk()); + + List findMatchingPolicyBasedOnFileName = policyService.findMatchingPolicyBasedOnFileName(fileName); + Assertions.assertEquals(1, findMatchingPolicyBasedOnFileName.size()); + Assertions.assertEquals("new_policy", findMatchingPolicyBasedOnFileName.get(0).getPolicyName()); + + } + + private PolicyModel getPolicy(String policyName) { + + List accessPolicies = List.of( + Policies.builder().technicalKey("BusinessPartnerNumber") + .value(List.of("BPNL00000005PROV", "BPNL00000005PROW", "BPNL00000005PROB")).build(), + Policies.builder().technicalKey("Membership").value(List.of("active")).build()); + + Policies usagePolicies = Policies.builder().technicalKey("Membership").value(List.of("active")).build(); + + return PolicyModel.builder().policyName(policyName).accessPolicies(accessPolicies) + .usagePolicies(List.of(usagePolicies)).build(); + } +} \ No newline at end of file diff --git a/modules/sde-core/src/test/resource/application-test.properties b/modules/sde-core/src/test/resource/application-test.properties new file mode 100644 index 000000000..5f2f80790 --- /dev/null +++ b/modules/sde-core/src/test/resource/application-test.properties @@ -0,0 +1,123 @@ +#/******************************************************************************** +#* Copyright (c) 2024 T-Systems International GmbH +#* Copyright (c) 2024 Contributors to the Eclipse Foundation +#* +#* See the NOTICE file(s) distributed with this work for additional +#* information regarding copyright ownership. +#* +#* This program and the accompanying materials are made available under the +#* terms of the Apache License, Version 2.0 which is available at +#* https://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. +#* +#* SPDX-License-Identifier: Apache-2.0 +#********************************************************************************/ + +#Oauth 2 configuration with keycloak +keycloak.clientid=test +spring.security.oauth2.resourceserver.jwt.issuer-uri=test + +# Enable multipart uploads +spring.servlet.multipart.enabled=true +spring.main.allow-bean-definition-overriding=true + +# Threshold after which files are written to disk. +spring.servlet.multipart.file-size-threshold=2KB + +# Max file size. +spring.servlet.multipart.max-file-size=200MB + + +# Max Request Size +spring.servlet.multipart.max-request-size=215MB +server.servlet.context-path=/api + + +## Flyway +spring.flyway.baseline-on-migrate=true +spring.flyway.locations=classpath:/flyway + +spring.datasource.driver-class-name=org.postgresql.Driver +spring.jpa.hibernate.ddl-auto=update +spring.jpa.open-in-view=false + +## File Storage Properties +file.upload-dir=./temp/ + + +## LOGGING +logging.level.org.apache.http=info +logging.level.root=info + +## Digital Twins +digital-twins.hostname=test +digital-twins.authentication.url=test +digital-twins.api=/api/v3.0 +digital-twins.authentication.clientId=test +digital-twins.authentication.clientSecret=test +digital-twins.authentication.grantType=client_credentials +digital-twins.managed.thirdparty=false +digital-twins.registry.uri=test +digital-twins.registry.lookup.uri=test +digital-twins.authentication.scope=test + +## EDC Provider +edc.hostname=test +edc.managementpath=/data/v2 +edc.apiKeyHeader=test +edc.apiKey=test +edc.managementpath.apiversion=/v2 +edc.managementpath.apiversion.asset=/v3 + +## EDC Consumer +edc.consumer.hostname=test +edc.consumer.apikeyheader=test +edc.consumer.apikey=test +edc.consumer.managementpath=/data/v2 +edc.consumer.protocol.path=/api/v1/dsp + +## DFT configuration for EDC +dft.hostname=test +dft.apiKeyHeader=test +dft.apiKey=test + +## Manufacturer Id +manufacturerId=test + + +##Partner Pool Api Host +partner.pool.hostname=test +partner.pool.authentication.url=test +partner.pool.clientId=test +partner.pool.clientSecret=test +partner.pool.grantType=client_credentials + + +## Portal backend and Connector discovery +portal.backend.hostname=test +portal.backend.authentication.url=test +portal.backend.clientId=test +portal.backend.clientSecret=test +portal.backend.grantType=client_credentials + +## bpn & discovery finder +bpndiscovery.hostname=http://test +discovery.authentication.url=test +discovery.clientId=test +discovery.clientSecret=test +discovery.grantType=client_credentials + +springdoc.api-docs.path=/api-docs +springdoc.swagger-ui.oauth.client-id=test + +# policy-hub config +policy.hub.hostname=test +policy.hub.authentication.url=test +policy.hub.clientId=test +policy.hub.clientSecret=test +policy.hub.grantType=client_credentials \ No newline at end of file diff --git a/modules/sde-external-services/bpn-discovery/src/main/java/org/eclipse/tractusx/sde/bpndiscovery/handler/BPNDiscoveryUseCaseHandler.java b/modules/sde-external-services/bpn-discovery/src/main/java/org/eclipse/tractusx/sde/bpndiscovery/handler/BPNDiscoveryUseCaseHandler.java index b176dc201..49a7c5fb2 100644 --- a/modules/sde-external-services/bpn-discovery/src/main/java/org/eclipse/tractusx/sde/bpndiscovery/handler/BPNDiscoveryUseCaseHandler.java +++ b/modules/sde-external-services/bpn-discovery/src/main/java/org/eclipse/tractusx/sde/bpndiscovery/handler/BPNDiscoveryUseCaseHandler.java @@ -42,7 +42,7 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; -@Service +@Service("bPNDiscoveryUseCaseHandler") @RequiredArgsConstructor public class BPNDiscoveryUseCaseHandler extends Step implements BPNDiscoveryUsecaseStep { diff --git a/pom.xml b/pom.xml index 6ecbc4bfa..330135856 100644 --- a/pom.xml +++ b/pom.xml @@ -43,8 +43,8 @@ 17 2.17.1 2022.0.3 - 1.5.5.Final - 1.5.5.Final + 1.4.2.Final + 1.4.2.Final 2.0 @@ -173,7 +173,7 @@ org.mapstruct mapstruct - ${org.mapstruct.version} + 1.4.2.Final org.springframework.boot @@ -203,6 +203,13 @@ pom import + + org.testcontainers + testcontainers-bom + 1.19.0 + pom + import + @@ -212,7 +219,7 @@ org.eclipse.dash license-tool-plugin - 1.1.0 + 1.0.3-SNAPSHOT automotive.tractusx @@ -233,6 +240,29 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + ${lombok.version} + + + org.mapstruct + mapstruct-processor + ${org.mapstruct.processor.version} + + + + + -Amapstruct.unmappedTargetPolicy=IGNORE + + + + @@ -277,4 +307,4 @@ - + \ No newline at end of file From 19944d68199553e1c4fc46475d1ba3e3b4d38a0d Mon Sep 17 00:00:00 2001 From: Sachin Argade Date: Fri, 10 May 2024 23:09:05 +0530 Subject: [PATCH 2/2] update change log --- CHANGELOG.md | 3 ++- DEPENDENCIES | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 458992be6..3371df6a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Support EDC 7. - Added new files for digital twin access rule support. - Refactor code for pcf, dt access API, EDC 7. -- Dt access api use in digital twin processing +- Dt access api use in digital twin processing. +- Added test cases for PCF and policy controller. ### Fixed - Remove garbage character from 'edc_request_template' path. Fixed [#147](https://github.com/eclipse-tractusx/managed-simple-data-exchanger-backend/issues/147). diff --git a/DEPENDENCIES b/DEPENDENCIES index 4ece843f5..b4216370e 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -11,6 +11,9 @@ maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.15.3 maven/mavencentral/com.fasterxml.jackson.module/jackson-module-parameter-names/2.15.3, Apache-2.0, approved, #8803 maven/mavencentral/com.fasterxml/classmate/1.5.1, Apache-2.0, approved, clearlydefined maven/mavencentral/com.github.ben-manes.caffeine/caffeine/3.1.8, Apache-2.0, approved, clearlydefined +maven/mavencentral/com.github.docker-java/docker-java-api/3.3.3, Apache-2.0, approved, #10346 +maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.3.3, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #7946 +maven/mavencentral/com.github.docker-java/docker-java-transport/3.3.3, Apache-2.0, approved, #7942 maven/mavencentral/com.github.stephenc.jcip/jcip-annotations/1.0-1, Apache-2.0, approved, CQ21949 maven/mavencentral/com.google.code.gson/gson/2.10.1, Apache-2.0, approved, #6159 maven/mavencentral/com.google.errorprone/error_prone_annotations/2.21.1, Apache-2.0, approved, #9834 @@ -43,12 +46,15 @@ maven/mavencentral/jakarta.persistence/jakarta.persistence-api/3.1.0, EPL-2.0 OR maven/mavencentral/jakarta.transaction/jakarta.transaction-api/2.0.1, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.jta maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, ee4j.validation maven/mavencentral/jakarta.xml.bind/jakarta.xml.bind-api/4.0.1, BSD-3-Clause, approved, ee4j.jaxb +maven/mavencentral/junit/junit/4.13.2, EPL-2.0, approved, CQ23636 maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.10, Apache-2.0, approved, #7164 maven/mavencentral/net.bytebuddy/byte-buddy/1.14.10, Apache-2.0 AND BSD-3-Clause, approved, #7163 +maven/mavencentral/net.java.dev.jna/jna/5.12.1, Apache-2.0 OR LGPL-2.1-or-later, approved, #3217 maven/mavencentral/net.minidev/accessors-smart/2.4.11, Apache-2.0, approved, #7515 maven/mavencentral/net.minidev/json-smart/2.4.11, Apache-2.0, approved, #3288 maven/mavencentral/org.antlr/antlr4-runtime/4.10.1, BSD-3-Clause AND LicenseRef-Public-domain AND MIT AND LicenseRef-Unicode-TOU, approved, #7065 maven/mavencentral/org.apache.commons/commons-collections4/4.4, Apache-2.0, approved, clearlydefined +maven/mavencentral/org.apache.commons/commons-compress/1.24.0, Apache-2.0 AND BSD-3-Clause AND bzip2-1.0.6 AND LicenseRef-Public-Domain, approved, #10368 maven/mavencentral/org.apache.commons/commons-csv/1.8, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apache.commons/commons-lang3/3.12.0, Apache-2.0, approved, clearlydefined maven/mavencentral/org.apache.commons/commons-text/1.10.0, Apache-2.0, approved, clearlydefined @@ -84,18 +90,20 @@ maven/mavencentral/org.flywaydb/flyway-core/9.16.3, Apache-2.0, approved, #7935 maven/mavencentral/org.glassfish.jaxb/jaxb-core/4.0.4, BSD-3-Clause, approved, ee4j.jaxb-impl maven/mavencentral/org.glassfish.jaxb/jaxb-runtime/4.0.4, BSD-3-Clause, approved, ee4j.jaxb-impl maven/mavencentral/org.glassfish.jaxb/txw2/4.0.4, BSD-3-Clause, approved, ee4j.jaxb-impl +maven/mavencentral/org.hamcrest/hamcrest-core/2.2, BSD-3-Clause, approved, clearlydefined maven/mavencentral/org.hamcrest/hamcrest/2.2, BSD-3-Clause, approved, clearlydefined maven/mavencentral/org.hibernate.common/hibernate-commons-annotations/6.0.6.Final, LGPL-2.1-only, approved, #6962 maven/mavencentral/org.hibernate.orm/hibernate-core/6.2.13.Final, LGPL-2.1-only AND Apache-2.0 AND MIT AND CC-PDDC AND (EPL-2.0 OR BSD-3-Clause), approved, #9121 maven/mavencentral/org.hibernate.validator/hibernate-validator/8.0.1.Final, Apache-2.0, approved, clearlydefined maven/mavencentral/org.jboss.logging/jboss-logging/3.5.3.Final, Apache-2.0, approved, #9471 +maven/mavencentral/org.jetbrains/annotations/17.0.0, Apache-2.0, approved, clearlydefined maven/mavencentral/org.junit.jupiter/junit-jupiter-api/5.9.3, EPL-2.0, approved, #3133 maven/mavencentral/org.junit.jupiter/junit-jupiter-engine/5.9.3, EPL-2.0, approved, #3125 maven/mavencentral/org.junit.jupiter/junit-jupiter-params/5.9.3, EPL-2.0, approved, #3134 maven/mavencentral/org.junit.jupiter/junit-jupiter/5.9.3, EPL-2.0, approved, #6972 maven/mavencentral/org.junit.platform/junit-platform-commons/1.9.3, EPL-2.0, approved, #3130 maven/mavencentral/org.junit.platform/junit-platform-engine/1.9.3, EPL-2.0, approved, #3128 -maven/mavencentral/org.mapstruct/mapstruct/1.5.5.Final, Apache-2.0, approved, #6277 +maven/mavencentral/org.mapstruct/mapstruct/1.4.2.Final, Apache-2.0, approved, #2483 maven/mavencentral/org.mockito/mockito-core/4.8.1, MIT, approved, clearlydefined maven/mavencentral/org.mockito/mockito-junit-jupiter/4.8.1, MIT, approved, clearlydefined maven/mavencentral/org.objenesis/objenesis/3.2, Apache-2.0, approved, clearlydefined @@ -103,6 +111,7 @@ maven/mavencentral/org.opentest4j/opentest4j/1.2.0, Apache-2.0, approved, clearl maven/mavencentral/org.ow2.asm/asm/9.3, BSD-3-Clause, approved, clearlydefined maven/mavencentral/org.postgresql/postgresql/42.7.3, BSD-2-Clause AND Apache-2.0, approved, #11681 maven/mavencentral/org.projectlombok/lombok/1.18.30, MIT AND LicenseRef-Public-Domain, approved, CQ23907 +maven/mavencentral/org.rnorth.duct-tape/duct-tape/1.0.8, MIT, approved, clearlydefined maven/mavencentral/org.skyscreamer/jsonassert/1.5.1, Apache-2.0, approved, clearlydefined maven/mavencentral/org.slf4j/jul-to-slf4j/2.0.9, MIT, approved, #7698 maven/mavencentral/org.slf4j/slf4j-api/2.0.9, MIT, approved, #5915 @@ -141,6 +150,7 @@ maven/mavencentral/org.springframework.security/spring-security-oauth2-core/6.1. maven/mavencentral/org.springframework.security/spring-security-oauth2-jose/6.1.5, Apache-2.0, approved, #9345 maven/mavencentral/org.springframework.security/spring-security-oauth2-resource-server/6.1.5, Apache-2.0, approved, #8798 maven/mavencentral/org.springframework.security/spring-security-rsa/1.0.11.RELEASE, Apache-2.0, approved, CQ20647 +maven/mavencentral/org.springframework.security/spring-security-test/6.1.5, Apache-2.0, approved, #10674 maven/mavencentral/org.springframework.security/spring-security-web/6.1.5, Apache-2.0, approved, #9800 maven/mavencentral/org.springframework/spring-aop/6.0.14, Apache-2.0, approved, #5940 maven/mavencentral/org.springframework/spring-aspects/6.0.14, Apache-2.0, approved, #5930 @@ -156,6 +166,11 @@ maven/mavencentral/org.springframework/spring-test/6.0.14, Apache-2.0, approved, maven/mavencentral/org.springframework/spring-tx/6.0.14, Apache-2.0, approved, #5926 maven/mavencentral/org.springframework/spring-web/6.0.14, Apache-2.0, approved, #5942 maven/mavencentral/org.springframework/spring-webmvc/6.0.14, Apache-2.0, approved, #5944 +maven/mavencentral/org.testcontainers/database-commons/1.19.0, Apache-2.0, approved, #10345 +maven/mavencentral/org.testcontainers/jdbc/1.19.0, Apache-2.0, approved, #10348 +maven/mavencentral/org.testcontainers/junit-jupiter/1.19.1, MIT, approved, #10344 +maven/mavencentral/org.testcontainers/postgresql/1.19.1, MIT, approved, #10350 +maven/mavencentral/org.testcontainers/testcontainers/1.19.1, Apache-2.0 AND MIT, approved, #10347 maven/mavencentral/org.webjars/swagger-ui/5.13.0, Apache-2.0, approved, #14547 maven/mavencentral/org.xmlunit/xmlunit-core/2.9.1, Apache-2.0, approved, #6272 maven/mavencentral/org.yaml/snakeyaml/2.0, Apache-2.0 AND (Apache-2.0 OR BSD-3-Clause OR EPL-1.0 OR GPL-2.0-or-later OR LGPL-2.1-or-later), approved, #7275