Skip to content

Commit

Permalink
test: make elastic tests independent of the time
Browse files Browse the repository at this point in the history
Some of the tests of the elastic repository depend on the time when they are executed.
This commit introduces a TimeProvider class that is used to provide the same date for both the data put in ES and the tests themselves.

(cherry picked from commit 94b9e2a)
  • Loading branch information
phiz71 committed Jan 17, 2025
1 parent 3b5bf46 commit 956df94
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
import io.reactivex.rxjava3.core.Single;
import io.vertx.core.buffer.Buffer;
import jakarta.annotation.PostConstruct;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

public class DatabaseHydrator {

private static final DateTimeFormatter FORMATTER_WITH_DASH = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
private static final DateTimeFormatter FORMATTER_WITH_DOT = DateTimeFormatter.ofPattern("yyyy.MM.dd").withZone(ZoneId.systemDefault());

Client client;
FreeMarkerComponent freeMarkerComponent;
String elasticMajorVersion;
private final TimeProvider timeProvider;

public DatabaseHydrator(Client client, FreeMarkerComponent freeMarkerComponent, String elasticsearchVersion) {
public DatabaseHydrator(
Client client,
FreeMarkerComponent freeMarkerComponent,
String elasticsearchVersion,
TimeProvider timeProvider
) {
this.client = client;
this.freeMarkerComponent = freeMarkerComponent;
this.elasticMajorVersion = elasticsearchVersion.split("\\.")[0];
this.timeProvider = timeProvider;
}

@PostConstruct
Expand Down Expand Up @@ -83,27 +83,20 @@ private Completable createTemplate(List<String> types) {
}

private List<Buffer> prepareData(List<String> types) {
final Instant now = Instant.now();
final Instant yesterday = now.minus(1, ChronoUnit.DAYS);

final String dateToday = FORMATTER_WITH_DASH.format(now);
final String dateYesterday = FORMATTER_WITH_DASH.format(yesterday);

final String todayWithDot = FORMATTER_WITH_DOT.format(now);
final String yesterdayWithDot = FORMATTER_WITH_DOT.format(yesterday);

return types
.stream()
.map(type -> {
Map<String, Object> data = Map.ofEntries(
Map.entry("dateToday", dateToday),
Map.entry("dateYesterday", dateYesterday),
Map.entry("indexNameToday", indexTemplate(type, todayWithDot)),
Map.entry("indexNameTodayEntrypoint", indexTemplate(type, todayWithDot, "entrypoint")),
Map.entry("indexNameTodayEndpoint", indexTemplate(type, todayWithDot, "endpoint")),
Map.entry("indexNameYesterday", indexTemplate(type, yesterdayWithDot)),
Map.entry("indexNameYesterdayEntrypoint", indexTemplate(type, yesterdayWithDot, "entrypoint")),
Map.entry("indexNameYesterdayEndpoint", indexTemplate(type, yesterdayWithDot, "endpoint"))
Map.entry("dateToday", this.timeProvider.getDateToday()),
Map.entry("dateYesterday", this.timeProvider.getDateYesterday()),
Map.entry("dateTimeToday", this.timeProvider.getDateTimeToday()),
Map.entry("dateTimeYesterday", this.timeProvider.getDateTimeYesterday()),
Map.entry("indexNameToday", indexTemplate(type, this.timeProvider.getTodayWithDot())),
Map.entry("indexNameTodayEntrypoint", indexTemplate(type, this.timeProvider.getTodayWithDot(), "entrypoint")),
Map.entry("indexNameTodayEndpoint", indexTemplate(type, this.timeProvider.getTodayWithDot(), "endpoint")),
Map.entry("indexNameYesterday", indexTemplate(type, this.timeProvider.getYesterdayWithDot())),
Map.entry("indexNameYesterdayEntrypoint", indexTemplate(type, this.timeProvider.getYesterdayWithDot(), "entrypoint")),
Map.entry("indexNameYesterdayEndpoint", indexTemplate(type, this.timeProvider.getYesterdayWithDot(), "endpoint"))
);
var filename = type + ".ftl";
return freeMarkerComponent.generateFromTemplate(filename, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ public Vertx vertx() {
}

@Bean
public DatabaseHydrator databaseHydrator(Client client, FreeMarkerComponent freeMarkerComponent) {
return new DatabaseHydrator(client, freeMarkerComponent, elasticsearchVersion);
public TimeProvider timeProvider() {
return new TimeProvider();
}

@Bean
public DatabaseHydrator databaseHydrator(Client client, FreeMarkerComponent freeMarkerComponent, TimeProvider timeProvider) {
return new DatabaseHydrator(client, freeMarkerComponent, elasticsearchVersion, timeProvider);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.repository.elasticsearch;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import lombok.Getter;

@Getter
public class TimeProvider {

private static final DateTimeFormatter DATE_TIME_FORMATTER_WITH_DASH = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
.withZone(ZoneId.systemDefault());
private static final DateTimeFormatter DATE_FORMATTER_WITH_DASH = DateTimeFormatter
.ofPattern("yyyy-MM-dd")
.withZone(ZoneId.systemDefault());
private static final DateTimeFormatter DATE_FORMATTER_WITH_DOT = DateTimeFormatter
.ofPattern("yyyy.MM.dd")
.withZone(ZoneId.systemDefault());

private final String dateToday;
private final String dateYesterday;
private final String dateTimeToday;
private final String dateTimeYesterday;
private final String todayWithDot;
private final String yesterdayWithDot;

public TimeProvider() {
final Instant now = Instant.now().minus(30, ChronoUnit.MINUTES);
final Instant yesterday = now.minus(1, ChronoUnit.DAYS);

dateToday = DATE_FORMATTER_WITH_DASH.format(now);
dateYesterday = DATE_FORMATTER_WITH_DASH.format(yesterday);

dateTimeToday = DATE_TIME_FORMATTER_WITH_DASH.format(now);
dateTimeYesterday = DATE_TIME_FORMATTER_WITH_DASH.format(yesterday);

todayWithDot = DATE_FORMATTER_WITH_DOT.format(now);
yesterdayWithDot = DATE_FORMATTER_WITH_DOT.format(yesterday);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void testCountWithTwoApis() throws Exception {
);

assertThat(response).isNotNull();
assertThat(response.getCount()).isEqualTo(4);
assertThat(response.getCount()).isEqualTo(7);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import io.gravitee.common.http.HttpMethod;
import io.gravitee.repository.common.query.QueryContext;
import io.gravitee.repository.elasticsearch.AbstractElasticsearchRepositoryTest;
import io.gravitee.repository.elasticsearch.TimeProvider;
import io.gravitee.repository.log.v4.model.connection.ConnectionLog;
import io.gravitee.repository.log.v4.model.connection.ConnectionLogDetail;
import io.gravitee.repository.log.v4.model.connection.ConnectionLogDetailQuery;
import io.gravitee.repository.log.v4.model.connection.ConnectionLogQuery;
import io.gravitee.repository.log.v4.model.connection.ConnectionLogQuery.Filter;
import io.gravitee.repository.log.v4.model.message.AggregatedMessageLog;
import io.gravitee.repository.log.v4.model.message.MessageLogQuery;
import jakarta.annotation.PostConstruct;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
Expand All @@ -53,13 +55,23 @@ public class LogElasticsearchRepositoryTest extends AbstractElasticsearchReposit
@Autowired
private LogElasticsearchRepository logV4Repository;

@Autowired
private TimeProvider timeProvider;

private String today;
private String yesterday;

@PostConstruct
public void init() {
today = timeProvider.getDateToday();
yesterday = timeProvider.getDateYesterday();
}

@Nested
class SearchConnectionLogs {

@Test
void should_return_the_1st_page_of_connection_logs_of_an_api() {
var today = DATE_FORMATTER.format(Instant.now());

var result = logV4Repository.searchConnectionLogs(
queryContext,
ConnectionLogQuery.builder().filter(Filter.builder().apiId("f1608475-dd77-4603-a084-75dd775603e9").build()).size(2).build()
Expand Down Expand Up @@ -101,8 +113,6 @@ void should_return_the_1st_page_of_connection_logs_of_an_api() {

@Test
void should_return_a_page_of_connection_logs_of_an_api() {
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchConnectionLogs(
queryContext,
ConnectionLogQuery
Expand Down Expand Up @@ -238,9 +248,6 @@ void should_return_a_page_of_connection_logs_from_today_to_whenever() {

@Test
void should_return_the_applications_logs() {
var today = DATE_FORMATTER.format(Instant.now());
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchConnectionLogs(
queryContext,
ConnectionLogQuery
Expand Down Expand Up @@ -269,8 +276,6 @@ void should_return_the_applications_logs() {

@Test
void should_return_the_connection_logs_for_plans() {
var today = DATE_FORMATTER.format(Instant.now());

var result = logV4Repository.searchConnectionLogs(
queryContext,
ConnectionLogQuery
Expand Down Expand Up @@ -349,8 +354,6 @@ void should_return_empty_result() {

@Test
void should_return_result() {
var today = DATE_FORMATTER.format(Instant.now());

var result = logV4Repository.searchConnectionLogDetail(
queryContext,
ConnectionLogDetailQuery
Expand Down Expand Up @@ -410,8 +413,6 @@ class SearchAggregateMessageLog {

@Test
void should_return_aggregated_message_log_with_only_entrypoint() {
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchAggregatedMessageLog(
queryContext,
MessageLogQuery
Expand Down Expand Up @@ -456,8 +457,6 @@ void should_return_aggregated_message_log_with_only_entrypoint() {

@Test
void should_return_aggregated_message_log_with_only_endpoint() {
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchAggregatedMessageLog(
queryContext,
MessageLogQuery
Expand Down Expand Up @@ -502,8 +501,6 @@ void should_return_aggregated_message_log_with_only_endpoint() {

@Test
void should_return_aggregated_message_log_with_entrypoint_and_endpoint_for_publish_operation() {
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchAggregatedMessageLog(
queryContext,
MessageLogQuery
Expand Down Expand Up @@ -560,8 +557,6 @@ void should_return_aggregated_message_log_with_entrypoint_and_endpoint_for_publi

@Test
void should_return_aggregated_message_log_with_entrypoint_and_endpoint_for_subscribe_operation() {
var yesterday = DATE_FORMATTER.format(Instant.now().minus(1, ChronoUnit.DAYS));

var result = logV4Repository.searchAggregatedMessageLog(
queryContext,
MessageLogQuery
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{ "index" :{ ${indexNameToday}, "_id": "AVsGcnBPkSadZDBaTBag"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": false,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T18:33:11.079+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": false,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameToday}, "_id": "AVsGcnBPkSadZDBaTBao"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T18:10:11.079+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameToday}, "_id": "AVsGgzxGooztmMPf1gOs"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T18:28:30.718+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameToday}, "_id": "AVsGgiqaTGG_RLWNOXe7"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T17:27:20.714+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameToday}, "_id": "AVsGgxUsooztmMPf1gOp"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T16:28:20.717+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameToday}, "_id": "AVsGg7GYooztmMPf1gO1"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateToday}T18:29:00.719+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeToday}"}
{ "index" :{ ${indexNameYesterday}, "_id": "AVsGg__MooztmMPf1gO7"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateYesterday}T18:29:20.720+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeYesterday}"}
{ "index" :{ ${indexNameYesterday}, "_id": "AVsGhl3aooztmMPf1gPs"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateYesterday}T18:32:00.052+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeYesterday}"}
{ "index" :{ ${indexNameYesterday}, "_id": "AVsGhoTzooztmMPf1gPv"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateYesterday}T18:32:10.052+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeYesterday}"}
{ "index" :{ ${indexNameYesterday}, "_id": "AVsGhoTzooztmuPf1gPv"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateYesterday}T18:32:10.052+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeYesterday}"}
{ "index" :{ ${indexNameYesterday}, "_id": "AVsGhaTzooztmuPf1gPv"} }
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateYesterday}T18:32:20.052+01:00"}
{"api": "bf19088c-f2c7-4fec-9908-8cf2c75fece4","status": 200,"url": "https://api.gravitee.io/echo/","method": 3,"success": true,"state": 0,"message": null,"hostname": "debian","@timestamp": "${dateTimeYesterday}"}
Loading

0 comments on commit 956df94

Please sign in to comment.