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 10, 2024
1 parent 720987e commit c5f8a6a
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 247 deletions.
Empty file added .mvn/lombok-config-copy.marker
Empty file.
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import target/configs/powsybl-build-tools.jar!powsybl-build-tools/lombok.config
35 changes: 18 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
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
Expand Up @@ -9,9 +9,9 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
import lombok.AllArgsConstructor;
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;
Expand All @@ -25,38 +25,41 @@
/**
* @author Nicolas Noir <nicolas.noir at rte-france.com>
*/

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

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

@Autowired
private DataSource dataSource;
private final 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 !!"));
public void run(final String... args) throws Exception {
final PlatformConfig platformConfig = PlatformConfig.defaultConfig();
final ModuleConfig moduleConfigAcquisitionServer = platformConfig.getOptionalModuleConfig("acquisition-server").orElseThrow(() -> new PowsyblException("Module acquisition-server not found !!"));
final ModuleConfig moduleConfigCaseServer = platformConfig.getOptionalModuleConfig("case-server").orElseThrow(() -> new PowsyblException("Module case-server not found !!"));
run(
moduleConfigCaseServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"),
moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("cases-directory"),
moduleConfigAcquisitionServer.getStringProperty("label")
);
}

final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(moduleConfigCaseServer.getStringProperty("url"));
public void run(String caseServerUrl, String acquisitionServerUrl, String acquisitionServerUsername, String acquisitionServerPassword,
String acquisitionServerCasesDirectory, String serverLabel) throws Exception {
final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(caseServerUrl);

try (AcquisitionServer acquisitionServer = new AcquisitionServer(moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"));
try (AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, acquisitionServerUsername, acquisitionServerPassword);
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);
Map<String, String> filesToAcquire = acquisitionServer.listFiles(acquisitionServerCasesDirectory);
LOGGER.info("{} files found on server", filesToAcquire.size());

List<String> filesImported = new ArrayList<>();
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.gridsuite.cases.importer.job;

import org.junit.jupiter.api.Test;
import org.mockftpserver.fake.FakeFtpServer;
import org.mockftpserver.fake.UserAccount;
import org.mockftpserver.fake.filesystem.DirectoryEntry;
import org.mockftpserver.fake.filesystem.FileEntry;
import org.mockftpserver.fake.filesystem.FileSystem;
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;

import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

class AcquisitionServerTest {
@Test
void testSftpAcquisition() throws Exception {
TestUtils.withSftp(server -> {
server.putFile("cases/case1.iidm", "fake file content 1", UTF_8);
server.putFile("cases/case2.iidm", "fake file content 2", UTF_8);
final String acquisitionServerUrl = "sftp://localhost:" + server.getPort();
try (final AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) {
acquisitionServer.open();
Map<String, String> retrievedFiles = acquisitionServer.listFiles("./cases");
assertEquals(2, retrievedFiles.size());

TransferableFile file1 = acquisitionServer.getFile("case1.iidm", acquisitionServerUrl + "/cases/case1.iidm");
assertEquals("case1.iidm", file1.getName());
assertEquals("fake file content 1", new String(file1.getData(), UTF_8));

TransferableFile file2 = acquisitionServer.getFile("case2.iidm", acquisitionServerUrl + "/cases/case2.iidm");
assertEquals("case2.iidm", file2.getName());
assertEquals("fake file content 2", new String(file2.getData(), UTF_8));
}
});
}

@Test
void testFtpAcquisition() throws Exception {
final FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new DirectoryEntry("/cases"));
fileSystem.add(new FileEntry("/cases/case1.iidm", "fake file content 1"));
fileSystem.add(new FileEntry("/cases/case2.iidm", "fake file content 2"));

final FakeFtpServer fakeFtpServer = new FakeFtpServer();
fakeFtpServer.addUserAccount(new UserAccount("dummy_ftp", "dummy_ftp", "/"));
fakeFtpServer.setFileSystem(fileSystem);
fakeFtpServer.setServerControlPort(0);

try {
fakeFtpServer.start();
final String acquisitionServerUrl = "ftp://localhost:" + fakeFtpServer.getServerControlPort();
try (final AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy_ftp", "dummy_ftp")) {
acquisitionServer.open();
Map<String, String> retrievedFiles = acquisitionServer.listFiles("./cases");
assertEquals(2, retrievedFiles.size());

TransferableFile file1 = acquisitionServer.getFile("case1.iidm", acquisitionServerUrl + "/cases/case1.iidm");
assertEquals("case1.iidm", file1.getName());
assertEquals("fake file content 1", new String(file1.getData(), UTF_8));

TransferableFile file2 = acquisitionServer.getFile("case2.iidm", acquisitionServerUrl + "/cases/case2.iidm");
assertEquals("case2.iidm", file2.getName());
assertEquals("fake file content 2", new String(file2.getData(), UTF_8));
}
} finally {
fakeFtpServer.stop();
}
}
}
Loading

0 comments on commit c5f8a6a

Please sign in to comment.