Skip to content

Commit

Permalink
Merge branch 'main' into feat/438/JsonSubTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
cromoteca committed Sep 25, 2023
2 parents 4b09c54 + 12db4c0 commit 7a50d0b
Show file tree
Hide file tree
Showing 91 changed files with 4,088 additions and 1,867 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ignorePatterns": ["packages/ts/*/test/**/*.snap.ts"],
"plugins": ["tsdoc"],
"rules": {
"@typescript-eslint/no-invalid-void-type": "off",
"@typescript-eslint/member-ordering": "off",
"class-methods-use-this": "off",
"import/no-duplicates": "off",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ packages/ts/*/*.d.ts
packages/ts/*/*.d.ts.map
packages/ts/*/*.js
packages/ts/*/*.js.map
packages/ts/*/*.module.css
packages/ts/*/.coverage
packages/ts/*/.vite
packages/ts/*/types/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,39 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
case CONTAINS:
return criteriaBuilder.like(expr,
"%" + value.toLowerCase() + "%");
case GREATER_THAN:
throw new IllegalArgumentException(
"A string cannot be filtered using greater than");
case LESS_THAN:
throw new IllegalArgumentException(
"A string cannot be filtered using less than");
default:
break;
}

throw new IllegalArgumentException(
"Matcher of type " + filter.getMatcher() + " is unknown");
} else {
return criteriaBuilder.equal(propertyPath, value.toLowerCase());
} else if (isNumber(javaType)) {
switch (filter.getMatcher()) {
case EQUALS:
return criteriaBuilder.equal(propertyPath, value);
case CONTAINS:
throw new IllegalArgumentException(
"A number cannot be filtered using contains");
case GREATER_THAN:
return criteriaBuilder.greaterThan(propertyPath, value);
case LESS_THAN:
return criteriaBuilder.lessThan(propertyPath, value);
default:
break;
}
}
throw new IllegalArgumentException("No implementation for " + javaType
+ " using " + filter.getMatcher() + ".");
}

private boolean isNumber(Class<?> javaType) {
return javaType == int.class || javaType == Integer.class
|| javaType == double.class || javaType == Double.class
|| javaType == long.class || javaType == Long.class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class PropertyStringFilter implements Filter {
public enum Matcher {
EQUALS, CONTAINS;
EQUALS, CONTAINS, LESS_THAN, GREATER_THAN;
}

private String propertyId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilla.crud;

import java.util.ArrayList;
import java.util.List;

import dev.hilla.crud.filter.AndFilter;
Expand All @@ -13,6 +14,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;

Expand Down Expand Up @@ -42,8 +44,61 @@ public void filterStringPropertyUsingEquals() {
assertFilterResult(filter, "John");
}

@Test(expected = InvalidDataAccessApiUsageException.class)
public void filterStringPropertyUsingLessThan() {
setupNames("Jack", "John", "Johnny", "Polly", "Josh");
PropertyStringFilter filter = createNameFilter(Matcher.LESS_THAN,
"John");
assertFilterResult(filter, "John");
}

@Test(expected = InvalidDataAccessApiUsageException.class)
public void filterStringPropertyUsingGreaterThan() {
setupNames("Jack", "John", "Johnny", "Polly", "Josh");
PropertyStringFilter filter = createNameFilter(Matcher.GREATER_THAN,
"John");
assertFilterResult(filter, "John");
}

@Test(expected = InvalidDataAccessApiUsageException.class)
public void filterNumberPropertyUsingContains() {
setupNames("Jack", "John", "Johnny", "Polly", "Josh");
PropertyStringFilter filter = createIdFilter(Matcher.CONTAINS, "2");
assertFilterResult(filter, "John");
}

@Test
public void filterNumberPropertyUsingEquals() {
List<TestObject> created = setupNames("Jack", "John", "Johnny", "Polly",
"Josh");
Integer johnId = created.get(1).getId();
PropertyStringFilter filter = createIdFilter(Matcher.EQUALS,
johnId + "");
assertFilterResult(filter, "John");
}

@Test
public void filterNumberPropertyUsingLessThan() {
List<TestObject> created = setupNames("Jack", "John", "Johnny", "Polly",
"Josh");
Integer johnnyId = created.get(2).getId();
PropertyStringFilter filter = createIdFilter(Matcher.LESS_THAN,
johnnyId + "");
assertFilterResult(filter, "Jack", "John");
}

@Test
public void filterNumberPropertyUsingGreaterThan() {
List<TestObject> created = setupNames("Jack", "John", "Johnny", "Polly",
"Josh");
Integer johnnyId = created.get(2).getId();
PropertyStringFilter filter = createIdFilter(Matcher.GREATER_THAN,
johnnyId + "");
assertFilterResult(filter, "Polly", "Josh");
}

@Test(expected = IllegalArgumentException.class)
public void filterNonExistingStringProperty() {
public void filterNonExistingProperty() {
setupNames("Jack", "John", "Johnny", "Polly", "Josh");
PropertyStringFilter filter = createNameFilter(Matcher.EQUALS, "John");
filter.setPropertyId("foo");
Expand Down Expand Up @@ -95,13 +150,23 @@ private PropertyStringFilter createNameFilter(Matcher matcher,
return filter;
}

private void setupNames(String... names) {
private PropertyStringFilter createIdFilter(Matcher matcher,
String filterString) {
PropertyStringFilter filter = new PropertyStringFilter();
filter.setPropertyId("id");
filter.setFilterValue(filterString);
filter.setMatcher(matcher);
return filter;
}

private List<TestObject> setupNames(String... names) {
List<TestObject> created = new ArrayList<>();
for (String name : names) {
TestObject testObject = new TestObject();
testObject.setName(name);
entityManager.persist(testObject);
created.add(entityManager.persist(testObject));
}
entityManager.flush();

return created;
}
}
1 change: 1 addition & 0 deletions packages/java/tests/csrf-context/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/java/tests/csrf/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7a50d0b

Please sign in to comment.