Skip to content

Commit 71585a3

Browse files
Add basic logs control
1 parent bd91ab1 commit 71585a3

File tree

6 files changed

+84
-30
lines changed

6 files changed

+84
-30
lines changed

src/test/java/org/gridsuite/voltageinit/server/VoltageInitParametersTest.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.databind.DeserializationFeature;
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212
import com.powsybl.commons.reporter.Report;
13+
import com.powsybl.commons.reporter.Reporter;
1314
import com.powsybl.commons.reporter.ReporterModel;
1415
import com.powsybl.commons.reporter.TypedValue;
1516
import com.powsybl.iidm.network.IdentifiableType;
@@ -38,10 +39,12 @@
3839
import org.gridsuite.voltageinit.server.service.parameters.FilterService;
3940
import org.gridsuite.voltageinit.server.service.parameters.VoltageInitParametersService;
4041
import org.gridsuite.voltageinit.server.util.VoltageLimitParameterType;
42+
import org.gridsuite.voltageinit.utils.TestUtils;
4143
import org.junit.jupiter.api.BeforeEach;
4244
import org.junit.jupiter.api.DynamicTest;
4345
import org.junit.jupiter.api.Test;
4446
import org.junit.jupiter.api.TestFactory;
47+
import org.skyscreamer.jsonassert.JSONAssert;
4548
import org.springframework.beans.factory.annotation.Autowired;
4649
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
4750
import org.springframework.boot.test.context.SpringBootTest;
@@ -52,10 +55,13 @@
5255
import org.springframework.test.web.servlet.MockMvc;
5356
import org.springframework.test.web.servlet.MvcResult;
5457
import org.springframework.transaction.annotation.Transactional;
58+
import org.springframework.util.function.ThrowingBiFunction;
5559

56-
import java.util.*;
60+
import java.util.List;
61+
import java.util.Map;
62+
import java.util.Objects;
63+
import java.util.UUID;
5764
import java.util.function.Consumer;
58-
import java.util.function.Function;
5965
import java.util.stream.Collectors;
6066
import java.util.stream.Stream;
6167

