Skip to content
Snippets Groups Projects
Commit f97995e4 authored by Nicolas.Rod's avatar Nicolas.Rod Committed by Hugues.Cazeaux
Browse files

fix(SearchSpecification): build of criteria

parent d5762a55
Branches
Tags
1 merge request!414fix(SearchSpecification): build of criteria
......@@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
......@@ -31,6 +31,9 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
......@@ -38,9 +41,6 @@ import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import ch.unige.solidify.exception.SolidifyRuntimeException;
import ch.unige.solidify.util.SearchCriteria;
......@@ -96,7 +96,7 @@ public abstract class SearchSpecification<T> implements Specification<T> {
builder.lower(fieldPath),
this.criteria.getValue().toString().toLowerCase());
}
return builder.greaterThan((Expression<String>) fieldPath, (Expression) criteriaValue);
return builder.greaterThan(fieldPath, (Comparable) criteriaValue);
case LESS_THAN:
if (this.criteria.isCaseInsensitive() && this.isString(fieldType)) {
......@@ -104,7 +104,7 @@ public abstract class SearchSpecification<T> implements Specification<T> {
builder.lower(fieldPath),
this.criteria.getValue().toString().toLowerCase());
}
return builder.lessThan((Expression<String>) fieldPath, (Expression) criteriaValue);
return builder.lessThan(fieldPath, (Comparable) criteriaValue);
case STARTS_WITH:
if (!this.isString(fieldType)) {
......@@ -145,7 +145,7 @@ public abstract class SearchSpecification<T> implements Specification<T> {
builder.lower(fieldPath),
this.criteria.getValue().toString().toLowerCase());
}
return builder.greaterThanOrEqualTo((Expression<String>) fieldPath, (Expression) criteriaValue);
return builder.greaterThanOrEqualTo(fieldPath, (Comparable) criteriaValue);
case LESS_EQUAL:
if (this.criteria.isCaseInsensitive() && this.isString(fieldType)) {
......@@ -160,8 +160,7 @@ public abstract class SearchSpecification<T> implements Specification<T> {
throw new SolidifyRuntimeException("BETWEEN operation can be applied only for number or date");
}
final Object[] values = this.convertBetweenValueString2Object(fieldType, this.criteria.getValue());
return builder
.between(builder.lower(fieldPath), (Expression) values[0], (Expression) values[1]);
return builder.between(fieldPath, (Comparable) values[0], (Comparable) values[1]);
default:
return null;
}
......@@ -189,7 +188,7 @@ public abstract class SearchSpecification<T> implements Specification<T> {
// for between operator, the value will be 20_30 or 2016-08-16_2018-09-25.
// it treats only non-string field
private Object[] convertBetweenValueString2Object(Class fieldType, Object inputValue) {
final String[] values = StringUtils.split((String) inputValue, "_");
final String[] values = StringUtils.split(String.valueOf(inputValue), "_");
if (values == null || values.length != 2) {
return new Object[0];
}
......@@ -201,28 +200,28 @@ public abstract class SearchSpecification<T> implements Specification<T> {
private Object convertString2Object(Class fieldType, Object inputValue) {
// first test number
if (Number.class.isAssignableFrom(fieldType)) {
return new BigDecimal((String) inputValue);
return new BigDecimal(String.valueOf(inputValue));
}
// Test boolean
if (Boolean.class.isAssignableFrom(fieldType) || boolean.class.isAssignableFrom(fieldType)) {
return Boolean.parseBoolean((String) inputValue);
return Boolean.parseBoolean(String.valueOf(inputValue));
}
// Obtains an instance of LocalDate from a text string such as "2016-08-16"
// The string must represent a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.
if (LocalDate.class.isAssignableFrom(fieldType)) {
return LocalDate.parse((String) inputValue);
return LocalDate.parse(String.valueOf(inputValue));
}
// Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.
// The string must represent a valid date-time and is parsed using
// DateTimeFormatter.ISO_LOCAL_DATE_TIME.
if (LocalDateTime.class.isAssignableFrom(fieldType)) {
return LocalDateTime.parse((String) inputValue);
return LocalDateTime.parse(String.valueOf(inputValue));
}
// Obtains an instance of OffsetDateTime from a text string such as 2007-12-03T10:15:30+01:00
if (OffsetDateTime.class.isAssignableFrom(fieldType)) {
return OffsetDateTime.parse((String) inputValue);
return OffsetDateTime.parse(String.valueOf(inputValue));
}
return inputValue;
}
......
......@@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
......@@ -34,8 +34,6 @@ import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;
import jakarta.annotation.Resource;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
......@@ -43,6 +41,8 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import jakarta.annotation.Resource;
import ch.unige.solidify.test.config.MockUserJpaConfig;
import ch.unige.solidify.test.specification.dao.MockUser;
import ch.unige.solidify.test.specification.dao.MockUserInfo;
......@@ -69,8 +69,7 @@ class SearchSpecificationTest {
MockUser user3;
// Test Greater Than and less Than
// TODO: Spring Boot 3
// @Test
@Test
void ageSearch_ByGreaterLess_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("i", "age", SearchOperation.LESS_THAN, 30);
final SearchCriteria criteria2 = new SearchCriteria("age", SearchOperation.GREATER_THAN, 20);
......@@ -109,7 +108,7 @@ class SearchSpecificationTest {
* Test contains and case insensitive
*/
@Test
void givenNamesearch_ByContainsCaseInsensitive_thenCorrect() {
void givenNameSearch_ByContainsCaseInsensitive_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("i", "name", SearchOperation.CONTAINS, "O");
final List<MockUser> results = this.userRepository.findAll(Specification.where(new MockUserSearchSpecification(criteria1)));
assertEquals(3, results.size());
......@@ -143,8 +142,7 @@ class SearchSpecificationTest {
}
// Test BETWEEN with Integer field type
// TODO: Spring Boot 3
// @Test
@Test
void IntegerSearch_between_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("childNumber", SearchOperation.BETWEEN, "4_8");
final List<MockUser> results = this.userRepository.findAll(Specification.where(new MockUserSearchSpecification(criteria1)));
......@@ -154,8 +152,7 @@ class SearchSpecificationTest {
}
// Test LESS THAN OR EQUAL with Localedate field type
// TODO: Spring Boot 3
// @Test
@Test
void LocaleDateSearch_lessEquals_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("birthday", SearchOperation.GREATER_EQUAL, "1997-01-30");
final List<MockUser> results = this.userRepository.findAll(Specification.where(new MockUserSearchSpecification(criteria1)));
......@@ -165,8 +162,7 @@ class SearchSpecificationTest {
}
// Test BETWEEN with LocaledateTime field type
// TODO: Spring Boot 3
// @Test
@Test
void LocaledateTimeSearch_between_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("marriageTime", SearchOperation.BETWEEN,
"2003-10-03T10:15:30_2016-02-03T12:15:30");
......@@ -177,8 +173,7 @@ class SearchSpecificationTest {
}
// Test GREATER THAN OR EQUAL with Long field type
// TODO: Spring Boot 3
// @Test
@Test
void LongSearch_greaterEquals_thenCorrect() {
final SearchCriteria criteria1 = new SearchCriteria("carKilos", SearchOperation.GREATER_EQUAL, "3000");
final List<MockUser> results = this.userRepository.findAll(Specification.where(new MockUserSearchSpecification(criteria1)));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment