Skip to content

Commit

Permalink
Migrate tests to Junit5
Browse files Browse the repository at this point in the history
Config and runner fixes needed to be tested.
  • Loading branch information
Tristan-WorkGH committed Oct 7, 2024
1 parent 720987e commit 71bef57
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 298 deletions.
37 changes: 19 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-parent-ws</artifactId>
<version>20</version>
<version>21-SNAPSHOT</version>
<relativePath/>
</parent>

Expand Down Expand Up @@ -43,11 +43,11 @@

<properties>
<gridsuite-dependencies.version>34</gridsuite-dependencies.version>

<lombok.version>1.18.34</lombok.version>
<jcraft.version>0.1.55</jcraft.version>
<fake.sftp.server.version>2.0.1</fake.sftp.server.version>
<fake.sftp.server.version>2.0.0</fake.sftp.server.version>
<mock.ftp.server.version>2.7.1</mock.ftp.server.version>
<mockserver.version>5.10.0</mockserver.version>
<mockserver.version>5.15.0</mockserver.version>
<apache.commons.vfs2.version>2.6.0</apache.commons.vfs2.version>
<commons.net.version>3.7</commons.net.version>
</properties>
Expand Down Expand Up @@ -92,6 +92,11 @@
</dependency>

<!-- project specific dependencies -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
Expand All @@ -109,7 +114,7 @@
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>fake-sftp-server-rule</artifactId>
<artifactId>fake-sftp-server-lambda</artifactId>
<version>${fake.sftp.server.version}</version>
</dependency>
<dependency>
Expand All @@ -119,7 +124,7 @@
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-rule</artifactId>
<artifactId>mockserver-junit-jupiter</artifactId>
<version>${mockserver.version}</version>
</dependency>
<dependency>
Expand All @@ -131,6 +136,12 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Compilation dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -197,32 +208,22 @@
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>fake-sftp-server-rule</artifactId>
<artifactId>fake-sftp-server-lambda</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-config-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-rule</artifactId>
<artifactId>mockserver-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public TransferableFile getFile(String fileName, String fileUrl) throws IOExcept
return new TransferableFile(fileName, file.getContent().getByteArray());
}

@Override
public void close() {
fsManager.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.gridsuite.cases.importer.job;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "org.gridsuite.job")
public class CaseAcquisitionConfig {
public CaseAcquisitionConfig() {
final PlatformConfig platformConfig = PlatformConfig.defaultConfig();
final ModuleConfig moduleConfigAcquisitionServer = platformConfig.getOptionalModuleConfig("acquisition-server").orElseThrow(() -> new PowsyblException("Module acquisition-server not found !!"));
this.acquisitionServer = new AcquisitionServerConfig(
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"),
moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("cases-directory"),
moduleConfigAcquisitionServer.getStringProperty("label")
);
final ModuleConfig moduleConfigCaseServer = platformConfig.getOptionalModuleConfig("case-server").orElseThrow(() -> new PowsyblException("Module case-server not found !!"));
this.caseServer = new CaseServerConfig(moduleConfigCaseServer.getStringProperty("url"));
}

private AcquisitionServerConfig acquisitionServer;
private CaseServerConfig caseServer;

@Data
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public static class AcquisitionServerConfig {
private String username;
private String password;
private String url;
private String casesDirectory;
private String label;
}

@Data
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public static class CaseServerConfig {
private String url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,17 @@
*/
package org.gridsuite.cases.importer.job;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Nicolas Noir <nicolas.noir at rte-france.com>
*/

@SuppressWarnings({"checkstyle:HideUtilityClassConstructor", "checkstyle:FinalClass"})
@SpringBootApplication
public class CaseAcquisitionJob implements CommandLineRunner {

public class CaseAcquisitionJob {
private static final Logger LOGGER = LoggerFactory.getLogger(CaseAcquisitionJob.class);

@Autowired
private DataSource dataSource;

public static void main(String... args) {
SpringApplication.run(CaseAcquisitionJob.class, args);
}

@Override
public void run(String... args) throws Exception {
PlatformConfig platformConfig = PlatformConfig.defaultConfig();

ModuleConfig moduleConfigAcquisitionServer = platformConfig.getOptionalModuleConfig("acquisition-server").orElseThrow(() -> new PowsyblException("Module acquisition-server not found !!"));
ModuleConfig moduleConfigCaseServer = platformConfig.getOptionalModuleConfig("case-server").orElseThrow(() -> new PowsyblException("Module case-server not found !!"));

final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(moduleConfigCaseServer.getStringProperty("url"));

try (AcquisitionServer acquisitionServer = new AcquisitionServer(moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"));
CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) {
acquisitionServer.open();

String casesDirectory = moduleConfigAcquisitionServer.getStringProperty("cases-directory");
String serverLabel = moduleConfigAcquisitionServer.getStringProperty("label");
Map<String, String> filesToAcquire = acquisitionServer.listFiles(casesDirectory);
LOGGER.info("{} files found on server", filesToAcquire.size());

List<String> filesImported = new ArrayList<>();
List<String> filesAlreadyImported = new ArrayList<>();
List<String> filesImportFailed = new ArrayList<>();
for (Map.Entry<String, String> fileInfo : filesToAcquire.entrySet()) {
if (!caseImportLogger.isImportedFile(fileInfo.getKey(), serverLabel)) {
TransferableFile acquiredFile = acquisitionServer.getFile(fileInfo.getKey(), fileInfo.getValue());
LOGGER.info("Importing file '{}'...", fileInfo.getKey());
boolean importOk = caseImportServiceRequester.importCase(acquiredFile);
if (importOk) {
caseImportLogger.logFileAcquired(acquiredFile.getName(), serverLabel, new Date());
filesImported.add(fileInfo.getKey());
} else {
filesImportFailed.add(fileInfo.getKey());
}
} else {
filesAlreadyImported.add(fileInfo.getKey());
}
}
LOGGER.info("===== JOB EXECUTION SUMMARY =====");
LOGGER.info("{} files already imported", filesAlreadyImported.size());
LOGGER.info("{} files successfully imported", filesImported.size());
filesImported.forEach(f -> LOGGER.info("File '{}' successfully imported", f));
LOGGER.info("{} files import failed", filesImportFailed.size());
filesImportFailed.forEach(f -> LOGGER.info("File '{}' import failed !!", f));
LOGGER.info("=================================");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2020, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.cases.importer.job;

import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Component
@AllArgsConstructor
public class CaseAcquisitionRunner implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(CaseAcquisitionRunner.class);

private final DataSource dataSource;
private final CaseAcquisitionConfig config;

@Override
public void run(final String... args) throws Exception {
final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(config.getCaseServer().getUrl());

try (AcquisitionServer acquisitionServer = new AcquisitionServer(config.getAcquisitionServer().getUrl(),
config.getAcquisitionServer().getUsername(),
config.getAcquisitionServer().getPassword());
CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) {
acquisitionServer.open();

final String serverLabel = config.getAcquisitionServer().getLabel();
Map<String, String> filesToAcquire = acquisitionServer.listFiles(config.getAcquisitionServer().getCasesDirectory());
LOGGER.info("{} files found on server", filesToAcquire.size());

List<String> filesImported = new ArrayList<>();
List<String> filesAlreadyImported = new ArrayList<>();
List<String> filesImportFailed = new ArrayList<>();
for (final Entry<String, String> fileInfo : filesToAcquire.entrySet()) {
if (!caseImportLogger.isImportedFile(fileInfo.getKey(), serverLabel)) {
TransferableFile acquiredFile = acquisitionServer.getFile(fileInfo.getKey(), fileInfo.getValue());
LOGGER.info("Importing file '{}'...", fileInfo.getKey());
boolean importOk = caseImportServiceRequester.importCase(acquiredFile);
if (importOk) {
caseImportLogger.logFileAcquired(acquiredFile.getName(), serverLabel, new Date());
filesImported.add(fileInfo.getKey());
} else {
filesImportFailed.add(fileInfo.getKey());
}
} else {
filesAlreadyImported.add(fileInfo.getKey());
}
}
LOGGER.info("===== JOB EXECUTION SUMMARY =====");
LOGGER.info("{} files already imported", filesAlreadyImported.size());
LOGGER.info("{} files successfully imported", filesImported.size());
filesImported.forEach(f -> LOGGER.info("File '{}' successfully imported", f));
LOGGER.info("{} files import failed", filesImportFailed.size());
filesImportFailed.forEach(f -> LOGGER.info("File '{}' import failed !!", f));
LOGGER.info("=================================");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CaseImportLogger implements AutoCloseable {
private static final String SELECT_QUERY = "SELECT (filename, origin, import_date) FROM files where filename = ? and origin = ?";
private static final String INSERT_QUERY = "INSERT INTO files (filename, origin, import_date) VALUES(?, ?, ?)";

private Connection connection;
private final Connection connection;

public CaseImportLogger(DataSource dataSource) {
try {
Expand Down Expand Up @@ -59,6 +59,7 @@ public void logFileAcquired(String fileName, String origin, Date date) {
}
}

@Override
public void close() {
try {
connection.close();
Expand Down
Loading

0 comments on commit 71bef57

Please sign in to comment.