diff --git a/pom.xml b/pom.xml index cc05850..831e9f9 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} @@ -216,7 +216,7 @@ com.github.stefanbirkner - fake-sftp-server-rule + fake-sftp-server-lambda test @@ -224,16 +224,6 @@ powsybl-config-test test - - junit - junit - test - - - org.junit.vintage - junit-vintage-engine - test - org.mock-server mockserver-netty @@ -241,7 +231,7 @@ org.mock-server - mockserver-junit-rule + mockserver-junit-jupiter test diff --git a/src/main/java/org/gridsuite/cgmes/assembling/job/AcquisitionServer.java b/src/main/java/org/gridsuite/cgmes/assembling/job/AcquisitionServer.java index c7c6526..1097527 100644 --- a/src/main/java/org/gridsuite/cgmes/assembling/job/AcquisitionServer.java +++ b/src/main/java/org/gridsuite/cgmes/assembling/job/AcquisitionServer.java @@ -54,7 +54,7 @@ public void open() throws FileSystemException { fsManager.init(); } - class FileObjectComparator implements Comparator { + private static class FileObjectComparator implements Comparator { @Override public int compare(FileObject fo1, FileObject fo2) { if (fo1 == fo2) { @@ -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(); } diff --git a/src/main/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLogger.java b/src/main/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLogger.java index 37e48d7..caf08a3 100644 --- a/src/main/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLogger.java +++ b/src/main/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLogger.java @@ -141,6 +141,7 @@ private boolean checkValue(String query, String filename, String origin) { } } + @Override public void close() { try { connection.close(); diff --git a/src/main/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJob.java b/src/main/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJob.java index 3cbeb0d..406d4fb 100644 --- a/src/main/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJob.java +++ b/src/main/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJob.java @@ -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; @@ -27,18 +29,13 @@ * @author Chamseddine Benhamed * @author Franck Lecuyer */ - -@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); @@ -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 authorizedTsos = cgmesBoundaryServiceRequester.getTsosList(); Set authorizedBusinessProcesses = cgmesBoundaryServiceRequester.getBusinessProcessesList(); @@ -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 @@ -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); } } } diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/AcquisitionServerTest.java b/src/test/java/org/gridsuite/cgmes/assembling/job/AcquisitionServerTest.java new file mode 100644 index 0000000..bc6d6f1 --- /dev/null +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/AcquisitionServerTest.java @@ -0,0 +1,80 @@ +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.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 Exception { + 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 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 authorizedSourcingActors = Set.of("RTEFRANCE-FR"); + final Set authorizedBusinessProcesses = Set.of("1D"); + + final String acquisitionServerUrl = "sftp://localhost:" + server.getPort(); + try (final AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) { + acquisitionServer.open(); + Map 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)); + } + }); + } +} diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLoggerTest.java b/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLoggerTest.java new file mode 100644 index 0000000..ce3a270 --- /dev/null +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesAssemblingLoggerTest.java @@ -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); + } + }); + } +} diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesUtilsTest.java b/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesUtilsTest.java new file mode 100644 index 0000000..337a00d --- /dev/null +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/CgmesUtilsTest.java @@ -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 authorizedSourcingActors = Set.of("XX"); + final Set 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)); + } +} diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJobTest.java b/src/test/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJobTest.java index 8727089..9dfe44f 100644 --- a/src/test/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJobTest.java +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/ProfilesAcquisitionJobTest.java @@ -1,449 +1,214 @@ -/** - * 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.cgmes.assembling.job; -import com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule; +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer; import org.apache.commons.io.IOUtils; import org.apache.commons.text.StringEscapeUtils; -import org.gridsuite.cgmes.assembling.job.dto.BoundaryInfo; -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.matchers.Times; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.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 org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.lang.NonNull; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import javax.sql.DataSource; import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.*; +import java.util.List; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.*; +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; -/** - * @author Chamseddine Benhamed - * @author Franck Lecuyer - */ -@RunWith(SpringRunner.class) -@SpringBootTest -@ContextConfiguration(classes = {ProfilesAcquisitionJob.class}) -public class ProfilesAcquisitionJobTest { +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@ExtendWith(MockServerExtension.class) +@SpringBootTest(classes = {ProfilesAcquisitionJob.class}) +class ProfilesAcquisitionJobTest { + @Autowired private DataSource dataSource; + @MockBean private ProfilesAcquisitionJob cliRunner; //we prevent the cli runner to run before tests + private ProfilesAcquisitionJob profilesAcquisitionJob; - @Autowired - private DataSource dataSource; - - @ClassRule - public static final FakeSftpServerRule SFTP_SERVER_RULE = new FakeSftpServerRule().addUser("dummy", "dummy").setPort(2222); - - @Rule - public final MockServerRule mockServer = new MockServerRule(this, 45385, 55487); - - @Before - public void setUp() throws IOException { - SFTP_SERVER_RULE.deleteAllFilesAndDirectories(); - truncate(); - } - - @After - public void tearDown() throws IOException { - SFTP_SERVER_RULE.deleteAllFilesAndDirectories(); - } - - @Test - public void historyLoggerTest() { - try (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 - public 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(); - - 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 { - String fileData = "Case file content"; - CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester("http://localhost:45385/"); - - expectRequestCase("/v1/cases/public", 200); - assertTrue(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", fileData.getBytes(UTF_8)))); - - mockServer.getClient().clear(request()); - expectRequestCase("/v1/cases/public", 500); - assertFalse(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", fileData.getBytes(UTF_8)))); + @BeforeEach + void setUp() { + profilesAcquisitionJob = new ProfilesAcquisitionJob(dataSource); } @Test - public void testCgmesBoundaryRequester() { - CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:55487/"); - - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"titi.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"content1\"}", 200); - BoundaryInfo res = cgmesBoundaryServiceRequester.getBoundary("urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358"); - assertEquals("titi.xml", res.getFilename()); - assertEquals("content1", new String(res.getBoundary(), UTF_8)); - assertEquals("urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", res.getId()); - - mockServer.getClient().clear(request()); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); - assertNull(cgmesBoundaryServiceRequester.getBoundary("urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71")); - } - - @Test - public void testCgmesLastBoundariesRequester() { - CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:55487/"); - - expectRequestBoundary("/v1/boundaries/last", "[{\"filename\":\"titi.xml\",\"id\":\"urn:uuid:11111111-2222-3333-4444-555555555555\",\"boundary\":\"content\"},{\"filename\":\"tutu.xml\",\"id\":\"urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\",\"boundary\":\"content2\"}]", 200); - List res = cgmesBoundaryServiceRequester.getLastBoundaries(); - assertEquals(2, res.size()); - assertEquals("titi.xml", res.get(0).getFilename()); - assertEquals("tutu.xml", res.get(1).getFilename()); - assertEquals("urn:uuid:11111111-2222-3333-4444-555555555555", res.get(0).getId()); - assertEquals("urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", res.get(1).getId()); - assertEquals("content", new String(res.get(0).getBoundary(), UTF_8)); - assertEquals("content2", new String(res.get(1).getBoundary(), UTF_8)); - - mockServer.getClient().clear(request()); - expectRequestBoundary("/v1/boundaries/last", "[]", 500); - assertTrue(cgmesBoundaryServiceRequester.getLastBoundaries().isEmpty()); - } - - @Test - public void testGetValidProfileFileName() { - Set authorizedSourcingActors = new HashSet<>(); - Set authorizedBusinessProcesses = new HashSet<>(); - - authorizedBusinessProcesses.add("1D"); - authorizedSourcingActors.add("XX"); - - 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)); - } - - @Test(expected = RuntimeException.class) - public void testLogDependencies() { - try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { - cgmesAssemblingLogger.logFileDependencies("uuid", null); - } - } - - private void expectRequestBoundary(String path, String response, Integer status) { - mockServer.getClient().when(request().withMethod("GET").withPath(path), - Times.exactly(1)) - .respond(response().withStatusCode(status) - .withBody(response)); - } - - private void expectRequestCase(String path, Integer status) { - mockServer.getClient().when(request().withMethod("POST").withPath(path), - Times.exactly(1)) - .respond(response().withStatusCode(status)); - } - - private void expectRequestTsos(String path, String response, Integer status) { - mockServer.getClient().when(request().withMethod("GET").withPath(path), - Times.exactly(1)) - .respond(response().withStatusCode(status) - .withBody(response)); - } - - private void expectRequestBusinessProcesses(String path, String response, Integer status) { - mockServer.getClient().when(request().withMethod("GET").withPath(path), - Times.exactly(1)) - .respond(response().withStatusCode(status) - .withBody(response)); - } - - @Test - public void testImportWithReferencedBoundaries() throws IOException { - SFTP_SERVER_RULE.createDirectory("/cases"); - - try (InputStream isSSH = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SSH_001.zip"); - InputStream isSV = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SV_001.zip"); - BufferedInputStream bisSSH = new BufferedInputStream(isSSH); - BufferedInputStream bisSV = new BufferedInputStream(isSV);) { - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SSH_001.zip", bisSSH.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SV_001.zip", bisSV.readAllBytes()); - } - - String[] args = {}; - - String boundary1Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/referenced_eqbd.xml"), Charset.defaultCharset())); - String boundary2Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/referenced_tpbd.xml"), Charset.defaultCharset())); - - // 2 files on SFTP server (SV and SSH), 2 cases will be handled, but no import will be requested (missing dependencies) - // - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); - ProfilesAcquisitionJob.main(args); - try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // No new files on SFTP server, no import requested (missing dependencies) - // - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); - ProfilesAcquisitionJob.main(args); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // One new file on SFTP server (EQ), one new case will be handled, but still no import will be requested (missing dependencies) - // - try (InputStream isEQ = getClass().getResourceAsStream("/20191106T0930Z__XX_EQ_001.zip"); - BufferedInputStream bisEQ = new BufferedInputStream(isEQ);) { - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z__XX_EQ_001.zip", bisEQ.readAllBytes()); + void testImportWithReferencedBoundaries(final MockServerClient mockServerClient) throws Exception { + TestUtils.withSftp(server -> { + putFile(server, "/20191106T0930Z_1D_XX_SSH_001.zip"); + putFile(server, "/20191106T0930Z_1D_XX_SV_001.zip"); + final String boundary1Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/referenced_eqbd.xml"), UTF_8)); + final String boundary2Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/referenced_tpbd.xml"), UTF_8)); + + // 2 files on SFTP server (SV and SSH), 2 cases will be handled, but no import will be requested (missing dependencies) + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // No new files on SFTP server, no import requested (missing dependencies) + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // One new file on SFTP server (EQ), one new case will be handled, but still no import will be requested (missing dependencies) + putFile(server, "/20191106T0930Z__XX_EQ_001.zip"); + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", TestUtils.SFTP_LABEL)); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // One new file on SFTP server (TP), one case import requested + putFile(server, "/20191106T0930Z_1D_XX_TP_001.zip"); + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", TestUtils.SFTP_LABEL)); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // dependencies-strict-mode=true and not all referenced boundaries available + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, true); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // dependencies-strict-mode=false and all referenced boundaries available, and expect import case + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + assertTrue(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); } - - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:25b861c6-3e06-4fa1-bb56-592330202c01", null, 500); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); - ProfilesAcquisitionJob.main(args); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", "my_sftp_server")); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // One new file on SFTP server (TP), one case import requested - // - try (InputStream isTP = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_TP_001.zip"); - BufferedInputStream bisTP = new BufferedInputStream(isTP);) { - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_TP_001.zip", bisTP.readAllBytes()); - } - - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); - ProfilesAcquisitionJob.main(args); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", "my_sftp_server")); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // dependencies-strict-mode=true and not all referenced boundaries available - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); - expectRequestCase("/v1/cases/public", 200); - new ProfilesAcquisitionJob(dataSource).handle(Boolean.TRUE); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // dependencies-strict-mode=false and all referenced boundaries available, and expect import case - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"referenced_eqbd.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"" + boundary1Content + "\"}", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", "{\"filename\":\"referenced_tpbd.xml\",\"id\":\"urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71\",\"boundary\":\"" + boundary2Content + "\"}", 200); - expectRequestCase("/v1/cases/public", 200); - ProfilesAcquisitionJob.main(args); - assertTrue(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - } + }); } @Test - public void testImportWithLastBoundaries() throws IOException { - SFTP_SERVER_RULE.createDirectory("/cases"); - - try (InputStream isSSH = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SSH_001.zip"); - InputStream isSV = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SV_001.zip"); - InputStream isTP = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_TP_001.zip"); - InputStream isEQ = getClass().getResourceAsStream("/20191106T0930Z__XX_EQ_001.zip"); - - BufferedInputStream bisSSH = new BufferedInputStream(isSSH); - BufferedInputStream bisSV = new BufferedInputStream(isSV); - BufferedInputStream bisTP = new BufferedInputStream(isTP); - BufferedInputStream bisEQ = new BufferedInputStream(isEQ);) { - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SSH_001.zip", bisSSH.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SV_001.zip", bisSV.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_TP_001.zip", bisTP.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z__XX_EQ_001.zip", bisEQ.readAllBytes()); - } - - String[] args = {}; - - String lastBoundary1Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/last_eqbd.xml"), Charset.defaultCharset())); - String lastBoundary2Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/last_tpbd.xml"), Charset.defaultCharset())); - - // All individual profile files on SFTP server, no boundaries available - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - ProfilesAcquisitionJob.main(args); - try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", "my_sftp_server")); - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // retry with import in case server available and referenced boundaries not available - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", null, 500); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); - expectRequestBoundary("/v1/boundaries/last", "[]", 500); - expectRequestCase("/v1/cases/public", 200); - ProfilesAcquisitionJob.main(args); - - assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - - // retry with last boundaries and import in case server available - mockServer.getClient().clear(request()); - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - expectRequestBoundary("/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", null, 500); - expectRequestBoundary("/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); - expectRequestBoundary("/v1/boundaries/last", "[{\"filename\":\"last_eqbd.xml\",\"id\":\"urn:uuid:11111111-2222-3333-4444-555555555555\",\"boundary\":\"" + lastBoundary1Content + "\"},{\"filename\":\"last_tpbd.xml\",\"id\":\"urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\",\"boundary\":\"" + lastBoundary2Content + "\"}]", 200); - expectRequestCase("/v1/cases/public", 200); - ProfilesAcquisitionJob.main(args); - - assertTrue(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - } + void testImportWithLastBoundaries(final MockServerClient mockServerClient) throws Exception { + TestUtils.withSftp(server -> { + putFile(server, "/20191106T0930Z_1D_XX_SSH_001.zip"); + putFile(server, "/20191106T0930Z_1D_XX_SV_001.zip"); + putFile(server, "/20191106T0930Z_1D_XX_TP_001.zip"); + putFile(server, "/20191106T0930Z__XX_EQ_001.zip"); + + // All individual profile files on SFTP server, no boundaries available + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", TestUtils.SFTP_LABEL)); + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // retry with import in case server available and referenced boundaries not available + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", null, 500); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/last", "[]", 500); + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + + assertFalse(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + + // retry with last boundaries and import in case server available + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", null, 500); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); + final String lastBoundary1Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/last_eqbd.xml"), UTF_8)); + final String lastBoundary2Content = StringEscapeUtils.escapeJava(IOUtils.toString(getClass().getResourceAsStream("/last_tpbd.xml"), UTF_8)); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/last", "[{\"filename\":\"last_eqbd.xml\",\"id\":\"urn:uuid:11111111-2222-3333-4444-555555555555\",\"boundary\":\"" + lastBoundary1Content + "\"},{\"filename\":\"last_tpbd.xml\",\"id\":\"urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\",\"boundary\":\"" + lastBoundary2Content + "\"}]", 200); + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + + assertTrue(cgmesAssemblingLogger.isImportedFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + } + }); } @Test - public void mainAssemblingTest() throws IOException { - SFTP_SERVER_RULE.createDirectory("/cases"); - - try (InputStream isSSH = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SSH_001.zip"); - InputStream isSV = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_SV_001.zip"); - InputStream isTP = getClass().getResourceAsStream("/20191106T0930Z_1D_XX_TP_001.zip"); - InputStream isEQ = getClass().getResourceAsStream("/20191106T0930Z__XX_EQ_001.zip"); - - BufferedInputStream bisSV = new BufferedInputStream(isSV); - BufferedInputStream bisSSH = new BufferedInputStream(isSSH); - BufferedInputStream bisTP = new BufferedInputStream(isTP); - BufferedInputStream bisEQ = new BufferedInputStream(isEQ);) { - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_TP_001.zip", bisTP.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z__XX_EQ_001.zip", bisEQ.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SV_001.zip", bisSV.readAllBytes()); - SFTP_SERVER_RULE.putFile("/cases/20191106T0930Z_1D_XX_SSH_001.zip", bisSSH.readAllBytes()); - } - - String[] args = {}; - - expectRequestTsos("/v1/tsos", "[\"XX\"]", 200); - expectRequestBusinessProcesses("/v1/business-processes", "[\"1D\"]", 200); - ProfilesAcquisitionJob.main(args); - - try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", "my_sftp_server")); - assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", "my_sftp_server")); - } + void mainAssemblingTest(final MockServerClient mockServerClient) throws Exception { + TestUtils.withSftp(server -> { + putFile(server, "/20191106T0930Z_1D_XX_SSH_001.zip"); + putFile(server, "/20191106T0930Z_1D_XX_SV_001.zip"); + putFile(server, "/20191106T0930Z_1D_XX_TP_001.zip"); + putFile(server, "/20191106T0930Z__XX_EQ_001.zip"); + + TestUtils.expectRequestGet(mockServerClient, "/v1/tsos", "[\"XX\"]", 200); + TestUtils.expectRequestGet(mockServerClient, "/v1/business-processes", "[\"1D\"]", 200); + runJob(profilesAcquisitionJob, server, mockServerClient, false); + + try (final CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SSH_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_SV_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z_1D_XX_TP_001.zip", TestUtils.SFTP_LABEL)); + assertTrue(cgmesAssemblingLogger.isHandledFile("20191106T0930Z__XX_EQ_001.zip", TestUtils.SFTP_LABEL)); + } + }); } - @Test - public void testAcceptedFilesConnection() throws IOException { - SFTP_SERVER_RULE.createDirectory("/cases"); - SFTP_SERVER_RULE.putFile("/cases/20200817T1705Z_1D_RTEFRANCE-FR_SV_002.zip", "fake file content 1", UTF_8); - SFTP_SERVER_RULE.putFile("/cases/20200817T1705Z__RTEFRANCE-FR_EQ_002.zip", "fake file content 2", UTF_8); - SFTP_SERVER_RULE.putFile("/cases/20200817T1705Z_1D_RTEFRANCE-FR_SSH_002.zip", "fake file content 3", UTF_8); - SFTP_SERVER_RULE.putFile("/cases/20200817T1705Z_1D_RTEFRANCE-FR_TP_002.zip", "fake file content 4", UTF_8); - - Set authorizedSourcingActors = Set.of("RTEFRANCE-FR"); - Set authorizedBusinessProcesses = Set.of("1D"); - - String acquisitionServerUrl = "sftp://localhost:2222"; - try (AcquisitionServer acquisitionServer = new AcquisitionServer(acquisitionServerUrl, "dummy", "dummy")) { - acquisitionServer.open(); - Map 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)); - - } catch (IOException e) { - e.printStackTrace(); + private static void putFile(FakeSftpServer server, String filepath) throws Exception { + try (final BufferedInputStream bisEQ = new BufferedInputStream(ProfilesAcquisitionJobTest.class.getResourceAsStream(filepath))) { + server.putFile("cases" + filepath, bisEQ.readAllBytes()); } } - private void truncate() { - List tables = List.of("handled_files", "imported_files", "handled_files_dependencies"); - tables.forEach(table -> truncate(table)); - } - - private void truncate(String table) { - try (CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource); - PreparedStatement truncateStatement = cgmesAssemblingLogger.getConnection().prepareStatement("TRUNCATE TABLE " + table)) { - truncateStatement.executeUpdate(); - } catch (SQLException e) { - throw new RuntimeException(e); + private static void runJob(@NonNull final ProfilesAcquisitionJob jobRunner, @NonNull final FakeSftpServer sftpServer, @NonNull final MockServerClient mockServerClient, final boolean depsStrictMode) { + jobRunner.handle( + depsStrictMode, + "http://localhost:" + mockServerClient.getPort() + "/", //random free port + "http://localhost:" + mockServerClient.getPort() + "/", + "dummy", //server.addUser(...) + "dummy", + "sftp://localhost:" + sftpServer.getPort(), + "./cases", //server.createDirectory(...) + TestUtils.SFTP_LABEL + ); + } + + @AfterEach + void cleanDB() throws Exception { + try (final CgmesAssemblingLogger cgmesAssemblingLogger = new CgmesAssemblingLogger(dataSource)) { + for (final String table : List.of("handled_files", "imported_files", "handled_files_dependencies")) { + try (final PreparedStatement truncateStatement = cgmesAssemblingLogger.getConnection().prepareStatement("TRUNCATE TABLE " + table)) { + truncateStatement.executeUpdate(); + } + } } } } diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/ServiceRequesterTest.java b/src/test/java/org/gridsuite/cgmes/assembling/job/ServiceRequesterTest.java new file mode 100644 index 0000000..0c52a0f --- /dev/null +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/ServiceRequesterTest.java @@ -0,0 +1,73 @@ +/** + * 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.cgmes.assembling.job; + +import org.gridsuite.cgmes.assembling.job.dto.BoundaryInfo; +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 java.util.List; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockserver.model.HttpRequest.request; + +/** + * @author Chamseddine Benhamed + * @author Franck Lecuyer + */ +@ExtendWith(MockServerExtension.class) +class ServiceRequesterTest { + @Test + void testCaseImportRequester(final MockServerClient mockServerClient) throws Exception { + String fileData = "Case file content"; + CaseImportServiceRequester caseImportServiceRequester = new CaseImportServiceRequester("http://localhost:" + mockServerClient.getPort() + "/"); + + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 200); + assertTrue(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", fileData.getBytes(UTF_8)))); + + mockServerClient.clear(request()); + TestUtils.expectRequestPost(mockServerClient, "/v1/cases/public", 500); + assertFalse(caseImportServiceRequester.importCase(new TransferableFile("case.iidm", fileData.getBytes(UTF_8)))); + } + + @Test + void testCgmesBoundaryRequester(final MockServerClient mockServerClient) { + CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:" + mockServerClient.getPort() + "/"); + + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", "{\"filename\":\"titi.xml\",\"id\":\"urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358\",\"boundary\":\"content1\"}", 200); + BoundaryInfo res = cgmesBoundaryServiceRequester.getBoundary("urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358"); + assertEquals("titi.xml", res.getFilename()); + assertEquals("content1", new String(res.getBoundary(), UTF_8)); + assertEquals("urn:uuid:f1582c44-d9e2-4ea0-afdc-dba189ab4358", res.getId()); + + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71", null, 500); + assertNull(cgmesBoundaryServiceRequester.getBoundary("urn:uuid:3e3f7738-aab9-4284-a965-71d5cd151f71")); + } + + @Test + void testCgmesLastBoundariesRequester(final MockServerClient mockServerClient) { + CgmesBoundaryServiceRequester cgmesBoundaryServiceRequester = new CgmesBoundaryServiceRequester("http://localhost:" + mockServerClient.getPort() + "/"); + + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/last", "[{\"filename\":\"titi.xml\",\"id\":\"urn:uuid:11111111-2222-3333-4444-555555555555\",\"boundary\":\"content\"},{\"filename\":\"tutu.xml\",\"id\":\"urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\",\"boundary\":\"content2\"}]", 200); + List res = cgmesBoundaryServiceRequester.getLastBoundaries(); + assertEquals(2, res.size()); + assertEquals("titi.xml", res.get(0).getFilename()); + assertEquals("tutu.xml", res.get(1).getFilename()); + assertEquals("urn:uuid:11111111-2222-3333-4444-555555555555", res.get(0).getId()); + assertEquals("urn:uuid:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", res.get(1).getId()); + assertEquals("content", new String(res.get(0).getBoundary(), UTF_8)); + assertEquals("content2", new String(res.get(1).getBoundary(), UTF_8)); + + mockServerClient.clear(request()); + TestUtils.expectRequestGet(mockServerClient, "/v1/boundaries/last", "[]", 500); + assertTrue(cgmesBoundaryServiceRequester.getLastBoundaries().isEmpty()); + } +} diff --git a/src/test/java/org/gridsuite/cgmes/assembling/job/TestUtils.java b/src/test/java/org/gridsuite/cgmes/assembling/job/TestUtils.java new file mode 100644 index 0000000..3b7d389 --- /dev/null +++ b/src/test/java/org/gridsuite/cgmes/assembling/job/TestUtils.java @@ -0,0 +1,37 @@ +package org.gridsuite.cgmes.assembling.job; + +import com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.ExceptionThrowingConsumer; +import org.mockserver.client.MockServerClient; +import org.mockserver.matchers.Times; +import org.springframework.lang.NonNull; + +import static com.github.stefanbirkner.fakesftpserver.lambda.FakeSftpServer.withSftpServer; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +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(); + }); + } + + public static void expectRequestGet(MockServerClient mockServerClient, String path, String response, Integer status) { + mockServerClient.when(request().withMethod("GET").withPath(path), Times.exactly(1)) + .respond(response().withStatusCode(status).withBody(response)); + } + + public static void expectRequestPost(MockServerClient mockServerClient, String path, Integer status) { + mockServerClient.when(request().withMethod("POST").withPath(path), Times.exactly(1)) + .respond(response().withStatusCode(status)); + } +} 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: ":"