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

Consider auto-update policy from channel types #3575

Merged
merged 1 commit into from
Apr 24, 2023
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 @@ -46,12 +46,12 @@ public enum AutoUpdatePolicy {
* Parses the input string into an {@link AutoUpdatePolicy}.
*
* @param input the input string
* @return the parsed AutoUpdatePolicy
* @return the parsed AutoUpdatePolicy or null if the input was null
* @throws IllegalArgumentException if the input couldn't be parsed.
*/
public static AutoUpdatePolicy parse(@Nullable String input) {
public static @Nullable AutoUpdatePolicy parse(@Nullable String input) {
if (input == null) {
return DEFAULT;
return null;
}

for (AutoUpdatePolicy value : values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@
@NonNullByDefault
public class ThingChannelsTest extends JavaOSGiTest {

private static final ThingTypeUID THING_TYPE_UID = new ThingTypeUID("bindingId", "thingTypeId");
private static final ThingUID THING_UID = new ThingUID(THING_TYPE_UID, "thingLabel");

private static final List<String> CHANNEL_IDS = List.of("polarBear", "alligator", "hippopotamus", "aardvark",
"whiteRabbit", "redHerring", "orangutan", "kangaroo", "rubberDuck", "timorousBeastie");

@Test
public void testThingChannelOrder() {
ThingTypeUID thingTypeUID = new ThingTypeUID("bindingId", "thingTypeId");
ThingUID thingUID = new ThingUID(thingTypeUID, "thingLabel");

// create and fill the list of origin channels
List<Channel> originChannels = new ArrayList<>();
CHANNEL_IDS.forEach(channelId -> originChannels
.add(ChannelBuilder.create(new ChannelUID(thingUID, channelId), null).build()));
.add(ChannelBuilder.create(new ChannelUID(THING_UID, channelId), null).build()));
assertEquals(CHANNEL_IDS.size(), originChannels.size());

// build a thing with the origin channels
Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withChannels(originChannels).build();
Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID).withChannels(originChannels).build();

List<Channel> resultChannels;

Expand All @@ -65,4 +65,10 @@ public void testThingChannelOrder() {
assertTrue(CHANNEL_IDS.get(i).equals(resultChannels.get(i).getUID().getId()));
}
}

@Test
public void testAutoUpdatePolicyNotSetOnNewChannels() {
Channel channel = ChannelBuilder.create(new ChannelUID(THING_UID, CHANNEL_IDS.get(0)), null).build();
assertNull(channel.getAutoUpdatePolicy());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2010-2023 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.core.thing.internal;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.Item;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.items.events.ItemEventFactory;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.test.java.JavaTest;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.type.AutoUpdatePolicy;
import org.openhab.core.thing.type.ChannelTypeBuilder;
import org.openhab.core.thing.type.ChannelTypeRegistry;
import org.openhab.core.thing.type.ChannelTypeUID;

/**
* Tests for {@link AutoUpdateManager}.
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class AutoUpdateManagerTest extends JavaTest {

private static final ChannelTypeUID CHANNEL_TYPE_UID = new ChannelTypeUID("binding:channelType");
private static final ChannelUID CHANNEL_UID = new ChannelUID("binding:thingtype1:thing1:channel1");
private static final String ITEM_NAME = "TestItem";

private @NonNullByDefault({}) ChannelTypeRegistry channelTypeRegistry;
private @NonNullByDefault({}) ThingRegistry thingRegistry;
private @NonNullByDefault({}) MetadataRegistry metadataRegistry;
private @NonNullByDefault({}) EventPublisher eventPublisher;
private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry;
private @NonNullByDefault({}) AutoUpdateManager autoUpdateManager;
private @NonNullByDefault({}) Item item;
private @NonNullByDefault({}) Thing thing;

@BeforeEach
public void setup() {
channelTypeRegistry = mock(ChannelTypeRegistry.class);
eventPublisher = mock(EventPublisher.class);
itemChannelLinkRegistry = mock(ItemChannelLinkRegistry.class);
assertNotNull(itemChannelLinkRegistry);

thingRegistry = mock(ThingRegistry.class);
thing = mock(Thing.class);
metadataRegistry = mock(MetadataRegistry.class);

Channel channel = ChannelBuilder.create(CHANNEL_UID).withType(CHANNEL_TYPE_UID).build();

autoUpdateManager = new AutoUpdateManager(Collections.emptyMap(), channelTypeRegistry, eventPublisher,
itemChannelLinkRegistry, metadataRegistry, thingRegistry);

item = mock(Item.class);
when(item.getName()).thenReturn(ITEM_NAME);
when(item.getAcceptedDataTypes()).thenReturn(List.of(OnOffType.class));
when(itemChannelLinkRegistry.getLinks(any(String.class)))
.thenReturn(Set.of(new ItemChannelLink(ITEM_NAME, CHANNEL_UID)));
when(thingRegistry.get(any(ThingUID.class))).thenReturn(thing);
when(thing.getStatus()).thenReturn(ThingStatus.ONLINE);
when(thing.getHandler()).thenReturn(mock(ThingHandler.class));
when(thing.getChannel(any(String.class))).thenReturn(channel);
}

@Test
public void testAutoUpdateVetoFromChannelType() {
when(channelTypeRegistry.getChannelType(any(ChannelTypeUID.class)))
.thenReturn(ChannelTypeBuilder.state(CHANNEL_TYPE_UID, "label", CoreItemFactory.SWITCH).withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build());

autoUpdateManager.receiveCommand(ItemEventFactory.createCommandEvent(ITEM_NAME, OnOffType.ON), item);

// No event should have been sent
verify(eventPublisher, never()).post(any(Event.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ public void beforeEach() {
SystemProfileFactory profileFactory = getService(ProfileTypeProvider.class, SystemProfileFactory.class);

assertNotNull(profileFactory);
if (profileFactory == null) {
throw new IllegalStateException("thing is null");
}

manager = new CommunicationManager(autoUpdateManagerMock, channelTypeRegistryMock, profileFactory, iclRegistry,
itemRegistryMock, itemStateConverterMock, eventPublisherMock, safeCaller, thingRegistryMock);
Expand Down