-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(policy-api):[#639] implement filtering and improvements
still missing: - filtering for validUntil and createdOn - documentation, further / adjustment of tests, see TODOs with #639
- Loading branch information
Showing
9 changed files
with
637 additions
and
148 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
...licy-store/src/main/java/org/eclipse/tractusx/irs/policystore/common/CommonConstants.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,28 @@ | ||
/******************************************************************************** | ||
* 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.common; | ||
|
||
public class CommonConstants { | ||
public static final String PROPERTY_BPN = "bpn"; | ||
public static final String PROPERTY_POLICY_ID = "policyId"; | ||
public static final String PROPERTY_ACTION = "action"; | ||
public static final String PROPERTY_CREATED_ON = "createdOn"; | ||
public static final String PROPERTY_VALID_UNTIL = "validUntil"; | ||
} |
95 changes: 95 additions & 0 deletions
95
...tore/src/main/java/org/eclipse/tractusx/irs/policystore/common/SearchParameterParser.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,95 @@ | ||
/******************************************************************************** | ||
* 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.common; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lombok.Getter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.tractusx.irs.policystore.models.SearchCriteria; | ||
|
||
@Getter | ||
public class SearchParameterParser { | ||
|
||
public static final List<String> SUPPORTED_PROPERTIES = List.of(CommonConstants.PROPERTY_BPN, | ||
CommonConstants.PROPERTY_VALID_UNTIL, CommonConstants.PROPERTY_POLICY_ID, | ||
CommonConstants.PROPERTY_CREATED_ON, CommonConstants.PROPERTY_ACTION); | ||
public static final List<String> DATE_PROPERTIES = List.of(CommonConstants.PROPERTY_VALID_UNTIL, | ||
CommonConstants.PROPERTY_CREATED_ON); | ||
|
||
public static final String CRITERIA_INNER_SEPARATOR = ","; | ||
|
||
final List<SearchCriteria<?>> searchCriteria; | ||
|
||
public SearchParameterParser(final List<String> searchParameters) { | ||
searchCriteria = parseSearchParameters(searchParameters); | ||
} | ||
|
||
private List<SearchCriteria<?>> parseSearchParameters(final List<String> searchParameterList) { | ||
final List<SearchCriteria<?>> searchCriteria = new ArrayList<>(); | ||
if (searchParameterList != null) { | ||
for (int i = 0; i < searchParameterList.size(); i++) { | ||
|
||
final String searchParameter = searchParameterList.get(i); | ||
final String[] splittedSearchParam = StringUtils.split(searchParameter, CRITERIA_INNER_SEPARATOR); | ||
|
||
if (splittedSearchParam.length < 3) { | ||
throw new IllegalArgumentException(("Illegal search parameter at index %s. " | ||
+ "Format should be <propertyName>,<operation>,<value>.").formatted(i)); | ||
} | ||
|
||
final String property = getProperty(splittedSearchParam[0]); | ||
final SearchCriteria.Operation operation = getOperation(splittedSearchParam[1], property); | ||
final String value = getValue(splittedSearchParam[2]); | ||
|
||
searchCriteria.add(new SearchCriteria<>(property, operation, value)); | ||
} | ||
} | ||
|
||
return searchCriteria; | ||
} | ||
|
||
private static String getValue(final String value) { | ||
return StringUtils.trimToEmpty(value); | ||
} | ||
|
||
private static String getProperty(final String property) { | ||
final String trimmedProperty = StringUtils.trimToEmpty(property); | ||
if (SUPPORTED_PROPERTIES.stream().noneMatch(p -> p.equalsIgnoreCase(trimmedProperty))) { | ||
throw new IllegalArgumentException("Only the following properties support filtering: %s".formatted( | ||
String.join(", ", SUPPORTED_PROPERTIES))); | ||
} | ||
return trimmedProperty; | ||
} | ||
|
||
private SearchCriteria.Operation getOperation(final String operationStr, final String property) { | ||
final SearchCriteria.Operation operation = SearchCriteria.Operation.valueOf( | ||
StringUtils.trimToEmpty(operationStr)); | ||
|
||
if (operation == SearchCriteria.Operation.BETWEEN && DATE_PROPERTIES.stream() | ||
.noneMatch(p -> p.equalsIgnoreCase( | ||
property))) { | ||
throw new IllegalArgumentException("Operation BETWEEN is only supported for date properties"); | ||
} | ||
return operation; | ||
} | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
...olicy-store/src/main/java/org/eclipse/tractusx/irs/policystore/models/SearchCriteria.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,37 @@ | ||
/******************************************************************************** | ||
* 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.models; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class SearchCriteria<T> { | ||
private String property; | ||
private Operation operation; | ||
private T value; | ||
|
||
public enum Operation { | ||
EQUALS, | ||
STARTS_WITH, | ||
BETWEEN | ||
} | ||
} |
Oops, something went wrong.