Skip to content

Commit

Permalink
add versioning and keep compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <etienne.lesot@rte-france.com>
  • Loading branch information
EtienneLt committed Mar 14, 2023
1 parent 0f64cb3 commit 3388dfc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public class IdentifierContingencyList implements ContingencyList {

private static final String VERSION = "1.1";
private final String name;
private final List<NetworkElementIdentifier> networkElementIdentifiers;

Expand All @@ -30,6 +31,10 @@ public IdentifierContingencyList(String name, List<NetworkElementIdentifier> net
this.networkElementIdentifiers = ImmutableList.copyOf(networkElementIdentifiers);
}

public static String getVersion() {
return VERSION;
}

@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +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 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 Down
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,8 +25,8 @@ 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());
serializerProvider.defaultSerializeField("identifiers",
identifierContingencyList.getIdentifiants(),
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,6 +32,9 @@
*/
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"));
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,6 +1,6 @@
{
"version" : "1.1",
"type" : "identifier",
"version" : "1.0",
"name" : "list1",
"identifiers" : [ {
"type" : "ID_BASED",
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"
} ]
} ]
}

0 comments on commit 3388dfc

Please sign in to comment.