@@ -281,14 +287,17 @@ private static Consumer<VoltageLimitOverride> assertVoltageLimitOverride(final S
281287

282288
@TestFactory
283289
List<DynamicTest> dynamicTestsBuildSpecificVoltageLimits() {
284-
final Function<List<VoltageLimitEntity>, ListAssert<VoltageLimitOverride>> initTestEnv = (voltageLimits) -> {
290+
final ThrowingBiFunction<List<VoltageLimitEntity>, String, ListAssert<VoltageLimitOverride>> initTestEnv = (voltageLimits, reportFilename) -> {
285291
final VoltageInitParametersEntity voltageInitParameters = parametersRepository.save(
286292
new VoltageInitParametersEntity(UUID.randomUUID(), null, "", voltageLimits, null, null, null)
287293
);
288294
final VoltageInitRunContext context = new VoltageInitRunContext(NETWORK_UUID, VARIANT_ID_1, null, REPORT_UUID, null, "", "", voltageInitParameters.getId());
289295
final OpenReacParameters openReacParameters = voltageInitParametersService.buildOpenReacParameters(context, network);
290296
/*TODO*/System.out.println(parametersRepository.findAll().stream().map(ToStringBuilder::reflectionToString).collect(Collectors.joining()));
291-
/*TODO*///System.out.println(mapper.writeValueAsString(context.getRootReporter()));
297+
ReporterModel reporter = new ReporterModel("test", "test");
298+
VoltageInitWorkerService.addRestrictedVoltageLevelReport(context.getVoltageLevelsIdsRestricted(), reporter);
299+
/*TODO*/System.out.println(mapper.writeValueAsString(reporter));
300+
JSONAssert.assertEquals("build parameters logs", TestUtils.resourceToString(reportFilename), mapper.writeValueAsString(reporter), false);
292301
return assertThat(openReacParameters.getSpecificVoltageLimits()).as("SpecificVoltageLimits");
293302
};
294303
final VoltageLimitEntity voltageLimit = new VoltageLimitEntity(UUID.randomUUID(), 5., 10., 0, VoltageLimitParameterType.DEFAULT, List.of(new FilterEquipmentsEmbeddable(FILTER_UUID_1, FILTER_1)));
@@ -297,7 +306,7 @@ List<DynamicTest> dynamicTestsBuildSpecificVoltageLimits() {
297306
final VoltageLimitEntity voltageLimit4 = new VoltageLimitEntity(UUID.randomUUID(), -20.0, 10.0, 0, VoltageLimitParameterType.MODIFICATION, List.of(new FilterEquipmentsEmbeddable(FILTER_UUID_1, FILTER_1)));
298307
final VoltageLimitEntity voltageLimit5 = new VoltageLimitEntity(UUID.randomUUID(), 10.0, 10.0, 0, VoltageLimitParameterType.DEFAULT, List.of(new FilterEquipmentsEmbeddable(FILTER_UUID_1, FILTER_1)));
299308
return List.of(
300-
DynamicTest.dynamicTest("No voltage limit modification", () -> initTestEnv.apply(List.of(voltageLimit, voltageLimit2))
309+
DynamicTest.dynamicTest("No voltage limit modification", () -> initTestEnv.apply(List.of(voltageLimit, voltageLimit2), "report_empty.json")
301310
.hasSize(4)
302311
//No override should be relative since there is no voltage limit modification
303312
.noneMatch(VoltageLimitOverride::isRelative)
@@ -310,7 +319,7 @@ List<DynamicTest> dynamicTestsBuildSpecificVoltageLimits() {
310319
assertVoltageLimitOverride("VLLOAD", VoltageLimitType.HIGH_VOLTAGE_LIMIT, 88.)
311320
)),
312321
//We now add limit modifications in additions to defaults settings
313-
DynamicTest.dynamicTest("With voltage limit modifications", () -> initTestEnv.apply(List.of(voltageLimit, voltageLimit2, voltageLimit3))
322+
DynamicTest.dynamicTest("With voltage limit modifications", () -> initTestEnv.apply(List.of(voltageLimit, voltageLimit2, voltageLimit3), "report_empty.json")
314323
//Limits that weren't impacted by default settings are now impacted by modification settings
315324
.hasSize(8)
316325
//There should (not?) be relative overrides since voltage limit modification are applied
@@ -323,13 +332,13 @@ List<DynamicTest> dynamicTestsBuildSpecificVoltageLimits() {
323332
.satisfiesOnlyOnce(assertVoltageLimitOverride("VLLOAD", VoltageLimitType.HIGH_VOLTAGE_LIMIT, 86.))),
324333
//note: VoltageLimitOverride implement equals() correctly, so we can use it
325334
// We need to check for the case of relative = true with the modification less than 0 => the new low voltage limit = low voltage limit * -1
326-
DynamicTest.dynamicTest("Case relative true overrides", () -> initTestEnv.apply(List.of(voltageLimit4))
335+
DynamicTest.dynamicTest("Case relative true overrides", () -> initTestEnv.apply(List.of(voltageLimit4), "report_case_relative_true.json")
327336
.hasSize(4)
328337
// isRelative: There should have relative true overrides since voltage limit modification are applied for VLGEN
329338
// getLimit: The low voltage limit must be impacted by the modification of the value
330339
.containsOnlyOnce(new VoltageLimitOverride("VLGEN", VoltageLimitType.LOW_VOLTAGE_LIMIT, true, -10.0))),
331340
// We need to check for the case of relative = false with the modification less than 0 => the new low voltage limit = 0
332-
DynamicTest.dynamicTest("Case relative false overrides", () -> initTestEnv.apply(List.of(voltageLimit4, voltageLimit5))
341+
DynamicTest.dynamicTest("Case relative false overrides", () -> initTestEnv.apply(List.of(voltageLimit4, voltageLimit5), "report_case_relative_false.json")
333342
.hasSize(8)
334343
// isRelative: There should have relative false overrides since voltage limit modification are applied for VLHV1
335344
// getLimit: The low voltage limit must be impacted by the modification of the value
@@ -342,18 +351,10 @@ void testAddRestrictedVoltageLevelReport() {
342351
Map<String, Double> restrictedVoltageLevel = Map.of("vl", 10.0);
343352
ReporterModel reporter = new ReporterModel("test", "test");
344353
VoltageInitWorkerService.addRestrictedVoltageLevelReport(restrictedVoltageLevel, reporter);
345-
assertThat(reporter.getReports()).first()
346-
.returns("restrictedVoltageLevels", Report::getReportKey)
347-
.returns("The modifications to the low limits for certain voltage levels have been restricted to avoid negative voltage limits: vl=10.0", Report::getDefaultMessage);
348-
349-
Optional<Map.Entry<String, TypedValue>> typedValues = reporter.getReports()
350-
.stream()
351-
.map(Report::getValues) //Stream<Map<String, TypedValue>>
352-
.findFirst() //Optional<Map<String, TypedValue>>
353-
.flatMap(values -> values.entrySet().stream().findFirst());
354-
assertThat(typedValues.map(Map.Entry::getKey)).contains("reportSeverity");
355-
assertThat(typedValues.map(value -> value.getValue().getValue())).contains("WARN");
356-
357-
//TODO replace by json resource
354+
Reporter expected = new ReporterModel("test", "test");
355+
expected.report("restrictedVoltageLevels",
356+
"The modifications to the low limits for certain voltage levels have been restricted to avoid negative voltage limits: vl=10.0",
357+
Map.of(Report.REPORT_SEVERITY_KEY, TypedValue.WARN_SEVERITY));
358+
assertThat(reporter).usingRecursiveComparison().isEqualTo(expected);
358359
}
359360
}

src/test/java/org/gridsuite/voltageinit/service/ReportServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
import java.io.IOException;
2727
import java.net.URI;
28+
import java.net.URISyntaxException;
2829
import java.util.UUID;
2930

3031
import static com.github.tomakehurst.wiremock.client.WireMock.*;
3132
import static org.gridsuite.voltageinit.utils.TestUtils.resourceToString;
32-
import static org.mockito.ArgumentMatchers.eq;
3333
import static org.mockito.Mockito.times;
3434
import static org.mockito.Mockito.verify;
3535

@@ -61,8 +61,8 @@ private void configureWireMockServer(String reportJson) {
6161
}
6262

6363
@BeforeEach
64-
public void setUp() throws IOException {
65-
String reportJson = resourceToString("/report.json");
64+
public void setUp() throws IOException, URISyntaxException {
65+
String reportJson = resourceToString("report.json");
6666
server = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
6767
server.start();
6868
WireMock.configureFor("localhost", server.port());

src/test/java/org/gridsuite/voltageinit/utils/TestUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
package org.gridsuite.voltageinit.utils;
99

10-
import com.google.common.io.ByteStreams;
11-
import org.junit.platform.commons.util.StringUtils;
12-
1310
import java.io.IOException;
11+
import java.net.URISyntaxException;
1412
import java.nio.charset.StandardCharsets;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
1515

1616
/**
1717
* @author Anis Touri <anis.touri at rte-france.com>
1818
*/
1919
public final class TestUtils {
2020

2121
private TestUtils() {
22+
throw new RuntimeException("Utility class can't be instantiated");
2223
}
2324

24-
public static String resourceToString(String resource) throws IOException {
25-
String content = new String(ByteStreams.toByteArray(TestUtils.class.getResourceAsStream(resource)), StandardCharsets.UTF_8);
26-
return StringUtils.replaceWhitespaceCharacters(content, "");
25+
public static String resourceToString(final String resource) throws IOException, URISyntaxException {
26+
return Files.readString(Paths.get(ClassLoader.getSystemClassLoader().getResource(resource).toURI()), StandardCharsets.UTF_8).trim();
2727
}
2828
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "1.0",
3+
"reportTree": {
4+
"taskKey": "test",
5+
"reports": [
6+
{
7+
"reportKey": "restrictedVoltageLevels",
8+
"values": {
9+
"reportSeverity": {
10+
"value": "WARN",
11+
"type": "SEVERITY"
12+
}
13+
}
14+
}
15+
]
16+
},
17+
"dics": {
18+
"default": {
19+
"restrictedVoltageLevels": "The modifications to the low limits for certain voltage levels have been restricted to avoid negative voltage limits: VLHV2=-10.0, VLHV1=0.0, VLGEN=-10.0, VLLOAD=0.0",
20+
"test": "test"
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "1.0",
3+
"reportTree": {
4+
"taskKey": "test",
5+
"reports": [
6+
{
7+
"reportKey": "restrictedVoltageLevels",
8+
"values": {
9+
"reportSeverity": {
10+
"value": "WARN",
11+
"type": "SEVERITY"
12+
}
13+
}
14+
}
15+
]
16+
},
17+
"dics": {
18+
"default": {
19+
"restrictedVoltageLevels": "The modifications to the low limits for certain voltage levels have been restricted to avoid negative voltage limits: VLHV2=-10.0, VLGEN=-10.0",
20+
"test": "test"
21+
}
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "1.0",
3+
"reportTree": { "taskKey": "test" },
4+
"dics": {
5+
"default": { "test": "test" }
6+
}
7+
}

0 commit comments

Comments
 (0)