|
| 1 | +package org.gridsuite.shortcircuit.server.dto; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.powsybl.shortcircuit.ShortCircuitParameters; |
| 5 | +import com.powsybl.shortcircuit.VoltageRange; |
| 6 | +import lombok.NonNull; |
| 7 | +import lombok.SneakyThrows; |
| 8 | +import org.assertj.core.api.WithAssertions; |
| 9 | +import org.gridsuite.shortcircuit.server.RestTemplateConfig; |
| 10 | +import org.json.JSONArray; |
| 11 | +import org.json.JSONObject; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 14 | +import org.skyscreamer.jsonassert.JSONCompareMode; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.autoconfigure.json.JsonTest; |
| 17 | +import org.springframework.test.context.ContextConfiguration; |
| 18 | + |
| 19 | +import static org.gridsuite.shortcircuit.server.service.ShortCircuitService.CEI909_VOLTAGE_PROFILE; |
| 20 | + |
| 21 | +@ContextConfiguration(classes = { RestTemplateConfig.class }) |
| 22 | +@JsonTest |
| 23 | +class ShortCircuitParametersInfosTest implements WithAssertions { |
| 24 | + @Autowired |
| 25 | + ObjectMapper objectMapper; |
| 26 | + |
| 27 | + @SneakyThrows |
| 28 | + private static JSONObject toJson(@NonNull final VoltageRange voltageRange) { |
| 29 | + return new JSONObject().put("minimumNominalVoltage", voltageRange.getMinimumNominalVoltage()) |
| 30 | + .put("maximumNominalVoltage", voltageRange.getMaximumNominalVoltage()) |
| 31 | + .put("voltageRangeCoefficient", voltageRange.getRangeCoefficient()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void shouldSerializeCei909VoltageRanges() throws Exception { |
| 36 | + JSONAssert.assertEquals( |
| 37 | + new JSONObject().put("predefinedParameters", ShortCircuitPredefinedConfiguration.ICC_MAX_WITH_CEI909.toString()) |
| 38 | + .put("parameters", new JSONObject().put("version", "1.3") |
| 39 | + .put("withLimitViolations", true) |
| 40 | + .put("withVoltageResult", true) |
| 41 | + .put("withFeederResult", true) |
| 42 | + .put("studyType", "TRANSIENT") |
| 43 | + .put("minVoltageDropProportionalThreshold", 0.0) |
| 44 | + .put("withFortescueResult", true) |
| 45 | + .put("withLoads", true) |
| 46 | + .put("withShuntCompensators", true) |
| 47 | + .put("withVSCConverterStations", true) |
| 48 | + .put("withNeutralPosition", false) |
| 49 | + .put("initialVoltageProfileMode", "NOMINAL") |
| 50 | + .put("detailedReport", true)) |
| 51 | + .put("cei909VoltageRanges", CEI909_VOLTAGE_PROFILE.stream() |
| 52 | + .map(ShortCircuitParametersInfosTest::toJson) |
| 53 | + .reduce(new JSONArray(), JSONArray::put, (arr1, arr2) -> null)), |
| 54 | + new JSONObject(objectMapper.writeValueAsString(new ShortCircuitParametersInfos(ShortCircuitPredefinedConfiguration.ICC_MAX_WITH_CEI909, new ShortCircuitParameters()))), |
| 55 | + JSONCompareMode.STRICT |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void shouldIgnoreCei909VoltageRangesWhenDeserialize() { |
| 61 | + final String dumbJson = "\"predefinedParameters\":\"ICC_MAX_WITH_CEI909\", \"parameters\":{\"version\":\"1.3\",\"withLimitViolations\":true,\"withVoltageResult\":true,\"withFeederResult\":true,\"studyType\":\"TRANSIENT\",\"minVoltageDropProportionalThreshold\":0.0,\"withFortescueResult\":false,\"withLoads\":true,\"withShuntCompensators\":true,\"withVSCConverterStations\":true,\"withNeutralPosition\":false,\"initialVoltageProfileMode\":\"NOMINAL\",\"detailedReport\":true}"; |
| 62 | + assertThatNoException().as("DTO with CEI909 field") |
| 63 | + .isThrownBy(() -> objectMapper.readValue("{" + dumbJson + ", \"cei909VoltageRanges\":[null,null]}", ShortCircuitParametersInfos.class)); |
| 64 | + assertThatNoException().as("DTO without CEI909 field") |
| 65 | + .isThrownBy(() -> objectMapper.readValue("{" + dumbJson + "}", ShortCircuitParametersInfos.class)); |
| 66 | + } |
| 67 | +} |
0 commit comments