Skip to content

Commit

Permalink
fix(executions): match problems to execution errors
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-chahbouni-decathlon committed Jun 1, 2022
1 parent 698d9cc commit 752bc8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -505,7 +486,7 @@ void errorSpecificationShouldHaveEqualsPredicateOnExceptionWhenExceptionProperty
CriteriaQuery<Error> criteriaQuery = criteriaBuilder.createQuery(Error.class);
Root<Error> 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<Error> errorSpecification = SpecificationUtil.toErrorSpecification(1, problemPattern, null);
Expand All @@ -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
Expand Down Expand Up @@ -744,35 +725,16 @@ void errorSpecificationShouldHaveInPredicateOnErrorIdsWhenErrorIdsIsNotNull() {
Root<Error> root = criteriaQuery.from(Error.class);
criteriaBuilder = Mockito.spy(criteriaBuilder);
prepareTest(criteriaBuilder);
Path<Object> executedScenario = root.get("executedScenario");
Path<Object> run = executedScenario.get("run");
Path<Object> idPath = run.get("id");
Path<Object> idPath = root.get("id");
root = Mockito.spy(root);
Path<Object> executedScenarioSpy = Mockito.spy(executedScenario);
Path<Object> runSpy = Mockito.spy(run);
Path<Object> 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<Long> value = invocationOnMock.getArgument(0);
Predicate predicate = (Predicate) invocationOnMock.callRealMethod();
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -82,7 +72,7 @@ private static void addEqualsOrStartWithPredicate(List<Predicate> predicates, Cr
}

private static void addEqualsPredicate(List<Predicate> predicates, CriteriaBuilder criteriaBuilder, Expression<String> expression, String value) {
if (StringUtil.isNotEmpty(value)) {
if (StringUtils.isNotEmpty(value)) {
predicates.add(criteriaBuilder.equal(expression, value));
}
}
Expand Down Expand Up @@ -197,7 +187,7 @@ public static Specification<Error> 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())) {
Expand All @@ -219,7 +209,7 @@ public static Specification<Error> 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()]));
};
Expand Down

0 comments on commit 752bc8f

Please sign in to comment.