Skip to content

Commit

Permalink
feat(policy-api):[#639] adjustments to prepared date operation for cr…
Browse files Browse the repository at this point in the history
…eatedOn and validUntil

According to discussion instead of BETWEEN operation there will be the operations AFTER_LOCAL_DATE and BEFORE_LOCAL_DATE. These will be implemented in a later story.
  • Loading branch information
dsmf committed Jul 4, 2024
1 parent 9665478 commit 88fbf39
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_CREATED_ON;
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_POLICY_ID;
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_VALID_UNTIL;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.BETWEEN;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.AFTER_LOCAL_DATE;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.BEFORE_LOCAL_DATE;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -44,6 +45,7 @@ public class SearchParameterParser {
public static final List<String> SUPPORTED_PROPERTIES = List.of(PROPERTY_BPN, PROPERTY_VALID_UNTIL,
PROPERTY_POLICY_ID, PROPERTY_CREATED_ON, PROPERTY_ACTION);
public static final List<String> DATE_PROPERTIES = List.of(PROPERTY_VALID_UNTIL, PROPERTY_CREATED_ON);
public static final List<Operation> DATE_OPERATORS = List.of(BEFORE_LOCAL_DATE, AFTER_LOCAL_DATE);

public static final String CRITERIA_INNER_SEPARATOR = ",";
public static final int NUM_PARTS_OF_FILTERS = 3;
Expand Down Expand Up @@ -93,8 +95,9 @@ private static String getProperty(final String property) {

private Operation getOperation(final String operationStr, final String property) {
final Operation operation = Operation.valueOf(StringUtils.trimToEmpty(operationStr));
if (operation == BETWEEN && DATE_PROPERTIES.stream().noneMatch(p -> p.equalsIgnoreCase(property))) {
throw new IllegalArgumentException("Operation BETWEEN is only supported for date properties");
if (DATE_OPERATORS.contains(operation) && DATE_PROPERTIES.stream()
.noneMatch(p -> p.equalsIgnoreCase(property))) {
throw new IllegalArgumentException("Date operation are only supported for date properties");
}
return operation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ public Map<String, List<PolicyResponse>> getPolicies(//
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
}

// TODO (mfischer): #750: add documentation
// TODO (mfischer): #750: add / adapt integration tests
// TODO (mfischer): #750: update insomnia
// TODO (mfischer): #639: add documentation and insomnia collection
@GetMapping("/policies/paged")
@ResponseStatus(HttpStatus.OK)
@PreAuthorize("hasAuthority('" + IrsRoles.ADMIN_IRS + "')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SearchCriteria<T> {
public enum Operation {
EQUALS,
STARTS_WITH,
BETWEEN
BEFORE_LOCAL_DATE,
AFTER_LOCAL_DATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,14 @@ private Predicate<PolicyWithBpn> getPolicyPredicate(final SearchCriteria<?> sear
return getBpnFilter(searchCriteria);
} else if (PROPERTY_POLICY_ID.equalsIgnoreCase(searchCriteria.getProperty())) {
return getPolicyIdFilter(searchCriteria);
// TODO (mfischer): #750: add test coverage for createdOn, validUntil
} else if (PROPERTY_ACTION.equalsIgnoreCase(searchCriteria.getProperty())) {
return getActionFilter(searchCriteria);
} else if (PROPERTY_CREATED_ON.equalsIgnoreCase(searchCriteria.getProperty())) {
// TODO (mfischer): #750: implement createdOn filter
// TODO (mfischer): #750: implement createdOn filter incl. test
throw new IllegalArgumentException(
"Filtering by '%s' has not been implemented yet".formatted(PROPERTY_CREATED_ON));
} else if (PROPERTY_VALID_UNTIL.equalsIgnoreCase(searchCriteria.getProperty())) {
// TODO (mfischer): #750: implement validUntil filter
// TODO (mfischer): #750: implement validUntil filter incl. test
throw new IllegalArgumentException(
"Filtering by '%s' has not been implemented yet".formatted(PROPERTY_VALID_UNTIL));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.AFTER_LOCAL_DATE;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.EQUALS;

import java.util.List;
Expand Down Expand Up @@ -58,9 +59,10 @@ void notExistingOperation() {

@Test
void betweenNotForNonDateProperties() {
final ThrowingCallable call = () -> new SearchParameterParser(List.of("policyId,BETWEEN,test"));
final ThrowingCallable call = () -> new SearchParameterParser(
List.of("policyId," + AFTER_LOCAL_DATE + ",test"));
assertThatThrownBy(call).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Operation BETWEEN is only supported for date properties");
.hasMessageContaining("Date operation are only supported for date properties");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_ACTION;
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_BPN;
import static org.eclipse.tractusx.irs.policystore.common.CommonConstants.PROPERTY_POLICY_ID;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.BETWEEN;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.BEFORE_LOCAL_DATE;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.EQUALS;
import static org.eclipse.tractusx.irs.policystore.models.SearchCriteria.Operation.STARTS_WITH;

Expand Down Expand Up @@ -203,10 +203,10 @@ class FilterTests {
public void policyId_invalidOperation() {

assertThatThrownBy(() -> testee.getPolicies(policiesMap, PageRequest.of(0, 10),
List.of(new SearchCriteria<>(PROPERTY_POLICY_ID, BETWEEN, "policy-2")))).isInstanceOf(
IllegalArgumentException.class)
.hasMessageContaining(
"The property 'policyId' only supports the following operations");
List.of(new SearchCriteria<>(PROPERTY_POLICY_ID, BEFORE_LOCAL_DATE, "policy-2")))).isInstanceOf(
IllegalArgumentException.class)
.hasMessageContaining(
"The property 'policyId' only supports the following operations");
}

@Test
Expand Down

0 comments on commit 88fbf39

Please sign in to comment.