Skip to content

Commit

Permalink
Add test for .gz, .xml
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne Homer <[email protected]>
  • Loading branch information
etiennehomer committed Oct 18, 2024
1 parent 5cbf729 commit df12461
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.caseserver.elasticsearch.DisableElasticsearch;
import com.powsybl.caseserver.service.CaseService;
import com.powsybl.commons.datasource.DataSource;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Set;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
Expand All @@ -40,27 +45,29 @@ public abstract class AbstractCaseDataSourceControllerTest {
@Autowired
private MockMvc mvc;

protected static CaseService caseService;

@Value("${case-store-directory:#{systemProperties['user.home'].concat(\"/cases\")}}")
protected String rootDirectory;

static String CGMES_ZIP_NAME = "CGMES_v2415_MicroGridTestConfiguration_BC_BE_v2.zip";

static String CGMES_FILE_NAME = "CGMES_v2415_MicroGridTestConfiguration_BC_BE_v2/MicroGridTestConfiguration_BC_BE_DL_V2.xml";

static String XIIDM_ZIP_NAME = "LF.zip";

static String XIIDM_FILE_NAME = "LF.xml";

UUID cgmesCaseUuid;

UUID xiidmCaseUuid;

protected DataSource cgmesDataSource;

protected DataSource xiidmDataSource;

private ObjectMapper mapper = new ObjectMapper();

public static UUID importCase(String filename, String contentType) throws IOException {
UUID caseUUID;
try (InputStream inputStream = S3CaseDataSourceControllerTest.class.getResourceAsStream("/" + filename)) {
caseUUID = caseService.importCase(new MockMultipartFile(filename, filename, contentType, inputStream.readAllBytes()), false, false);
}
return caseUUID;
}

@Test
public void testBaseName() throws Exception {
MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/baseName", cgmesCaseUuid))
Expand Down Expand Up @@ -99,24 +106,60 @@ public void testInputStreamWithFileName() throws Exception {
}
}

@Test
public void testInputStreamWithZipFile() throws Exception {
MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", xiidmCaseUuid)
.param("fileName", XIIDM_FILE_NAME))
.andExpect(status().isOk())
.andReturn();

try (InputStreamReader isReader = new InputStreamReader(xiidmDataSource.newInputStream(XIIDM_FILE_NAME), StandardCharsets.UTF_8)) {
private static String readDataSource(DataSource dataSource, String fileName) throws Exception {
try (InputStreamReader isReader = new InputStreamReader(dataSource.newInputStream(fileName), StandardCharsets.UTF_8)) {
BufferedReader reader = new BufferedReader(isReader);
StringBuilder datasourceResponse = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
datasourceResponse.append(str).append("\n");
}
assertEquals(datasourceResponse.toString(), mvcResult.getResponse().getContentAsString());
return datasourceResponse.toString();
}
}

@Test
public void testInputStreamWithZipFile() throws Exception {
String zipName = "LF.zip";
String fileName = "LF.xml";
UUID caseUuid = importCase(zipName, "application/zip");
DataSource dataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + zipName).toURI()));

MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", caseUuid)
.param("fileName", fileName))
.andExpect(status().isOk())
.andReturn();
assertEquals(readDataSource(dataSource, fileName), mvcResult.getResponse().getContentAsString());
}

@Test
public void testInputStreamWithGZipFile() throws Exception {
String gzipName = "LF.xml.gz";
String fileName = "LF.xml";
UUID caseUuid = importCase(gzipName, "application/zip");
DataSource dataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + gzipName).toURI()));

MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", caseUuid)
.param("fileName", fileName))
.andExpect(status().isOk())
.andReturn();
assertEquals(readDataSource(dataSource, fileName), mvcResult.getResponse().getContentAsString());
}

@Test
public void testInputStreamWithXiidmPlainFile() throws Exception {
String fileName = "LF.xml";
UUID caseUuid = importCase(fileName, "application/zip");
DataSource dataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + fileName).toURI()));

MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource", caseUuid)
.param("fileName", fileName))
.andExpect(status().isOk())
.andReturn();

assertEquals(readDataSource(dataSource, fileName), mvcResult.getResponse().getContentAsString());
}

@Test
public void testInputStreamWithSuffixExt() throws Exception {
String suffix = "/MicroGridTestConfiguration_BC_BE_DL_V2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,29 @@
public class FsCaseDataSourceControllerTest extends AbstractCaseDataSourceControllerTest {

@Autowired
protected FsCaseService caseService;
protected FsCaseService fsCaseService;

FileSystem fileSystem = FileSystems.getDefault();

@Before
public void setUp() throws URISyntaxException, IOException {
caseService = fsCaseService;
cgmesCaseUuid = UUID.randomUUID();
xiidmCaseUuid = UUID.randomUUID();
Path path = fileSystem.getPath(rootDirectory);
if (!Files.exists(path)) {
Files.createDirectories(path);
}
Path cgmesCaseDirectory = fileSystem.getPath(rootDirectory).resolve(cgmesCaseUuid.toString());
Path xiidmCaseDirectory = fileSystem.getPath(rootDirectory).resolve(xiidmCaseUuid.toString());
if (!Files.exists(cgmesCaseDirectory)) {
Files.createDirectories(cgmesCaseDirectory);
Files.createDirectories(xiidmCaseDirectory);
}

caseService.setFileSystem(fileSystem);
fsCaseService.setFileSystem(fileSystem);
//insert a cgmes in the FS
try (InputStream cgmesURL = getClass().getResourceAsStream("/" + CGMES_ZIP_NAME);
InputStream xiidmURL = getClass().getResourceAsStream("/" + XIIDM_ZIP_NAME)
) {
Files.copy(cgmesURL, cgmesCaseDirectory.resolve(CGMES_ZIP_NAME), StandardCopyOption.REPLACE_EXISTING);
Files.copy(xiidmURL, xiidmCaseDirectory.resolve(XIIDM_ZIP_NAME), StandardCopyOption.REPLACE_EXISTING);
}
cgmesDataSource = DataSource.fromPath(Paths.get(getClass().getResource("/" + CGMES_ZIP_NAME).toURI()));
xiidmDataSource = DataSource.fromPath(Paths.get(getClass().getResource("/" + XIIDM_ZIP_NAME).toURI()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

Expand All @@ -38,13 +37,9 @@ public class S3CaseDataSourceControllerTest extends AbstractCaseDataSourceContro

@Before
public void setUp() throws URISyntaxException, IOException {
//insert a cgmes file in the S3
try (InputStream cgmesIS = getClass().getResourceAsStream("/" + CGMES_ZIP_NAME);
InputStream xiidmIS = getClass().getResourceAsStream("/" + XIIDM_ZIP_NAME)) {
cgmesCaseUuid = s3CaseService.importCase(new MockMultipartFile(CGMES_ZIP_NAME, CGMES_ZIP_NAME, "application/zip", cgmesIS.readAllBytes()), false, false);
xiidmCaseUuid = s3CaseService.importCase(new MockMultipartFile(XIIDM_ZIP_NAME, XIIDM_ZIP_NAME, "application/zip", xiidmIS.readAllBytes()), false, false);
}
cgmesDataSource = DataSource.fromPath(Paths.get(getClass().getResource("/" + CGMES_ZIP_NAME).toURI()));
xiidmDataSource = DataSource.fromPath(Paths.get(getClass().getResource("/" + XIIDM_ZIP_NAME).toURI()));
caseService = s3CaseService;
cgmesCaseUuid = importCase(CGMES_ZIP_NAME, "application/zip");
cgmesDataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + CGMES_ZIP_NAME).toURI()));
}

}
42 changes: 42 additions & 0 deletions src/test/resources/LF.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_2" id="test" caseDate="2018-11-08T12:33:26.208+01:00" forecastDistance="0" sourceFormat="test">
<iidm:substation id="_c49942d6-8b01-4b01-b5e8-f1180f84906c">
<iidm:voltageLevel id="vl1" nominalV="400.0" topologyKind="NODE_BREAKER">
<iidm:nodeBreakerTopology>
<iidm:busbarSection id="b1" node="1"/>
<iidm:switch id="br1" kind="BREAKER" retained="false" open="false" node1="0" node2="1"/>
<iidm:switch id="br2" kind="BREAKER" retained="false" open="false" node1="1" node2="2"/>
<iidm:switch id="br3" kind="BREAKER" retained="false" open="false" node1="1" node2="3"/>
</iidm:nodeBreakerTopology>
<iidm:generator id="g1" energySource="OTHER" minP="0.0" maxP="200.0" voltageRegulatorOn="true" targetP="200.0" targetV="400.0" node="0" p="-200.0" q="-40.0">
<iidm:minMaxReactiveLimits minQ="-1.7976931348623157E308" maxQ="1.7976931348623157E308"/>
</iidm:generator>
</iidm:voltageLevel>
</iidm:substation>
<iidm:substation id="_87f7002b-056f-4a6a-a872-1744eea757e3">
<iidm:voltageLevel id="vl2" nominalV="400.0" topologyKind="NODE_BREAKER">
<iidm:nodeBreakerTopology>
<iidm:busbarSection id="b2" node="1"/>
<iidm:switch id="br4" kind="BREAKER" retained="false" open="false" node1="1" node2="0"/>
<iidm:switch id="br5" kind="BREAKER" retained="false" open="false" node1="1" node2="2"/>
</iidm:nodeBreakerTopology>
</iidm:voltageLevel>
</iidm:substation>
<iidm:substation id="_37e14a0f-5e34-4647-a062-8bfd9305fa9d">
<iidm:voltageLevel id="vl3" nominalV="400.0" topologyKind="NODE_BREAKER">
<iidm:nodeBreakerTopology>
<iidm:busbarSection id="b3" node="1"/>
<iidm:switch id="br6" kind="BREAKER" retained="false" open="false" node1="0" node2="1"/>
<iidm:switch id="br7" kind="BREAKER" retained="false" open="false" node1="1" node2="2"/>
<iidm:switch id="br8" kind="BREAKER" retained="false" open="false" node1="1" node2="3"/>
</iidm:nodeBreakerTopology>
<iidm:load id="l1" loadType="UNDEFINED" p0="180.0" q0="18.0" node="0" p="180.0" q="36.0"/>
</iidm:voltageLevel>
</iidm:substation>
<iidm:line id="line1" r="0.1" x="10.0" g1="0.0" b1="0.0" g2="0.0" b2="0.0" node1="2" voltageLevelId1="vl1" node2="0" voltageLevelId2="vl2" p1="100.0" q1="20.0" p2="-95.0" q2="-19.0"/>
<iidm:line id="line2" r="0.1" x="10.0" g1="0.0" b1="0.0" g2="0.0" b2="0.0" node1="2" voltageLevelId1="vl2" node2="2" voltageLevelId2="vl3" p1="95.0" q1="19.0" p2="-90.0" q2="-18.0"/>
<iidm:line id="line3" r="0.1" x="10.0" g1="0.0" b1="0.0" g2="0.0" b2="0.0" node1="3" voltageLevelId1="vl1" node2="3" voltageLevelId2="vl3" p1="-100.0" q1="-20.0" p2="90.0" q2="18.0">
<iidm:currentLimits1>
<iidm:temporaryLimit name="N/A"/>
</iidm:currentLimits1>
</iidm:line>
</iidm:network>
Binary file added src/test/resources/LF.xml.gz
Binary file not shown.

0 comments on commit df12461

Please sign in to comment.