diff --git a/contingency/contingency-api/src/main/java/com/powsybl/contingency/contingency/list/IdentifierContingencyList.java b/contingency/contingency-api/src/main/java/com/powsybl/contingency/contingency/list/IdentifierContingencyList.java index 263c58c3a7c..772913b60fc 100644 --- a/contingency/contingency-api/src/main/java/com/powsybl/contingency/contingency/list/IdentifierContingencyList.java +++ b/contingency/contingency-api/src/main/java/com/powsybl/contingency/contingency/list/IdentifierContingencyList.java @@ -10,7 +10,6 @@ import com.powsybl.contingency.Contingency; import com.powsybl.contingency.ContingencyElement; import com.powsybl.contingency.contingency.list.identifier.NetworkElementIdentifier; -import com.powsybl.iidm.network.IdentifiableType; import com.powsybl.iidm.network.Network; import java.util.List; @@ -23,20 +22,19 @@ */ public class IdentifierContingencyList implements ContingencyList { + private static final String VERSION = "1.1"; private final String name; - private final IdentifiableType identifiableType; private final List networkElementIdentifiers; - public IdentifierContingencyList(String name, String identifiableType, List networkElementIdentifiers) { - this(name, IdentifiableType.valueOf(identifiableType), networkElementIdentifiers); - } - - public IdentifierContingencyList(String name, IdentifiableType identifiableType, List networkElementIdentifiers) { + public IdentifierContingencyList(String name, List networkElementIdentifiers) { this.name = Objects.requireNonNull(name); - this.identifiableType = Objects.requireNonNull(identifiableType); this.networkElementIdentifiers = ImmutableList.copyOf(networkElementIdentifiers); } + public static String getVersion() { + return VERSION; + } + @Override public String getName() { return name; @@ -47,10 +45,6 @@ public String getType() { return "identifier"; } - public IdentifiableType getIdentifiableType() { - return identifiableType; - } - public List getIdentifiants() { return ImmutableList.copyOf(networkElementIdentifiers); } diff --git a/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListDeserializer.java b/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListDeserializer.java index 7ff787f212d..f44464a8dd6 100644 --- a/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListDeserializer.java +++ b/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListDeserializer.java @@ -23,6 +23,8 @@ */ public class IdentifierContingencyListDeserializer extends StdDeserializer { + private static final String CONTEXT_NAME = "identifierContingencyList"; + public IdentifierContingencyListDeserializer() { super(IdentifierContingencyList.class); } @@ -30,12 +32,17 @@ public IdentifierContingencyListDeserializer() { @Override public IdentifierContingencyList deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException { String name = null; - String identifiableType = null; + String version = null; List networkElementIdentifiers = Collections.emptyList(); while (parser.nextToken() != JsonToken.END_OBJECT) { switch (parser.getCurrentName()) { case "version": + version = parser.nextTextValue(); + break; + + case "identifiableType": + JsonUtil.assertLessThanOrEqualToReferenceVersion(CONTEXT_NAME, "identifiableType", version, "1.0"); parser.nextToken(); break; @@ -47,10 +54,6 @@ public IdentifierContingencyList deserialize(JsonParser parser, DeserializationC parser.nextToken(); break; - case "identifiableType": - identifiableType = parser.nextTextValue(); - break; - case "identifiers": parser.nextToken(); networkElementIdentifiers = JsonUtil.readList(deserializationContext, parser, NetworkElementIdentifier.class); @@ -60,6 +63,6 @@ public IdentifierContingencyList deserialize(JsonParser parser, DeserializationC throw new IllegalStateException("Unexpected field: " + parser.getCurrentName()); } } - return new IdentifierContingencyList(name, identifiableType, networkElementIdentifiers); + return new IdentifierContingencyList(name, networkElementIdentifiers); } } diff --git a/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListSerializer.java b/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListSerializer.java index 084ee749e4e..2bfe6119a93 100644 --- a/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListSerializer.java +++ b/contingency/contingency-api/src/main/java/com/powsybl/contingency/json/IdentifierContingencyListSerializer.java @@ -9,7 +9,6 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; -import com.powsybl.contingency.contingency.list.ContingencyList; import com.powsybl.contingency.contingency.list.IdentifierContingencyList; import java.io.IOException; @@ -26,10 +25,9 @@ public IdentifierContingencyListSerializer() { @Override public void serialize(IdentifierContingencyList identifierContingencyList, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField("version", IdentifierContingencyList.getVersion()); jsonGenerator.writeStringField("type", identifierContingencyList.getType()); - jsonGenerator.writeStringField("version", ContingencyList.getVersion()); jsonGenerator.writeStringField("name", identifierContingencyList.getName()); - jsonGenerator.writeStringField("identifiableType", identifierContingencyList.getIdentifiableType().toString()); serializerProvider.defaultSerializeField("identifiers", identifierContingencyList.getIdentifiants(), jsonGenerator); diff --git a/contingency/contingency-api/src/test/java/com/powsybl/contingency/NetworkElementIdentifierContingencyListTest.java b/contingency/contingency-api/src/test/java/com/powsybl/contingency/NetworkElementIdentifierContingencyListTest.java index 77b1cfc734c..c6a02314bd9 100644 --- a/contingency/contingency-api/src/test/java/com/powsybl/contingency/NetworkElementIdentifierContingencyListTest.java +++ b/contingency/contingency-api/src/test/java/com/powsybl/contingency/NetworkElementIdentifierContingencyListTest.java @@ -33,7 +33,7 @@ void testSimpleIdentifier() { networkElementIdentifierList.add(new IdBasedNetworkElementIdentifier("LINE_S3S4")); networkElementIdentifierList.add(new IdBasedNetworkElementIdentifier("LINE_S4S1")); networkElementIdentifierList.add(new IdBasedNetworkElementIdentifier("test")); - IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", "LINE", networkElementIdentifierList); + IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", networkElementIdentifierList); List contingencies = contingencyList.getContingencies(network); assertEquals(2, contingencies.size()); assertEquals(new Contingency("LINE_S2S3", new LineContingency("LINE_S2S3")), contingencies.get(0)); @@ -45,7 +45,7 @@ void testUcteIdentifier() { Network network = EurostagTutorialExample1Factory.create(); List networkElementIdentifierList = new ArrayList<>(); networkElementIdentifierList.add(new VoltageLevelAndOrderNetworkElementIdentifier("VLHV1", "VLHV2", '1')); - IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", "LINE", networkElementIdentifierList); + IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", networkElementIdentifierList); List contingencies = contingencyList.getContingencies(network); assertEquals(1, contingencies.size()); assertEquals(new Contingency("NHV1_NHV2_1", new LineContingency("NHV1_NHV2_1")), contingencies.get(0)); @@ -61,7 +61,7 @@ void testIdentifierList() { networkElementIdentifierListElements.add(new VoltageLevelAndOrderNetworkElementIdentifier("VLHV1", "VLHV2", '2')); networkElementIdentifierListElements.add(new VoltageLevelAndOrderNetworkElementIdentifier("VLHV1", "VLHV2", '1')); networkElementIdentifierList.add(new NetworkElementIdentifierList(networkElementIdentifierListElements)); - IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", "LINE", networkElementIdentifierList); + IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", networkElementIdentifierList); List contingencies = contingencyList.getContingencies(network); assertEquals(1, contingencies.size()); assertEquals(new Contingency("NHV1_NHV2_2", new LineContingency("NHV1_NHV2_2")), contingencies.get(0)); diff --git a/contingency/contingency-api/src/test/java/com/powsybl/contingency/json/NetworkElementIdentifierContingencyListJsonTest.java b/contingency/contingency-api/src/test/java/com/powsybl/contingency/json/NetworkElementIdentifierContingencyListJsonTest.java index 4c64ecfa32c..03b85d7bdd7 100644 --- a/contingency/contingency-api/src/test/java/com/powsybl/contingency/json/NetworkElementIdentifierContingencyListJsonTest.java +++ b/contingency/contingency-api/src/test/java/com/powsybl/contingency/json/NetworkElementIdentifierContingencyListJsonTest.java @@ -10,6 +10,8 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.powsybl.commons.test.AbstractConverterTest; import com.powsybl.commons.json.JsonUtil; +import com.powsybl.commons.test.ComparisonUtils; +import com.powsybl.contingency.contingency.list.ContingencyList; import com.powsybl.contingency.contingency.list.IdentifierContingencyList; import com.powsybl.contingency.contingency.list.identifier.NetworkElementIdentifier; import com.powsybl.contingency.contingency.list.identifier.NetworkElementIdentifierList; @@ -17,10 +19,7 @@ import com.powsybl.contingency.contingency.list.identifier.VoltageLevelAndOrderNetworkElementIdentifier; import org.junit.jupiter.api.Test; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UncheckedIOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -33,12 +32,15 @@ */ class NetworkElementIdentifierContingencyListJsonTest extends AbstractConverterTest { + private static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new ContingencyJsonModule()); + private static final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter(); + private static IdentifierContingencyList create() { List networkElementIdentifiers = new ArrayList<>(); networkElementIdentifiers.add(new IdBasedNetworkElementIdentifier("identifier")); networkElementIdentifiers.add(new VoltageLevelAndOrderNetworkElementIdentifier("vl1", "vl2", '1')); networkElementIdentifiers.add(new NetworkElementIdentifierList(Collections.singletonList(new IdBasedNetworkElementIdentifier("identifier1")))); - return new IdentifierContingencyList("list1", "LINE", networkElementIdentifiers); + return new IdentifierContingencyList("list1", networkElementIdentifiers); } @Test @@ -47,6 +49,19 @@ void roundTripTest() throws IOException { "/identifierContingencyList.json"); } + @Test + void readVersion10() { + ContingencyList contingencyList = NetworkElementIdentifierContingencyListJsonTest + .readJsonInputStream(Objects.requireNonNull(getClass() + .getResourceAsStream("/identifierContingencyListv1_0.json"))); + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + WRITER.writeValue(bos, contingencyList); + ComparisonUtils.compareTxt(getClass().getResourceAsStream("/identifierContingencyList.json"), new ByteArrayInputStream(bos.toByteArray())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private static IdentifierContingencyList readContingencyList(Path jsonFile) { return read(jsonFile, IdentifierContingencyList.class); } @@ -65,6 +80,15 @@ private static T read(Path jsonFile, Class clazz) { } } + private static IdentifierContingencyList readJsonInputStream(InputStream is) { + Objects.requireNonNull(is); + try { + return MAPPER.readValue(is, IdentifierContingencyList.class); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + private static void write(T object, Path jsonFile) { Objects.requireNonNull(object); Objects.requireNonNull(jsonFile); diff --git a/contingency/contingency-api/src/test/resources/identifierContingencyList.json b/contingency/contingency-api/src/test/resources/identifierContingencyList.json index 94b2a36d7d2..38d758d1634 100644 --- a/contingency/contingency-api/src/test/resources/identifierContingencyList.json +++ b/contingency/contingency-api/src/test/resources/identifierContingencyList.json @@ -1,8 +1,7 @@ { + "version" : "1.1", "type" : "identifier", - "version" : "1.0", "name" : "list1", - "identifiableType" : "LINE", "identifiers" : [ { "type" : "ID_BASED", "identifier" : "identifier" diff --git a/contingency/contingency-api/src/test/resources/identifierContingencyListv1_0.json b/contingency/contingency-api/src/test/resources/identifierContingencyListv1_0.json new file mode 100644 index 00000000000..a148c412c25 --- /dev/null +++ b/contingency/contingency-api/src/test/resources/identifierContingencyListv1_0.json @@ -0,0 +1,21 @@ +{ + "version" : "1.0", + "type" : "identifier", + "name" : "list1", + "identifiableType" : "type", + "identifiers" : [ { + "type" : "ID_BASED", + "identifier" : "identifier" + }, { + "type" : "VOLTAGE_LEVELS_AND_ORDER", + "voltageLevelId1" : "vl1", + "voltageLevelId2" : "vl2", + "order" : "1" + }, { + "type" : "LIST", + "identifierList" : [ { + "type" : "ID_BASED", + "identifier" : "identifier1" + } ] + } ] +} \ No newline at end of file