Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

Commit 83bfce80 authored by Framboise Orange's avatar Framboise Orange
Browse files

refactor(test): externalize integration test of report stats feature in a...

refactor(test): externalize integration test of report stats feature in a dedicated class (black-box testing)
parent 112d0734
Pipeline #325980 passed with stages
in 42 minutes and 4 seconds
package fr.gouv.clea.consumer;
import fr.gouv.clea.consumer.model.ReportStat;
import fr.gouv.clea.consumer.test.IntegrationTest;
import fr.gouv.clea.consumer.test.KafkaManager;
import fr.inria.clea.lsp.utils.TimeUtils;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import static fr.gouv.clea.consumer.test.ElasticManager.assertThatAllDocumentsFromElastic;
import static org.awaitility.Awaitility.await;
@IntegrationTest
public class ConsumeReportStatTest {
@Test
void should_send_report_stats_to_elastic() {
final var instant = Instant.parse("2019-07-22T09:37:42.251Z");
final var reportStat = ReportStat.builder()
.reported(10)
.rejected(2)
.backwards(5)
.forwards(3)
.close(4)
.timestamp(TimeUtils.ntpTimestampFromInstant(instant))
.build();
KafkaManager.sendReportStat(reportStat);
await().atMost(Duration.ofSeconds(10)).untilAsserted(
() -> assertThatAllDocumentsFromElastic()
.containsExactlyInAnyOrder(
Map.of(
"@timestamp", "2019-07-22T09:37:42.000Z",
"reported", 10,
"rejected", 2,
"backwards", 5,
"forwards", 3,
"close", 4
)
)
);
}
}
......@@ -2,14 +2,12 @@ package fr.gouv.clea.consumer.service;
import fr.gouv.clea.consumer.configuration.CleaKafkaProperties;
import fr.gouv.clea.consumer.model.DecodedVisit;
import fr.gouv.clea.consumer.model.ReportStat;
import fr.gouv.clea.consumer.test.IntegrationTest;
import fr.gouv.clea.consumer.test.KafkaManager;
import fr.gouv.clea.consumer.test.QrCode;
import fr.gouv.clea.consumer.utils.KafkaVisitSerializer;
import fr.inria.clea.lsp.LocationSpecificPartDecoder;
import fr.inria.clea.lsp.exception.CleaEncodingException;
import fr.inria.clea.lsp.utils.TimeUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
......@@ -18,7 +16,6 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import java.time.Instant;
......@@ -63,34 +60,4 @@ class ConsumerServiceTest {
);
}
@Test
@DisplayName("test that consumeStat listener triggers when something is sent to stat queue")
void testConsumeStat() {
Map<String, Object> configs = KafkaTestUtils.producerProps(KafkaManager.getBootstrapServers());
Producer<String, ReportStat> producer = new DefaultKafkaProducerFactory<>(
configs,
new StringSerializer(),
new JsonSerializer<ReportStat>()
).createProducer();
long timestamp = TimeUtils.currentNtpTime();
ReportStat reportStat = ReportStat.builder()
.reported(10)
.rejected(2)
.backwards(5)
.forwards(3)
.close(4)
.timestamp(timestamp)
.build();
producer.send(new ProducerRecord<>(cleaKafkaProperties.getReportStatsTopic(), reportStat));
producer.flush();
producer.close();
// kafka listener has been called when something appears in elastic
await().atMost(10, SECONDS)
.untilAsserted(
() -> assertThatAllDocumentsFromElastic().hasSize(1)
);
}
}
package fr.gouv.clea.consumer.service;
import fr.gouv.clea.consumer.model.ReportStat;
import fr.gouv.clea.consumer.model.Visit;
import fr.gouv.clea.consumer.test.IntegrationTest;
import fr.inria.clea.lsp.utils.TimeUtils;
......@@ -303,33 +302,6 @@ class StatisticsServiceTest {
);
}
@Test
void should_send_report_stats_to_elastic() {
final var instant = Instant.parse("2019-07-22T09:37:42.251Z");
final var reportStat = ReportStat.builder()
.reported(10)
.rejected(2)
.backwards(5)
.forwards(3)
.close(4)
.timestamp(TimeUtils.ntpTimestampFromInstant(instant))
.build();
statisticsService.logStats(reportStat);
assertThatAllDocumentsFromElastic()
.containsExactlyInAnyOrder(
Map.of(
"@timestamp", "2019-07-22T09:37:42.000Z",
"reported", 10,
"rejected", 2,
"backwards", 5,
"forwards", 3,
"close", 4
)
);
}
private Object elasticDefaultStringRepresentation(Instant instant) {
return instant.atOffset(UTC)
.format(DateTimeFormatter.ofPattern(DateFormat.date_time.getPattern()));
......
package fr.gouv.clea.consumer.test;
import com.fasterxml.jackson.databind.JsonNode;
import fr.gouv.clea.consumer.model.ReportStat;
import lombok.SneakyThrows;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.connect.json.JsonDeserializer;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.kafka.support.serializer.JsonSerializer;
import org.springframework.kafka.test.utils.ContainerTestUtils;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.test.context.TestContext;
......@@ -151,4 +157,17 @@ public class KafkaManager implements TestExecutionListener {
.collect(Collectors.toList());
}
}
public static void sendReportStat(ReportStat reportStat) {
Map<String, Object> configs = KafkaTestUtils.producerProps(KafkaManager.getBootstrapServers());
Producer<String, ReportStat> producer = new DefaultKafkaProducerFactory<>(
configs,
new StringSerializer(),
new JsonSerializer<ReportStat>()
).createProducer();
producer.send(new ProducerRecord<>("test.clea.fct.report-stat", reportStat));
producer.flush();
producer.close();
}
}
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