[hue] Fix compiler warnings (#15419)

* Fix compiler warnings
* Fix integration test compiler/SAT warnings

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
pull/15475/head
Jacob Laursen 2023-08-21 21:14:18 +02:00 committed by GitHub
parent a1b79b6b7b
commit bf892b6b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 183 additions and 132 deletions

View File

@ -14,10 +14,9 @@ package org.openhab.binding.hue.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
/** /**
* Configuration for the {@link HueBridgeHandler}. * Configuration for the {@link org.openhab.binding.hue.internal.handler.HueBridgeHandler}.
* *
* @author Christoph Weitkamp - Initial contribution * @author Christoph Weitkamp - Initial contribution
*/ */

View File

@ -996,7 +996,12 @@ public class HueBridge {
// Methods that convert gson exceptions into ApiExceptions // Methods that convert gson exceptions into ApiExceptions
private <T> T safeFromJson(String json, Type typeOfT) throws ApiException { private <T> T safeFromJson(String json, Type typeOfT) throws ApiException {
try { try {
return gson.fromJson(json, typeOfT); @Nullable
T safe = gson.fromJson(json, typeOfT);
if (safe == null) {
throw new ApiException("JSON is null or empty");
}
return safe;
} catch (JsonParseException e) { } catch (JsonParseException e) {
throw new ApiException("API returned unexpected result: " + e.getMessage()); throw new ApiException("API returned unexpected result: " + e.getMessage());
} }
@ -1004,7 +1009,12 @@ public class HueBridge {
private <T> T safeFromJson(String json, Class<T> classOfT) throws ApiException { private <T> T safeFromJson(String json, Class<T> classOfT) throws ApiException {
try { try {
return gson.fromJson(json, classOfT); @Nullable
T safe = gson.fromJson(json, classOfT);
if (safe == null) {
throw new ApiException("JSON is null or empty");
}
return safe;
} catch (JsonParseException e) { } catch (JsonParseException e) {
throw new ApiException("API returned unexpected result: " + e.getMessage()); throw new ApiException("API returned unexpected result: " + e.getMessage());
} }

View File

@ -12,6 +12,9 @@
*/ */
package org.openhab.binding.hue.internal.discovery; package org.openhab.binding.hue.internal.discovery;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
/** /**
@ -22,15 +25,17 @@ import com.google.gson.annotations.SerializedName;
* @author Awelkiyar Wehabrebi - Initial contribution and API * @author Awelkiyar Wehabrebi - Initial contribution and API
* @author Christoph Knauf - Refactorings * @author Christoph Knauf - Refactorings
*/ */
@NonNullByDefault
public class BridgeJsonParameters { public class BridgeJsonParameters {
private String id; private @Nullable String id;
@SerializedName("internalipaddress") @SerializedName("internalipaddress")
private String internalIpAddress; private @Nullable String internalIpAddress;
@SerializedName("macaddress") @SerializedName("macaddress")
private String macAddress; private @Nullable String macAddress;
private String name; private @Nullable String name;
@SuppressWarnings("unused")
private BridgeJsonParameters() { private BridgeJsonParameters() {
// This no arguments constructor is required for Gson deserialization // This no arguments constructor is required for Gson deserialization
} }
@ -42,19 +47,19 @@ public class BridgeJsonParameters {
this.name = name; this.name = name;
} }
public String getInternalIpAddress() { public @Nullable String getInternalIpAddress() {
return internalIpAddress; return internalIpAddress;
} }
public String getId() { public @Nullable String getId() {
return id; return id;
} }
public String getMacAddress() { public @Nullable String getMacAddress() {
return macAddress; return macAddress;
} }
public String getName() { public @Nullable String getName() {
return name; return name;
} }
} }

View File

@ -80,9 +80,18 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
*/ */
private void discoverHueBridges() { private void discoverHueBridges() {
for (BridgeJsonParameters bridge : getBridgeList()) { for (BridgeJsonParameters bridge : getBridgeList()) {
if (isReachableAndValidHueBridge(bridge)) { if (!isReachableAndValidHueBridge(bridge)) {
continue;
}
String host = bridge.getInternalIpAddress(); String host = bridge.getInternalIpAddress();
String serialNumber = bridge.getId().toLowerCase(); if (host == null) {
continue;
}
String id = bridge.getId();
if (id == null) {
continue;
}
String serialNumber = id.toLowerCase();
ThingUID uid = new ThingUID(THING_TYPE_BRIDGE, serialNumber); ThingUID uid = new ThingUID(THING_TYPE_BRIDGE, serialNumber);
ThingUID legacyUID = null; ThingUID legacyUID = null;
String label = String.format(DISCOVERY_LABEL_PATTERN, host); String label = String.format(DISCOVERY_LABEL_PATTERN, host);
@ -111,7 +120,6 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
thingDiscovered(builder.build()); thingDiscovered(builder.build());
} }
} }
}
/** /**
* Checks if the Bridge is a reachable Hue Bridge with a valid id. * Checks if the Bridge is a reachable Hue Bridge with a valid id.

View File

@ -32,7 +32,6 @@ import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService; import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant; import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID; import org.openhab.core.thing.ThingUID;
@ -45,7 +44,7 @@ import org.slf4j.LoggerFactory;
/** /**
* The {@link HueBridgeUPNPDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the * The {@link HueBridgeUPNPDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
* central {@link UpnpDiscoveryService}. * central {@link org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService}.
* *
* The discovery through UPnP was replaced by mDNS discovery for recent bridges (V2). * The discovery through UPnP was replaced by mDNS discovery for recent bridges (V2).
* For old bridges (V1), the UPnP discovery is still required (as mDNS is not implemented). * For old bridges (V1), the UPnP discovery is still required (as mDNS is not implemented).

View File

@ -12,7 +12,6 @@
*/ */
package org.openhab.binding.hue.internal.dto; package org.openhab.binding.hue.internal.dto;
import java.util.Comparator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -77,7 +76,7 @@ public class ApiVersion {
} }
/** /**
* compare API versions according to {@link Comparator#compare(Object, Object)} * compare API versions according to {@link java.util.Comparator#compare(Object, Object)}
* *
* @param other * @param other
* @return * @return

View File

@ -17,6 +17,8 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.gson.annotations.SerializedName;
/** /**
* Detailed bridge info available if authenticated. * Detailed bridge info available if authenticated.
* *
@ -37,7 +39,8 @@ public class Config {
private String gateway; private String gateway;
private String proxyaddress; private String proxyaddress;
private int proxyport; private int proxyport;
private Date UTC; @SerializedName("UTC")
private Date utc;
private boolean linkbutton; private boolean linkbutton;
private Map<String, User> whitelist; private Map<String, User> whitelist;
private SoftwareUpdate swupdate; private SoftwareUpdate swupdate;
@ -150,7 +153,7 @@ public class Config {
* @return time on the bridge * @return time on the bridge
*/ */
public Date getUTCTime() { public Date getUTCTime() {
return UTC; return utc;
} }
/** /**

View File

@ -35,8 +35,9 @@ public class FullConfig {
public List<FullLight> getLights() { public List<FullLight> getLights() {
ArrayList<FullLight> lightsList = new ArrayList<>(); ArrayList<FullLight> lightsList = new ArrayList<>();
for (String id : lights.keySet()) { for (Map.Entry<String, FullLight> entry : lights.entrySet()) {
FullLight light = lights.get(id); String id = entry.getKey();
FullLight light = entry.getValue();
light.setId(id); light.setId(id);
lightsList.add(light); lightsList.add(light);
} }
@ -52,8 +53,9 @@ public class FullConfig {
public List<FullGroup> getGroups() { public List<FullGroup> getGroups() {
ArrayList<FullGroup> groupsList = new ArrayList<>(); ArrayList<FullGroup> groupsList = new ArrayList<>();
for (String id : groups.keySet()) { for (Map.Entry<String, FullGroup> entry : groups.entrySet()) {
FullGroup group = groups.get(id); String id = entry.getKey();
FullGroup group = entry.getValue();
group.setId(id); group.setId(id);
groupsList.add(group); groupsList.add(group);
} }

View File

@ -13,9 +13,13 @@
package org.openhab.binding.hue.internal.dto; package org.openhab.binding.hue.internal.dto;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
/** /**
@ -25,13 +29,14 @@ import com.google.gson.reflect.TypeToken;
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding * @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
* @author Laurent Garnier - field state added * @author Laurent Garnier - field state added
*/ */
@NonNullByDefault
public class FullGroup extends Group { public class FullGroup extends Group {
public static final Type GSON_TYPE = new TypeToken<Map<String, FullGroup>>() { public static final Type GSON_TYPE = new TypeToken<Map<String, FullGroup>>() {
}.getType(); }.getType();
private State action; private @Nullable State action;
private List<String> lights; private @Nullable List<String> lights;
private State groupState; // Will not be set by hue API private @Nullable State groupState; // Will not be set by hue API
FullGroup() { FullGroup() {
super(); super();
@ -53,7 +58,7 @@ public class FullGroup extends Group {
* *
* @return last state update * @return last state update
*/ */
public State getAction() { public @Nullable State getAction() {
return action; return action;
} }
@ -63,7 +68,8 @@ public class FullGroup extends Group {
* @return lights in the group * @return lights in the group
*/ */
public List<String> getLightIds() { public List<String> getLightIds() {
return lights; List<String> lights = this.lights;
return lights != null ? lights : new ArrayList<>();
} }
/** /**
@ -72,6 +78,10 @@ public class FullGroup extends Group {
* @return current state * @return current state
*/ */
public State getState() { public State getState() {
State groupState = this.groupState;
if (groupState == null) {
throw new IllegalStateException("Group state not initialized when requested");
}
return groupState; return groupState;
} }

View File

@ -67,6 +67,7 @@ public class FullHueObject extends HueObject {
} }
public @Nullable String getNormalizedModelID() { public @Nullable String getNormalizedModelID() {
String modelid = this.modelid;
return modelid != null ? modelid.replaceAll(NORMALIZE_ID_REGEX, "_") : modelid; return modelid != null ? modelid.replaceAll(NORMALIZE_ID_REGEX, "_") : modelid;
} }

View File

@ -13,6 +13,7 @@
package org.openhab.binding.hue.internal.dto; package org.openhab.binding.hue.internal.dto;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -36,7 +37,7 @@ public class Scene {
private @NonNullByDefault({}) String id; private @NonNullByDefault({}) String id;
private @NonNullByDefault({}) String name; private @NonNullByDefault({}) String name;
@SerializedName("lights") @SerializedName("lights")
private @NonNullByDefault({}) List<String> lightIds; private @Nullable List<String> lightIds;
@SerializedName("group") @SerializedName("group")
private @Nullable String groupId; private @Nullable String groupId;
private boolean recycle; private boolean recycle;
@ -84,7 +85,8 @@ public class Scene {
* @return list of lights that the scene applies to * @return list of lights that the scene applies to
*/ */
public List<String> getLightIds() { public List<String> getLightIds() {
return lightIds; List<String> lightIds = this.lightIds;
return lightIds != null ? lightIds : new ArrayList<String>();
} }
/** /**
@ -141,11 +143,11 @@ public class Scene {
* </ol> * </ol>
*/ */
public boolean isApplicableTo(FullGroup group) { public boolean isApplicableTo(FullGroup group) {
if (getGroupId() == null) { String groupId = this.groupId;
if (groupId == null) {
return getLightIds().stream().allMatch(id -> group.getLightIds().contains(id)); return getLightIds().stream().allMatch(id -> group.getLightIds().contains(id));
} else { } else {
String groupId = getGroupId(); return group.getId().contentEquals(groupId);
return groupId != null ? group.getId().contentEquals(groupId) : false;
} }
} }

View File

@ -14,22 +14,23 @@ package org.openhab.binding.hue.internal.dto;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* *
* @author Q42 - Initial contribution * @author Q42 - Initial contribution
* @author Andre Fuechsel - search for lights with given serial number added * @author Andre Fuechsel - search for lights with given serial number added
* @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding * @author Denis Dudnik - moved Jue library source code inside the smarthome Hue binding
*/ */
@SuppressWarnings("unused") @NonNullByDefault
public class SearchForLightsRequest { public class SearchForLightsRequest {
@SuppressWarnings("unused")
private List<String> deviceid; private List<String> deviceid;
public SearchForLightsRequest(List<String> deviceid) { public SearchForLightsRequest(List<String> deviceid) {
if (deviceid != null && (deviceid.isEmpty() || deviceid.size() > 16)) { if (deviceid.isEmpty() || deviceid.size() > 16) {
throw new IllegalArgumentException("Group cannot be empty and cannot have more than 16 lights"); throw new IllegalArgumentException("Group cannot be empty and cannot have more than 16 lights");
} }
if (deviceid != null) {
this.deviceid = deviceid; this.deviceid = deviceid;
} }
}
} }

View File

@ -13,7 +13,6 @@
package org.openhab.binding.hue.internal.dto; package org.openhab.binding.hue.internal.dto;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -45,6 +44,6 @@ public class SetAttributesRequest {
} }
this.name = name; this.name = name;
this.lights = lights == null ? null : lights.stream().map(l -> l.getId()).collect(Collectors.toList()); this.lights = lights == null ? null : lights.stream().map(l -> l.getId()).toList();
} }
} }

View File

@ -235,7 +235,7 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
sensorStatusListener.onSensorRemoved(); sensorStatusListener.onSensorRemoved();
} }
if (discovery != null && sensor != null) { if (discovery != null) {
discovery.removeSensorDiscovery(sensor); discovery.removeSensorDiscovery(sensor);
} }
}); });
@ -295,7 +295,7 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
lightStatusListener.onLightRemoved(); lightStatusListener.onLightRemoved();
} }
if (discovery != null && light != null) { if (discovery != null) {
discovery.removeLightDiscovery(light); discovery.removeLightDiscovery(light);
} }
}); });
@ -385,7 +385,7 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
groupStatusListener.onGroupRemoved(); groupStatusListener.onGroupRemoved();
} }
if (discovery != null && group != null) { if (discovery != null) {
discovery.removeGroupDiscovery(group); discovery.removeGroupDiscovery(group);
} }
}); });
@ -402,12 +402,11 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
private void setBridgeSceneChannelStateOptions(List<Scene> scenes, Map<String, FullGroup> groups) { private void setBridgeSceneChannelStateOptions(List<Scene> scenes, Map<String, FullGroup> groups) {
Map<String, String> groupNames = groups.entrySet().stream() Map<String, String> groupNames = groups.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getName())); .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getName()));
List<StateOption> stateOptions = scenes.stream().map(scene -> scene.toStateOption(groupNames)) List<StateOption> stateOptions = scenes.stream().map(scene -> scene.toStateOption(groupNames)).toList();
.collect(Collectors.toList());
stateDescriptionOptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_SCENE), stateDescriptionOptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_SCENE),
stateOptions); stateOptions);
consoleScenesList = scenes.stream().map(scene -> "Id is \"" + scene.getId() + "\" for scene \"" consoleScenesList = scenes.stream().map(scene -> "Id is \"" + scene.getId() + "\" for scene \""
+ scene.toStateOption(groupNames).getLabel() + "\"").collect(Collectors.toList()); + scene.toStateOption(groupNames).getLabel() + "\"").toList();
} }
}; };

