Skip to content

Commit

Permalink
feat(impl): [eclipse-tractusx#734] Handling for modification attempts…
Browse files Browse the repository at this point in the history
… on read-only default policies: tests, readability

- Add missing tests for trial to register, update or delete fallback policy
- Enhance code readability by relocating checks from the catch block to the beginning of relevant methods.
  • Loading branch information
dsmf committed Jul 23, 2024
1 parent 51bef68 commit 8e0ab60
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,19 @@ private static boolean containsNoDefaultPolicy(final Map<String, List<Policy>> b

public void deletePolicy(final String policyId) {

handleTrialToModifyFallbackPolicy(policyId);

log.info("Getting all policies to find correct BPN");
final List<String> 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);
Expand All @@ -206,30 +203,22 @@ public void deletePolicy(final String policyId) {
}

public void deletePolicyForEachBpn(final String policyId, final List<String> 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) {
Expand All @@ -242,6 +231,8 @@ public void updatePolicies(final UpdatePolicyRequest request) {
public void updatePolicy(final String policyId, final OffsetDateTime newValidUntil,
final List<String> newBusinessPartnerNumbers) {

handleTrialToModifyFallbackPolicy(policyId);

log.info("Updating policy with id {}", policyId);

final List<String> businessPartnerNumbersContainingPolicyId = findBusinessPartnerNumbersByPolicyId(policyId);
Expand Down Expand Up @@ -326,4 +317,14 @@ private List<Policy> 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));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8e0ab60

Please sign in to comment.