Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[autoupdatemanager] Get only relevant Links of an Item instead of all #1874

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void receiveCommand(ItemCommandEvent commandEvent, Item item) {
// consider user-override via item meta-data
MetadataKey key = new MetadataKey(AUTOUPDATE_KEY, itemName);
Metadata metadata = metadataRegistry.get(key);
if (metadata != null && !metadata.getValue().trim().isEmpty()) {
if (metadata != null && !metadata.getValue().isBlank()) {
boolean override = Boolean.parseBoolean(metadata.getValue());
if (override) {
logger.trace("Auto update strategy {} overriden by item metadata to REQUIRED", autoUpdate);
Expand Down Expand Up @@ -189,10 +189,8 @@ private Recommendation shouldAutoUpdate(String itemName) {
Recommendation ret = Recommendation.REQUIRED;

List<ChannelUID> linkedChannelUIDs = new ArrayList<>();
for (ItemChannelLink link : itemChannelLinkRegistry.getAll()) {
if (link.getItemName().equals(itemName)) {
linkedChannelUIDs.add(link.getLinkedUID());
}
for (ItemChannelLink link : itemChannelLinkRegistry.getLinks(itemName)) {
linkedChannelUIDs.add(link.getLinkedUID());
}

// check if there is any channel ONLINE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.core.thing.link;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -183,7 +182,7 @@ public Set<String> getLinkedItemNames(final UID uid) {
try {
final Set<L> forLinkedUID = linkedUidToLink.get(uid);
if (forLinkedUID == null) {
return Collections.emptySet();
return Set.of();
}
return forLinkedUID.parallelStream().map(link -> link.getItemName()).collect(Collectors.toSet());
} finally {
Expand All @@ -201,7 +200,7 @@ public Set<L> getLinks(final UID uid) {
toLinkLock.readLock().lock();
try {
final Set<L> forLinkedUID = linkedUidToLink.get(uid);
return forLinkedUID != null ? new HashSet<>(forLinkedUID) : Collections.emptySet();
return forLinkedUID != null ? new HashSet<>(forLinkedUID) : Set.of();
} finally {
toLinkLock.readLock().unlock();
}
Expand All @@ -217,7 +216,7 @@ public Set<L> getLinks(final String itemName) {
toLinkLock.readLock().lock();
try {
final Set<L> forLinkedUID = itemNameToLink.get(itemName);
return forLinkedUID != null ? new HashSet<>(forLinkedUID) : Collections.emptySet();
return forLinkedUID != null ? new HashSet<>(forLinkedUID) : Set.of();
} finally {
toLinkLock.readLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import static org.mockito.Mockito.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,6 +61,7 @@
@MockitoSettings(strictness = Strictness.WARN)
public class AutoUpdateManagerTest {

private static final String ITEM_NAME = "test";
private static final ThingUID THING_UID_ONLINE = new ThingUID("test::mock-online");
private static final ThingUID THING_UID_OFFLINE = new ThingUID("test::mock-offline");
private static final ThingUID THING_UID_HANDLER_MISSING = new ThingUID("test::handlerMissing");
Expand All @@ -82,18 +83,17 @@ public class AutoUpdateManagerTest {
private @Mock ThingHandler handlerMock;
private @Mock MetadataRegistry metadataRegistryMock;

private final List<ItemChannelLink> links = new LinkedList<>();
private final Set<ItemChannelLink> links = new HashSet<>();
private AutoUpdateManager aum;
private final Map<ChannelUID, AutoUpdatePolicy> policies = new HashMap<>();

@BeforeEach
public void setup() {
event = ItemEventFactory.createCommandEvent("test", new StringType("AFTER"));
item = new StringItem("test");
event = ItemEventFactory.createCommandEvent(ITEM_NAME, new StringType("AFTER"));
item = new StringItem(ITEM_NAME);
item.setState(new StringType("BEFORE"));

when(iclRegistryMock.stream()).then(answer -> links.stream());
when(iclRegistryMock.getAll()).then(answer -> links);
when(iclRegistryMock.getLinks(eq(ITEM_NAME))).then(answer -> links);

when(thingRegistryMock.get(eq(THING_UID_ONLINE))).thenReturn(onlineThingMock);
when(thingRegistryMock.get(eq(THING_UID_OFFLINE))).thenReturn(offlineThingMock);
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testAutoUpdateNoLink() {

@Test
public void testAutoUpdateNoPolicy() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));

aum.receiveCommand(event, item);

Expand All @@ -173,7 +173,7 @@ public void testAutoUpdateNoPolicy() {

@Test
public void testAutoUpdateNoPolicyThingOFFLINE() {
links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_OFFLINE_1));

aum.receiveCommand(event, item);

Expand All @@ -182,8 +182,8 @@ public void testAutoUpdateNoPolicyThingOFFLINE() {

@Test
public void testAutoUpdateNoPolicyThingOFFLINEandThingONLINE() {
links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_OFFLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));

aum.receiveCommand(event, item);

Expand All @@ -192,8 +192,8 @@ public void testAutoUpdateNoPolicyThingOFFLINEandThingONLINE() {

@Test
public void testAutoUpdateNoPolicyThingONLINEandThingOFFLINE() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_OFFLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_OFFLINE_1));

aum.receiveCommand(event, item);

Expand All @@ -202,7 +202,7 @@ public void testAutoUpdateNoPolicyThingONLINEandThingOFFLINE() {

@Test
public void testAutoUpdateNoPolicyNoHandler() {
links.add(new ItemChannelLink("test", CHANNEL_UID_HANDLER_MISSING));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_HANDLER_MISSING));

aum.receiveCommand(event, item);

Expand All @@ -211,7 +211,7 @@ public void testAutoUpdateNoPolicyNoHandler() {

@Test
public void testAutoUpdateNoPolicyNoThing() {
links.add(new ItemChannelLink("test", new ChannelUID(new ThingUID("test::missing"), "gone")));
links.add(new ItemChannelLink(ITEM_NAME, new ChannelUID(new ThingUID("test::missing"), "gone")));

aum.receiveCommand(event, item);

Expand All @@ -220,7 +220,7 @@ public void testAutoUpdateNoPolicyNoThing() {

@Test
public void testAutoUpdateNoPolicyNoChannel() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));

aum.receiveCommand(event, item);

Expand All @@ -229,7 +229,7 @@ public void testAutoUpdateNoPolicyNoChannel() {

@Test
public void testAutoUpdatePolicyVETOThingONLINE() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO);

aum.receiveCommand(event, item);
Expand All @@ -239,7 +239,7 @@ public void testAutoUpdatePolicyVETOThingONLINE() {

@Test
public void testAutoUpdatePolicyRECOMMEND() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND);

aum.receiveCommand(event, item);
Expand All @@ -249,8 +249,8 @@ public void testAutoUpdatePolicyRECOMMEND() {

@Test
public void testAutoUpdatePolicyVETObeatsDEFAULT() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_2));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.DEFAULT);

Expand All @@ -261,8 +261,8 @@ public void testAutoUpdatePolicyVETObeatsDEFAULT() {

@Test
public void testAutoUpdatePolicyVETObeatsRECOMMEND() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_2));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.VETO);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.RECOMMEND);

Expand All @@ -273,8 +273,8 @@ public void testAutoUpdatePolicyVETObeatsRECOMMEND() {

@Test
public void testAutoUpdatePolicyDEFAULTbeatsRECOMMEND() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_2));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_2));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.DEFAULT);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_2, AutoUpdatePolicy.RECOMMEND);

Expand All @@ -285,8 +285,8 @@ public void testAutoUpdatePolicyDEFAULTbeatsRECOMMEND() {

@Test
public void testAutoUpdateErrorInvalidatesVETO() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.VETO);

Expand All @@ -297,8 +297,8 @@ public void testAutoUpdateErrorInvalidatesVETO() {

@Test
public void testAutoUpdateErrorInvalidatesVETO2() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.DEFAULT);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.VETO);

Expand All @@ -309,8 +309,8 @@ public void testAutoUpdateErrorInvalidatesVETO2() {

@Test
public void testAutoUpdateErrorInvalidatesDEFAULT() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_1, AutoUpdatePolicy.RECOMMEND);
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.DEFAULT);

Expand All @@ -321,8 +321,8 @@ public void testAutoUpdateErrorInvalidatesDEFAULT() {

@Test
public void testAutoUpdateMultipleErrors() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_GONE));
setAutoUpdatePolicy(CHANNEL_UID_ONLINE_GONE, AutoUpdatePolicy.DEFAULT);

aum.receiveCommand(event, item);
Expand All @@ -341,7 +341,7 @@ public void testAutoUpdateDisabled() {

@Test
public void testAutoUpdateSendOptimisticUpdates() {
links.add(new ItemChannelLink("test", CHANNEL_UID_ONLINE_1));
links.add(new ItemChannelLink(ITEM_NAME, CHANNEL_UID_ONLINE_1));
aum.modified(Map.of(AutoUpdateManager.PROPERTY_SEND_OPTIMISTIC_UPDATES, "true"));

aum.receiveCommand(event, item);
Expand Down