View File

@ -455,11 +455,11 @@ public class HueGroupHandler extends BaseThingHandler implements HueLightActions
FullGroup group = handler.getGroupById(groupId); FullGroup group = handler.getGroupById(groupId);
if (group != null) { if (group != null) {
stateOptions = updatedScenes.stream().filter(scene -> scene.isApplicableTo(group)) stateOptions = updatedScenes.stream().filter(scene -> scene.isApplicableTo(group))
.map(Scene::toStateOption).collect(Collectors.toList()); .map(Scene::toStateOption).toList();
consoleScenesList = updatedScenes consoleScenesList = updatedScenes.stream().filter(scene -> scene.isApplicableTo(group))
.stream().filter(scene -> scene.isApplicableTo(group)).map(scene -> "Id is \"" + scene.getId() .map(scene -> "Id is \"" + scene.getId() + "\" for scene \"" + scene.toStateOption().getLabel()
+ "\" for scene \"" + scene.toStateOption().getLabel() + "\"") + "\"")
.collect(Collectors.toList()); .toList();
} }
} }
stateDescriptionOptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_SCENE), stateDescriptionOptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_SCENE),

View File

@ -58,6 +58,7 @@ public class HueStateDescriptionProvider extends BaseDynamicStateDescriptionProv
StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID); StateDescriptionFragment oldStateDescriptionFragment = stateDescriptionFragments.get(channelUID);
if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) { if (!stateDescriptionFragment.equals(oldStateDescriptionFragment)) {
stateDescriptionFragments.put(channelUID, stateDescriptionFragment); stateDescriptionFragments.put(channelUID, stateDescriptionFragment);
ItemChannelLinkRegistry itemChannelLinkRegistry = this.itemChannelLinkRegistry;
postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID, postEvent(ThingEventFactory.createChannelDescriptionChangedEvent(channelUID,
itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(), itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
stateDescriptionFragment, oldStateDescriptionFragment)); stateDescriptionFragment, oldStateDescriptionFragment));

