From 051489e41aba2e4f21ec91adc7152e427e1c4ad2 Mon Sep 17 00:00:00 2001 From: parapluplu Date: Wed, 3 Jul 2024 11:46:10 +0200 Subject: [PATCH] Unit Tests: Add test case for alphabetical scheduler (#2698) * Unit Tests: Add test case for alphabetical scheduler * Improve test coverage; fix Eclipse autoformat --------- Co-authored-by: Felix Remmel Co-authored-by: Stefan Feilmeier --- .../SchedulerAllAlphabeticallyImplTest.java | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/io.openems.edge.scheduler.allalphabetically/test/io/openems/edge/scheduler/allalphabetically/SchedulerAllAlphabeticallyImplTest.java b/io.openems.edge.scheduler.allalphabetically/test/io/openems/edge/scheduler/allalphabetically/SchedulerAllAlphabeticallyImplTest.java index d70d542d30b..204e4e48109 100644 --- a/io.openems.edge.scheduler.allalphabetically/test/io/openems/edge/scheduler/allalphabetically/SchedulerAllAlphabeticallyImplTest.java +++ b/io.openems.edge.scheduler.allalphabetically/test/io/openems/edge/scheduler/allalphabetically/SchedulerAllAlphabeticallyImplTest.java @@ -2,7 +2,9 @@ import static org.junit.Assert.assertEquals; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Test; @@ -25,7 +27,7 @@ public class SchedulerAllAlphabeticallyImplTest { private static final String CTRL4_ID = "ctrl4"; @Test - public void test() throws Exception { + public void testWithFixedPriorities() throws Exception { final SchedulerAllAlphabetically sut = new SchedulerAllAlphabeticallyImpl(); new ComponentTest(sut) // .addReference("componentManager", new DummyComponentManager()) // @@ -36,15 +38,55 @@ public void test() throws Exception { .addComponent(new DummyController(CTRL4_ID)) // .activate(MyConfig.create() // .setId(SCHEDULER_ID) // - .setControllersIds(CTRL2_ID, CTRL1_ID) // + .setControllersIds(CTRL2_ID, CTRL1_ID, "") // .build()) - .next(new TestCase()); + .next(new TestCase()) // + .deactivate(); assertEquals(// Arrays.asList(CTRL2_ID, CTRL1_ID, CTRL0_ID, CTRL3_ID, CTRL4_ID), // getControllerIds(sut)); } + @Test + public void testOnlyAlphabeticalOrdering() throws Exception { + final var controllerIds = new ArrayList<>(List.of(// + "ctrlController1", // + "a", // + "aa", // + "aA", // + "ab", // + "aB", // + "A", // + "0", // + "1", // + "0controller", // + "0Controller", // + "bla", // + "controller0", // + "controller1", // + "dontroller0", // + "dontroller1", // + "d0", // + "D0", // + "Z", // + "z")); + final var sut = new SchedulerAllAlphabeticallyImpl(); + final var test = new ComponentTest(sut); // + controllerIds.forEach(controllerId -> test.addComponent(new DummyController(controllerId))); + test // + .addReference("componentManager", new DummyComponentManager()) // + .activate(MyConfig.create() // + .setId(SCHEDULER_ID) // + .setControllersIds() // + .build()) // + .next(new TestCase()); + + Collections.sort(controllerIds); + + assertEquals(controllerIds, getControllerIds(sut)); + } + private static List getControllerIds(Scheduler scheduler) throws OpenemsNamedException { return scheduler.getControllers().stream() // .toList();