Commit 9db0d7b1 authored by Figue Orange's avatar Figue Orange
Browse files

fix tests

parent 3ef9dc5c
......@@ -9,21 +9,27 @@ import fr.gouv.clea.entity.ExposedVisit;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class SinglePlaceExposedVisitsProcessorTest {
private BatchProperties properties=new BatchProperties();
private BatchProperties properties = new BatchProperties();
RiskConfigurationService eval = new RiskConfigurationService();
@Mock
RiskConfigurationService riskConfigurationServiceMock;
public SinglePlaceExposedVisitsProcessorTest() {
properties.durationUnitInSeconds =180;
properties.durationUnitInSeconds = 180;
}
private final UUID UUID_SAMPLE = UUID.fromString("fa35fa88-2c44-4f13-9ec9-d38e77324c93");
......@@ -31,61 +37,73 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void noClusterPeriod() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
spe.setVenueType(18);
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNull();
}
@Test
void noClusterVisits() throws Exception {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
spe.setVenueType(18);
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(1).forwardVisits(0).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNull();
}
@Test
void oneClusterPeriod() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
spe.setVenueType(18);
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(1).forwardVisits(1).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(2).forwardVisits(1).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(3).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(4).forwardVisits(0).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
ClusterPeriod p = res.getPeriods().get(0);
// cluster start at slot 1, not at slot 0
assertThat(p.getClusterStart()).as("clusterStart").isEqualTo(periodStart + properties.durationUnitInSeconds);
// Cluster for 2 slots
assertThat(p.getClusterDurationInSeconds()).as("clusterDuration").isEqualTo(2* properties.durationUnitInSeconds);
assertThat(p.getClusterDurationInSeconds()).as("clusterDuration").isEqualTo(2 * properties.durationUnitInSeconds);
}
@Test
void manyClusterPeriodSlots() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
long anotherPeriodStart = 3822422480L;
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
......@@ -98,15 +116,15 @@ class SinglePlaceExposedVisitsProcessorTest {
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(1).forwardVisits(1).build());
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(2).forwardVisits(0).build());
spe.addVisit(ExposedVisit.builder().periodStart(anotherPeriodStart).timeSlot(0).forwardVisits(1).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(2);
ClusterPeriod p = res.getPeriods().get(0);
// cluster start at slot 1, not at slot 0
assertThat(p.getClusterStart()).as("clusterStart").isEqualTo(this.periodStart + properties.durationUnitInSeconds);
assertThat(p.getClusterDurationInSeconds()).as("clusterDuration").isEqualTo(1* properties.durationUnitInSeconds);
assertThat(p.getClusterDurationInSeconds()).as("clusterDuration").isEqualTo(1 * properties.durationUnitInSeconds);
p = res.getPeriods().get(1);
// cluster start at slot 0, not at slot 1
......@@ -119,38 +137,53 @@ class SinglePlaceExposedVisitsProcessorTest {
@Test
void forwardRiskLevel() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
spe.setVenueType(18);
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).forwardVisits(100).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
ClusterPeriod p = res.getPeriods().get(0);
Optional<RiskRule> riskLevelEvaluation = eval.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
Optional<RiskRule> riskLevelEvaluation = riskConfigurationServiceMock.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
riskLevelEvaluation.ifPresent(evaluatedRiskLevel -> assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelForward(), Offset.offset(0.01f)));
}
@Test
void backwardRiskLevel() {
when(riskConfigurationServiceMock.evaluate(anyInt(), anyInt(), anyInt())).thenReturn(buildRiskRule());
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
spe.setLocationTemporaryPublicId(UUID_SAMPLE);
spe.setVenueType(18);
spe.setVenueCategory1(3);
spe.setVenueCategory2(2);
spe.addVisit(ExposedVisit.builder().periodStart(periodStart).timeSlot(0).backwardVisits(100).build());
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties, riskConfigurationServiceMock).process(spe);
assertThat(res).isNotNull();
assertThat(res.getPeriods()).hasSize(1);
ClusterPeriod p = res.getPeriods().get(0);
Optional<RiskRule> riskLevelEvaluation = eval.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
Optional<RiskRule> riskLevelEvaluation = riskConfigurationServiceMock.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
riskLevelEvaluation.ifPresent(evaluatedRiskLevel -> assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getRiskLevelBackward(), Offset.offset(0.01f)));
}
private Optional<RiskRule> buildRiskRule() {
return Optional.of(RiskRule.builder()
.clusterThresholdForward(1)
.clusterThresholdBackward(3)
.riskLevelForward(3.0f)
.riskLevelBackward(2.0f)
.build());
}
}
......@@ -11,16 +11,16 @@ public abstract class ScoringConfiguration {
public abstract List<? extends ScoringConfigurationItem> getScorings();
public ScoringConfigurationItem getConfigurationFor(int venueType, int venueCategory1, int venueCategory2) {
log.info("Fetching rules candidates for venueType : {}, venueCategory1 : {}, venuCategory2: {}", venueType, venueCategory1, venueCategory2);
log.debug("Fetching rules candidates for venueType : {}, venueCategory1 : {}, venuCategory2: {}", venueType, venueCategory1, venueCategory2);
List<ScoringConfigurationItem> compatibleRules = this.getScorings().stream()
.filter(scoring -> scoring.isCompatibleWith(venueType, venueCategory1, venueCategory2))
.collect(Collectors.toList());
log.info("Found {} compatibles rules", compatibleRules.size());
log.debug("Found {} compatibles rules", compatibleRules.size());
//Only one match found
if (compatibleRules.size() == 1) {
log.info("Found suitable rule {}", compatibleRules.get(0));
log.debug("Found suitable rule {}", compatibleRules.get(0));
return compatibleRules.get(0);
}
......@@ -35,17 +35,17 @@ public abstract class ScoringConfiguration {
ScoringConfigurationItem bestRuleCandidate = compatibleRules.get(0);
for (ScoringConfigurationItem rule : compatibleRules) {
if (rule.isFullMatch()) {
log.info("Found matching rule {}", rule);
log.debug("Found full matching rule {}", rule);
return rule;
} else if (bothCategoryAreWildcard(bestRuleCandidate) && firstCategoryIsNotWildcarded(rule)) {
bestRuleCandidate = rule;
} else if (eitherOneCategoryIsWildcard(bestRuleCandidate) && firstCategoryIsNotWildcarded(rule)) {
bestRuleCandidate = rule;
} else if (firstCategoryIsNotWildcarded(bestRuleCandidate) && secondCategoryIsNotWildcarded(bestRuleCandidate)) {
return bestRuleCandidate;
bestRuleCandidate = rule;
}
}
log.info("Retrieving best rule {}", bestRuleCandidate);
log.info("Retrieving best rule {} for venueType : {}, venueCategory1 : {}, venuCategory2: {}", bestRuleCandidate, venueType, venueCategory1, venueCategory2);
return bestRuleCandidate;
}
......
......@@ -32,7 +32,7 @@ class ExposureTimeConfigurationTest {
@Test
void testExposureTimeConfigurationHasExpectedData() {
ExposureTimeRule scoring = (ExposureTimeRule) configuration.getScorings().get(2);
//'1,2,3,2,12,22,32'
assertThat(scoring.getVenueType()).isEqualTo(3);
assertThat(scoring.getVenueCategory1()).isEqualTo(ScoringConfigurationItem.wildcardValue);
assertThat(scoring.getVenueCategory2()).isEqualTo(ScoringConfigurationItem.wildcardValue);
......
......@@ -4,6 +4,7 @@ import fr.gouv.clea.clea.scoring.configuration.exposure.ExposureTimeConfiguratio
import fr.gouv.clea.clea.scoring.configuration.exposure.ExposureTimeConfigurationConverter;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfiguration;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskConfigurationConverter;
import fr.gouv.clea.clea.scoring.configuration.risk.RiskRule;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -56,7 +57,7 @@ class ScoringConfigurationItemTest {
@Test
void should_return_the_rule_three_one_wildcard() {
assertThat(riskConfiguration.getConfigurationFor(3, 1, 2))
assertThat(riskConfiguration.getConfigurationFor(3, 1, 3))
.isEqualTo(riskConfiguration.getScorings().get(4));
}
......@@ -72,5 +73,19 @@ class ScoringConfigurationItemTest {
.isEqualTo(riskConfiguration.getScorings().get(6));
}
private ScoringConfigurationItem buildRiskRule(int venueType, int venueCategory1, int venueCategory2,
int clusterThresholdBackward, int clusterThresholdForward,
float riskLevelBackward, float riskLevelForward) {
return RiskRule.builder()
.venueType(venueType)
.venueCategory1(venueCategory1)
.venueCategory2(venueCategory2)
.clusterThresholdBackward(clusterThresholdBackward)
.clusterThresholdForward(clusterThresholdForward)
.riskLevelBackward(riskLevelBackward)
.riskLevelForward(riskLevelForward)
.build();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment