forked from eclipse-tractusx/item-relationship-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(impl): [#199] Avoid too many methods by extraction to helper class
inlcuding test for helper class
- Loading branch information
Showing
3 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...olicy-store/src/main/java/org/eclipse/tractusx/irs/policystore/services/PolicyHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...y-store/src/test/java/org/eclipse/tractusx/irs/policystore/services/PolicyHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |