Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed May 16, 2024
1 parent f1d3fb0 commit 5b85593
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static DynamicModelConfig parseModelConfig(JsonParser parser) {
var parsingContext = new Object() {
String model = null;
String group = null;
SetGroupType groupType = null;
SetGroupType groupType = SetGroupType.FIXED;
final List<Property> properties = new ArrayList<>();
};
JsonUtil.parseObject(parser, name -> switch (name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<Property> getProperties() {
return properties;
}

public void setProperties(List<Property> properties) {
this.properties = properties;
public void addProperty(Property property) {
properties.add(property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static EventModelConfig parseModelConfig(JsonParser parser) {
yield true;
}
case "properties" -> {
JsonUtil.parseObjectArray(parser, modelConfig.getProperties()::add, PropertyParserUtils::parseProperty);
JsonUtil.parseObjectArray(parser, modelConfig::addProperty, PropertyParserUtils::parseProperty);
yield true;
}
default -> false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ void testWrongNameBuilder() {
}

@Test
void testEventModelConfigDeserializer() throws IOException {
void testModelConfigDeserializer() throws IOException {
ObjectMapper objectMapper = setupObjectMapper();
try (InputStream is = getClass().getResourceAsStream("/suppliers/mappingDynamicModel.json")) {
List<DynamicModelConfig> configs = objectMapper.readValue(is, new TypeReference<>() {
});
assertEquals(1, configs.size());
assertEquals(2, configs.size());
assertThat(configs.get(0)).usingRecursiveComparison().isEqualTo(getLoadConfig());
assertThat(configs.get(1)).usingRecursiveComparison().isEqualTo(getTcbConfig());
}
}

Expand All @@ -89,6 +90,21 @@ void groupTypeException() {
assertEquals("No ID found for parameter set id", e.getMessage());
}

@Test
void wrongPropertyException() {
Network network = EurostagTutorialExample1Factory.create();
DynamicModelConfig modelConfig = new DynamicModelConfig("LoadAlphaBeta", "LAB", List.of(
new PropertyBuilder()
.name("wrongName")
.value("LOAD")
.type(PropertyType.STRING)
.build()
));
DynawoDynamicModelSupplier supplier = new DynawoDynamicModelSupplier(List.of(modelConfig));
Exception e = assertThrows(PowsyblException.class, () -> supplier.get(network, ReportNode.NO_OP));
assertEquals("Method wrongName not found for parameter LOAD on builder BaseLoadBuilder", e.getMessage());
}

private static List<DynamicModelConfig> getModelConfigs() {
return List.of(
new DynamicModelConfig("GeneratorPQ", "DM_", SetGroupType.PREFIX, List.of(
Expand All @@ -97,23 +113,7 @@ private static List<DynamicModelConfig> getModelConfigs() {
.value("GEN")
.type(PropertyType.STRING)
.build())),
new DynamicModelConfig("TapChangerBlockingAutomaton", "tcb_par", List.of(
new PropertyBuilder()
.name("dynamicModelId")
.value("TCB1")
.type(PropertyType.STRING)
.build(),
new PropertyBuilder()
.name("transformers")
.values(List.of("NGEN_NHV1", "NHV2_NLOAD"))
.type(PropertyType.STRINGS)
.build(),
new PropertyBuilder()
.name("uMeasurements")
.arrays(List.of(List.of("OldNGen", "NGEN"), List.of("NHV1", "NHV2")))
.type(PropertyType.STRINGS_ARRAYS)
.build()
))
getTcbConfig()
);
}

Expand All @@ -126,6 +126,26 @@ private static DynamicModelConfig getLoadConfig() {
.build()));
}

private static DynamicModelConfig getTcbConfig() {
return new DynamicModelConfig("TapChangerBlockingAutomaton", "tcb_par", SetGroupType.FIXED, List.of(
new PropertyBuilder()
.name("dynamicModelId")
.value("TCB1")
.type(PropertyType.STRING)
.build(),
new PropertyBuilder()
.name("transformers")
.values(List.of("NGEN_NHV1", "NHV2_NLOAD"))
.type(PropertyType.STRINGS)
.build(),
new PropertyBuilder()
.name("uMeasurements")
.arrays(List.of(List.of("OldNGen", "NGEN"), List.of("NHV1", "NHV2")))
.type(PropertyType.STRINGS_ARRAYS)
.build()
));
}

private static ObjectMapper setupObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.EventModel;
import com.powsybl.dynawaltz.models.events.EventActivePowerVariationBuilder;
Expand All @@ -26,8 +27,7 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
Expand Down Expand Up @@ -77,6 +77,21 @@ void testEventModelConfigDeserializer() throws IOException {
}
}

@Test
void wrongPropertyException() {
Network network = EurostagTutorialExample1Factory.create();
EventModelConfig eventModelConfig = new EventModelConfig("Disconnect", List.of(
new PropertyBuilder()
.name("wrongName")
.value("NHV1_NHV2_2")
.type(PropertyType.STRING)
.build()
));
DynawoEventModelsSupplier supplier = new DynawoEventModelsSupplier(List.of(eventModelConfig));
Exception e = assertThrows(PowsyblException.class, () -> supplier.get(network, ReportNode.NO_OP));
assertEquals("Method wrongName not found for parameter NHV1_NHV2_2 on builder EventDisconnectionBuilder", e.getMessage());
}

private static List<EventModelConfig> getEventConfigs() {
return List.of(
new EventModelConfig("Disconnect", List.of(
Expand Down
21 changes: 21 additions & 0 deletions dynawaltz/src/test/resources/suppliers/mappingDynamicModel.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
"type":"STRING"
}
]
},
{
"model": "TapChangerBlockingAutomaton",
"group": "tcb_par",
"properties":[
{
"name": "dynamicModelId",
"value": "TCB1",
"type": "STRING"
},
{
"name": "transformers",
"values": ["NGEN_NHV1", "NHV2_NLOAD"],
"type": "STRINGS"
},
{
"name": "uMeasurements",
"arrays": [["OldNGen", "NGEN"], ["NHV1", "NHV2"]],
"type": "STRINGS_ARRAYS"
}
]
}
]
}

0 comments on commit 5b85593

Please sign in to comment.