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 c8c5d21 commit 048054b
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 462 deletions.
24 changes: 7 additions & 17 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 @@ -45,9 +45,9 @@
<gridsuite-dependencies.version>34</gridsuite-dependencies.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>
<org.json.version>20200518</org.json.version>
<apache.commons.vfs2.version>2.6.0</apache.commons.vfs2.version>
<commons.net.version>3.7</commons.net.version>
Expand Down Expand Up @@ -110,7 +110,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 @@ -120,7 +120,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 Down Expand Up @@ -216,32 +216,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 @@ -54,7 +54,7 @@ public void open() throws FileSystemException {
fsManager.init();
}

class FileObjectComparator implements Comparator<FileObject> {
private static class FileObjectComparator implements Comparator<FileObject> {
@Override
public int compare(FileObject fo1, FileObject fo2) {
if (fo1 == fo2) {
Expand Down Expand Up @@ -102,6 +102,7 @@ public TransferableFile getFile(String fileName, String fileUrl) throws IOExcept
return new TransferableFile(fileName, file.getContent().getByteArray());
}

@Override
public void close() throws IOException {
fsManager.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private boolean checkValue(String query, String filename, String origin) {
}
}

@Override
public void close() {
try {
connection.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
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.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.lang.Nullable;

import javax.sql.DataSource;
import java.io.InputStreamReader;
Expand All @@ -27,18 +29,13 @@
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
*/

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

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

private DataSource dataSource;

public ProfilesAcquisitionJob(DataSource dataSource) {
this.dataSource = dataSource;
}
private final DataSource dataSource;

public static void main(String... args) {
SpringApplication.run(ProfilesAcquisitionJob.class, args);
Expand All @@ -49,26 +46,34 @@ public void run(String... args) {
handle(null);
}

public void handle(Boolean dependenciesStrictMode) {

PlatformConfig platformConfig = PlatformConfig.defaultConfig();
private void handle(@Nullable final Boolean dependenciesStrictMode) {
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 !!"));
final ModuleConfig moduleConfigCgmesBoundaryServer = platformConfig.getOptionalModuleConfig("cgmes-boundary-server").orElseThrow(() -> new PowsyblException("Module cgmes-boundary-server not found !!"));
handle(
Optional.ofNullable(dependenciesStrictMode).orElseGet(() -> moduleConfigAcquisitionServer.getBooleanProperty("dependencies-strict-mode", false)),
moduleConfigCaseServer.getStringProperty("url"),
moduleConfigCgmesBoundaryServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"),
moduleConfigAcquisitionServer.getStringProperty("cases-directory"),
moduleConfigAcquisitionServer.getStringProperty("label")
);
}

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 !!"));
ModuleConfig moduleConfigCgmesBoundaryServer = platformConfig.getOptionalModuleConfig("cgmes-boundary-server").orElseThrow(() -> new PowsyblException("Module cgmes-boundary-server not found !!"));
public void handle(final boolean dependenciesStrictMode, final String caseServerUrl, final String cgmesBoundaryServerUrl,
final String acquisitionServerUsername, final String acquisitionServerPassword, final String acquisitionServerUrl,
final String casesDirectory, final String acquisitionServerLabel) {

final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(moduleConfigCaseServer.getStringProperty("url"));
final CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester(moduleConfigCgmesBoundaryServer.getStringProperty("url"));
final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester(caseServerUrl);
final CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester(cgmesBoundaryServerUrl);

try (AcquisitionServer acquisitionServer = new AcquisitionServer(moduleConfigAcquisitionServer.getStringProperty("url"),
moduleConfigAcquisitionServer.getStringProperty("username"),
moduleConfigAcquisitionServer.getStringProperty("password"));
CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) {
try (final AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, acquisitionServerUsername, acquisitionServerPassword);
final CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) {
acquisitionServer.open();

String casesDirectory = moduleConfigAcquisitionServer.getStringProperty("cases-directory");
String acquisitionServerLabel = moduleConfigAcquisitionServer.getStringProperty("label");

// Get list of all tsos and business processes from cgmes boundary server
Set<String> authorizedTsos = cgmesBoundaryServiceRequester.getTsosList();
Set<String> authorizedBusinessProcesses = cgmesBoundaryServiceRequester.getBusinessProcessesList();
Expand Down Expand Up @@ -132,10 +137,7 @@ public void handle(Boolean dependenciesStrictMode) {
// Assembling profiles
TransferableFile assembledFile = CgmesUtils.prepareFinalZip(fileInfo.getKey(), availableFileDependencies,
missingDependencies, acquisitionServer, cgmesBoundaryServiceRequester,
dependenciesStrictMode == null
? moduleConfigAcquisitionServer.getBooleanProperty("dependencies-strict-mode", false)
: dependenciesStrictMode,
authorizedTsos, authorizedBusinessProcesses);
dependenciesStrictMode, authorizedTsos, authorizedBusinessProcesses);

if (assembledFile != null) {
// Import assembled file in the case server
Expand Down Expand Up @@ -170,7 +172,7 @@ public void handle(Boolean dependenciesStrictMode) {
LOGGER.error("Interruption during assembling");
Thread.currentThread().interrupt();
} catch (Exception exc) {
LOGGER.error("Job execution error: {}", exc);
LOGGER.error("Job execution error", exc);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.gridsuite.cgmes.assembling.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.io.IOException;
import java.util.Map;
import java.util.Set;

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

class AcquisitionServerTest {
@Test
void testFtpAcquisition() throws IOException {
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"));

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

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();
}
}

@Test
void testAcceptedFilesConnection() throws Exception {
TestUtils.withSftp(server -> {
server.putFile("cases/20200817T1705Z_1D_RTEFRANCE-FR_SV_002.zip", "fake file content 1", UTF_8);
server.putFile("cases/20200817T1705Z__RTEFRANCE-FR_EQ_002.zip", "fake file content 2", UTF_8);
server.putFile("cases/20200817T1705Z_1D_RTEFRANCE-FR_SSH_002.zip", "fake file content 3", UTF_8);
server.putFile("cases/20200817T1705Z_1D_RTEFRANCE-FR_TP_002.zip", "fake file content 4", UTF_8);

final Set<String> authorizedSourcingActors = Set.of("RTEFRANCE-FR");
final Set<String> authorizedBusinessProcesses = Set.of("1D");

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(4, retrievedFiles.size());

TransferableFile file1 = acquisitionServer.getFile("20200817T1705Z_1D_RTEFRANCE-FR_SV_002.zip", acquisitionServerUrl + "/cases/20200817T1705Z_1D_RTEFRANCE-FR_SV_002.zip");
assertTrue(CgmesUtils.isValidProfileFileName(file1.getName(), authorizedSourcingActors, authorizedBusinessProcesses));
assertEquals("20200817T1705Z_1D_RTEFRANCE-FR_SV_002.zip", file1.getName());
assertEquals("fake file content 1", new String(file1.getData(), UTF_8));

TransferableFile file2 = acquisitionServer.getFile("20200817T1705Z__RTEFRANCE-FR_EQ_002.zip", acquisitionServerUrl + "/cases/20200817T1705Z__RTEFRANCE-FR_EQ_002.zip");
assertTrue(CgmesUtils.isValidProfileFileName(file2.getName(), authorizedSourcingActors, authorizedBusinessProcesses));
assertEquals("20200817T1705Z__RTEFRANCE-FR_EQ_002.zip", file2.getName());
assertEquals("fake file content 2", new String(file2.getData(), UTF_8));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.gridsuite.cgmes.assembling.job;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.*;

@JdbcTest
class CgmesAssemblingLoggerTest {
@Autowired private DataSource dataSource;
@MockBean private ProfilesAcquisitionJob runner; //we don't want the runner to run during tests

@Test
void historyLoggerTest() {
try (final CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) {
assertFalse(cgmesAssemblingLogger.isHandledFile("testFile.iidm", "my_sftp_server"));
cgmesAssemblingLogger.logFileAvailable("testFile.iidm", "uuid", "my_sftp_server", new Date());
assertEquals("testFile.iidm", cgmesAssemblingLogger.getFileNameByUuid("uuid", "my_sftp_server"));
assertEquals("uuid", cgmesAssemblingLogger.getUuidByFileName("testFile.iidm", "my_sftp_server"));
assertTrue(cgmesAssemblingLogger.isHandledFile("testFile.iidm", "my_sftp_server"));

cgmesAssemblingLogger.logFileDependencies("uuid", Arrays.asList("uuid1", "uuid2"));
assertEquals(2, cgmesAssemblingLogger.getDependencies("uuid").size(), 2);
}
}

@Test
void testLogDependencies() {
assertThrows(RuntimeException.class, () -> {
try (final CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) {
cgmesAssemblingLogger.logFileDependencies("uuid", null);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.gridsuite.cgmes.assembling.job;

import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CgmesUtilsTest {
@Test
void testGetValidProfileFileName() {
final Set<String> authorizedSourcingActors = Set.of("XX");
final Set<String> authorizedBusinessProcesses = Set.of("1D");

assertEquals("SSH", CgmesUtils.getValidProfileFileName("20191106T0930Z_1D_XX_SSH_001.zip", authorizedSourcingActors, authorizedBusinessProcesses));
assertNull(CgmesUtils.getValidProfileFileName("20191106T0930Z_1D_XX_SSH_1002.zip", authorizedSourcingActors, authorizedBusinessProcesses));
assertNull(CgmesUtils.getValidProfileFileName("20191106T0930Z_1D_YY_SSH_001.zip", authorizedSourcingActors, authorizedBusinessProcesses));
assertNull(CgmesUtils.getValidProfileFileName("20191106T0930Z_6D_XX_SSH_001.zip", authorizedSourcingActors, authorizedBusinessProcesses));
assertNull(CgmesUtils.getValidProfileFileName("20191106T0930Z_1D_XX_SSH_001.xml", authorizedSourcingActors, authorizedBusinessProcesses));
assertNull(CgmesUtils.getValidProfileFileName("20191106T0930Z_1D_XX_SSH_abc.zip", authorizedSourcingActors, authorizedBusinessProcesses));
}
}
Loading

0 comments on commit 048054b

Please sign in to comment.