Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmitaros committed Oct 6, 2024
1 parent 6dc9b46 commit 0b30fa4
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public interface VesselDataRepository extends JpaRepository<VesselData, Long> {

@Query("SELECT DISTINCT vd.vesselCode FROM VesselData vd")
List<String> findDistinctVesselCode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,7 +24,6 @@

@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class OutlierDetectionServiceIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,7 +15,6 @@

@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class StatisticsCalculationServiceIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,7 +14,6 @@

@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class VesselComplianceServiceIntegrationTest {

@Autowired
Expand All @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,6 @@

@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class VesselDataServiceIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0b30fa4

Please sign in to comment.