Skip to content

Commit

Permalink
Added rule condition for not a holiday (openhab#2043)
Browse files Browse the repository at this point in the history
Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
GitOrigin-RevId: 65218f8
  • Loading branch information
soenkekueper authored and splatch committed Jul 11, 2023
1 parent c185adb commit 0c06654
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class EphemerisModuleHandlerFactory extends BaseModuleHandlerFactory impl
private final Logger logger = LoggerFactory.getLogger(EphemerisModuleHandlerFactory.class);

private static final Collection<String> TYPES = List.of(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID,
EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID, EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID,
EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID);
EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID, EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID,
EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID, EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID);

private final EphemerisManager ephemerisManager;

Expand Down Expand Up @@ -73,6 +73,7 @@ public Collection<String> getTypes() {
if (module instanceof Condition) {
switch (moduleTypeUID) {
case EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID:
case EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID:
case EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID:
case EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID:
case EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class EphemerisConditionHandler extends BaseModuleHandler<Condition> implements ConditionHandler {

public static final String HOLIDAY_MODULE_TYPE_ID = "ephemeris.HolidayCondition";
public static final String NOT_HOLIDAY_MODULE_TYPE_ID = "ephemeris.NotHolidayCondition";
public static final String WEEKEND_MODULE_TYPE_ID = "ephemeris.WeekendCondition";
public static final String WEEKDAY_MODULE_TYPE_ID = "ephemeris.WeekdayCondition";
public static final String DAYSET_MODULE_TYPE_ID = "ephemeris.DaysetCondition";
Expand Down Expand Up @@ -65,6 +66,8 @@ public boolean isSatisfied(Map<String, Object> inputs) {
switch (module.getTypeUID()) {
case HOLIDAY_MODULE_TYPE_ID:
return ephemerisManager.isBankHoliday(target);
case NOT_HOLIDAY_MODULE_TYPE_ID:
return !ephemerisManager.isBankHoliday(target);
case WEEKEND_MODULE_TYPE_ID:
return ephemerisManager.isWeekend(target);
case WEEKDAY_MODULE_TYPE_ID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,34 @@ public void testFactoryCreatesModuleHandlerForWeekdayCondition() {
assertThat(handler, is(notNullValue()));
assertThat(handler, instanceOf(EphemerisConditionHandler.class));
}

@Test
public void testFactoryCreatesModuleHandlerForHolidayCondition() {
when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID);

when(moduleMock.getConfiguration()).thenReturn(new Configuration());
ModuleHandler handler = factory.internalCreate(moduleMock, "My first rule");
assertThat(handler, is(notNullValue()));
assertThat(handler, instanceOf(EphemerisConditionHandler.class));

when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("offset", 5)));
handler = factory.internalCreate(moduleMock, "My second rule");
assertThat(handler, is(notNullValue()));
assertThat(handler, instanceOf(EphemerisConditionHandler.class));
}

@Test
public void testFactoryCreatesModuleHandlerForNotHolidayCondition() {
when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID);

when(moduleMock.getConfiguration()).thenReturn(new Configuration());
ModuleHandler handler = factory.internalCreate(moduleMock, "My first rule");
assertThat(handler, is(notNullValue()));
assertThat(handler, instanceOf(EphemerisConditionHandler.class));

when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("offset", 5)));
handler = factory.internalCreate(moduleMock, "My second rule");
assertThat(handler, is(notNullValue()));
assertThat(handler, instanceOf(EphemerisConditionHandler.class));
}
}

0 comments on commit 0c06654

Please sign in to comment.