View File

@ -30,6 +30,7 @@ import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType; import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units; import org.openhab.core.library.unit.Units;
import org.openhab.core.util.ColorUtil;
/** /**
* The {@link LightStateConverter} is responsible for mapping to/from jue types. * The {@link LightStateConverter} is responsible for mapping to/from jue types.
@ -276,7 +277,7 @@ public class LightStateConverter {
private static HSBType fromXYtoHSBType(State lightState) { private static HSBType fromXYtoHSBType(State lightState) {
float[] xy = lightState.getXY(); float[] xy = lightState.getXY();
HSBType hsb = HSBType.fromXY(xy[0], xy[1]); HSBType hsb = ColorUtil.xyToHsb(new double[] { xy[0], xy[1] });
int brightnessInPercent = (int) Math.ceil(lightState.getBrightness() / BRIGHTNESS_FACTOR); int brightnessInPercent = (int) Math.ceil(lightState.getBrightness() / BRIGHTNESS_FACTOR);
brightnessInPercent = restrictToBounds(brightnessInPercent); brightnessInPercent = restrictToBounds(brightnessInPercent);

View File

@ -15,7 +15,6 @@ package org.openhab.binding.hue.internal;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -39,7 +38,7 @@ public class SceneTest {
@Test @Test
public void testIsApplicableToHasGroupIdMatchingGroup() { public void testIsApplicableToHasGroupIdMatchingGroup() {
String groupId = "groupId"; String groupId = "groupId";
List<String> lights = Arrays.asList("1", "2"); List<String> lights = List.of("1", "2");
Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, groupId, lights, false); Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, groupId, lights, false);
FullGroup group = new FullGroup(groupId, PLACEHOLDER, PLACEHOLDER, PLACEHOLDER_STATE, lights, FullGroup group = new FullGroup(groupId, PLACEHOLDER, PLACEHOLDER, PLACEHOLDER_STATE, lights,
@ -56,8 +55,8 @@ public class SceneTest {
public void testIsApplicableToHasGroupIdNotMatchingGroup() { public void testIsApplicableToHasGroupIdNotMatchingGroup() {
String groupId = "groupId"; String groupId = "groupId";
String otherGroupId = "otherGroupId"; String otherGroupId = "otherGroupId";
List<String> lights = Arrays.asList("1", "2"); List<String> lights = List.of("1", "2");
List<String> otherLights = Arrays.asList("1", "2", "3"); List<String> otherLights = List.of("1", "2", "3");
Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, groupId, lights, false); Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, groupId, lights, false);
@ -76,8 +75,8 @@ public class SceneTest {
*/ */
@Test @Test
public void testIsApplicableToNoGroupIdSceneLightsContainedInGroup() { public void testIsApplicableToNoGroupIdSceneLightsContainedInGroup() {
List<String> lights = Arrays.asList("1", "2"); List<String> lights = List.of("1", "2");
List<String> moreLights = Arrays.asList("1", "2", "3"); List<String> moreLights = List.of("1", "2", "3");
Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, null, lights, false); Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, null, lights, false);
@ -96,9 +95,9 @@ public class SceneTest {
*/ */
@Test @Test
public void testIsApplicableToNoGroupIdSceneLightsNotContainedInGroup() { public void testIsApplicableToNoGroupIdSceneLightsNotContainedInGroup() {
List<String> lights = Arrays.asList("1", "2"); List<String> lights = List.of("1", "2");
List<String> lessLights = Arrays.asList("1"); List<String> lessLights = List.of("1");
List<String> differentLights = Arrays.asList("3"); List<String> differentLights = List.of("3");
Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, null, lights, false); Scene scene = new Scene(PLACEHOLDER, PLACEHOLDER, null, lights, false);

View File

@ -14,13 +14,16 @@ package org.openhab.binding.hue.internal;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.test.java.JavaOSGiTest; import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandler;
/** /**
* @author Markus Rathgeb - Initial contribution
* @author Markus Rathgeb - migrated to plain Java test * @author Markus Rathgeb - migrated to plain Java test
*/ */
@NonNullByDefault
public class AbstractHueOSGiTestParent extends JavaOSGiTest { public class AbstractHueOSGiTestParent extends JavaOSGiTest {
/** /**
@ -38,7 +41,7 @@ public class AbstractHueOSGiTestParent extends JavaOSGiTest {
} else { } else {
assertNotNull(tmp); assertNotNull(tmp);
assertEquals(clazz, tmp.getClass()); assertEquals(clazz, tmp.getClass());
throw new RuntimeException(); throw new IllegalStateException();
} }
}); });
} }

View File

@ -25,6 +25,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -61,16 +63,17 @@ import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
* @author Denis Dudnik - switched to internally integrated source of Jue library * @author Denis Dudnik - switched to internally integrated source of Jue library
* @author Markus Rathgeb - migrated to plain Java test * @author Markus Rathgeb - migrated to plain Java test
*/ */
@NonNullByDefault
public class HueDeviceDiscoveryServiceOSGiTest extends AbstractHueOSGiTestParent { public class HueDeviceDiscoveryServiceOSGiTest extends AbstractHueOSGiTestParent {
protected DiscoveryListener discoveryListener; protected @Nullable DiscoveryListener discoveryListener;
protected ThingRegistry thingRegistry; protected @NonNullByDefault({}) ThingRegistry thingRegistry;
protected Bridge hueBridge; protected @NonNullByDefault({}) Bridge hueBridge;
protected HueBridgeHandler hueBridgeHandler; protected @NonNullByDefault({}) HueBridgeHandler hueBridgeHandler;
protected HueDeviceDiscoveryService discoveryService; protected @NonNullByDefault({}) HueDeviceDiscoveryService discoveryService;
protected final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID("hue", "bridge"); protected static final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID("hue", "bridge");
protected final ThingUID BRIDGE_THING_UID = new ThingUID(BRIDGE_THING_TYPE_UID, "testBridge"); protected static final ThingUID BRIDGE_THING_UID = new ThingUID(BRIDGE_THING_TYPE_UID, "testBridge");
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
@ -139,8 +142,8 @@ public class HueDeviceDiscoveryServiceOSGiTest extends AbstractHueOSGiTestParent
} }
@Override @Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) { @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
return null; return null;
} }
}); });

