From a027b45ea7093b9212355156403dd29a2e864ae4 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Sat, 26 Nov 2022 22:00:12 +0100 Subject: [PATCH 1/6] enrich memberOf with options to just listen on items or groups --- .../jrule/internal/engine/JRuleEngine.java | 38 ++++++++++--------- .../JRuleItemChangeExecutionContext.java | 30 +++++++++++---- .../JRuleItemExecutionContext.java | 20 ++++++---- ...leItemReceivedCommandExecutionContext.java | 31 ++++++++++----- ...uleItemReceivedUpdateExecutionContext.java | 30 ++++++++++----- .../automation/jrule/rules/JRuleMemberOf.java | 37 ++++++++++++++++++ .../jrule/rules/JRuleWhenItemChange.java | 2 +- .../rules/JRuleWhenItemReceivedCommand.java | 2 +- .../rules/JRuleWhenItemReceivedUpdate.java | 2 +- .../itemchange/JRuleGroupItemChangeRules.java | 21 ++++++++-- .../itemchange/JRuleGroupItemChangeTest.java | 35 +++++++++++++++++ 11 files changed, 189 insertions(+), 59 deletions(-) create mode 100644 src/main/java/org/openhab/automation/jrule/rules/JRuleMemberOf.java diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index 1763a2a3..adc793e7 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -37,6 +37,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.eclipse.jdt.annotation.NonNull; import org.openhab.automation.jrule.exception.JRuleItemNotFoundException; +import org.openhab.automation.jrule.exception.JRuleRuntimeException; import org.openhab.automation.jrule.internal.JRuleConfig; import org.openhab.automation.jrule.internal.JRuleLog; import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleChannelExecutionContext; @@ -55,6 +56,7 @@ import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleCondition; import org.openhab.automation.jrule.rules.JRuleLogName; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.JRuleName; import org.openhab.automation.jrule.rules.JRulePrecondition; import org.openhab.automation.jrule.rules.JRuleTag; @@ -68,6 +70,7 @@ import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.things.JRuleThingStatus; import org.openhab.core.events.AbstractEvent; +import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.ItemRegistry; @@ -275,14 +278,19 @@ public static Predicate distinctByKey(Function keyExtractor } private JRuleItemExecutionContext.JRuleAdditionalItemCheckData getAdditionalCheckData(AbstractEvent event) { - return Optional.ofNullable(event instanceof ItemEvent ? ((ItemEvent) event).getItemName() : null).map(s -> { - try { - return itemRegistry.getItem(s); - } catch (ItemNotFoundException e) { - throw new IllegalStateException("this can never occur", e); - } - }).map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getGroupNames())) - .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(List.of())); + return Optional.ofNullable(event instanceof ItemEvent ? ((ItemEvent) event).getItemName() : null) + .map(s -> getItem(s)) + .map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getGroupNames().stream() + .collect(Collectors.toMap(name -> name, name -> getItem(name) instanceof GroupItem)))) + .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(Map.of())); + } + + private Item getItem(String name) { + try { + return itemRegistry.getItem(name); + } catch (ItemNotFoundException e) { + throw new JRuleRuntimeException(String.format("cannot find item: %s", name), e); + } } public boolean matchPrecondition(JRuleExecutionContext jRuleExecutionContext) { @@ -356,18 +364,14 @@ public synchronized void reset() { } public boolean watchingForItem(String itemName) { - List belongingGroups = Optional.of(itemName).map(s -> { - try { - return itemRegistry.getItem(s); - } catch (ItemNotFoundException e) { - throw new IllegalStateException("this can never occur", e); - } - }).map(Item::getGroupNames).orElse(List.of()); + List parentGroups = Optional.of(itemName).map(this::getItem).map(Item::getGroupNames).orElse(List.of()); boolean b = this.contextList.stream().filter(context -> context instanceof JRuleItemExecutionContext) .map(context -> ((JRuleItemExecutionContext) context)) - .anyMatch(context -> (context.getItemName().equals(itemName) && !context.isMemberOf()) - || (belongingGroups.contains(context.getItemName()) && context.isMemberOf())); + .anyMatch(context -> (context.getItemName().equals(itemName) + && context.getMemberOf() == JRuleMemberOf.None) + || (parentGroups.contains(context.getItemName()) + && context.getMemberOf() != JRuleMemberOf.None)); logDebug("watching for item: '{}'? -> {}", itemName, b); return b; } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java index 6621394e..9a6f576f 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java @@ -20,6 +20,7 @@ import org.openhab.automation.jrule.internal.JRuleLog; import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleEventState; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; import org.openhab.core.events.AbstractEvent; @@ -40,7 +41,7 @@ public class JRuleItemChangeExecutionContext extends JRuleItemExecutionContext { private final Optional to; public JRuleItemChangeExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, - String itemName, boolean memberOf, Optional lt, Optional lte, Optional gt, + String itemName, JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, Optional gte, Optional eq, Optional neq, List preconditionContextList, Optional from, Optional to) { super(jRule, logName, loggingTags, method, itemName, memberOf, lt, lte, gt, gte, eq, neq, @@ -58,28 +59,41 @@ public boolean match(AbstractEvent event, JRuleAdditionalCheckData checkData) { && to.map(s -> ((ItemStateChangedEvent) event).getItemState().toString().equals(s)).orElse(true))) { return false; } - if (!isMemberOf() && ((ItemStateChangedEvent) event).getItemName().equals(this.getItemName())) { + if (getMemberOf() == JRuleMemberOf.None + && ((ItemStateChangedEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (isMemberOf() && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName())) { - return true; + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData + && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + switch (getMemberOf()) { + case All: + return true; + case Groups: + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + case Items: + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + default: + return false; + } } return false; } @Override public JRuleEvent createJRuleEvent(AbstractEvent event) { + final String itemName; final String memberName; - if (isMemberOf()) { - memberName = ((ItemEvent) event).getItemName(); + if (getMemberOf() != JRuleMemberOf.None) { + itemName = ((ItemEvent) event).getItemName(); + memberName = null; } else { + itemName = this.getItemName(); memberName = event instanceof GroupItemStateChangedEvent ? ((GroupItemStateChangedEvent) event).getMemberName() : null; } - return new JRuleItemEvent(this.getItemName(), memberName, + return new JRuleItemEvent(itemName, memberName, new JRuleEventState(((ItemStateChangedEvent) event).getItemState().toString()), new JRuleEventState(((ItemStateChangedEvent) event).getOldItemState().toString())); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java index 856d586b..bc300ea2 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java @@ -14,10 +14,12 @@ import java.lang.reflect.Method; import java.util.List; +import java.util.Map; import java.util.Optional; import org.apache.commons.lang3.math.NumberUtils; import org.openhab.automation.jrule.rules.JRule; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.core.library.types.QuantityType; /** @@ -27,7 +29,7 @@ */ public abstract class JRuleItemExecutionContext extends JRuleExecutionContext { protected final String itemName; - protected final boolean memberOf; + protected final JRuleMemberOf memberOf; protected final Optional gt; protected final Optional gte; protected final Optional lt; @@ -36,8 +38,9 @@ public abstract class JRuleItemExecutionContext extends JRuleExecutionContext { protected final Optional neq; public JRuleItemExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, String itemName, - boolean memberOf, Optional lt, Optional lte, Optional gt, Optional gte, - Optional eq, Optional neq, List preconditionContextList) { + JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, + Optional gte, Optional eq, Optional neq, + List preconditionContextList) { super(jRule, logName, loggingTags, method, preconditionContextList); this.itemName = itemName; this.memberOf = memberOf; @@ -103,18 +106,21 @@ public Optional getNeq() { return neq; } - public boolean isMemberOf() { + public JRuleMemberOf getMemberOf() { return memberOf; } public static class JRuleAdditionalItemCheckData extends JRuleAdditionalCheckData { - private final List belongingGroups; + /** + * Itemname, IsGroup + */ + private final Map belongingGroups; - public JRuleAdditionalItemCheckData(List belongingGroups) { + public JRuleAdditionalItemCheckData(Map belongingGroups) { this.belongingGroups = belongingGroups; } - public List getBelongingGroups() { + public Map getBelongingGroups() { return belongingGroups; } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java index 698ed8bf..ead5838b 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java @@ -20,6 +20,7 @@ import org.openhab.automation.jrule.internal.JRuleLog; import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleEventState; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; import org.openhab.core.events.AbstractEvent; @@ -38,7 +39,7 @@ public class JRuleItemReceivedCommandExecutionContext extends JRuleItemExecution protected final Optional command; public JRuleItemReceivedCommandExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, - String itemName, boolean memberOf, Optional lt, Optional lte, Optional gt, + String itemName, JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, Optional gte, Optional eq, Optional neq, List preconditionContextList, Optional command) { super(jRule, logName, loggingTags, method, itemName, memberOf, lt, lte, gt, gte, eq, neq, @@ -56,26 +57,36 @@ public boolean match(AbstractEvent event, JRuleAdditionalCheckData checkData) { return false; } - if (!isMemberOf() && ((ItemCommandEvent) event).getItemName().equals(this.getItemName())) { + if (getMemberOf() == JRuleMemberOf.None + && ((ItemCommandEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (isMemberOf() && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName())) { - return true; + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData + && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + switch (getMemberOf()) { + case All: + return true; + case Groups: + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + case Items: + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + default: + return false; + } } return false; } @Override public JRuleEvent createJRuleEvent(AbstractEvent event) { - final String memberName; - if (isMemberOf()) { - memberName = ((ItemEvent) event).getItemName(); + final String itemName; + if (getMemberOf() != JRuleMemberOf.None) { + itemName = ((ItemEvent) event).getItemName(); } else { - memberName = null; + itemName = this.getItemName(); } - return new JRuleItemEvent(this.getItemName(), memberName, + return new JRuleItemEvent(itemName, null, new JRuleEventState(((ItemCommandEvent) event).getItemCommand().toString()), null); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java index ea7b12d6..d1173ba8 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java @@ -20,6 +20,7 @@ import org.openhab.automation.jrule.internal.JRuleLog; import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleEventState; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; import org.openhab.core.events.AbstractEvent; @@ -38,7 +39,7 @@ public class JRuleItemReceivedUpdateExecutionContext extends JRuleItemExecutionC private final Optional state; public JRuleItemReceivedUpdateExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, - String itemName, boolean memberOf, Optional lt, Optional lte, Optional gt, + String itemName, JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, Optional gte, Optional eq, Optional neq, List preconditionContextList, Optional state) { super(jRule, logName, loggingTags, method, itemName, memberOf, lt, lte, gt, gte, eq, neq, @@ -55,26 +56,35 @@ public boolean match(AbstractEvent event, JRuleAdditionalCheckData checkData) { && state.map(s -> ((ItemStateEvent) event).getItemState().toString().equals(s)).orElse(true))) { return false; } - if (!isMemberOf() && ((ItemStateEvent) event).getItemName().equals(this.getItemName())) { + if (getMemberOf() == JRuleMemberOf.None && ((ItemStateEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (isMemberOf() && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName())) { - return true; + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData + && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + switch (getMemberOf()) { + case All: + return true; + case Groups: + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + case Items: + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + default: + return false; + } } return false; } @Override public JRuleEvent createJRuleEvent(AbstractEvent event) { - final String memberName; - if (isMemberOf()) { - memberName = ((ItemEvent) event).getItemName(); + final String itemName; + if (getMemberOf() != JRuleMemberOf.None) { + itemName = ((ItemEvent) event).getItemName(); } else { - memberName = null; + itemName = this.getItemName(); } - return new JRuleItemEvent(this.getItemName(), memberName, + return new JRuleItemEvent(itemName, null, new JRuleEventState(((ItemStateEvent) event).getItemState().toString()), null); } diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleMemberOf.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleMemberOf.java new file mode 100644 index 00000000..54133ead --- /dev/null +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleMemberOf.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.automation.jrule.rules; + +/** + * The {@link JRuleMemberOf} + * + * @author Robert Delbrück + */ +public enum JRuleMemberOf { + /** + * Not using memberOf + */ + None, + /** + * Listen on all child items + */ + All, + /** + * Just listen on child groups + */ + Groups, + /** + * Just listen on concrete Items (without groups) + */ + Items +} diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemChange.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemChange.java index 89765319..0388552a 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemChange.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemChange.java @@ -33,7 +33,7 @@ String to() default ""; - boolean memberOf() default false; + JRuleMemberOf memberOf() default JRuleMemberOf.None; JRuleCondition condition() default @JRuleCondition; } diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedCommand.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedCommand.java index 7b4006e2..bdb1396c 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedCommand.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedCommand.java @@ -31,7 +31,7 @@ String command() default ""; - boolean memberOf() default false; + JRuleMemberOf memberOf() default JRuleMemberOf.None; JRuleCondition condition() default @JRuleCondition; } diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedUpdate.java b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedUpdate.java index f972a302..2f33be45 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedUpdate.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRuleWhenItemReceivedUpdate.java @@ -31,7 +31,7 @@ String state() default ""; - boolean memberOf() default false; + JRuleMemberOf memberOf() default JRuleMemberOf.None; JRuleCondition condition() default @JRuleCondition; } diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java index 75031bab..4553f8df 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java @@ -13,6 +13,7 @@ package org.openhab.binding.jrule.internal.triggers.itemchange; import org.openhab.automation.jrule.rules.JRule; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.JRuleName; import org.openhab.automation.jrule.rules.JRuleWhenItemChange; import org.openhab.automation.jrule.rules.event.JRuleEvent; @@ -26,27 +27,39 @@ public class JRuleGroupItemChangeRules extends JRule { public static final String GROUP_ITEM = "group_item"; + public static final String GROUP_ITEM_JUST_ITEMS = "group_item_just_items"; + public static final String GROUP_ITEM_JUST_GROUPS = "group_item_just_groups"; public static final String GROUP_ITEM_FROM = "group_item_from"; public static final String GROUP_ITEM_TO = "group_item_to"; public static final String GROUP_ITEM_FROM_TO = "group_item_from_to"; @JRuleName("Test JRuleWhenGroupItemChange") - @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = true) + @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = JRuleMemberOf.All) public void groupItemChange(JRuleEvent event) { } + @JRuleName("Test JRuleWhenGroupItemChange/JustItems") + @JRuleWhenItemChange(item = GROUP_ITEM_JUST_ITEMS, memberOf = JRuleMemberOf.Items) + public void groupItemChangeJustItems(JRuleEvent event) { + } + + @JRuleName("Test JRuleWhenGroupItemChange/JustGroups") + @JRuleWhenItemChange(item = GROUP_ITEM_JUST_GROUPS, memberOf = JRuleMemberOf.Groups) + public void groupItemChangeJustGroups(JRuleEvent event) { + } + @JRuleName("Test JRuleWhenGroupItemChange/from") - @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = true) + @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = JRuleMemberOf.All) public void groupItemChangeFrom(JRuleEvent event) { } @JRuleName("Test JRuleWhenGroupItemChange/to") - @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = true) + @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = JRuleMemberOf.All) public void groupItemChangeTo(JRuleEvent event) { } @JRuleName("Test JRuleWhenGroupItemChange/from/to") - @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = true) + @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = JRuleMemberOf.All) public void groupItemChangeFromTo(JRuleEvent event) { } } diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java index 21c05e0b..dd860b63 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java @@ -25,6 +25,7 @@ import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.binding.jrule.internal.rules.JRuleAbstractTest; import org.openhab.core.events.Event; +import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.events.ItemEventFactory; @@ -72,6 +73,40 @@ public void testItemChange_no_from_to() throws ItemNotFoundException { verify(rule, times(1)).groupItemChange(Mockito.any(JRuleEvent.class)); } + @Test + public void testItemChangeJustItems() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()) + .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS)); + return stringItem; + }); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); + verify(rule, times(0)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); + verify(rule, times(1)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); + } + + @Test + public void testItemChangeJustGroups() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()) + .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); + return stringItem; + }); + Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)) + .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); + verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); + verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); + } + @Test public void testItemChange_from() throws ItemNotFoundException { Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { From 1547e7e84a3dcb56ea740358b689651ca78b5934 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Wed, 30 Nov 2022 17:35:49 +0100 Subject: [PATCH 2/6] fill memberName as well as itemName --- .../excutioncontext/JRuleItemChangeExecutionContext.java | 2 +- .../JRuleItemReceivedCommandExecutionContext.java | 5 ++++- .../JRuleItemReceivedUpdateExecutionContext.java | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java index 9a6f576f..105fad0b 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java @@ -85,7 +85,7 @@ public JRuleEvent createJRuleEvent(AbstractEvent event) { final String memberName; if (getMemberOf() != JRuleMemberOf.None) { itemName = ((ItemEvent) event).getItemName(); - memberName = null; + memberName = ((ItemEvent) event).getItemName(); } else { itemName = this.getItemName(); memberName = event instanceof GroupItemStateChangedEvent diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java index ead5838b..f0686a28 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java @@ -80,13 +80,16 @@ public boolean match(AbstractEvent event, JRuleAdditionalCheckData checkData) { @Override public JRuleEvent createJRuleEvent(AbstractEvent event) { final String itemName; + final String memberName; if (getMemberOf() != JRuleMemberOf.None) { + memberName = ((ItemEvent) event).getItemName(); itemName = ((ItemEvent) event).getItemName(); } else { + memberName = null; itemName = this.getItemName(); } - return new JRuleItemEvent(itemName, null, + return new JRuleItemEvent(itemName, memberName, new JRuleEventState(((ItemCommandEvent) event).getItemCommand().toString()), null); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java index d1173ba8..dd99fca3 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java @@ -78,13 +78,16 @@ public boolean match(AbstractEvent event, JRuleAdditionalCheckData checkData) { @Override public JRuleEvent createJRuleEvent(AbstractEvent event) { final String itemName; + final String memberName; if (getMemberOf() != JRuleMemberOf.None) { + memberName = ((ItemEvent) event).getItemName(); itemName = ((ItemEvent) event).getItemName(); } else { + memberName = null; itemName = this.getItemName(); } - return new JRuleItemEvent(itemName, null, + return new JRuleItemEvent(itemName, memberName, new JRuleEventState(((ItemStateEvent) event).getItemState().toString()), null); } From b961db81f928a00f6e65c761c0aea6ed39906da8 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Wed, 28 Dec 2022 15:08:23 +0100 Subject: [PATCH 3/6] merge --- .../jrule/internal/engine/JRuleEngine.java | 14 +- .../JRuleItemChangeExecutionContext.java | 16 +- .../JRuleItemExecutionContext.java | 9 +- ...leItemReceivedCommandExecutionContext.java | 18 +- ...uleItemReceivedUpdateExecutionContext.java | 14 +- .../jrule/rules/user/TestRules.java | 7 +- .../itemchange/JRuleGroupItemChangeRules.java | 46 ++--- .../itemchange/JRuleGroupItemChangeTest.java | 174 +++++++++--------- 8 files changed, 148 insertions(+), 150 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index 737b0ee8..eb13108a 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -70,7 +70,6 @@ import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.things.JRuleThingStatus; import org.openhab.core.events.AbstractEvent; -import org.openhab.core.items.GroupItem; import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.ItemRegistry; @@ -265,11 +264,14 @@ public static Predicate distinctByKey(Function keyExtractor } private JRuleItemExecutionContext.JRuleAdditionalItemCheckData getAdditionalCheckData(AbstractEvent event) { - return Optional.ofNullable(event instanceof ItemEvent ? ((ItemEvent) event).getItemName() : null) - .map(s -> getItem(s)) - .map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getGroupNames().stream() - .collect(Collectors.toMap(name -> name, name -> getItem(name) instanceof GroupItem)))) - .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(Map.of())); + return Optional.ofNullable(event instanceof ItemEvent ? ((ItemEvent) event).getItemName() : null).map(s -> { + try { + return itemRegistry.getItem(s); + } catch (ItemNotFoundException e) { + throw new IllegalStateException("this can never occur", e); + } + }).map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getGroupNames())) + .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(List.of())); } private Item getItem(String name) { diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java index fe7131b3..d3ef56a3 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java @@ -19,7 +19,6 @@ import org.openhab.automation.jrule.internal.handler.JRuleEventHandler; import org.openhab.automation.jrule.rules.JRule; -import org.openhab.automation.jrule.rules.JRuleEventState; import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; @@ -69,15 +68,15 @@ && matchCondition(((ItemStateChangedEvent) event).getItemState().toString(), && ((ItemStateChangedEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() + .contains(this.getItemName()); default: return false; } @@ -89,7 +88,8 @@ && matchCondition(((ItemStateChangedEvent) event).getItemState().toString(), public JRuleEvent createJRuleEvent(AbstractEvent event) { final String itemName; final String memberName; - if (isMemberOf()) { + if (getMemberOf() != JRuleMemberOf.None) { + itemName = this.getItemName(); memberName = ((ItemEvent) event).getItemName(); } else { itemName = this.getItemName(); @@ -99,8 +99,8 @@ public JRuleEvent createJRuleEvent(AbstractEvent event) { } return new JRuleItemEvent(itemName, memberName, - new JRuleEventState(((ItemStateChangedEvent) event).getItemState().toString()), - new JRuleEventState(((ItemStateChangedEvent) event).getOldItemState().toString())); + JRuleEventHandler.get().toValue(((ItemStateChangedEvent) event).getItemState()), + JRuleEventHandler.get().toValue(((ItemStateChangedEvent) event).getOldItemState())); } @Override diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java index 2f4ca2ad..3a8a98b9 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java @@ -14,14 +14,13 @@ import java.lang.reflect.Method; import java.util.List; -import java.util.Map; import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.openhab.automation.jrule.rules.JRule; -import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.JRuleCondition; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.core.library.types.QuantityType; /** @@ -31,11 +30,11 @@ */ public abstract class JRuleItemExecutionContext extends JRuleExecutionContext { protected final String itemName; - protected final boolean memberOf; + protected final JRuleMemberOf memberOf; protected final Optional conditionContext; public JRuleItemExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, String itemName, - boolean memberOf, Optional conditionContext, + JRuleMemberOf memberOf, Optional conditionContext, List preconditionContextList) { super(jRule, logName, loggingTags, method, preconditionContextList); this.itemName = itemName; @@ -51,7 +50,7 @@ public boolean matchCondition(String state, String previousState) { return conditionContext.map(c -> c.matchCondition(state)).orElse(true); } - public boolean isMemberOf() { + public JRuleMemberOf getMemberOf() { return memberOf; } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java index f43606b1..535d0903 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java @@ -19,13 +19,13 @@ import org.openhab.automation.jrule.internal.handler.JRuleEventHandler; import org.openhab.automation.jrule.rules.JRule; -import org.openhab.automation.jrule.rules.JRuleEventState; import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; import org.openhab.core.events.AbstractEvent; import org.openhab.core.items.events.ItemCommandEvent; import org.openhab.core.items.events.ItemEvent; +import org.openhab.core.items.events.ItemStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,9 +39,7 @@ public class JRuleItemReceivedCommandExecutionContext extends JRuleItemExecution protected final Optional command; public JRuleItemReceivedCommandExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, - String itemName, boolean memberOf, Optional conditionContext, - String itemName, JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, - Optional gte, Optional eq, Optional neq, + String itemName, JRuleMemberOf memberOf, Optional conditionContext, List preconditionContextList, Optional command) { super(jRule, logName, loggingTags, method, itemName, memberOf, conditionContext, preconditionContextList); this.command = command; @@ -59,15 +57,15 @@ && matchCondition(((ItemCommandEvent) event).getItemCommand().toString(), null) && ((ItemCommandEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() + .contains(this.getItemName()); default: return false; } @@ -87,10 +85,8 @@ public JRuleEvent createJRuleEvent(AbstractEvent event) { itemName = this.getItemName(); } - return new JRuleItemEvent(this.getItemName(), memberName, - JRuleEventHandler.get().toValue(((ItemCommandEvent) event).getItemCommand()), null); return new JRuleItemEvent(itemName, memberName, - new JRuleEventState(((ItemCommandEvent) event).getItemCommand().toString()), null); + JRuleEventHandler.get().toValue(((ItemStateEvent) event).getItemState()), null); } @Override diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java index 5771540e..223f9e73 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java @@ -19,7 +19,6 @@ import org.openhab.automation.jrule.internal.handler.JRuleEventHandler; import org.openhab.automation.jrule.rules.JRule; -import org.openhab.automation.jrule.rules.JRuleEventState; import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; @@ -39,8 +38,7 @@ public class JRuleItemReceivedUpdateExecutionContext extends JRuleItemExecutionC private final Optional state; public JRuleItemReceivedUpdateExecutionContext(JRule jRule, String logName, String[] loggingTags, Method method, - String itemName, JRuleMemberOf memberOf, Optional lt, Optional lte, Optional gt, - Optional gte, Optional eq, Optional neq, + String itemName, JRuleMemberOf memberOf, Optional conditionContext, List preconditionContextList, Optional state) { super(jRule, logName, loggingTags, method, itemName, memberOf, conditionContext, preconditionContextList); this.state = state; @@ -56,15 +54,15 @@ && matchCondition(((ItemStateEvent) event).getItemState().toString(), null) if (getMemberOf() == JRuleMemberOf.None && ((ItemStateEvent) event).getItemName().equals(this.getItemName())) { return true; } - if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData - && ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().containsKey(this.getItemName())) { + if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().get(this.getItemName()); + return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() + .contains(this.getItemName()); default: return false; } @@ -85,7 +83,7 @@ public JRuleEvent createJRuleEvent(AbstractEvent event) { } return new JRuleItemEvent(itemName, memberName, - new JRuleEventState(((ItemStateEvent) event).getItemState().toString()), null); + JRuleEventHandler.get().toValue(((ItemStateEvent) event).getItemState()), null); } @Override diff --git a/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java b/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java index 02b9f646..7fb0a9da 100755 --- a/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java +++ b/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java @@ -39,6 +39,7 @@ import org.openhab.automation.jrule.items.JRuleSwitchItem; import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleCondition; +import org.openhab.automation.jrule.rules.JRuleMemberOf; import org.openhab.automation.jrule.rules.JRuleName; import org.openhab.automation.jrule.rules.JRulePrecondition; import org.openhab.automation.jrule.rules.JRuleWhenChannelTrigger; @@ -178,20 +179,20 @@ public void mqttThingChangedToOffline(JRuleThingEvent event) { } @JRuleName(NAME_MEMBER_OF_GROUP_RECEIVED_COMMAND) - @JRuleWhenItemReceivedCommand(item = ITEM_SWITCH_GROUP, memberOf = true) + @JRuleWhenItemReceivedCommand(item = ITEM_SWITCH_GROUP, memberOf = JRuleMemberOf.All) public synchronized void memberOfGroupReceivedCommand(JRuleItemEvent event) { logInfo("Member of Group ({}) received command", event.getMemberName()); } @JRuleName(NAME_MEMBER_OF_GROUP_RECEIVED_UPDATE) - @JRuleWhenItemReceivedUpdate(item = ITEM_SWITCH_GROUP, memberOf = true) + @JRuleWhenItemReceivedUpdate(item = ITEM_SWITCH_GROUP, memberOf = JRuleMemberOf.All) public synchronized void memberOfGroupReceivedUpdate(JRuleItemEvent event) { final String memberThatChangedStatus = event.getMemberName(); logInfo("Member of Group ({}) received update", event.getMemberName()); } @JRuleName(NAME_MEMBER_OF_GROUP_CHANGED) - @JRuleWhenItemChange(item = ITEM_SWITCH_GROUP, memberOf = true) + @JRuleWhenItemChange(item = ITEM_SWITCH_GROUP, memberOf = JRuleMemberOf.All) public synchronized void memberOfGroupChanged(JRuleItemEvent event) { final String memberThatChangedStatus = event.getMemberName(); logInfo("Member of Group ({}) changed", event.getMemberName()); diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java index 4553f8df..803862cc 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java @@ -33,33 +33,33 @@ public class JRuleGroupItemChangeRules extends JRule { public static final String GROUP_ITEM_TO = "group_item_to"; public static final String GROUP_ITEM_FROM_TO = "group_item_from_to"; - @JRuleName("Test JRuleWhenGroupItemChange") - @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = JRuleMemberOf.All) - public void groupItemChange(JRuleEvent event) { - } +// @JRuleName("Test JRuleWhenGroupItemChange") +// @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = JRuleMemberOf.All) +// public void groupItemChange(JRuleEvent event) { +// } @JRuleName("Test JRuleWhenGroupItemChange/JustItems") @JRuleWhenItemChange(item = GROUP_ITEM_JUST_ITEMS, memberOf = JRuleMemberOf.Items) public void groupItemChangeJustItems(JRuleEvent event) { } - @JRuleName("Test JRuleWhenGroupItemChange/JustGroups") - @JRuleWhenItemChange(item = GROUP_ITEM_JUST_GROUPS, memberOf = JRuleMemberOf.Groups) - public void groupItemChangeJustGroups(JRuleEvent event) { - } - - @JRuleName("Test JRuleWhenGroupItemChange/from") - @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = JRuleMemberOf.All) - public void groupItemChangeFrom(JRuleEvent event) { - } - - @JRuleName("Test JRuleWhenGroupItemChange/to") - @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = JRuleMemberOf.All) - public void groupItemChangeTo(JRuleEvent event) { - } - - @JRuleName("Test JRuleWhenGroupItemChange/from/to") - @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = JRuleMemberOf.All) - public void groupItemChangeFromTo(JRuleEvent event) { - } +// @JRuleName("Test JRuleWhenGroupItemChange/JustGroups") +// @JRuleWhenItemChange(item = GROUP_ITEM_JUST_GROUPS, memberOf = JRuleMemberOf.Groups) +// public void groupItemChangeJustGroups(JRuleEvent event) { +// } +// +// @JRuleName("Test JRuleWhenGroupItemChange/from") +// @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = JRuleMemberOf.All) +// public void groupItemChangeFrom(JRuleEvent event) { +// } +// +// @JRuleName("Test JRuleWhenGroupItemChange/to") +// @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = JRuleMemberOf.All) +// public void groupItemChangeTo(JRuleEvent event) { +// } +// +// @JRuleName("Test JRuleWhenGroupItemChange/from/to") +// @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = JRuleMemberOf.All) +// public void groupItemChangeFromTo(JRuleEvent event) { +// } } diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java index dd860b63..4c905ece 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java @@ -50,28 +50,28 @@ public void initTestClass() throws ItemNotFoundException { .then((Answer) invocationOnMock -> new StringItem(invocationOnMock.getArgument(0))); } - @Test - public void testItemChange_selfGroup() { - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - // Only last event should trigger rule method - fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.GROUP_ITEM, "2", "1"))); - verify(rule, times(0)).groupItemChange(Mockito.any(JRuleEvent.class)); - } - - @Test - public void testItemChange_no_from_to() throws ItemNotFoundException { - Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { - StringItem stringItem = Mockito.mock(StringItem.class); - Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); - Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM)); - return stringItem; - }); - - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - // Only last event should trigger rule method - fireEvents(List.of(itemChangeEvent(OTHER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); - verify(rule, times(1)).groupItemChange(Mockito.any(JRuleEvent.class)); - } +// @Test +// public void testItemChange_selfGroup() { +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// // Only last event should trigger rule method +// fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.GROUP_ITEM, "2", "1"))); +// verify(rule, times(0)).groupItemChange(Mockito.any(JRuleEvent.class)); +// } +// +// @Test +// public void testItemChange_no_from_to() throws ItemNotFoundException { +// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { +// StringItem stringItem = Mockito.mock(StringItem.class); +// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); +// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM)); +// return stringItem; +// }); +// +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// // Only last event should trigger rule method +// fireEvents(List.of(itemChangeEvent(OTHER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); +// verify(rule, times(1)).groupItemChange(Mockito.any(JRuleEvent.class)); +// } @Test public void testItemChangeJustItems() throws ItemNotFoundException { @@ -82,76 +82,78 @@ public void testItemChangeJustItems() throws ItemNotFoundException { .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS)); return stringItem; }); + Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS)) + .thenAnswer(invocationOnMock -> new StringItem("Any")); JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); - verify(rule, times(0)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); +// verify(rule, times(0)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); verify(rule, times(1)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); } - @Test - public void testItemChangeJustGroups() throws ItemNotFoundException { - Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { - StringItem stringItem = Mockito.mock(StringItem.class); - Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); - Mockito.when(stringItem.getGroupNames()) - .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); - return stringItem; - }); - Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)) - .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); - - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); - verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); - verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); - } - - @Test - public void testItemChange_from() throws ItemNotFoundException { - Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { - StringItem stringItem = Mockito.mock(StringItem.class); - Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); - Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM)); - return stringItem; - }); - - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - // Only last event should trigger rule method - fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "1", "2"))); - verify(rule, times(1)).groupItemChangeFrom(Mockito.any(JRuleEvent.class)); - } - - @Test - public void testItemChange_to() throws ItemNotFoundException { - Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { - StringItem stringItem = Mockito.mock(StringItem.class); - Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); - Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_TO)); - return stringItem; - }); - - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - // Only last event should trigger rule method - fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "1", "2"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); - verify(rule, times(1)).groupItemChangeTo(Mockito.any(JRuleEvent.class)); - } - - @Test - public void testItemChange_from_to() throws ItemNotFoundException { - Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { - StringItem stringItem = Mockito.mock(StringItem.class); - Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); - Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM_TO)); - return stringItem; - }); - - JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - // Only last event should trigger rule method - fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "3", "2"), - itemChangeEvent(MEMBER_ITEM, "1", "2"))); - verify(rule, times(1)).groupItemChangeFromTo(Mockito.any(JRuleEvent.class)); - } +// @Test +// public void testItemChangeJustGroups() throws ItemNotFoundException { +// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { +// StringItem stringItem = Mockito.mock(StringItem.class); +// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); +// Mockito.when(stringItem.getGroupNames()) +// .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); +// return stringItem; +// }); +// Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)) +// .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); +// +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); +// verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); +// verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); +// } +// +// @Test +// public void testItemChange_from() throws ItemNotFoundException { +// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { +// StringItem stringItem = Mockito.mock(StringItem.class); +// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); +// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM)); +// return stringItem; +// }); +// +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// // Only last event should trigger rule method +// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "1", "2"))); +// verify(rule, times(1)).groupItemChangeFrom(Mockito.any(JRuleEvent.class)); +// } +// +// @Test +// public void testItemChange_to() throws ItemNotFoundException { +// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { +// StringItem stringItem = Mockito.mock(StringItem.class); +// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); +// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_TO)); +// return stringItem; +// }); +// +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// // Only last event should trigger rule method +// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "1", "2"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); +// verify(rule, times(1)).groupItemChangeTo(Mockito.any(JRuleEvent.class)); +// } +// +// @Test +// public void testItemChange_from_to() throws ItemNotFoundException { +// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { +// StringItem stringItem = Mockito.mock(StringItem.class); +// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); +// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM_TO)); +// return stringItem; +// }); +// +// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); +// // Only last event should trigger rule method +// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "3", "2"), +// itemChangeEvent(MEMBER_ITEM, "1", "2"))); +// verify(rule, times(1)).groupItemChangeFromTo(Mockito.any(JRuleEvent.class)); +// } // Syntactic sugar private Event itemChangeEvent(String item, String from, String to) { From c8d3960a53194e42fdad328c14b7cae4df330c5b Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Wed, 11 Jan 2023 12:50:03 +0100 Subject: [PATCH 4/6] fix memberOf and added IT tests --- .../jrule/internal/engine/JRuleEngine.java | 9 +- .../JRuleItemChangeExecutionContext.java | 6 +- .../JRuleItemExecutionContext.java | 10 +- ...leItemReceivedCommandExecutionContext.java | 9 +- ...uleItemReceivedUpdateExecutionContext.java | 6 +- .../jrule/rules/integration_test/ITJRule.java | 14 ++ .../jrule/rules/user/TestRules.java | 24 ++- .../itemchange/JRuleGroupItemChangeRules.java | 47 ++--- .../itemchange/JRuleGroupItemChangeTest.java | 183 +++++++++--------- .../resources/docker/conf/items/default.items | 7 + 10 files changed, 185 insertions(+), 130 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index 462122d6..d2663823 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -73,9 +73,7 @@ import org.openhab.automation.jrule.rules.event.JRuleEvent; import org.openhab.automation.jrule.things.JRuleThingStatus; import org.openhab.core.events.AbstractEvent; -import org.openhab.core.items.Item; -import org.openhab.core.items.ItemNotFoundException; -import org.openhab.core.items.ItemRegistry; +import org.openhab.core.items.*; import org.openhab.core.items.events.ItemEvent; import org.openhab.core.library.types.QuantityType; import org.openhab.core.scheduler.CronScheduler; @@ -280,8 +278,9 @@ private JRuleItemExecutionContext.JRuleAdditionalItemCheckData getAdditionalChec } catch (ItemNotFoundException e) { throw new IllegalStateException("this can never occur", e); } - }).map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getGroupNames())) - .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(List.of())); + }).map(item -> new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(item.getType().equals(GroupItem.TYPE), + item.getGroupNames())) + .orElse(new JRuleItemExecutionContext.JRuleAdditionalItemCheckData(false, List.of())); } private Item getItem(String name) { diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java index 10407f38..3382fe3e 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java @@ -72,14 +72,14 @@ && matchCondition(((ItemStateChangedEvent) event).getItemState().toString(), return true; } if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { + JRuleAdditionalItemCheckData itemCheckData = (JRuleAdditionalItemCheckData) checkData; switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && itemCheckData.isGroup(); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() - .contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && !itemCheckData.isGroup(); default: return false; } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java index 87bf7ed5..e76f4532 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemExecutionContext.java @@ -56,9 +56,11 @@ public JRuleMemberOf getMemberOf() { } public static class JRuleAdditionalItemCheckData extends JRuleAdditionalCheckData { + private final boolean group; private final List belongingGroups; - public JRuleAdditionalItemCheckData(List belongingGroups) { + public JRuleAdditionalItemCheckData(boolean group, List belongingGroups) { + this.group = group; this.belongingGroups = belongingGroups; } @@ -66,9 +68,13 @@ public List getBelongingGroups() { return belongingGroups; } + public boolean isGroup() { + return group; + } + @Override public String toString() { - return "JRuleAdditionalItemCheckData{" + "belongingGroups=" + belongingGroups + '}'; + return "JRuleAdditionalItemCheckData{" + "group=" + group + ", belongingGroups=" + belongingGroups + '}'; } } diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java index 19194f93..c0bf4c2c 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedCommandExecutionContext.java @@ -26,7 +26,6 @@ import org.openhab.core.events.AbstractEvent; import org.openhab.core.items.events.ItemCommandEvent; import org.openhab.core.items.events.ItemEvent; -import org.openhab.core.items.events.ItemStateEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,14 +59,14 @@ && matchCondition(((ItemCommandEvent) event).getItemCommand().toString(), null) return true; } if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { + JRuleAdditionalItemCheckData itemCheckData = (JRuleAdditionalItemCheckData) checkData; switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && itemCheckData.isGroup(); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() - .contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && !itemCheckData.isGroup(); default: return false; } @@ -88,7 +87,7 @@ public JRuleEvent createJRuleEvent(AbstractEvent event) { } return new JRuleItemEvent(itemName, memberName, - JRuleEventHandler.get().toValue(((ItemStateEvent) event).getItemState()), null); + JRuleEventHandler.get().toValue(((ItemCommandEvent) event).getItemCommand()), null); } @Override diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java index f4be9318..c2f0b800 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemReceivedUpdateExecutionContext.java @@ -57,14 +57,14 @@ && matchCondition(((ItemStateEvent) event).getItemState().toString(), null) return true; } if (getMemberOf() != JRuleMemberOf.None && checkData instanceof JRuleAdditionalItemCheckData) { + JRuleAdditionalItemCheckData itemCheckData = (JRuleAdditionalItemCheckData) checkData; switch (getMemberOf()) { case All: return true; case Groups: - return ((JRuleAdditionalItemCheckData) checkData).getBelongingGroups().contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && itemCheckData.isGroup(); case Items: - return !((JRuleAdditionalItemCheckData) checkData).getBelongingGroups() - .contains(this.getItemName()); + return itemCheckData.getBelongingGroups().contains(this.getItemName()) && !itemCheckData.isGroup(); default: return false; } diff --git a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRule.java b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRule.java index 2282cbb3..073dbbbb 100644 --- a/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRule.java +++ b/src/test/java/org/openhab/automation/jrule/rules/integration_test/ITJRule.java @@ -103,6 +103,20 @@ public void memberOfGroupReceivedCommand() throws IOException { verifyRuleWasExecuted(TestRules.NAME_MEMBER_OF_GROUP_RECEIVED_COMMAND); } + @Test + public void triggerJustItems() throws IOException { + sendCommand(TestRules.ITEM_STRING_GROUP_MEMBER_1, "123"); + verifyRuleWasExecuted(TestRules.NAME_TRIGGER_JUST_ITEMS); + verifyRuleWasNotExecuted(TestRules.NAME_TRIGGER_JUST_GROUPS); + } + + @Test + public void triggerJustGroups() throws IOException { + sendCommand(TestRules.ITEM_STRING_GROUP_MEMBER, "234"); + verifyRuleWasExecuted(TestRules.NAME_TRIGGER_JUST_GROUPS); + verifyRuleWasNotExecuted(TestRules.NAME_TRIGGER_JUST_ITEMS); + } + @Test public void memberOfGroupReceivedUpdate() throws IOException { postUpdate(TestRules.ITEM_SWITCH_GROUP_MEMBER1, JRuleSwitchItem.ON); diff --git a/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java b/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java index 264956a3..12218612 100755 --- a/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java +++ b/src/test/java/org/openhab/automation/jrule/rules/user/TestRules.java @@ -41,6 +41,7 @@ import org.openhab.automation.jrule.items.JRuleStringItem; import org.openhab.automation.jrule.items.JRuleSwitchGroupItem; import org.openhab.automation.jrule.items.JRuleSwitchItem; +import org.openhab.automation.jrule.rules.*; import org.openhab.automation.jrule.rules.JRule; import org.openhab.automation.jrule.rules.JRuleCondition; import org.openhab.automation.jrule.rules.JRuleMemberOf; @@ -52,7 +53,6 @@ import org.openhab.automation.jrule.rules.JRuleWhenItemReceivedCommand; import org.openhab.automation.jrule.rules.JRuleWhenItemReceivedUpdate; import org.openhab.automation.jrule.rules.JRuleWhenThingTrigger; -import org.openhab.automation.jrule.rules.*; import org.openhab.automation.jrule.rules.event.JRuleChannelEvent; import org.openhab.automation.jrule.rules.event.JRuleItemEvent; import org.openhab.automation.jrule.rules.event.JRuleThingEvent; @@ -108,7 +108,11 @@ public class TestRules extends JRule { public static final String ITEM_IMAGE_TO_CAST = "Image_To_Cast"; public static final String ITEM_ROLLERSHUTTER_TO_CAST = "Rollershutter_To_Cast"; public static final String ITEM_LOCATION_TO_CAST = "Location_To_Cast"; - private static final String ITEM_NUMBER_GROUP = "Number_Group"; + public static final String ITEM_NUMBER_GROUP_MEMBER_3 = "Number_Group_Member3"; + public static final String ITEM_STRING_GROUP_MEMBER_3 = "String_Group_Member3"; + public static final String ITEM_STRING_GROUP_MEMBER_1 = "String_Group_Member1"; + public static final String ITEM_NUMBER_GROUP = "Number_Group"; + public static final String ITEM_STRING_GROUP = "String_Group"; public static final String ITEM_RULE_FROM_RULE = "Rule_From_Rule"; public static final String NAME_TRIGGER_RULE_FROM_RULE = "Trigger Rule From Rule"; public static final String ITEM_TRIGGER_RULE_FROM_RULE = "Trigger_Rule_From_Rule"; @@ -122,6 +126,10 @@ public class TestRules extends JRule { public static final String COMMAND_TIMERS = "timers"; public static final String NAME_DEBOUNCE = "debounce"; public static final String COMMAND_DEBOUNCE = "debounce"; + public static final String NAME_TRIGGER_JUST_ITEMS = "trigger just items"; + public static final String NAME_TRIGGER_JUST_GROUPS = "trigger just groups"; + public static final String ITEM_NUMBER_GROUP_MEMBER = "Number_Group_Member"; + public static final String ITEM_STRING_GROUP_MEMBER = "String_Group_Member"; @JRuleName(NAME_SWITCH_ITEM_RECEIVED_ANY_COMMAND) @JRuleWhenItemReceivedCommand(item = ITEM_RECEIVING_COMMAND_SWITCH) @@ -262,6 +270,18 @@ public void getMembersOfNumberGroup(JRuleItemEvent event) throws JRuleExecutionE .map(jRuleItem -> jRuleItem.getName() + ":" + jRuleItem.getType()).collect(Collectors.joining(", "))); } + @JRuleName(NAME_TRIGGER_JUST_ITEMS) + @JRuleWhenItemReceivedCommand(item = ITEM_STRING_GROUP, memberOf = JRuleMemberOf.Items) + public void triggerJustItems() { + logInfo("Triggered for Item"); + } + + @JRuleName(NAME_TRIGGER_JUST_GROUPS) + @JRuleWhenItemReceivedCommand(item = ITEM_STRING_GROUP, memberOf = JRuleMemberOf.Groups) + public void triggerJustGroups() { + logInfo("Triggered for Group"); + } + @JRuleName(NAME_CAST_ALL_TYPES) @JRuleWhenItemReceivedCommand(item = ITEM_CAST_ALL_TYPES_SWITCH) public void castAllTypes(JRuleItemEvent event) throws JRuleExecutionException { diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java index 803862cc..6310f8ae 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeRules.java @@ -27,39 +27,40 @@ public class JRuleGroupItemChangeRules extends JRule { public static final String GROUP_ITEM = "group_item"; + public static final String SUB_GROUP_ITEM = "sub_group_item"; public static final String GROUP_ITEM_JUST_ITEMS = "group_item_just_items"; public static final String GROUP_ITEM_JUST_GROUPS = "group_item_just_groups"; public static final String GROUP_ITEM_FROM = "group_item_from"; public static final String GROUP_ITEM_TO = "group_item_to"; public static final String GROUP_ITEM_FROM_TO = "group_item_from_to"; -// @JRuleName("Test JRuleWhenGroupItemChange") -// @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = JRuleMemberOf.All) -// public void groupItemChange(JRuleEvent event) { -// } + @JRuleName("Test JRuleWhenGroupItemChange") + @JRuleWhenItemChange(item = GROUP_ITEM, memberOf = JRuleMemberOf.All) + public void groupItemChange(JRuleEvent event) { + } @JRuleName("Test JRuleWhenGroupItemChange/JustItems") @JRuleWhenItemChange(item = GROUP_ITEM_JUST_ITEMS, memberOf = JRuleMemberOf.Items) public void groupItemChangeJustItems(JRuleEvent event) { } -// @JRuleName("Test JRuleWhenGroupItemChange/JustGroups") -// @JRuleWhenItemChange(item = GROUP_ITEM_JUST_GROUPS, memberOf = JRuleMemberOf.Groups) -// public void groupItemChangeJustGroups(JRuleEvent event) { -// } -// -// @JRuleName("Test JRuleWhenGroupItemChange/from") -// @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = JRuleMemberOf.All) -// public void groupItemChangeFrom(JRuleEvent event) { -// } -// -// @JRuleName("Test JRuleWhenGroupItemChange/to") -// @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = JRuleMemberOf.All) -// public void groupItemChangeTo(JRuleEvent event) { -// } -// -// @JRuleName("Test JRuleWhenGroupItemChange/from/to") -// @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = JRuleMemberOf.All) -// public void groupItemChangeFromTo(JRuleEvent event) { -// } + @JRuleName("Test JRuleWhenGroupItemChange/JustGroups") + @JRuleWhenItemChange(item = GROUP_ITEM_JUST_GROUPS, memberOf = JRuleMemberOf.Groups) + public void groupItemChangeJustGroups(JRuleEvent event) { + } + + @JRuleName("Test JRuleWhenGroupItemChange/from") + @JRuleWhenItemChange(item = GROUP_ITEM_FROM, from = "1", memberOf = JRuleMemberOf.All) + public void groupItemChangeFrom(JRuleEvent event) { + } + + @JRuleName("Test JRuleWhenGroupItemChange/to") + @JRuleWhenItemChange(item = GROUP_ITEM_TO, to = "1", memberOf = JRuleMemberOf.All) + public void groupItemChangeTo(JRuleEvent event) { + } + + @JRuleName("Test JRuleWhenGroupItemChange/from/to") + @JRuleWhenItemChange(item = GROUP_ITEM_FROM_TO, from = "1", to = "2", memberOf = JRuleMemberOf.All) + public void groupItemChangeFromTo(JRuleEvent event) { + } } diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java index 4c905ece..5641933d 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java @@ -29,6 +29,7 @@ import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.events.ItemEventFactory; +import org.openhab.core.library.CoreItemFactory; import org.openhab.core.library.items.StringItem; import org.openhab.core.library.types.StringType; @@ -50,28 +51,29 @@ public void initTestClass() throws ItemNotFoundException { .then((Answer) invocationOnMock -> new StringItem(invocationOnMock.getArgument(0))); } -// @Test -// public void testItemChange_selfGroup() { -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// // Only last event should trigger rule method -// fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.GROUP_ITEM, "2", "1"))); -// verify(rule, times(0)).groupItemChange(Mockito.any(JRuleEvent.class)); -// } -// -// @Test -// public void testItemChange_no_from_to() throws ItemNotFoundException { -// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { -// StringItem stringItem = Mockito.mock(StringItem.class); -// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); -// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM)); -// return stringItem; -// }); -// -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// // Only last event should trigger rule method -// fireEvents(List.of(itemChangeEvent(OTHER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); -// verify(rule, times(1)).groupItemChange(Mockito.any(JRuleEvent.class)); -// } + @Test + public void testItemChange_selfGroup() { + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + // Only last event should trigger rule method + fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.GROUP_ITEM, "2", "1"))); + verify(rule, times(0)).groupItemChange(Mockito.any(JRuleEvent.class)); + } + + @Test + public void testItemChange_no_from_to() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + // Only last event should trigger rule method + fireEvents(List.of(itemChangeEvent(OTHER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); + verify(rule, times(1)).groupItemChange(Mockito.any(JRuleEvent.class)); + } @Test public void testItemChangeJustItems() throws ItemNotFoundException { @@ -80,80 +82,87 @@ public void testItemChangeJustItems() throws ItemNotFoundException { Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); Mockito.when(stringItem.getGroupNames()) .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); return stringItem; }); Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS)) - .thenAnswer(invocationOnMock -> new StringItem("Any")); + .thenAnswer(invocationOnMock -> new GroupItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_ITEMS, + new StringItem("Any"))); JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); -// verify(rule, times(0)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); + verify(rule, times(0)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); verify(rule, times(1)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); } -// @Test -// public void testItemChangeJustGroups() throws ItemNotFoundException { -// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { -// StringItem stringItem = Mockito.mock(StringItem.class); -// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); -// Mockito.when(stringItem.getGroupNames()) -// .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); -// return stringItem; -// }); -// Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)) -// .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); -// -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"))); -// verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); -// verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); -// } -// -// @Test -// public void testItemChange_from() throws ItemNotFoundException { -// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { -// StringItem stringItem = Mockito.mock(StringItem.class); -// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); -// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM)); -// return stringItem; -// }); -// -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// // Only last event should trigger rule method -// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "1", "2"))); -// verify(rule, times(1)).groupItemChangeFrom(Mockito.any(JRuleEvent.class)); -// } -// -// @Test -// public void testItemChange_to() throws ItemNotFoundException { -// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { -// StringItem stringItem = Mockito.mock(StringItem.class); -// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); -// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_TO)); -// return stringItem; -// }); -// -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// // Only last event should trigger rule method -// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "1", "2"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); -// verify(rule, times(1)).groupItemChangeTo(Mockito.any(JRuleEvent.class)); -// } -// -// @Test -// public void testItemChange_from_to() throws ItemNotFoundException { -// Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { -// StringItem stringItem = Mockito.mock(StringItem.class); -// Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); -// Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM_TO)); -// return stringItem; -// }); -// -// JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); -// // Only last event should trigger rule method -// fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "3", "2"), -// itemChangeEvent(MEMBER_ITEM, "1", "2"))); -// verify(rule, times(1)).groupItemChangeFromTo(Mockito.any(JRuleEvent.class)); -// } + @Test + public void testItemChangeJustGroups() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.SUB_GROUP_ITEM)) + .then((Answer) invocationOnMock -> { + GroupItem groupItem = Mockito.mock(GroupItem.class); + Mockito.when(groupItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(groupItem.getGroupNames()) + .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); + Mockito.when(groupItem.getType()).thenReturn(GroupItem.TYPE); + return groupItem; + }); + Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)) + .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.SUB_GROUP_ITEM, "2", "1"))); + verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); + verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); + } + + @Test + public void testItemChange_from() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + // Only last event should trigger rule method + fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "1", "2"))); + verify(rule, times(1)).groupItemChangeFrom(Mockito.any(JRuleEvent.class)); + } + + @Test + public void testItemChange_to() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_TO)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + // Only last event should trigger rule method + fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "1", "2"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); + verify(rule, times(1)).groupItemChangeTo(Mockito.any(JRuleEvent.class)); + } + + @Test + public void testItemChange_from_to() throws ItemNotFoundException { + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_FROM_TO)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); + // Only last event should trigger rule method + fireEvents(List.of(itemChangeEvent(MEMBER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "3", "2"), + itemChangeEvent(MEMBER_ITEM, "1", "2"))); + verify(rule, times(1)).groupItemChangeFromTo(Mockito.any(JRuleEvent.class)); + } // Syntactic sugar private Event itemChangeEvent(String item, String from, String to) { diff --git a/src/test/resources/docker/conf/items/default.items b/src/test/resources/docker/conf/items/default.items index 73fd6af2..f757c216 100644 --- a/src/test/resources/docker/conf/items/default.items +++ b/src/test/resources/docker/conf/items/default.items @@ -17,6 +17,13 @@ Number Number_Group_Member1 (Number_Group) Number Number_Group_Member2 (Number_Group_Member) Number Number_Group_Member3 (Number_Group_Member) + +Group String_Group +Group String_Group_Member (String_Group) +String String_Group_Member1 (String_Group) +String String_Group_Member2 (String_Group_Member) +String String_Group_Member3 (String_Group_Member) + Switch Cast_All_Types_Switch Switch Switch_To_Cast Number Number_To_Cast From 42955b18a2ae32ecb59c42deb34e15e002d3fdb5 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Wed, 11 Jan 2023 12:56:58 +0100 Subject: [PATCH 5/6] update readme --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 53e6274b..b1b06b42 100644 --- a/README.md +++ b/README.md @@ -718,11 +718,14 @@ public void testPower(JRuleEvent event) { ## Example 35 -Use case: Want to listen on all Item events of a group (without the groupstate must change) +Use case: Want to listen on all Item events of a group (without the groupstate must change). + Alternatively you could just listen to just Group changes or (real) Item changes ```java @JRuleName("MemberOfUpdateTrigger") - @JRuleWhenItemReceivedUpdate(item = _MySwitchGroup.ITEM, memberOf = true) + @JRuleWhenItemReceivedUpdate(item = _MySwitchGroup.ITEM, memberOf = JRuleMemberOf.All) + //@JRuleWhenItemReceivedUpdate(item = _MySwitchGroup.ITEM, memberOf = JRuleMemberOf.Items) + //@JRuleWhenItemReceivedUpdate(item = _MySwitchGroup.ITEM, memberOf = JRuleMemberOf.Groups) public synchronized void memberOfUpdateTrigger(JRuleItemEvent event) { final String memberThatChangedStatus = event.getMemberName(); logInfo("Member that changed the status of the Group of switches: {}", memberThatChangedStatus); From b7ffcb408789a503cdc5a331af0e9444215024c9 Mon Sep 17 00:00:00 2001 From: querdenker2k Date: Sun, 12 Feb 2023 07:00:49 +0100 Subject: [PATCH 6/6] fix listen to ALL error --- .../JRuleItemChangeExecutionContext.java | 2 +- .../itemchange/JRuleGroupItemChangeTest.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java index 3382fe3e..86e69030 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/excutioncontext/JRuleItemChangeExecutionContext.java @@ -75,7 +75,7 @@ && matchCondition(((ItemStateChangedEvent) event).getItemState().toString(), JRuleAdditionalItemCheckData itemCheckData = (JRuleAdditionalItemCheckData) checkData; switch (getMemberOf()) { case All: - return true; + return itemCheckData.getBelongingGroups().contains(this.getItemName()); case Groups: return itemCheckData.getBelongingGroups().contains(this.getItemName()) && itemCheckData.isGroup(); case Items: diff --git a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java index 5641933d..7cb923d7 100644 --- a/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java +++ b/src/test/java/org/openhab/binding/jrule/internal/triggers/itemchange/JRuleGroupItemChangeTest.java @@ -69,6 +69,14 @@ public void testItemChange_no_from_to() throws ItemNotFoundException { return stringItem; }); + Mockito.when(itemRegistry.getItem(OTHER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()).thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_TO)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); // Only last event should trigger rule method fireEvents(List.of(itemChangeEvent(OTHER_ITEM, "2", "1"), itemChangeEvent(MEMBER_ITEM, "2", "1"))); @@ -97,6 +105,17 @@ public void testItemChangeJustItems() throws ItemNotFoundException { @Test public void testItemChangeJustGroups() throws ItemNotFoundException { + // should not trigger + Mockito.when(itemRegistry.getItem(MEMBER_ITEM)).then((Answer) invocationOnMock -> { + StringItem stringItem = Mockito.mock(StringItem.class); + Mockito.when(stringItem.getName()).thenReturn(invocationOnMock.getArgument(0)); + Mockito.when(stringItem.getGroupNames()) + .thenReturn(List.of(JRuleGroupItemChangeRules.GROUP_ITEM_JUST_GROUPS)); + Mockito.when(stringItem.getType()).thenReturn(CoreItemFactory.STRING); + return stringItem; + }); + + // should trigger Mockito.when(itemRegistry.getItem(JRuleGroupItemChangeRules.SUB_GROUP_ITEM)) .then((Answer) invocationOnMock -> { GroupItem groupItem = Mockito.mock(GroupItem.class); @@ -110,7 +129,8 @@ public void testItemChangeJustGroups() throws ItemNotFoundException { .thenAnswer(invocationOnMock -> new GroupItem(invocationOnMock.getArgument(0), new StringItem("Any"))); JRuleGroupItemChangeRules rule = initRule(JRuleGroupItemChangeRules.class); - fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.SUB_GROUP_ITEM, "2", "1"))); + fireEvents(List.of(itemChangeEvent(JRuleGroupItemChangeRules.SUB_GROUP_ITEM, "2", "1"), + itemChangeEvent(MEMBER_ITEM, "2", "1"))); verify(rule, times(1)).groupItemChangeJustGroups(Mockito.any(JRuleEvent.class)); verify(rule, times(0)).groupItemChangeJustItems(Mockito.any(JRuleEvent.class)); }