|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2024 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.binding.govee.internal; |
| 14 | + |
| 15 | +import static org.mockito.ArgumentMatchers.argThat; |
| 16 | +import static org.mockito.ArgumentMatchers.eq; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.spy; |
| 19 | +import static org.mockito.Mockito.verify; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import javax.measure.Unit; |
| 26 | + |
| 27 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.openhab.binding.govee.internal.model.StatusResponse; |
| 31 | +import org.openhab.core.config.core.Configuration; |
| 32 | +import org.openhab.core.library.types.DecimalType; |
| 33 | +import org.openhab.core.library.types.HSBType; |
| 34 | +import org.openhab.core.library.types.PercentType; |
| 35 | +import org.openhab.core.library.types.QuantityType; |
| 36 | +import org.openhab.core.library.unit.Units; |
| 37 | +import org.openhab.core.thing.Channel; |
| 38 | +import org.openhab.core.thing.ChannelUID; |
| 39 | +import org.openhab.core.thing.Thing; |
| 40 | +import org.openhab.core.thing.ThingStatus; |
| 41 | +import org.openhab.core.thing.ThingStatusDetail; |
| 42 | +import org.openhab.core.thing.ThingUID; |
| 43 | +import org.openhab.core.thing.binding.ThingHandlerCallback; |
| 44 | +import org.openhab.core.types.State; |
| 45 | + |
| 46 | +import com.google.gson.Gson; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Leo Siepel - Initial contribution |
| 50 | + */ |
| 51 | +@NonNullByDefault |
| 52 | +public class GoveeSerializeGoveeHandlerTest { |
| 53 | + |
| 54 | + private static final Gson GSON = new Gson(); |
| 55 | + private final String invalidValueJsonString = "{\"msg\": {\"cmd\": \"devStatus\", \"data\": {\"onOff\": 0, \"brightness\": 100, \"color\": {\"r\": 1, \"g\": 10, \"b\": 0}, \"colorTemInKelvin\": 9070}}}"; |
| 56 | + |
| 57 | + private static final Configuration CONFIG = createConfig(true); |
| 58 | + private static final Configuration BAD_CONFIG = createConfig(false); |
| 59 | + |
| 60 | + private static Configuration createConfig(boolean returnValid) { |
| 61 | + final Configuration config = new Configuration(); |
| 62 | + if (returnValid) { |
| 63 | + config.put("hostname", "1.2.3.4"); |
| 64 | + } |
| 65 | + return config; |
| 66 | + } |
| 67 | + |
| 68 | + private static Thing mockThing(boolean withConfiguration) { |
| 69 | + final Thing thing = mock(Thing.class); |
| 70 | + when(thing.getUID()).thenReturn(new ThingUID(GoveeBindingConstants.THING_TYPE_LIGHT, "govee-test-thing")); |
| 71 | + when(thing.getConfiguration()).thenReturn(withConfiguration ? CONFIG : BAD_CONFIG); |
| 72 | + |
| 73 | + final List<Channel> channelList = Arrays.asList( |
| 74 | + mockChannel(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR), // |
| 75 | + mockChannel(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR_TEMPERATURE), // |
| 76 | + mockChannel(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS)); |
| 77 | + |
| 78 | + when(thing.getChannels()).thenReturn(channelList); |
| 79 | + return thing; |
| 80 | + } |
| 81 | + |
| 82 | + private static Channel mockChannel(final ThingUID thingId, final String channelId) { |
| 83 | + final Channel channel = Mockito.mock(Channel.class); |
| 84 | + when(channel.getUID()).thenReturn(new ChannelUID(thingId, channelId)); |
| 85 | + return channel; |
| 86 | + } |
| 87 | + |
| 88 | + private static GoveeHandlerMock createAndInitHandler(final ThingHandlerCallback callback, final Thing thing) { |
| 89 | + CommunicationManager communicationManager = mock(CommunicationManager.class); |
| 90 | + final GoveeHandlerMock handler = spy(new GoveeHandlerMock(thing, communicationManager)); |
| 91 | + |
| 92 | + handler.setCallback(callback); |
| 93 | + handler.initialize(); |
| 94 | + |
| 95 | + return handler; |
| 96 | + } |
| 97 | + |
| 98 | + private static State getState(final int input, Unit<?> unit) { |
| 99 | + return new QuantityType<>(input, unit); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testInvalidConfiguration() { |
| 104 | + final Thing thing = mockThing(false); |
| 105 | + final ThingHandlerCallback callback = mock(ThingHandlerCallback.class); |
| 106 | + final GoveeHandlerMock handler = createAndInitHandler(callback, thing); |
| 107 | + |
| 108 | + try { |
| 109 | + verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.OFFLINE) |
| 110 | + && arg.getStatusDetail().equals(ThingStatusDetail.CONFIGURATION_ERROR))); |
| 111 | + } finally { |
| 112 | + handler.dispose(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testInvalidResponseMessage() { |
| 118 | + final Thing thing = mockThing(true); |
| 119 | + final ThingHandlerCallback callback = mock(ThingHandlerCallback.class); |
| 120 | + final GoveeHandlerMock handler = createAndInitHandler(callback, thing); |
| 121 | + |
| 122 | + // inject StatusResponseMessage |
| 123 | + StatusResponse statusMessage = GSON.fromJson(invalidValueJsonString, StatusResponse.class); |
| 124 | + handler.updateDeviceState(statusMessage); |
| 125 | + |
| 126 | + try { |
| 127 | + verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN))); |
| 128 | + |
| 129 | + verify(callback).stateUpdated(new ChannelUID(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR), |
| 130 | + new HSBType(new DecimalType(114), new PercentType(100), new PercentType(0))); |
| 131 | + |
| 132 | + verify(callback).stateUpdated( |
| 133 | + new ChannelUID(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR_TEMPERATURE), |
| 134 | + new PercentType(100)); |
| 135 | + |
| 136 | + verify(callback).stateUpdated( |
| 137 | + new ChannelUID(thing.getUID(), GoveeBindingConstants.CHANNEL_COLOR_TEMPERATURE_ABS), |
| 138 | + getState(2000, Units.KELVIN)); |
| 139 | + } finally { |
| 140 | + handler.dispose(); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments