From 5b7796d924ffd4319471d67e96cfa8802000f177 Mon Sep 17 00:00:00 2001 From: lisrte Date: Mon, 10 Jun 2024 11:27:19 +0200 Subject: [PATCH 1/2] Fix ModelConfigsHandler.eventBuilderConstructorByName setup Add UT for BuilderConfig and EventBuilderConfig instantiation Signed-off-by: lisrte --- .../builders/ModelConfigsHandler.java | 8 ++- .../dynawaltz/builders/BuilderConfigTest.java | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/builders/ModelConfigsHandler.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/builders/ModelConfigsHandler.java index fa0575e50..a8353a259 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/builders/ModelConfigsHandler.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/builders/ModelConfigsHandler.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; import java.util.ServiceLoader; +import java.util.stream.Collectors; /** * @author Laurent Issertial {@literal } @@ -37,11 +38,8 @@ public final class ModelConfigsHandler { new EventBuilderConfig(EventDisconnectionBuilder::of, EventDisconnectionBuilder.TAG), new EventBuilderConfig(NodeFaultEventBuilder::of, NodeFaultEventBuilder.TAG)); - private final Map eventBuilderConstructorByName = Map.of( - EventDisconnectionBuilder.TAG, EventDisconnectionBuilder::of, - NodeFaultEventBuilder.TAG, EventDisconnectionBuilder::of, - EventActivePowerVariationBuilder.TAG, EventActivePowerVariationBuilder::of - ); + private final Map eventBuilderConstructorByName = + eventBuilderConfigs.stream().collect(Collectors.toMap(EventBuilderConfig::getTag, EventBuilderConfig::getBuilderConstructor)); private ModelConfigsHandler() { List modelConfigLoaders = Lists.newArrayList(ServiceLoader.load(ModelConfigLoader.class)); diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java new file mode 100644 index 000000000..c84219cae --- /dev/null +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com/) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.dynawaltz.builders; + +import com.powsybl.commons.report.ReportNode; +import com.powsybl.dynamicsimulation.DynamicModel; +import com.powsybl.dynamicsimulation.EventModel; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Laurent Issertial {@literal } + */ +public class BuilderConfigTest { + + private static Network NETWORK; + private static ModelConfigsHandler MODEL_CONFIGS_HANDLER; + + @BeforeAll + static void setup() { + NETWORK = EurostagTutorialExample1Factory.create(); + MODEL_CONFIGS_HANDLER = ModelConfigsHandler.getInstance(); + } + + @Test + void testModelBuilders() { + for (BuilderConfig builderConfig : MODEL_CONFIGS_HANDLER.getBuilderConfigs()) { + String lib = builderConfig.getLibs().iterator().next(); + ModelBuilder tagBuilder = MODEL_CONFIGS_HANDLER.getModelBuilder(NETWORK, lib, ReportNode.NO_OP); + ModelBuilder configBuilder = builderConfig.getBuilderConstructor().createBuilder(NETWORK, lib, ReportNode.NO_OP); + assertNotNull(tagBuilder); + assertEquals(tagBuilder.getClass(), configBuilder.getClass()); + } + } + + @Test + void testEventBuilders() { + for (EventBuilderConfig eventBuilderConfig : MODEL_CONFIGS_HANDLER.getEventBuilderConfigs()) { + ModelBuilder tagBuilder = MODEL_CONFIGS_HANDLER.getEventModelBuilder(NETWORK, eventBuilderConfig.getTag(), ReportNode.NO_OP); + ModelBuilder configBuilder = eventBuilderConfig.getBuilderConstructor().createBuilder(NETWORK, ReportNode.NO_OP); + assertNotNull(tagBuilder); + assertEquals(tagBuilder.getClass(), configBuilder.getClass()); + } + } +} From 1393130f9bdfed34a3ee4ed2f7574bd011b065cd Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Mon, 10 Jun 2024 16:45:51 +0200 Subject: [PATCH 2/2] Fix code smell Signed-off-by: Florian Dupuy --- .../java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java index c84219cae..3e50da712 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/builders/BuilderConfigTest.java @@ -20,7 +20,7 @@ /** * @author Laurent Issertial {@literal } */ -public class BuilderConfigTest { +class BuilderConfigTest { private static Network NETWORK; private static ModelConfigsHandler MODEL_CONFIGS_HANDLER;