View File

@ -26,7 +26,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.core.config.discovery.DiscoveryListener; import org.openhab.core.config.discovery.DiscoveryListener;
@ -43,26 +44,31 @@ import org.openhab.core.thing.ThingUID;
* @author Christoph Knauf - Initial contribution * @author Christoph Knauf - Initial contribution
* @author Markus Rathgeb - migrated to plain Java test * @author Markus Rathgeb - migrated to plain Java test
*/ */
@NonNullByDefault
public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest { public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
HueBridgeNupnpDiscovery sut; private @NonNullByDefault({}) HueBridgeNupnpDiscovery sut;
VolatileStorageService volatileStorageService = new VolatileStorageService(); private VolatileStorageService volatileStorageService = new VolatileStorageService();
DiscoveryListener discoveryListener; private @Nullable DiscoveryListener discoveryListener;
Inbox inbox; private @NonNullByDefault({}) Inbox inbox;
final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID("hue", "bridge"); private static final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID("hue", "bridge");
final String ip1 = "192.168.31.17"; private static final String IP1 = "192.168.31.17";
final String ip2 = "192.168.30.28"; private static final String IP2 = "192.168.30.28";
final String sn1 = "001788fffe20057f"; private static final String SN1 = "001788fffe20057f";
final String sn2 = "001788fffe141b41"; private static final String SN2 = "001788fffe141b41";
final ThingUID BRIDGE_THING_UID_1 = new ThingUID(BRIDGE_THING_TYPE_UID, sn1); private static final ThingUID BRIDGE_THING_UID_1 = new ThingUID(BRIDGE_THING_TYPE_UID, SN1);
final ThingUID BRIDGE_THING_UID_2 = new ThingUID(BRIDGE_THING_TYPE_UID, sn2); private static final ThingUID BRIDGE_THING_UID_2 = new ThingUID(BRIDGE_THING_TYPE_UID, SN2);
final String validBridgeDiscoveryResult = "[{\"id\":\"" + sn1 + "\",\"internalipaddress\":" + ip1 + "},{\"id\":\""
+ sn2 + "\",\"internalipaddress\":" + ip2 + "}]";
String discoveryResult;
String expBridgeDescription = "{\"name\":\"Philips hue\",\"datastoreversion\":\"149\",\"swversion\":\"1957113050\",\"apiversion\":\"1.57.0\",\"mac\":\"00:11:22:33:44\",\"bridgeid\":\"$SN\",\"factorynew\":false,\"replacesbridgeid\":null,\"modelid\":\"BSB002\",\"starterkitid\":\"\"}";
private void checkDiscoveryResult(DiscoveryResult result, String expIp, String expSn) { private final String validBridgeDiscoveryResult = "[{\"id\":\"" + SN1 + "\",\"internalipaddress\":" + IP1
+ "},{\"id\":\"" + SN2 + "\",\"internalipaddress\":" + IP2 + "}]";
private @Nullable String discoveryResult;
private String expBridgeDescription = "{\"name\":\"Philips hue\",\"datastoreversion\":\"149\",\"swversion\":\"1957113050\",\"apiversion\":\"1.57.0\",\"mac\":\"00:11:22:33:44\",\"bridgeid\":\"$SN\",\"factorynew\":false,\"replacesbridgeid\":null,\"modelid\":\"BSB002\",\"starterkitid\":\"\"}";
private void checkDiscoveryResult(@Nullable DiscoveryResult result, String expIp, String expSn) {
if (result == null) {
return;
}
assertThat(result.getBridgeUID(), nullValue()); assertThat(result.getBridgeUID(), nullValue());
assertThat(result.getLabel(), is(String.format(DISCOVERY_LABEL_PATTERN, expIp))); assertThat(result.getLabel(), is(String.format(DISCOVERY_LABEL_PATTERN, expIp)));
assertThat(result.getProperties().get("ipAddress"), is(expIp)); assertThat(result.getProperties().get("ipAddress"), is(expIp));
@ -88,19 +94,19 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
} }
@Override @Override
protected String doGetRequest(String url) throws IOException { protected @Nullable String doGetRequest(String url) throws IOException {
if (url.contains("meethue")) { if (url.contains("meethue")) {
return discoveryResult; return discoveryResult;
} else if (url.contains(ip1)) { } else if (url.contains(IP1)) {
return expBridgeDescription.replaceAll("$SN", sn1); return expBridgeDescription.replaceAll("$SN", SN1);
} else if (url.contains(ip2)) { } else if (url.contains(IP2)) {
return expBridgeDescription.replaceAll("$SN", sn2); return expBridgeDescription.replaceAll("$SN", SN2);
} }
throw new IOException(); throw new IOException();
} }
@Override @Override
protected boolean isClip2Supported(@NonNull String ipAddress) { protected boolean isClip2Supported(String ipAddress) {
return false; return false;
} }
} }
@ -147,8 +153,8 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
} }
@Override @Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) { @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
return null; return null;
} }
}); });
@ -158,9 +164,9 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
waitForAssert(() -> { waitForAssert(() -> {
assertThat(results.size(), is(2)); assertThat(results.size(), is(2));
assertThat(results.get(BRIDGE_THING_UID_1), is(notNullValue())); assertThat(results.get(BRIDGE_THING_UID_1), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_1), ip1, sn1); checkDiscoveryResult(results.get(BRIDGE_THING_UID_1), IP1, SN1);
assertThat(results.get(BRIDGE_THING_UID_2), is(notNullValue())); assertThat(results.get(BRIDGE_THING_UID_2), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_2), ip2, sn2); checkDiscoveryResult(results.get(BRIDGE_THING_UID_2), IP2, SN2);
final List<DiscoveryResult> inboxResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID)) final List<DiscoveryResult> inboxResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -195,8 +201,8 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
} }
@Override @Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) { @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
return null; return null;
} }
}); });
@ -254,7 +260,7 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
// invalid bridge description // invalid bridge description
expBridgeDescription = ""; expBridgeDescription = "";
discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":" + ip1 + "}]"; discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":" + IP1 + "}]";
sut.startScan(); sut.startScan();
waitForAssert(() -> { waitForAssert(() -> {
assertThat(results.size(), is(0)); assertThat(results.size(), is(0));

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -50,15 +51,15 @@ import org.openhab.core.thing.ThingUID;
* @author Michael Grammling - Initial contribution * @author Michael Grammling - Initial contribution
* @author Denis Dudnik - switched to internally integrated source of Jue library * @author Denis Dudnik - switched to internally integrated source of Jue library
*/ */
@NonNullByDefault
public class HueBridgeHandlerOSGiTest extends AbstractHueOSGiTestParent { public class HueBridgeHandlerOSGiTest extends AbstractHueOSGiTestParent {
private static final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID(BINDING_ID, "bridge"); private static final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID(BINDING_ID, "bridge");
private static final String TEST_USER_NAME = "eshTestUser"; private static final String TEST_USER_NAME = "eshTestUser";
private static final String DUMMY_HOST = "1.2.3.4"; private static final String DUMMY_HOST = "1.2.3.4";
private ThingRegistry thingRegistry; private @NonNullByDefault({}) ThingRegistry thingRegistry;
private @NonNullByDefault({}) ScheduledExecutorService scheduler;
private ScheduledExecutorService scheduler;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {