diff --git a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java index 3b2d0d1d7..706e23a9b 100644 --- a/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java +++ b/irs-policy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreService.java @@ -182,22 +182,19 @@ private static boolean containsNoDefaultPolicy(final Map> b public void deletePolicy(final String policyId) { + handleTrialToModifyFallbackPolicy(policyId); + log.info("Getting all policies to find correct BPN"); final List bpnsContainingPolicyId = PolicyHelper.findBpnsByPolicyId(getAllStoredPolicies(), policyId); if (bpnsContainingPolicyId.isEmpty()) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Policy with id '%s' not found".formatted(policyId)); - } else if (bpnsContainingPolicyId.stream().noneMatch(StringUtils::isNotEmpty)) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, // - "A configured default policy cannot be deleted. " - + "It can be overridden by defining a default policy via the API instead."); } else { try { log.info("Deleting policy with id {}", policyId); bpnsContainingPolicyId.forEach(bpn -> persistence.delete(bpn, policyId)); } catch (final ResourceDoesNotExistException e) { - handleTrialToModifyConfiguredDefaultPolicy(policyId, e); throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e); } catch (final PolicyStoreException e) { throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e); @@ -206,30 +203,22 @@ public void deletePolicy(final String policyId) { } public void deletePolicyForEachBpn(final String policyId, final List bpnList) { + + handleTrialToModifyFallbackPolicy(policyId); + try { for (final String bpn : bpnList) { persistence.delete(bpn, policyId); } } catch (final ResourceDoesNotExistException e) { - handleTrialToModifyConfiguredDefaultPolicy(policyId, e); throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e); } catch (final PolicyStoreException e) { throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e); } } - private void handleTrialToModifyConfiguredDefaultPolicy(final String policyId, final Exception cause) { - if (isConfiguredDefaultPolicy(policyId)) { - throw new ResponseStatusException(HttpStatus.BAD_REQUEST, - ("The policy '%s' is a configured default policy which cannot be modified. " - + "However you can define custom default policies via the API by registering a policy " - + "without a business partner number. " - + "These will take precedence over configured default policies.").formatted(policyId), cause); - } - } - - private boolean isConfiguredDefaultPolicy(final String policyId) { - return this.allowedPoliciesFromConfig.stream().anyMatch(p -> p.getPolicyId().equals(policyId)); + private boolean isFallbackPolicyFromConfiguration(final String policyId) { + return this.allowedPoliciesFromConfig.stream().map(Policy::getPolicyId).anyMatch(p -> p.equals(policyId)); } public void updatePolicies(final UpdatePolicyRequest request) { @@ -242,6 +231,8 @@ public void updatePolicies(final UpdatePolicyRequest request) { public void updatePolicy(final String policyId, final OffsetDateTime newValidUntil, final List newBusinessPartnerNumbers) { + handleTrialToModifyFallbackPolicy(policyId); + log.info("Updating policy with id {}", policyId); final List businessPartnerNumbersContainingPolicyId = findBusinessPartnerNumbersByPolicyId(policyId); @@ -326,4 +317,14 @@ private List createDefaultPolicyFromConfig(final DefaultAcceptedPolicies return StringMapper.mapFromBase64String(defaultPoliciesConfig.getAcceptedPolicies(), LIST_OF_POLICIES_TYPE); } + private void handleTrialToModifyFallbackPolicy(final String policyId) { + if (isFallbackPolicyFromConfiguration(policyId)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, + ("The policy '%s' is a configured default policy which cannot be modified. " + + "However you can define custom default policies via the API by registering a policy " + + "without a business partner number. " + + "These will take precedence over configured default policies.").formatted(policyId)); + } + } + } diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java index f5a43e34e..f9bb518de 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/controllers/PolicyStoreControllerTest.java @@ -68,7 +68,7 @@ public class PolicyStoreControllerTest { "@context": { "odrl": "http://www.w3.org/ns/odrl/2/" }, - "@id": "e917f5f-8dac-49ac-8d10-5b4d254d2b48", + "@id": "%s", "policy": { "odrl:permission": [ { @@ -118,7 +118,8 @@ class RegisterPolicyTests { void registerAllowedPolicy() { // arrange final OffsetDateTime now = OffsetDateTime.now(); - final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD); + final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject( + REGISTER_POLICY_EXAMPLE_PAYLOAD.formatted("e917f5f-8dac-49ac-8d10-5b4d254d2b48")); // act final CreatePolicyRequest request = new CreatePolicyRequest(now.plusMinutes(1), null, diff --git a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java index 8022ad32a..bf27031a8 100644 --- a/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java +++ b/irs-policy-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyStoreServiceTest.java @@ -149,19 +149,38 @@ void setUp() { @DisplayName("registerPolicy") class RegisterPolicyTests { + @Test + void registerPolicy_whenFallbackPolicyId_shouldReturnHttpStatus400() { + final JsonObject policy = PolicyStoreTestUtil.toJsonObject( + REGISTER_POLICY_EXAMPLE_PAYLOAD.formatted(CONFIGURED_DEFAULT_POLICY_ID)); + assertThatThrownBy(() -> testee.registerPolicy(CreatePolicyRequest.builder() + .businessPartnerNumber("BPNL1234567890AB") + .validUntil(OffsetDateTime.now(clock) + .plusYears(2)) + .payload(policy) + .build())).isInstanceOf( + ResponseStatusException.class) + .hasMessageContaining( + "400 BAD_REQUEST") + .hasMessageContaining( + CONFIGURED_DEFAULT_POLICY_ID); + } + @Test void registerPolicy_withBpnNull_shouldStoreAsDefault() { // ARRANGE final OffsetDateTime now = OffsetDateTime.now(); - final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD); + final String policyId = "e917f5f-8dac-49ac-8d10-5b4d254d2b48"; + final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject( + REGISTER_POLICY_EXAMPLE_PAYLOAD.formatted(policyId)); // ACT testee.registerPolicy(new CreatePolicyRequest(now, null, jsonObject)); // ASSERT verify(persistenceMock).save(eq(PolicyStoreService.BPN_DEFAULT), policyCaptor.capture()); - assertThat(policyCaptor.getValue().getPolicyId()).isEqualTo("e917f5f-8dac-49ac-8d10-5b4d254d2b48"); + assertThat(policyCaptor.getValue().getPolicyId()).isEqualTo(policyId); assertThat(policyCaptor.getValue().getValidUntil()).isEqualTo(now); assertThat(policyCaptor.getValue().getPermissions()).hasSize(1); } @@ -171,7 +190,9 @@ void registerPolicy_success() { // ARRANGE final OffsetDateTime now = OffsetDateTime.now(); - final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject(REGISTER_POLICY_EXAMPLE_PAYLOAD); + final String policyId = "e917f5f-8dac-49ac-8d10-5b4d254d2b48"; + final JsonObject jsonObject = PolicyStoreTestUtil.toJsonObject( + REGISTER_POLICY_EXAMPLE_PAYLOAD.formatted(policyId)); // ACT final OffsetDateTime validUntil = now.plusMonths(1); @@ -182,7 +203,7 @@ void registerPolicy_success() { // ASSERT verify(persistenceMock).save(eq("BPNL00000123ABCD"), policyCaptor.capture()); - assertThat(policyCaptor.getValue().getPolicyId()).isEqualTo("e917f5f-8dac-49ac-8d10-5b4d254d2b48"); + assertThat(policyCaptor.getValue().getPolicyId()).isEqualTo(policyId); assertThat(policyCaptor.getValue().getValidUntil()).isEqualTo(validUntil); assertThat(policyCaptor.getValue().getPermissions()).isNotEmpty(); @@ -538,11 +559,48 @@ void deletePolicy_whenPolicyNotFound_shouldReturnHttpStatus404() { notExistingPolicyId); } + @Test + void deletePolicy_whenFallbackPolicy_shouldReturnHttpStatus400() { + assertThatThrownBy(() -> testee.deletePolicy(CONFIGURED_DEFAULT_POLICY_ID)).isInstanceOf( + ResponseStatusException.class) + .hasMessageContaining( + "400 BAD_REQUEST") + .hasMessageContaining( + CONFIGURED_DEFAULT_POLICY_ID); + } + + @Test + void deletePolicyForEachBpn_whenFallbackPolicy_shouldReturnHttpStatus400() { + assertThatThrownBy(() -> testee.deletePolicyForEachBpn(CONFIGURED_DEFAULT_POLICY_ID, + List.of("BPNL1234567890AB", "BPNL1234567890CD"))).isInstanceOf(ResponseStatusException.class) + .hasMessageContaining("400 BAD_REQUEST") + .hasMessageContaining( + CONFIGURED_DEFAULT_POLICY_ID); + } + } @Nested class UpdatePoliciesTests { + @Test + void updatePolicies_whenFallbackPolicy_shouldReturnHttpStatus400() { + assertThatThrownBy(() -> testee.updatePolicies( + new UpdatePolicyRequest(OffsetDateTime.now(clock), List.of("BPNL1234567890AB", "BPNL1234567890CD"), + List.of(CONFIGURED_DEFAULT_POLICY_ID)))).isInstanceOf(ResponseStatusException.class) + .hasMessageContaining("400 BAD_REQUEST") + .hasMessageContaining(CONFIGURED_DEFAULT_POLICY_ID); + } + + @Test + void updatePolicy_whenFallbackPolicy_shouldReturnHttpStatus400() { + assertThatThrownBy(() -> testee.updatePolicy(CONFIGURED_DEFAULT_POLICY_ID, OffsetDateTime.now(clock), + List.of("BPNL1234567890AB", "BPNL1234567890CD"))).isInstanceOf(ResponseStatusException.class) + .hasMessageContaining("400 BAD_REQUEST") + .hasMessageContaining( + CONFIGURED_DEFAULT_POLICY_ID); + } + @Test void updatePolicies_shouldUpdateBpnAndValidUntil() { // ARRANGE