diff --git a/code/api/api/src/test/java/com/decathlon/ara/repository/util/SpecificationUtilTest.java b/code/api/api/src/test/java/com/decathlon/ara/repository/util/SpecificationUtilTest.java index d23f05914..1551a5b6e 100644 --- a/code/api/api/src/test/java/com/decathlon/ara/repository/util/SpecificationUtilTest.java +++ b/code/api/api/src/test/java/com/decathlon/ara/repository/util/SpecificationUtilTest.java @@ -1,30 +1,12 @@ package com.decathlon.ara.repository.util; -import static org.mockito.ArgumentMatchers.any; - -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.Period; -import java.time.ZoneId; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Expression; -import javax.persistence.criteria.Order; -import javax.persistence.criteria.Path; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import javax.persistence.criteria.Selection; - +import com.decathlon.ara.domain.Error; +import com.decathlon.ara.domain.*; +import com.decathlon.ara.domain.enumeration.DefectExistence; +import com.decathlon.ara.domain.enumeration.ProblemStatus; +import com.decathlon.ara.domain.enumeration.ProblemStatusFilter; +import com.decathlon.ara.domain.filter.ProblemFilter; +import com.decathlon.ara.util.TestUtil; import org.hibernate.query.criteria.internal.PathImplementor; import org.hibernate.query.criteria.internal.expression.function.ParameterizedFunctionExpression; import org.junit.jupiter.api.Assertions; @@ -38,17 +20,16 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.data.jpa.domain.Specification; -import com.decathlon.ara.domain.Country; -import com.decathlon.ara.domain.Error; -import com.decathlon.ara.domain.ExecutedScenario; -import com.decathlon.ara.domain.Problem; -import com.decathlon.ara.domain.ProblemPattern; -import com.decathlon.ara.domain.Type; -import com.decathlon.ara.domain.enumeration.DefectExistence; -import com.decathlon.ara.domain.enumeration.ProblemStatus; -import com.decathlon.ara.domain.enumeration.ProblemStatusFilter; -import com.decathlon.ara.domain.filter.ProblemFilter; -import com.decathlon.ara.util.TestUtil; +import javax.persistence.EntityManager; +import javax.persistence.criteria.*; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.Period; +import java.time.ZoneId; +import java.util.*; +import java.util.stream.Collectors; + +import static org.mockito.ArgumentMatchers.any; @DataJpaTest @ExtendWith(MockitoExtension.class) @@ -505,7 +486,7 @@ void errorSpecificationShouldHaveEqualsPredicateOnExceptionWhenExceptionProperty CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Error.class); Root root = criteriaQuery.from(Error.class); criteriaBuilder = Mockito.spy(criteriaBuilder); - prepareTest(criteriaBuilder); + prepareTest(criteriaBuilder, PredicateType.LIKE); ProblemPattern problemPattern = new ProblemPattern(); TestUtil.setField(problemPattern, "exception", "exception"); Specification errorSpecification = SpecificationUtil.toErrorSpecification(1, problemPattern, null); @@ -516,9 +497,9 @@ void errorSpecificationShouldHaveEqualsPredicateOnExceptionWhenExceptionProperty Assertions.assertEquals(1l, predicates.get(0).value()); Assertions.assertEquals("root.executedScenario.run.execution.cycleDefinition.projectId", predicates.get(0).getName()); Assertions.assertEquals(PredicateType.EQUAL, predicates.get(0).type()); - Assertions.assertEquals(problemPattern.getException(), predicates.get(1).value()); + Assertions.assertEquals(problemPattern.getException() + "%", predicates.get(1).value()); Assertions.assertEquals("root.exception", predicates.get(1).getName()); - Assertions.assertEquals(PredicateType.EQUAL, predicates.get(1).type()); + Assertions.assertEquals(PredicateType.LIKE, predicates.get(1).type()); } @Test @@ -744,35 +725,16 @@ void errorSpecificationShouldHaveInPredicateOnErrorIdsWhenErrorIdsIsNotNull() { Root root = criteriaQuery.from(Error.class); criteriaBuilder = Mockito.spy(criteriaBuilder); prepareTest(criteriaBuilder); - Path executedScenario = root.get("executedScenario"); - Path run = executedScenario.get("run"); - Path idPath = run.get("id"); + Path idPath = root.get("id"); root = Mockito.spy(root); - Path executedScenarioSpy = Mockito.spy(executedScenario); - Path runSpy = Mockito.spy(run); Path idPathSpy = Mockito.spy(idPath); - Mockito.doAnswer(invocationOnMock -> { - if ("executedScenario".equals(invocationOnMock.getArgument(0))) { - return executedScenarioSpy; - } else { - return invocationOnMock.callRealMethod(); - } - }).when(root).get(Mockito.anyString()); - Mockito.doAnswer(invocationOnMock -> { - if ("run".equals(invocationOnMock.getArgument(0))) { - return runSpy; - } else { - return invocationOnMock.callRealMethod(); - } - }).when(executedScenarioSpy).get(Mockito.anyString()); - ; Mockito.doAnswer(invocationOnMock -> { if ("id".equals(invocationOnMock.getArgument(0))) { return idPathSpy; } else { return invocationOnMock.callRealMethod(); } - }).when(runSpy).get(Mockito.anyString()); + }).when(root).get(Mockito.anyString()); Mockito.doAnswer(invocationOnMock -> { List value = invocationOnMock.getArgument(0); Predicate predicate = (Predicate) invocationOnMock.callRealMethod(); @@ -788,7 +750,7 @@ void errorSpecificationShouldHaveInPredicateOnErrorIdsWhenErrorIdsIsNotNull() { Assertions.assertEquals("root.executedScenario.run.execution.cycleDefinition.projectId", predicates.get(0).getName()); Assertions.assertEquals(PredicateType.EQUAL, predicates.get(0).type()); Assertions.assertEquals(errorIds, predicates.get(1).value()); - Assertions.assertEquals("root.executedScenario.run.id", predicates.get(1).getName()); + Assertions.assertEquals("root.id", predicates.get(1).getName()); Assertions.assertEquals(PredicateType.IN, predicates.get(1).type()); } diff --git a/code/api/database/src/main/java/com/decathlon/ara/repository/util/SpecificationUtil.java b/code/api/database/src/main/java/com/decathlon/ara/repository/util/SpecificationUtil.java index f2f9aa63f..a08b257b3 100644 --- a/code/api/database/src/main/java/com/decathlon/ara/repository/util/SpecificationUtil.java +++ b/code/api/database/src/main/java/com/decathlon/ara/repository/util/SpecificationUtil.java @@ -1,5 +1,18 @@ package com.decathlon.ara.repository.util; +import com.decathlon.ara.domain.Error; +import com.decathlon.ara.domain.*; +import com.decathlon.ara.domain.enumeration.DefectExistence; +import com.decathlon.ara.domain.enumeration.ProblemStatus; +import com.decathlon.ara.domain.enumeration.ProblemStatusFilter; +import com.decathlon.ara.domain.filter.ProblemFilter; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.jpa.domain.Specification; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.Expression; +import javax.persistence.criteria.Path; +import javax.persistence.criteria.Predicate; import java.time.LocalDateTime; import java.time.Period; import java.time.ZoneId; @@ -10,29 +23,6 @@ import java.util.Optional; import java.util.function.BiFunction; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.Expression; -import javax.persistence.criteria.Path; -import javax.persistence.criteria.Predicate; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.data.jpa.domain.Specification; - -import com.decathlon.ara.domain.Country; -import com.decathlon.ara.domain.Error; -import com.decathlon.ara.domain.ExecutedScenario; -import com.decathlon.ara.domain.Execution; -import com.decathlon.ara.domain.Problem; -import com.decathlon.ara.domain.ProblemPattern; -import com.decathlon.ara.domain.Run; -import com.decathlon.ara.domain.Type; -import com.decathlon.ara.domain.enumeration.DefectExistence; -import com.decathlon.ara.domain.enumeration.ProblemStatus; -import com.decathlon.ara.domain.enumeration.ProblemStatusFilter; -import com.decathlon.ara.domain.filter.ProblemFilter; - -import liquibase.util.StringUtil; - public class SpecificationUtil { private static final String NAME_ATTRIBUTE = "name"; @@ -82,7 +72,7 @@ private static void addEqualsOrStartWithPredicate(List predicates, Cr } private static void addEqualsPredicate(List predicates, CriteriaBuilder criteriaBuilder, Expression expression, String value) { - if (StringUtil.isNotEmpty(value)) { + if (StringUtils.isNotEmpty(value)) { predicates.add(criteriaBuilder.equal(expression, value)); } } @@ -197,7 +187,7 @@ public static Specification toErrorSpecification(long projectId, ProblemP addEqualsOrStartWithPredicate(predicates, criteriaBuilder, executedScenario.get(NAME_ATTRIBUTE), problemPattern.getScenarioName(), problemPattern.isScenarioNameStartsWith()); addEqualsOrStartWithPredicate(predicates, criteriaBuilder, root.get("step"), problemPattern.getStep(), problemPattern.isStepStartsWith()); addEqualsOrStartWithPredicate(predicates, criteriaBuilder, root.get("stepDefinition"), problemPattern.getStepDefinition(), problemPattern.isStepDefinitionStartsWith()); - addEqualsPredicate(predicates, criteriaBuilder, root.get("exception"), problemPattern.getException()); + addEqualsOrStartWithPredicate(predicates, criteriaBuilder, root.get("exception"), problemPattern.getException(), true); addEqualsPredicate(predicates, criteriaBuilder, execution.get("release"), problemPattern.getRelease()); Country country = problemPattern.getCountry(); if (country != null && StringUtils.isNotEmpty(country.getCode())) { @@ -219,7 +209,7 @@ public static Specification toErrorSpecification(long projectId, ProblemP predicates.add(criteriaBuilder.equal(typePath.get("isMobile"), typeIsMobile)); } if (errorIds != null) { - predicates.add(run.get("id").in(errorIds)); + predicates.add(root.get("id").in(errorIds)); } return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])); };