Commit dc30923c authored by Bergamote Orange's avatar Bergamote Orange
Browse files

unit tests

parent 713638ca
......@@ -21,6 +21,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.util.List;
......@@ -79,7 +80,7 @@ public class IdentificationStepBatchConfig {
@Bean
public ItemProcessor<String, SinglePlaceExposedVisits> exposedVisitBuilder() {
return new SinglePlaceExposedVisitsBuilder(dataSource, new ExposedVisitRowMapper());
return new SinglePlaceExposedVisitsBuilder(new JdbcTemplate(dataSource), new ExposedVisitRowMapper());
}
@Bean
......
......@@ -31,14 +31,13 @@ public class SinglePlaceExposedVisitsBuilder implements ItemProcessor<String, Si
private final ExposedVisitRowMapper rowMapper;
public SinglePlaceExposedVisitsBuilder(DataSource dataSource, ExposedVisitRowMapper rowMapper) {
jdbcTemplate = new JdbcTemplate(dataSource);
public SinglePlaceExposedVisitsBuilder(JdbcTemplate jdbcTemplate, ExposedVisitRowMapper rowMapper) {
this.jdbcTemplate = jdbcTemplate;
this.rowMapper = rowMapper;
}
@Override
public SinglePlaceExposedVisits process(final String ltid) {
final long time1 = System.currentTimeMillis();
final List<ExposedVisit> list = jdbcTemplate.query("select * from " + EXPOSED_VISITS_TABLE
+ " WHERE ltid= ? ORDER BY " + PERIOD_COLUMN + ", " + TIMESLOT_COLUMN,
rowMapper, UUID.fromString(ltid));
......@@ -48,12 +47,10 @@ public class SinglePlaceExposedVisitsBuilder implements ItemProcessor<String, Si
if (0 == ln % 1000) {
log.info("Loaded {} visits, current LTId={} ", ln, ltid);
}
final long time2 = System.currentTimeMillis();
return SinglePlaceExposedVisits.builder()
.locationTemporaryPublicId(v.getLocationTemporaryPublicId())
.venueType(v.getVenueType()).venueCategory1(v.getVenueCategory1())
.venueCategory2(v.getVenueCategory2()).visits(list).build();
}
return null;
}
......@@ -63,5 +60,4 @@ public class SinglePlaceExposedVisitsBuilder implements ItemProcessor<String, Si
log.info("building {} SinglePlaceExposedVisits", counter.get());
return ExitStatus.COMPLETED;
}
}
......@@ -47,7 +47,7 @@ public class ExposedVisitGenerator {
// hour of 21-01-01 : 3818448000
// diff: 7560000
final int NBLIEUX = 5;
final int NBLIEUX = 5000;
final int batchSize = 10;
final Random r = new Random();
......
package fr.gouv.clea.identification;
package fr.gouv.clea.identification.processor;
import fr.gouv.clea.dto.ClusterPeriod;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceClusterPeriod;
import fr.gouv.clea.identification.processor.ClusterToPeriodsProcessor;
import fr.gouv.clea.mapper.ClusterPeriodModelsMapper;
import fr.gouv.clea.mapper.ClusterPeriodModelsMapperImpl;
import org.assertj.core.api.Assertions;
......
package fr.gouv.clea.identification.processor;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.entity.ExposedVisit;
import fr.gouv.clea.identification.ExposedVisitRowMapper;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.UUID;
import static fr.gouv.clea.config.BatchConstants.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class SinglePlaceExposedVisitsBuilderTest {
@Mock
private JdbcTemplate jdbcTemplate;
@Mock
private ExposedVisitRowMapper rowMapper;
@InjectMocks
private SinglePlaceExposedVisitsBuilder builder;
@Test
void builder_processes_ltid_and_returns_object_with_agregated_visits_list() {
final String ltid = "e9d58612-993f-11eb-a8b3-0242ac130003";
final UUID ltidUUID = UUID.fromString(ltid);
final int venueType = 2;
final int vCat1 = 1;
final int vCat2 = 2;
// only v1 is detailed because only this one's information are queried to build SinglePlaceExposedVisits
final ExposedVisit v1 = ExposedVisit.builder()
.locationTemporaryPublicId(ltidUUID)
.venueType(venueType)
.venueCategory1(vCat1)
.venueCategory2(vCat2)
.build();
final ExposedVisit v2 = new ExposedVisit();
final String sql = "select * from " + EXPOSED_VISITS_TABLE
+ " WHERE ltid= ? ORDER BY " + PERIOD_COLUMN + ", " + TIMESLOT_COLUMN;
when(jdbcTemplate.query(sql, rowMapper, ltidUUID)).thenReturn(List.of(v1, v2));
final SinglePlaceExposedVisits processResult = builder.process(ltid);
assertThat(processResult).isNotNull();
assertThat(processResult.getLocationTemporaryPublicId()).isEqualTo(ltidUUID);
assertThat(processResult.getVenueCategory1()).isEqualTo(vCat1);
assertThat(processResult.getVenueCategory2()).isEqualTo(vCat2);
assertThat(processResult.getVisits()).containsExactly(v1, v2);
assertThat(processResult.getVenueType()).isEqualTo(venueType);
}
@Test
void builder_returns_null_when_ltid_does_not_match_any_base_entry() {
final String ltid = "e9d58612-993f-11eb-a8b3-0242ac130003";
final UUID ltidUUID = UUID.fromString(ltid);
final String sql = "select * from " + EXPOSED_VISITS_TABLE
+ " WHERE ltid= ? ORDER BY " + PERIOD_COLUMN + ", " + TIMESLOT_COLUMN;
when(jdbcTemplate.query(sql, rowMapper, ltidUUID)).thenReturn(List.of());
final SinglePlaceExposedVisits processResult = builder.process(ltid);
assertThat(processResult).isNull();
}
}
package fr.gouv.clea.identification;
package fr.gouv.clea.identification.processor;
import fr.gouv.clea.config.BatchProperties;
import fr.gouv.clea.dto.ClusterPeriod;
import fr.gouv.clea.dto.SinglePlaceCluster;
import fr.gouv.clea.dto.SinglePlaceExposedVisits;
import fr.gouv.clea.entity.ExposedVisit;
import fr.gouv.clea.identification.RiskConfigurationService;
import fr.gouv.clea.identification.RiskLevelConfig;
import fr.gouv.clea.identification.processor.SinglePlaceExposedVisitsProcessor;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
......@@ -19,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(MockitoExtension.class)
class SinglePlaceExposedVisitsProcessorTest {
private BatchProperties properties=new BatchProperties();
private BatchProperties properties = new BatchProperties();
RiskConfigurationService eval = new RiskConfigurationService();
public SinglePlaceExposedVisitsProcessorTest() {
......@@ -54,8 +56,7 @@ class SinglePlaceExposedVisitsProcessorTest {
SinglePlaceCluster res = new SinglePlaceExposedVisitsProcessor(properties,eval).process(spe);
assertThat(res).isNull();
}
}
@Test
void oneClusterPeriod() {
......@@ -114,9 +115,6 @@ class SinglePlaceExposedVisitsProcessorTest {
assertThat(p.getClusterDurationInSeconds()).as("clusterDuration").isEqualTo(1* properties.getDurationUnitInSeconds());
}
@Test
void forwardRiskLevel() {
SinglePlaceExposedVisits spe = new SinglePlaceExposedVisits();
......@@ -152,5 +150,4 @@ class SinglePlaceExposedVisitsProcessorTest {
Optional<RiskLevelConfig> riskLevelEvaluation = eval.evaluate(spe.getVenueType(), spe.getVenueCategory1(), spe.getVenueCategory2());
riskLevelEvaluation.ifPresent(evaluatedRiskLevel -> assertThat(p.getRiskLevel()).as("riskLevel").isCloseTo(evaluatedRiskLevel.getBackwardRisk(), Offset.offset(0.01f)));
}
}
Supports Markdown
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