Skip to content

Commit

Permalink
replace OffsetDateTime with Instant (#36)
Browse files Browse the repository at this point in the history
Signed-off-by: TOURI ANIS <anis-1.touri@rte-france.com>
  • Loading branch information
jonenst committed Sep 16, 2024
1 parent 1df0716 commit a9ab69f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -249,9 +249,9 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) {
}

private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration) {
LocalDateTime expirationTime = null;
Instant expirationTime = null;
if (withExpiration) {
expirationTime = LocalDateTime.now(ZoneOffset.UTC).plusHours(1);
expirationTime = Instant.now().plus(1, ChronoUnit.HOURS);
}
caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
Expand All @@ -34,10 +33,10 @@ public ScheduledCaseCleaner(CaseMetadataRepository caseMetadataRepository, CaseS

@Scheduled(cron = "${cleaning-cases-cron}", zone = "UTC")
public void deleteExpiredCases() {
LocalDateTime localDateTime = LocalDateTime.now(ZoneOffset.UTC);
LOGGER.info("Cleaning cases cron starting execution at {}", localDateTime);
Instant now = Instant.now();
LOGGER.info("Cleaning cases cron starting execution at {}", now);
caseMetadataRepository.findAll().stream().filter(caseMetadataEntity -> caseMetadataEntity.getExpirationDate() != null)
.filter(caseMetadataEntity -> localDateTime.isAfter(caseMetadataEntity.getExpirationDate()))
.filter(caseMetadataEntity -> now.isAfter(caseMetadataEntity.getExpirationDate()))
.forEach(caseMetadataEntity -> {
caseService.deleteCase(caseMetadataEntity.getId());
caseMetadataRepository.deleteById(caseMetadataEntity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package com.powsybl.caseserver.repository;

import java.time.LocalDateTime;
import java.time.Instant;
import java.util.UUID;

import lombok.*;
Expand All @@ -31,6 +31,6 @@ public class CaseMetadataEntity {
@Column(name = "id")
private UUID id;

@Column(name = "expirationDate")
private LocalDateTime expirationDate;
@Column(name = "expirationDate", columnDefinition = "timestamptz")
private Instant expirationDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="tourian1 (generated)" id="1718877811140-1">
<modifyDataType tableName="case_metadata" columnName="expiration_date" newDataType="timestamptz"/>
</changeSet>
</databaseChangeLog>
4 changes: 4 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
databaseChangeLog:
- include:
file: changesets/changelog_20221206T100716Z.xml
relativeToChangelogFile: true

- include:
file: changesets/changelog_20240620T100317Z.xml
relativeToChangelogFile: true
8 changes: 4 additions & 4 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -332,9 +332,9 @@ public void test() throws Exception {
assertNull(caseMetadataEntity.getExpirationDate());

// import a case with expiration
LocalDateTime beforeImportDate = LocalDateTime.now(ZoneOffset.UTC).plusHours(1);
Instant beforeImportDate = Instant.now().plus(1, ChronoUnit.HOURS);
UUID thirdCaseUuid = importCase(TEST_CASE, true);
LocalDateTime afterImportDate = LocalDateTime.now(ZoneOffset.UTC).plusHours(1);
Instant afterImportDate = Instant.now().plus(1, ChronoUnit.HOURS);

// assert that the broker message has been sent
messageImport = outputDestination.receive(1000, "case.import.destination");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -58,10 +58,10 @@ private void cleanDB() {

@Test
public void test() {
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
LocalDateTime yesterday = now.minusDays(1);
CaseMetadataEntity shouldNotExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), now.plusHours(1));
CaseMetadataEntity shouldExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), yesterday.plusHours(1));
Instant now = Instant.now();
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
CaseMetadataEntity shouldNotExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), now.plus(1, ChronoUnit.HOURS));
CaseMetadataEntity shouldExpireEntity = new CaseMetadataEntity(UUID.randomUUID(), yesterday.plus(1, ChronoUnit.HOURS));
CaseMetadataEntity noExpireDateEntity = new CaseMetadataEntity(UUID.randomUUID(), null);
caseMetadataRepository.save(shouldExpireEntity);
caseMetadataRepository.save(shouldNotExpireEntity);
Expand Down
10 changes: 6 additions & 4 deletions src/test/resources/application-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ spring:
properties:
dialect: org.hibernate.dialect.H2Dialect
hibernate.format_sql: true
hibernate:
format_sql: true
generate_statistics: true
dialect: org.hibernate.dialect.H2Dialect
hibernate:
format_sql: true
generate_statistics: true
dialect: org.hibernate.dialect.H2Dialect
#to turn off schema validation that fails (because of timestampz types) and blocks tests even if the schema is compatible
ddl-auto: none

logging:
level:
Expand Down

0 comments on commit a9ab69f

Please sign in to comment.