diff --git a/pom.xml b/pom.xml index 63ad6a8..db4294f 100644 --- a/pom.xml +++ b/pom.xml @@ -45,9 +45,9 @@ 34 0.1.55 - 2.0.1 + 2.0.0 2.7.1 - 5.10.0 + 5.15.0 20200518 2.6.0 3.7 @@ -110,7 +110,7 @@ com.github.stefanbirkner - fake-sftp-server-rule + fake-sftp-server-lambda ${fake.sftp.server.version} @@ -120,7 +120,7 @@ org.mock-server - mockserver-junit-rule + mockserver-junit-jupiter ${mockserver.version} @@ -194,18 +194,18 @@ - com.github.stefanbirkner - fake-sftp-server-rule + org.junit.jupiter + junit-jupiter test - com.powsybl - powsybl-config-test + com.github.stefanbirkner + fake-sftp-server-lambda test - junit - junit + com.powsybl + powsybl-config-test test @@ -215,7 +215,7 @@ org.mock-server - mockserver-junit-rule + mockserver-junit-jupiter test diff --git a/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionJob.java b/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionJob.java index 1193051..5377e67 100644 --- a/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionJob.java +++ b/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionJob.java @@ -82,9 +82,9 @@ private static void handleZipBoundaryContainer(TransferableFile acquiredFile, fileName = FilenameUtils.getName(entry.getName()); // Check if it is a boundary file - if (!fileName.equals("") && + if (!fileName.isEmpty() && (fileName.matches(CgmesBoundaryUtils.EQBD_FILE_REGEX) || - fileName.matches(CgmesBoundaryUtils.TPBD_FILE_REGEX))) { + fileName.matches(CgmesBoundaryUtils.TPBD_FILE_REGEX))) { byte[] boundaryContent = zis.readAllBytes(); @@ -97,20 +97,25 @@ private static void handleZipBoundaryContainer(TransferableFile acquiredFile, } public static void main(String... args) { + final PlatformConfig platformConfig = PlatformConfig.defaultConfig(); + final ModuleConfig moduleConfigAcquisitionServer = platformConfig.getOptionalModuleConfig("acquisition-server").orElseThrow(() -> new PowsyblException("Module acquisition-server not found !!")); + final ModuleConfig moduleConfigCgmesBoundaryServer = platformConfig.getOptionalModuleConfig("cgmes-boundary-server").orElseThrow(() -> new PowsyblException("Module cgmes-boundary-server not found !!")); + run( + moduleConfigCgmesBoundaryServer.getStringProperty("url"), + moduleConfigAcquisitionServer.getStringProperty("username"), + moduleConfigAcquisitionServer.getStringProperty("password"), + moduleConfigAcquisitionServer.getStringProperty("url"), + moduleConfigAcquisitionServer.getStringProperty("cgmes-boundary-directory") + ); + } - PlatformConfig platformConfig = PlatformConfig.defaultConfig(); - - ModuleConfig moduleConfigAcquisitionServer = platformConfig.getOptionalModuleConfig("acquisition-server").orElseThrow(() -> new PowsyblException("Module acquisition-server not found !!")); - ModuleConfig moduleConfigCgmesBoundaryServer = platformConfig.getOptionalModuleConfig("cgmes-boundary-server").orElseThrow(() -> new PowsyblException("Module cgmes-boundary-server not found !!")); - - final CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester(moduleConfigCgmesBoundaryServer.getStringProperty("url")); + public static void run(String cgmesBoundaryServerUrl, String acquisitionServerUrl, String acquisitionServerUsername, String acquisitionServerPassword, + String boundaryDirectory) { + final CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester(cgmesBoundaryServerUrl); - try (BoundaryAcquisitionServer boundaryAcquisitionServer = new BoundaryAcquisitionServer(moduleConfigAcquisitionServer.getStringProperty("url"), - moduleConfigAcquisitionServer.getStringProperty("username"), - moduleConfigAcquisitionServer.getStringProperty("password"))) { + try (BoundaryAcquisitionServer boundaryAcquisitionServer = new BoundaryAcquisitionServer(acquisitionServerUrl, acquisitionServerUsername, acquisitionServerPassword)) { boundaryAcquisitionServer.open(); - String boundaryDirectory = moduleConfigAcquisitionServer.getStringProperty("cgmes-boundary-directory"); Map filesToAcquire = boundaryAcquisitionServer.listFiles(boundaryDirectory); LOGGER.info("{} files found on server", filesToAcquire.size()); diff --git a/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionServer.java b/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionServer.java index 36f4b9b..c3d82bc 100644 --- a/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionServer.java +++ b/src/main/java/org/gridsuite/boundary/importer/job/BoundaryAcquisitionServer.java @@ -89,6 +89,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/test/java/org/gridsuite/boundary/importer/job/BoundaryImportJobTest.java b/src/test/java/org/gridsuite/boundary/importer/job/BoundaryImportJobTest.java index 30b2a8a..2b770dd 100644 --- a/src/test/java/org/gridsuite/boundary/importer/job/BoundaryImportJobTest.java +++ b/src/test/java/org/gridsuite/boundary/importer/job/BoundaryImportJobTest.java @@ -6,84 +6,66 @@ */ package org.gridsuite.boundary.importer.job; -import com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer; +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.ExceptionThrowingConsumer; +import jakarta.validation.constraints.NotNull; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; 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.client.MockServerClient; +import org.mockserver.junit.jupiter.MockServerExtension; import org.mockserver.model.MediaType; import org.mockserver.verify.VerificationTimes; import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; import java.time.LocalDateTime; import java.util.Map; +import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; /** * @author Franck Lecuyer */ -public class BoundaryImportJobTest { - @ClassRule - public static final FakeSftpServerRule SFTP_SERVER_RULE = new FakeSftpServerRule().addUser("dummy", "dummy").setPort(2222); - - @Rule - public final MockServerRule mockServer = new MockServerRule(this, 45385); - - @Before - public void setUp() throws IOException { - SFTP_SERVER_RULE.deleteAllFilesAndDirectories(); - } - - @After - public void tearDown() throws IOException { - SFTP_SERVER_RULE.deleteAllFilesAndDirectories(); - } - +@ExtendWith(MockServerExtension.class) +class BoundaryImportJobTest { @Test - public void testSftpAcquisition() throws IOException { - SFTP_SERVER_RULE.createDirectory("/boundaries"); - SFTP_SERVER_RULE.putFile("/boundaries/20210325T1030Z__ENTSOE_BD_001.zip", "fake file content 1", UTF_8); - SFTP_SERVER_RULE.putFile("/boundaries/20210328T0030Z__ENTSOE_BD_006.zip", "fake file content 2", UTF_8); - SFTP_SERVER_RULE.putFile("/boundaries/20210328T0030Z__ENTSOE_BD_006.doc", "fake file content 3", UTF_8); - SFTP_SERVER_RULE.putFile("/boundaries/20210328T0030Z__ENTSOE_BD_aaa.zip", "fake file content 4", UTF_8); - SFTP_SERVER_RULE.putFile("/boundaries/20210328T0030Z__ENTSOE_XX_aaa.zip", "fake file content 5", UTF_8); - SFTP_SERVER_RULE.putFile("/boundaries/20210328T0030Z_FOO_ENTSOE_BD_007.zip", "fake file content 6", UTF_8); - - String acquisitionServerUrl = "sftp://localhost:" + SFTP_SERVER_RULE.getPort(); - - try (BoundaryAcquisitionServer boundaryAcquisitionServer = new BoundaryAcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) { - boundaryAcquisitionServer.open(); - Map retrievedFiles = boundaryAcquisitionServer.listFiles("./boundaries"); - assertEquals(2, retrievedFiles.size()); - - TransferableFile file1 = boundaryAcquisitionServer.getFile("20210325T1030Z__ENTSOE_BD_001.zip", acquisitionServerUrl + "/boundaries/20210325T1030Z__ENTSOE_BD_001.zip"); - assertEquals("20210325T1030Z__ENTSOE_BD_001.zip", file1.getName()); - assertEquals("fake file content 1", new String(file1.getData(), UTF_8)); - - TransferableFile file2 = boundaryAcquisitionServer.getFile("20210328T0030Z__ENTSOE_BD_006.zip", acquisitionServerUrl + "/boundaries/20210328T0030Z__ENTSOE_BD_006.zip"); - assertEquals("20210328T0030Z__ENTSOE_BD_006.zip", file2.getName()); - assertEquals("fake file content 2", new String(file2.getData(), UTF_8)); - } catch (IOException e) { - e.printStackTrace(); - } + void testSftpAcquisition() throws Exception { + withSftp(server -> { + server.putFile("boundaries/20210325T1030Z__ENTSOE_BD_001.zip", "fake file content 1", UTF_8); + server.putFile("boundaries/20210328T0030Z__ENTSOE_BD_006.zip", "fake file content 2", UTF_8); + server.putFile("boundaries/20210328T0030Z__ENTSOE_BD_006.doc", "fake file content 3", UTF_8); + server.putFile("boundaries/20210328T0030Z__ENTSOE_BD_aaa.zip", "fake file content 4", UTF_8); + server.putFile("boundaries/20210328T0030Z__ENTSOE_XX_aaa.zip", "fake file content 5", UTF_8); + server.putFile("boundaries/20210328T0030Z_FOO_ENTSOE_BD_007.zip", "fake file content 6", UTF_8); + + final String acquisitionServerUrl = "sftp://localhost:" + server.getPort(); + try (final BoundaryAcquisitionServer boundaryAcquisitionServer = new BoundaryAcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) { + boundaryAcquisitionServer.open(); + Map retrievedFiles = boundaryAcquisitionServer.listFiles("./boundaries"); + assertEquals(2, retrievedFiles.size()); + + TransferableFile file1 = boundaryAcquisitionServer.getFile("20210325T1030Z__ENTSOE_BD_001.zip", acquisitionServerUrl + "/boundaries/20210325T1030Z__ENTSOE_BD_001.zip"); + assertEquals("20210325T1030Z__ENTSOE_BD_001.zip", file1.getName()); + assertEquals("fake file content 1", new String(file1.getData(), UTF_8)); + + TransferableFile file2 = boundaryAcquisitionServer.getFile("20210328T0030Z__ENTSOE_BD_006.zip", acquisitionServerUrl + "/boundaries/20210328T0030Z__ENTSOE_BD_006.zip"); + assertEquals("20210328T0030Z__ENTSOE_BD_006.zip", file2.getName()); + assertEquals("fake file content 2", new String(file2.getData(), UTF_8)); + } + }); } @Test - public void testFtpAcquisition() throws IOException { + void testFtpAcquisition() throws Exception { FileSystem fileSystem = new UnixFakeFileSystem(); fileSystem.add(new DirectoryEntry("/boundaries")); fileSystem.add(new FileEntry("/boundaries/20210325T1030Z__ENTSOE_BD_001.zip", "fake file content 1")); @@ -113,82 +95,92 @@ public void testFtpAcquisition() throws IOException { TransferableFile file2 = boundaryAcquisitionServer.getFile("20210328T0030Z__ENTSOE_BD_006.zip", acquisitionServerUrl + "/boundaries/20210328T0030Z__ENTSOE_BD_006.zip"); assertEquals("20210328T0030Z__ENTSOE_BD_006.zip", file2.getName()); assertEquals("fake file content 2", new String(file2.getData(), UTF_8)); - } catch (IOException e) { - e.printStackTrace(); } finally { fakeFtpServer.stop(); } } - private void addGetBoundariesInfosExpectation(int status, String json) { - mockServer.getClient().when(request().withMethod("GET").withPath("/v1/boundaries/infos")) - .respond(response().withStatusCode(status) - .withContentType(MediaType.JSON_UTF_8) - .withBody(json)); + private static void addGetBoundariesInfosExpectation(MockServerClient mockServerClient, int status, String json) { + mockServerClient.when(request().withMethod("GET").withPath("/v1/boundaries/infos")) + .respond(response().withStatusCode(status).withContentType(MediaType.JSON_UTF_8).withBody(json)); } - private void addPostBoundaryExpectation(int status) { - mockServer.getClient().when(request().withMethod("POST").withPath("/v1/boundaries")) + private static void addPostBoundaryExpectation(MockServerClient mockServerClient, int status) { + mockServerClient.when(request().withMethod("POST").withPath("/v1/boundaries")) .respond(response().withStatusCode(status)); } @Test - public void testBoundaryImportRequester() throws IOException, InterruptedException { - CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:45385/"); - addPostBoundaryExpectation(200); + void testBoundaryImportRequester(final MockServerClient mockServerClient) throws Exception { + CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:" + mockServerClient.getPort() + "/"); + addPostBoundaryExpectation(mockServerClient, 200); assertTrue(cgmesBoundaryServiceRequester.importBoundary(new TransferableFile("20210325T1030Z__ENTSOE_EQBD_001.xml", "Boundary file content".getBytes(UTF_8)))); - mockServer.getClient().clear(request()); - addPostBoundaryExpectation(500); + mockServerClient.clear(request()); + addPostBoundaryExpectation(mockServerClient, 500); assertFalse(cgmesBoundaryServiceRequester.importBoundary(new TransferableFile("20210328T0030Z__ENTSOE_TPBD_006.xml", "Boundary file content".getBytes(UTF_8)))); } @Test - public void mainTest() throws IOException { - SFTP_SERVER_RULE.createDirectory("/boundaries"); - - try (InputStream isBoundary = getClass().getResourceAsStream("/20210315T0000Z__ENTSOE_BD_002.zip"); - BufferedInputStream bisBoundary = new BufferedInputStream(isBoundary)) { - SFTP_SERVER_RULE.putFile("/boundaries/20210315T0000Z__ENTSOE_BD_002.zip", bisBoundary.readAllBytes()); - } - - String[] args = null; - - // 1 zip container for boundaries on SFTP server, no boundary already present in cgmes boundary server, 2 boundaries will be imported - addGetBoundariesInfosExpectation(200, "[]"); - addPostBoundaryExpectation(200); - - BoundaryAcquisitionJob.main(args); - - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(2)); - - // 1 boundary already present in cgmes boundary server, only 1 boundary will be imported - mockServer.getClient().clear(request()); - addGetBoundariesInfosExpectation(200, "[{\"id\":\"urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb\",\"filename\":\"20210315T0000Z__ENTSOE_TPBD_002.xml\",\"scenarioTime\":\"2020-02-02T00:00\"}]"); - - addPostBoundaryExpectation(200); - - BoundaryAcquisitionJob.main(args); - - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(1)); - - // all boundaries already present in cgmes boundary server, no boundary will be imported - mockServer.getClient().clear(request()); - addGetBoundariesInfosExpectation(200, "[{\"id\":\"urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb\",\"filename\":\"20210315T0000Z__ENTSOE_TPBD_002.xml\",\"scenarioTime\":\"2020-02-02T00:00\"},{\"id\":\"urn:uuid:11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"filename\":\"20210315T0000Z__ENTSOE_EQBD_002.xml\",\"scenarioTime\":\"2020-02-02T18:35\"}]"); - addPostBoundaryExpectation(200); + void mainTest(final MockServerClient mockServerClient) throws Exception { + withSftp(server -> { + putFile(server, "/20210315T0000Z__ENTSOE_BD_002.zip"); + + // 1 zip container for boundaries on SFTP server, no boundary already present in cgmes boundary server, 2 boundaries will be imported + addGetBoundariesInfosExpectation(mockServerClient, 200, "[]"); + addPostBoundaryExpectation(mockServerClient, 200); + + runJob(server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(2)); + + // 1 boundary already present in cgmes boundary server, only 1 boundary will be imported + mockServerClient.clear(request()); + addGetBoundariesInfosExpectation(mockServerClient, 200, "[{\"id\":\"urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb\",\"filename\":\"20210315T0000Z__ENTSOE_TPBD_002.xml\",\"scenarioTime\":\"2020-02-02T00:00\"}]"); + addPostBoundaryExpectation(mockServerClient, 200); + runJob(server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(1)); + + // all boundaries already present in cgmes boundary server, no boundary will be imported + mockServerClient.clear(request()); + addGetBoundariesInfosExpectation(mockServerClient, 200, "[{\"id\":\"urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb\",\"filename\":\"20210315T0000Z__ENTSOE_TPBD_002.xml\",\"scenarioTime\":\"2020-02-02T00:00\"},{\"id\":\"urn:uuid:11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"filename\":\"20210315T0000Z__ENTSOE_EQBD_002.xml\",\"scenarioTime\":\"2020-02-02T18:35\"}]"); + addPostBoundaryExpectation(mockServerClient, 200); + runJob(server, mockServerClient); + mockServerClient.verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(0)); + + // test error case when retrieving boundaries infos + mockServerClient.clear(request()); + addGetBoundariesInfosExpectation(mockServerClient, 500, "[]"); + runJob(server, mockServerClient); + }); + } - BoundaryAcquisitionJob.main(args); + private static void withSftp(@NotNull final ExceptionThrowingConsumer testCode) throws Exception { + withSftpServer(server -> { + server.addUser("dummy", "dummy")/*.setPort(2222)*/; + server.createDirectory("boundaries"); + testCode.accept(server); + server.deleteAllFilesAndDirectories(); + }); + } - mockServer.getClient().verify(request().withMethod("POST").withPath("/v1/boundaries"), VerificationTimes.exactly(0)); + private static void putFile(FakeSftpServer server, String filepath) throws Exception { + try (final BufferedInputStream bisEQ = new BufferedInputStream(BoundaryImportJobTest.class.getResourceAsStream(filepath))) { + server.putFile("boundaries" + filepath, bisEQ.readAllBytes()); + } + } - // test error case when retrieving boundaries infos - mockServer.getClient().clear(request()); - addGetBoundariesInfosExpectation(500, "[]"); - BoundaryAcquisitionJob.main(args); + private static void runJob(final FakeSftpServer sftpServer, final MockServerClient mockServerClient) { + BoundaryAcquisitionJob.run( + "http://localhost:" + mockServerClient.getPort() + "/", + "sftp://localhost:" + sftpServer.getPort(), + "dummy", + "dummy", + "./boundaries" + ); } @Test - public void boundaryInfoTest() { + void boundaryInfoTest() { LocalDateTime date = LocalDateTime.of(2021, 5, 10, 10, 0, 0); BoundaryInfo info = new BoundaryInfo("urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb", "20210315T0000Z__ENTSOE_TPBD_002.xml", date); assertEquals("urn:uuid:22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb", info.getId());