Skip to content

Commit

Permalink
feat(impl): [#199] Avoid too many methods by extraction to helper class
Browse files Browse the repository at this point in the history
inlcuding test for helper class
  • Loading branch information
dsmf committed Mar 13, 2024
1 parent 05ec245 commit 038b85a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,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.irs.policystore.services;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import org.eclipse.tractusx.irs.edc.client.policy.Policy;

/**
* Helper methods for policies.
*/
public final class PolicyHelper {

private PolicyHelper() {
}

public static List<String> findBpnsByPolicyId(final Map<String, List<Policy>> policyMap, final String policyId) {
return policyMap.entrySet().stream().filter(mapEntriesByPolicyId(policyId)).map(Map.Entry::getKey).toList();
}

private static Predicate<Map.Entry<String, List<Policy>>> mapEntriesByPolicyId(final String policyId) {
return entry -> entry.getValue().stream().anyMatch(havingPolicyId(policyId));
}

public static Predicate<Policy> havingPolicyId(final String policyId) {
return policy -> policy.getPolicyId().equals(policyId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.function.Predicate;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPoliciesProvider;
Expand Down Expand Up @@ -91,10 +90,12 @@ public void registerPolicy(final Policy policy, final List<String> businessPartn
* @param policy policy to register
*/
private void validatePolicy(final Policy policy) {

if (policy.getPermissions() == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
String.format(MISSING_REQUEST_FIELD_MESSAGE, "odrl:permission"));
}

if (policy.getPermissions().stream().anyMatch(p -> p.getConstraint() == null)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
String.format(MISSING_REQUEST_FIELD_MESSAGE, "odrl:constraint"));
Expand Down Expand Up @@ -125,7 +126,8 @@ public Map<String, List<Policy>> getAllStoredPolicies() {
public void deletePolicy(final String policyId) {
try {
log.info("Getting all policies to find correct bpn number");
final List<String> bpnsContainingPolicyId = findBpnsByPolicyId(policyId);
final List<String> bpnsContainingPolicyId = PolicyHelper.findBpnsByPolicyId(getAllStoredPolicies(),
policyId);

log.info("Deleting policy with id {}", policyId);
bpnsContainingPolicyId.forEach(bpn -> persistence.delete(bpn, policyId));
Expand All @@ -144,11 +146,12 @@ public void updatePolicies(final UpdatePolicyRequest request) {
private void updatePolicy(final String policyId, final OffsetDateTime validUntil, final List<String> bpns) {
try {
log.info("Updating policy with id {}", policyId);
final List<String> bpnsContainingPolicyId = findBpnsByPolicyId(policyId);
final List<String> bpnsContainingPolicyId = PolicyHelper.findBpnsByPolicyId(getAllStoredPolicies(),
policyId);

final Policy policyToUpdate = getStoredPolicies(bpnsContainingPolicyId).stream()
.filter(policy -> policy.getPolicyId()
.equals(policyId))
.filter(PolicyHelper.havingPolicyId(
policyId))
.findAny()
.orElseThrow(
() -> new PolicyStoreException(
Expand All @@ -164,18 +167,6 @@ private void updatePolicy(final String policyId, final OffsetDateTime validUntil
}
}

private List<String> findBpnsByPolicyId(final String policyId) {
return findBpnsByPolicyId(getAllStoredPolicies(), policyId);
}

private static List<String> findBpnsByPolicyId(final Map<String, List<Policy>> policyMap, final String policyId) {
return policyMap.entrySet().stream().filter(byPolicyId(policyId)).map(Map.Entry::getKey).toList();
}

private static Predicate<Map.Entry<String, List<Policy>>> byPolicyId(final String policyId) {
return entry -> entry.getValue().stream().anyMatch(policy -> policy.getPolicyId().equals(policyId));
}

@Override
public List<AcceptedPolicy> getAcceptedPolicies(final List<String> bpns) {
if (bpns == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,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.irs.policystore.services;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

import java.util.HashMap;
import java.util.List;

import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.junit.jupiter.api.Test;

class PolicyHelperTest {

@Test
void shouldFilterMapByPolicyId() {

// ARRANGE
final HashMap<String, List<Policy>> policyMap = new HashMap<>();
final String policyIdToFind = "policyIdToFind";
policyMap.put("BPN1", List.of(Policy.builder().policyId(policyIdToFind).build(),
Policy.builder().policyId("policy1").build()));
policyMap.put("BPN2", List.of(Policy.builder().policyId("policy2").build(),
Policy.builder().policyId(policyIdToFind).build()));
policyMap.put("BPN3",
List.of(Policy.builder().policyId("policy3").build(), Policy.builder().policyId("policy4").build()));
policyMap.put("BPN4", List.of(Policy.builder().policyId(policyIdToFind).build()));

// ACT
final List<String> result = PolicyHelper.findBpnsByPolicyId(policyMap, policyIdToFind);

// ASSERT
assertThat(result).containsExactlyInAnyOrder("BPN1", "BPN2", "BPN4");
}

}

0 comments on commit 038b85a

Please sign in to comment.