Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless identifiableType from IdentifierContingencyList #2491

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<NetworkElementIdentifier> networkElementIdentifiers;

public IdentifierContingencyList(String name, String identifiableType, List<NetworkElementIdentifier> networkElementIdentifiers) {
this(name, IdentifiableType.valueOf(identifiableType), networkElementIdentifiers);
}

public IdentifierContingencyList(String name, IdentifiableType identifiableType, List<NetworkElementIdentifier> networkElementIdentifiers) {
public IdentifierContingencyList(String name, List<NetworkElementIdentifier> 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;
Expand All @@ -47,10 +45,6 @@ public String getType() {
return "identifier";
}

public IdentifiableType getIdentifiableType() {
return identifiableType;
}

public List<NetworkElementIdentifier> getIdentifiants() {
return ImmutableList.copyOf(networkElementIdentifiers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@
*/
public class IdentifierContingencyListDeserializer extends StdDeserializer<IdentifierContingencyList> {

private static final String CONTEXT_NAME = "identifierContingencyList";

public IdentifierContingencyListDeserializer() {
super(IdentifierContingencyList.class);
}

@Override
public IdentifierContingencyList deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException {
String name = null;
String identifiableType = null;
String version = null;
List<NetworkElementIdentifier> 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;

Expand All @@ -47,10 +54,6 @@ public IdentifierContingencyList deserialize(JsonParser parser, DeserializationC
parser.nextToken();
break;

case "identifiableType":
identifiableType = parser.nextTextValue();
break;
EtienneLt marked this conversation as resolved.
Show resolved Hide resolved

case "identifiers":
parser.nextToken();
networkElementIdentifiers = JsonUtil.readList(deserializationContext, parser, NetworkElementIdentifier.class);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
EtienneLt marked this conversation as resolved.
Show resolved Hide resolved
serializerProvider.defaultSerializeField("identifiers",
identifierContingencyList.getIdentifiants(),
jsonGenerator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Contingency> contingencies = contingencyList.getContingencies(network);
assertEquals(2, contingencies.size());
assertEquals(new Contingency("LINE_S2S3", new LineContingency("LINE_S2S3")), contingencies.get(0));
Expand All @@ -45,7 +45,7 @@ void testUcteIdentifier() {
Network network = EurostagTutorialExample1Factory.create();
List<NetworkElementIdentifier> networkElementIdentifierList = new ArrayList<>();
networkElementIdentifierList.add(new VoltageLevelAndOrderNetworkElementIdentifier("VLHV1", "VLHV2", '1'));
IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", "LINE", networkElementIdentifierList);
IdentifierContingencyList contingencyList = new IdentifierContingencyList("list", networkElementIdentifierList);
List<Contingency> contingencies = contingencyList.getContingencies(network);
assertEquals(1, contingencies.size());
assertEquals(new Contingency("NHV1_NHV2_1", new LineContingency("NHV1_NHV2_1")), contingencies.get(0));
Expand All @@ -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<Contingency> contingencies = contingencyList.getContingencies(network);
assertEquals(1, contingencies.size());
assertEquals(new Contingency("NHV1_NHV2_2", new LineContingency("NHV1_NHV2_2")), contingencies.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
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;
import com.powsybl.contingency.contingency.list.identifier.IdBasedNetworkElementIdentifier;
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;
Expand All @@ -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<NetworkElementIdentifier> 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
Expand All @@ -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);
}
Expand All @@ -65,6 +80,15 @@ private static <T> T read(Path jsonFile, Class<T> 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 <T> void write(T object, Path jsonFile) {
Objects.requireNonNull(object);
Objects.requireNonNull(jsonFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"version" : "1.1",
"type" : "identifier",
"version" : "1.0",
"name" : "list1",
"identifiableType" : "LINE",
"identifiers" : [ {
"type" : "ID_BASED",
"identifier" : "identifier"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
} ]
} ]
}