diff --git a/.mvn/lombok-config-copy.marker b/.mvn/lombok-config-copy.marker new file mode 100644 index 0000000..e69de29 diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..4fa8250 --- /dev/null +++ b/lombok.config @@ -0,0 +1 @@ +import target/configs/powsybl-build-tools.jar!powsybl-build-tools/lombok.config diff --git a/pom.xml b/pom.xml index d7a4976..71357e0 100644 --- a/pom.xml +++ b/pom.xml @@ -43,11 +43,11 @@ 34 - + 1.18.34 0.1.55 - 2.0.1 + 2.0.0 2.7.1 - 5.10.0 + 5.15.0 2.6.0 3.7 @@ -92,6 +92,11 @@ + + org.projectlombok + lombok + ${lombok.version} + com.jcraft jsch @@ -109,7 +114,7 @@ com.github.stefanbirkner - fake-sftp-server-rule + fake-sftp-server-lambda ${fake.sftp.server.version} @@ -119,7 +124,7 @@ org.mock-server - mockserver-junit-rule + mockserver-junit-jupiter ${mockserver.version} @@ -131,6 +136,12 @@ + + org.projectlombok + lombok + provided + + org.apache.commons @@ -197,7 +208,7 @@ com.github.stefanbirkner - fake-sftp-server-rule + fake-sftp-server-lambda test @@ -205,16 +216,6 @@ powsybl-config-test test - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - org.mock-server mockserver-netty @@ -222,7 +223,7 @@ org.mock-server - mockserver-junit-rule + mockserver-junit-jupiter test diff --git a/src/main/java/org/gridsuite/cases/importer/job/AcquisitionServer.java b/src/main/java/org/gridsuite/cases/importer/job/AcquisitionServer.java index 83df7b4..ac6b229 100644 --- a/src/main/java/org/gridsuite/cases/importer/job/AcquisitionServer.java +++ b/src/main/java/org/gridsuite/cases/importer/job/AcquisitionServer.java @@ -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(); } diff --git a/src/main/java/org/gridsuite/cases/importer/job/CaseAcquisitionJob.java b/src/main/java/org/gridsuite/cases/importer/job/CaseAcquisitionJob.java index 83e15e1..c5e1121 100644 --- a/src/main/java/org/gridsuite/cases/importer/job/CaseAcquisitionJob.java +++ b/src/main/java/org/gridsuite/cases/importer/job/CaseAcquisitionJob.java @@ -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; @@ -25,38 +25,41 @@ /** * @author Nicolas Noir */ - -@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 filesToAcquire = acquisitionServer.listFiles(casesDirectory); + Map filesToAcquire = acquisitionServer.listFiles(acquisitionServerCasesDirectory); LOGGER.info("{} files found on server", filesToAcquire.size()); List filesImported = new ArrayList<>(); diff --git a/src/main/java/org/gridsuite/cases/importer/job/CaseImportLogger.java b/src/main/java/org/gridsuite/cases/importer/job/CaseImportLogger.java index 7352d49..bb01a49 100644 --- a/src/main/java/org/gridsuite/cases/importer/job/CaseImportLogger.java +++ b/src/main/java/org/gridsuite/cases/importer/job/CaseImportLogger.java @@ -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 { @@ -59,6 +59,7 @@ public void logFileAcquired(String fileName, String origin, Date date) { } } + @Override public void close() { try { connection.close(); diff --git a/src/test/java/org/gridsuite/cases/importer/job/AcquisitionServerTest.java b/src/test/java/org/gridsuite/cases/importer/job/AcquisitionServerTest.java new file mode 100644 index 0000000..b1e6949 --- /dev/null +++ b/src/test/java/org/gridsuite/cases/importer/job/AcquisitionServerTest.java @@ -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 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 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(); + } + } +} diff --git a/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionJobTest.java b/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionJobTest.java deleted file mode 100644 index 9000b1a..0000000 --- a/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionJobTest.java +++ /dev/null @@ -1,210 +0,0 @@ -/** - * 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 com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule; -import org.junit.*; -import org.junit.runner.RunWith; -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 org.mockserver.junit.MockServerRule; -import org.mockserver.verify.VerificationTimes; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.sql.DataSource; -import java.io.IOException; -import java.util.Date; -import java.util.Map; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.*; -import static org.mockserver.model.HttpRequest.request; -import static org.mockserver.model.HttpResponse.response; - -/** - * @author Nicolas Noir - */ - -@RunWith(SpringRunner.class) -@SpringBootTest -@ContextConfiguration(classes = {CaseAcquisitionJob.class}) -public class CaseAcquisitionJobTest { - - @ClassRule - public static final FakeSftpServerRule SFTP_SERVER_RULE = new FakeSftpServerRule().addUser("dummy", "dummy").setPort(2222); - - @Rule - public final MockServerRule mockServer = new MockServerRule(this, 45385); - - @Autowired - private DataSource dataSource; - - @BeforeClass - public static void init() throws IOException { - SFTP_SERVER_RULE.createDirectory("/cases"); - } - - @After - public void tearDown() throws IOException { - SFTP_SERVER_RULE.deleteAllFilesAndDirectories(); - } - - @Test - public void historyLoggerTest() { - try (CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { - Date importDate = new Date(); - assertFalse(caseImportLogger.isImportedFile("testFile.iidm", "my_sftp_server")); - caseImportLogger.logFileAcquired("testFile.iidm", "my_sftp_server", importDate); - assertTrue(caseImportLogger.isImportedFile("testFile.iidm", "my_sftp_server")); - } - } - - @Test(expected = RuntimeException.class) - public void testLogFileAcquiredError() { - try (CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { - Date importDate = new Date(); - caseImportLogger.logFileAcquired("test.iidm", null, importDate); - caseImportLogger.logFileAcquired(null, "my_sftp_server", importDate); - } - } - - @Test - public void testSftpAcquisition() throws IOException { - - SFTP_SERVER_RULE.createDirectory("/cases"); - SFTP_SERVER_RULE.putFile("/cases/case1.iidm", "fake file content 1", UTF_8); - SFTP_SERVER_RULE.putFile("/cases/case2.iidm", "fake file content 2", UTF_8); - String acquisitionServerUrl = "sftp://localhost:" + SFTP_SERVER_RULE.getPort(); - - try (AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) { - acquisitionServer.open(); - Map 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)); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Test - public void testFtpAcquisition() { - - 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(); - - String acquisitionServerUrl = "ftp://localhost:" + fakeFtpServer.getServerControlPort(); - try (AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy_ftp", "dummy_ftp")) { - acquisitionServer.open(); - Map 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)); - } catch (IOException e) { - e.printStackTrace(); - } finally { - fakeFtpServer.stop(); - } - - } - - @Test - public void testCaseImportRequester() throws IOException, InterruptedException { - CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester("http://localhost:45385/"); - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(200)); - assertTrue(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", new String("Case file content").getBytes(UTF_8)))); - mockServer.getClient().clear(request()); - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(500)); - assertFalse(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", new String("Case file content").getBytes(UTF_8)))); - } - - @Test - public void mainTest() throws IOException { - - SFTP_SERVER_RULE.createDirectory("/cases"); - SFTP_SERVER_RULE.putFile("/cases/case1.iidm", "fake file content 1", UTF_8); - SFTP_SERVER_RULE.putFile("/cases/case2.iidm", "fake file content 2", UTF_8); - - CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource); - - String[] args = {}; - - // 2 files on SFTP server, 2 cases will be imported - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(200)); - CaseAcquisitionJob.main(args); - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(2)); - assertTrue(caseImportLogger.isImportedFile("case1.iidm", "my_sftp_server")); - assertTrue(caseImportLogger.isImportedFile("case2.iidm", "my_sftp_server")); - - // No new files on SFTP server, no import requested - mockServer.getClient().clear(request()); - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(200)); - CaseAcquisitionJob.main(args); - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(0)); - - // One new file on SFTP server, one case import requested - mockServer.getClient().clear(request()); - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(200)); - SFTP_SERVER_RULE.putFile("/cases/case3.iidm", "fake file content 3", UTF_8); - CaseAcquisitionJob.main(args); - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(1)); - assertTrue(caseImportLogger.isImportedFile("case3.iidm", "my_sftp_server")); - - // One new file on server, but error when requesting import: case is not logged as imported - mockServer.getClient().clear(request()); - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/cases")) - .respond(response().withStatusCode(500)); - SFTP_SERVER_RULE.putFile("/cases/case4.iidm", "fake file content 4", UTF_8); - CaseAcquisitionJob.main(args); - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(1)); - assertFalse(caseImportLogger.isImportedFile("case4.iidm", "my_sftp_server")); - } - - @Test(expected = RuntimeException.class) - public void testIslogFileAcquiredException() { - CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource); - caseImportLogger.logFileAcquired(null, null, null); - } - - @Test(expected = RuntimeException.class) - public void testDatasourceNullException() { - new CaseImportLogger(null); - } -} diff --git a/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionRunnerTest.java b/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionRunnerTest.java new file mode 100644 index 0000000..5926175 --- /dev/null +++ b/src/test/java/org/gridsuite/cases/importer/job/CaseAcquisitionRunnerTest.java @@ -0,0 +1,80 @@ +package org.gridsuite.cases.importer.job; + +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockserver.client.MockServerClient; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.verify.VerificationTimes; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; + +import javax.sql.DataSource; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +@DirtiesContext(classMode = ClassMode.AFTER_CLASS) +@ExtendWith(MockServerExtension.class) +@SpringBootTest(classes = { CaseAcquisitionJob.class }) +class CaseAcquisitionRunnerTest { + @Autowired private DataSource dataSource; + @MockBean private CaseAcquisitionJob clRunner; //prevent runner to run before tests + + @Test + void mainTest(final MockServerClient mockServerClient) throws Exception { + final CaseAcquisitionJob runner = new CaseAcquisitionJob(dataSource); + 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); + try (final CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { + + // 2 files on SFTP server, 2 cases will be imported + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(200)); + runJob(runner, server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(2)); + assertTrue(caseImportLogger.isImportedFile("case1.iidm", TestUtils.SFTP_LABEL)); + assertTrue(caseImportLogger.isImportedFile("case2.iidm", TestUtils.SFTP_LABEL)); + + // No new files on SFTP server, no import requested + mockServerClient.clear(request()); + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(200)); + runJob(runner, server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(0)); + + // One new file on SFTP server, one case import requested + mockServerClient.clear(request()); + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(200)); + server.putFile("cases/case3.iidm", "fake file content 3", UTF_8); + runJob(runner, server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(1)); + assertTrue(caseImportLogger.isImportedFile("case3.iidm", TestUtils.SFTP_LABEL)); + + // One new file on server, but error when requesting import: case is not logged as imported + mockServerClient.clear(request()); + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(500)); + server.putFile("cases/case4.iidm", "fake file content 4", UTF_8); + runJob(runner, server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/cases"), VerificationTimes.exactly(1)); + assertFalse(caseImportLogger.isImportedFile("case4.iidm", TestUtils.SFTP_LABEL)); + } + }); + } + + private static void runJob(final CaseAcquisitionJob runner, final FakeSftpServer sftpServer, final MockServerClient mockServerClient) throws Exception { + runner.run( + "http://localhost:" + mockServerClient.getPort() + "/", + "sftp://localhost:" + sftpServer.getPort(), //random free port + "dummy", //server.addUser(...) + "dummy", + "./cases", //server.createDirectory(...) + TestUtils.SFTP_LABEL + ); + } +} diff --git a/src/test/java/org/gridsuite/cases/importer/job/CaseImportLoggerTest.java b/src/test/java/org/gridsuite/cases/importer/job/CaseImportLoggerTest.java new file mode 100644 index 0000000..6730e5a --- /dev/null +++ b/src/test/java/org/gridsuite/cases/importer/job/CaseImportLoggerTest.java @@ -0,0 +1,66 @@ +/** + * 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 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.Date; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Nicolas Noir + */ +@JdbcTest +class CaseImportLoggerTest { + @Autowired private DataSource dataSource; + @MockBean private CaseAcquisitionJob job; //we don't want to run the cli runner + + @Test + void historyLoggerTest() { + try (CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { + Date importDate = new Date(); + assertFalse(caseImportLogger.isImportedFile("testFile.iidm", TestUtils.SFTP_LABEL)); + caseImportLogger.logFileAcquired("testFile.iidm", TestUtils.SFTP_LABEL, importDate); + assertTrue(caseImportLogger.isImportedFile("testFile.iidm", TestUtils.SFTP_LABEL)); + } + } + + @Test + void testLogFileAcquiredError() { + assertThrows(RuntimeException.class, () -> { + try (CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { + Date importDate = new Date(); + caseImportLogger.logFileAcquired("test.iidm", null, importDate); + caseImportLogger.logFileAcquired(null, TestUtils.SFTP_LABEL, importDate); + } + }); + } + + @Test + void testIsLogFileAcquiredException() { + assertThrows(RuntimeException.class, () -> { + try (final CaseImportLogger caseImportLogger = new CaseImportLogger(dataSource)) { + caseImportLogger.logFileAcquired(null, null, null); + } + }); + } + + @SuppressWarnings("EmptyTryBlock") + @Test + void testDatasourceNullException() { + assertThrows(RuntimeException.class, () -> { + try (final CaseImportLogger caseImportLogger = new CaseImportLogger(null)) { + /* case not accessible */ + } + }); + } +} diff --git a/src/test/java/org/gridsuite/cases/importer/job/CaseImportServiceRequesterTest.java b/src/test/java/org/gridsuite/cases/importer/job/CaseImportServiceRequesterTest.java new file mode 100644 index 0000000..5f6cfea --- /dev/null +++ b/src/test/java/org/gridsuite/cases/importer/job/CaseImportServiceRequesterTest.java @@ -0,0 +1,25 @@ +package org.gridsuite.cases.importer.job; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockserver.client.MockServerClient; +import org.mockserver.junit.jupiter.MockServerExtension; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +@ExtendWith(MockServerExtension.class) +class CaseImportServiceRequesterTest { + @Test + void testCaseImportRequester(final MockServerClient mockServerClient) throws Exception { + final CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester("http://localhost:" + mockServerClient.getPort() + "/"); + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(200)); + assertTrue(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", "Case file content".getBytes(UTF_8)))); + mockServerClient.clear(request()); + mockServerClient.when(request().withMethod("POST").withPath("/v1/cases")).respond(response().withStatusCode(500)); + assertFalse(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", "Case file content".getBytes(UTF_8)))); + } +} diff --git a/src/test/java/org/gridsuite/cases/importer/job/TestUtils.java b/src/test/java/org/gridsuite/cases/importer/job/TestUtils.java new file mode 100644 index 0000000..7e62f22 --- /dev/null +++ b/src/test/java/org/gridsuite/cases/importer/job/TestUtils.java @@ -0,0 +1,23 @@ +package org.gridsuite.cases.importer.job; + +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.ExceptionThrowingConsumer; +import org.springframework.lang.NonNull; + +import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer; + +public final class TestUtils { + public static final String SFTP_LABEL = "my_sftp_server"; + + private TestUtils() { + throw new IllegalCallerException("Utility class"); + } + + public static void withSftp(@NonNull final ExceptionThrowingConsumer testCode) throws Exception { + withSftpServer(server -> { + server.addUser("dummy", "dummy")/*.setPort(2222)*/; + server.createDirectory("cases"); + testCode.accept(server); + server.deleteAllFilesAndDirectories(); + }); + } +} diff --git a/src/test/resources/application-default.yml b/src/test/resources/application-default.yml index 87bde7e..f5ac7df 100644 --- a/src/test/resources/application-default.yml +++ b/src/test/resources/application-default.yml @@ -1,5 +1,8 @@ +# Fix "Replacing 'dataSource' DataSource bean with embedded version" +spring.test.database.replace: none + powsybl-ws: database: vendor: h2:mem query: ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE - hostPort: ":" + host-port: ":"