diff --git a/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java b/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java index 39b2190..374afe0 100644 --- a/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java +++ b/src/main/java/com/gmitaros/vesselmetrics/parser/impl/CsvParserService.java @@ -2,6 +2,7 @@ import com.gmitaros.vesselmetrics.model.VesselData; import com.gmitaros.vesselmetrics.parser.DataParser; +import com.gmitaros.vesselmetrics.repository.VesselDataRepository; import com.gmitaros.vesselmetrics.service.MetricsCalculationService; import com.gmitaros.vesselmetrics.service.OutlierDetectionService; import com.gmitaros.vesselmetrics.service.ValidationService; @@ -40,9 +41,14 @@ public class CsvParserService implements DataParser { @Value("${spring.jpa.properties.hibernate.jdbc.batch_size}") private int batchSize; + + @Value("${vessel.metrics.csv.load.if.already.have.data:false}") + private boolean loadCsvIfAlreadyHaveData; + @Value("${vessel.metrics.csv.path}") private String vesselDataPath; + private final VesselDataRepository vesselDataRepository; private final VesselDataBatchService vesselDataBatchService; private final OutlierDetectionService outlierDetectionService; private final ValidationService validationService; @@ -54,12 +60,17 @@ public class CsvParserService implements DataParser { */ @PostConstruct public void init() { - try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) { - parseAndSave(inputStream); - checkForOutliers(); - } catch (Exception e) { - log.error("Error initializing data: ", e); - throw new RuntimeException("Failed to initialize data", e); + long vesselData = vesselDataRepository.count(); + boolean loadData = vesselData <= 0 || loadCsvIfAlreadyHaveData; + if (loadData) { + log.info("CsvParserService will load data from {} file", vesselDataPath); + try (InputStream inputStream = getClass().getResourceAsStream(vesselDataPath)) { + parseAndSave(inputStream); + checkForOutliers(); + } catch (Exception e) { + log.error("Error initializing data: ", e); + throw new RuntimeException("Failed to initialize data", e); + } } } diff --git a/src/main/java/com/gmitaros/vesselmetrics/repository/VesselDataRepository.java b/src/main/java/com/gmitaros/vesselmetrics/repository/VesselDataRepository.java index b7ab512..f849d70 100644 --- a/src/main/java/com/gmitaros/vesselmetrics/repository/VesselDataRepository.java +++ b/src/main/java/com/gmitaros/vesselmetrics/repository/VesselDataRepository.java @@ -26,4 +26,5 @@ public interface VesselDataRepository extends JpaRepository { @Query("SELECT DISTINCT vd.vesselCode FROM VesselData vd") List findDistinctVesselCode(); + } diff --git a/src/test/java/com/gmitaros/vesselmetrics/service/OutlierDetectionServiceIntegrationTest.java b/src/test/java/com/gmitaros/vesselmetrics/service/OutlierDetectionServiceIntegrationTest.java index 920a7ce..dd02fe5 100644 --- a/src/test/java/com/gmitaros/vesselmetrics/service/OutlierDetectionServiceIntegrationTest.java +++ b/src/test/java/com/gmitaros/vesselmetrics/service/OutlierDetectionServiceIntegrationTest.java @@ -11,7 +11,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -25,7 +24,6 @@ @ActiveProfiles("test") @SpringBootTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class OutlierDetectionServiceIntegrationTest { @Autowired diff --git a/src/test/java/com/gmitaros/vesselmetrics/service/StatisticsCalculationServiceIntegrationTest.java b/src/test/java/com/gmitaros/vesselmetrics/service/StatisticsCalculationServiceIntegrationTest.java index 7a1e0a2..78c507f 100644 --- a/src/test/java/com/gmitaros/vesselmetrics/service/StatisticsCalculationServiceIntegrationTest.java +++ b/src/test/java/com/gmitaros/vesselmetrics/service/StatisticsCalculationServiceIntegrationTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -16,7 +15,6 @@ @ActiveProfiles("test") @SpringBootTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class StatisticsCalculationServiceIntegrationTest { @Autowired diff --git a/src/test/java/com/gmitaros/vesselmetrics/service/VesselComplianceServiceIntegrationTest.java b/src/test/java/com/gmitaros/vesselmetrics/service/VesselComplianceServiceIntegrationTest.java index 9855d17..7906058 100644 --- a/src/test/java/com/gmitaros/vesselmetrics/service/VesselComplianceServiceIntegrationTest.java +++ b/src/test/java/com/gmitaros/vesselmetrics/service/VesselComplianceServiceIntegrationTest.java @@ -4,7 +4,6 @@ import com.gmitaros.vesselmetrics.exception.VesselNotFoundException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; @@ -15,7 +14,6 @@ @ActiveProfiles("test") @SpringBootTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class VesselComplianceServiceIntegrationTest { @Autowired @@ -31,7 +29,7 @@ void testCompareVesselCompliance_ValidVessels() { assertNotNull(response); assertEquals(vesselCode1, response.getVesselCode1()); assertEquals(vesselCode2, response.getVesselCode2()); - assertEquals(-219.60153732307575, response.getCompliance1(), 0.01, "Compliance 1 is incorrect"); + assertEquals(-220.33949162642512, response.getCompliance1(), 0.01, "Compliance 1 is incorrect"); assertEquals(1.4894573890349938, response.getCompliance2(), 0.01, "Compliance 2 is incorrect"); assertEquals(response.getResult(), "19310 is more compliant."); } diff --git a/src/test/java/com/gmitaros/vesselmetrics/service/VesselDataServiceIntegrationTest.java b/src/test/java/com/gmitaros/vesselmetrics/service/VesselDataServiceIntegrationTest.java index 5951117..857876e 100644 --- a/src/test/java/com/gmitaros/vesselmetrics/service/VesselDataServiceIntegrationTest.java +++ b/src/test/java/com/gmitaros/vesselmetrics/service/VesselDataServiceIntegrationTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -19,7 +18,6 @@ @ActiveProfiles("test") @SpringBootTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class VesselDataServiceIntegrationTest { @Autowired diff --git a/src/test/resources/db/migration-test/R__clear_database_tables.sql b/src/test/resources/db/migration-test/R__clear_database_tables.sql index e47a5e1..5945435 100644 --- a/src/test/resources/db/migration-test/R__clear_database_tables.sql +++ b/src/test/resources/db/migration-test/R__clear_database_tables.sql @@ -1,3 +1,4 @@ SET REFERENTIAL_INTEGRITY FALSE; -TRUNCATE TABLE vessel_data_validation_errors, vessel_data; +TRUNCATE TABLE vessel_data_validation_errors; +TRUNCATE TABLE vessel_data; SET REFERENTIAL_INTEGRITY TRUE; \ No newline at end of file