Skip to content

Commit

Permalink
openhab#15 Write test for changing (color)brightness for RGB
Browse files Browse the repository at this point in the history
  • Loading branch information
magx2 committed May 17, 2019
1 parent e1d1c58 commit 9da45d4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
import org.openhab.binding.supla.internal.cloud.ChannelFunctionDispatcher;
import org.openhab.binding.supla.internal.cloud.ChannelInfo;
import org.openhab.binding.supla.internal.cloud.ChannelInfoParser;
import org.openhab.binding.supla.internal.cloud.LedCommandExecutor;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.api.IoDevicesCloudApi;
import org.openhab.binding.supla.internal.cloud.api.IoDevicesCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.api.SwaggerChannelsCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.api.SwaggerIoDevicesCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.executors.LedCommandExecutor;
import org.openhab.binding.supla.internal.cloud.executors.LedCommandExecutorFactory;
import org.openhab.binding.supla.internal.cloud.executors.SuplaLedCommandExecutorFactory;
import org.openhab.binding.supla.internal.cloud.functionswitch.CreateChannelFunctionSwitch;
import org.openhab.binding.supla.internal.cloud.functionswitch.FindStateFunctionSwitch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.grzeslowski.jsupla.api.generated.ApiClient;
import pl.grzeslowski.jsupla.api.generated.ApiException;
import pl.grzeslowski.jsupla.api.generated.model.ChannelExecuteActionRequest;
import pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum;
Expand Down Expand Up @@ -83,8 +84,8 @@ public final class CloudDeviceHandler extends AbstractDeviceHandler {
private final Logger logger = LoggerFactory.getLogger(CloudBridgeHandler.class);
private final ChannelsCloudApiFactory channelsCloudApiFactory;
private final IoDevicesCloudApiFactory ioDevicesCloudApiFactory;
private final LedCommandExecutorFactory ledCommandExecutorFactory;

private ApiClient apiClient;
private ChannelsCloudApi channelsApi;
private int cloudId;
private IoDevicesCloudApi ioDevicesApi;
Expand All @@ -95,14 +96,20 @@ public final class CloudDeviceHandler extends AbstractDeviceHandler {
CloudDeviceHandler(
final Thing thing,
final ChannelsCloudApiFactory channelsCloudApiFactory,
final IoDevicesCloudApiFactory ioDevicesCloudApiFactory) {
final IoDevicesCloudApiFactory ioDevicesCloudApiFactory,
final LedCommandExecutorFactory ledCommandExecutorFactory) {
super(thing);
this.channelsCloudApiFactory = channelsCloudApiFactory;
this.ioDevicesCloudApiFactory = ioDevicesCloudApiFactory;
this.ledCommandExecutorFactory = ledCommandExecutorFactory;
}

public CloudDeviceHandler(final Thing thing) {
this(thing, SwaggerChannelsCloudApiFactory.FACTORY, SwaggerIoDevicesCloudApiFactory.FACTORY);
this(
thing,
SwaggerChannelsCloudApiFactory.FACTORY,
SwaggerIoDevicesCloudApiFactory.FACTORY,
SuplaLedCommandExecutorFactory.FACTORY);
}

@Override
Expand Down Expand Up @@ -202,7 +209,7 @@ private void initChannels() {
}

private void initCommandExecutors() {
ledCommandExecutor = new LedCommandExecutor(channelsApi);
ledCommandExecutor = ledCommandExecutorFactory.newLedCommandExecutor(channelsApi);
}

private void updateChannels(final List<Channel> channels) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openhab.binding.supla.internal.cloud.executors;

import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import pl.grzeslowski.jsupla.api.generated.ApiException;

public interface LedCommandExecutor {
void setLedState(ChannelUID channelUID, PercentType brightness);

void setLedState(ChannelUID channelUID, HSBType hsb);

void changeColor(final int channelId, final ChannelUID channelUID, final HSBType command) throws ApiException;

void changeColorBrightness(final int channelId, final ChannelUID channelUID, final PercentType command) throws ApiException;

void changeBrightness(final int channelId, final ChannelUID channelUID, final PercentType command) throws ApiException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openhab.binding.supla.internal.cloud.executors;

import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;

public interface LedCommandExecutorFactory {
LedCommandExecutor newLedCommandExecutor(ChannelsCloudApi channelsCloudApi);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.openhab.binding.supla.internal.cloud;
package org.openhab.binding.supla.internal.cloud.executors;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.openhab.binding.supla.internal.cloud.HsbTypeConverter;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,15 +20,16 @@
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum.SET_RGBW_PARAMETERS;

@SuppressWarnings("PackageAccessibility")
public class LedCommandExecutor {
private final Logger logger = LoggerFactory.getLogger(LedCommandExecutor.class);
final class SuplaLedCommandExecutor implements LedCommandExecutor {
private final Logger logger = LoggerFactory.getLogger(SuplaLedCommandExecutor.class);
private final Map<ChannelUID, LedState> ledStates = new HashMap<>();
private final ChannelsCloudApi channelsApi;

public LedCommandExecutor(final ChannelsCloudApi channelsApi) {
SuplaLedCommandExecutor(final ChannelsCloudApi channelsApi) {
this.channelsApi = channelsApi;
}

@Override
public void setLedState(ChannelUID channelUID, PercentType brightness) {
final Optional<LedState> ledState = findLedState(channelUID);
if (ledState.isPresent()) {
Expand All @@ -37,6 +39,7 @@ public void setLedState(ChannelUID channelUID, PercentType brightness) {
}
}

@Override
public void setLedState(ChannelUID channelUID, HSBType hsb) {
final Optional<LedState> ledState = findLedState(channelUID);
if (ledState.isPresent()) {
Expand All @@ -46,13 +49,15 @@ public void setLedState(ChannelUID channelUID, HSBType hsb) {
}
}

@Override
public void changeColor(final int channelId, final ChannelUID channelUID, final HSBType command) throws ApiException {
final Optional<LedState> state = findLedState(channelUID);
if (state.isPresent()) {
sendNewLedValue(channelUID, channelId, command, state.get().brightness);
}
}

@Override
public void changeColorBrightness(final int channelId, final ChannelUID channelUID, final PercentType command) throws ApiException {
final Optional<LedState> state = findLedState(channelUID);
if (state.isPresent()) {
Expand All @@ -66,6 +71,7 @@ public void changeColorBrightness(final int channelId, final ChannelUID channelU
}
}

@Override
public void changeBrightness(final int channelId, final ChannelUID channelUID, final PercentType command) throws ApiException {
final Optional<LedState> state = findLedState(channelUID);
if (state.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.openhab.binding.supla.internal.cloud.executors;

import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;

public final class SuplaLedCommandExecutorFactory implements LedCommandExecutorFactory {
public static final SuplaLedCommandExecutorFactory FACTORY = new SuplaLedCommandExecutorFactory();

@Override
public LedCommandExecutor newLedCommandExecutor(final ChannelsCloudApi channelsCloudApi) {
return new SuplaLedCommandExecutor(channelsCloudApi);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.openhab.binding.supla.internal.cloud.ChannelInfo;
import org.openhab.binding.supla.internal.cloud.ChannelInfoParser;
import org.openhab.binding.supla.internal.cloud.HsbTypeConverter;
import org.openhab.binding.supla.internal.cloud.LedCommandExecutor;
import org.openhab.binding.supla.internal.cloud.executors.LedCommandExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.grzeslowski.jsupla.api.generated.model.Channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.supla.internal.cloud.AdditionalChannelType;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApi;
import org.openhab.binding.supla.internal.cloud.api.ChannelsCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.api.IoDevicesCloudApi;
import org.openhab.binding.supla.internal.cloud.api.IoDevicesCloudApiFactory;
import org.openhab.binding.supla.internal.cloud.executors.LedCommandExecutor;
import org.openhab.binding.supla.internal.cloud.executors.LedCommandExecutorFactory;
import pl.grzeslowski.jsupla.api.generated.ApiException;
import pl.grzeslowski.jsupla.api.generated.model.Channel;
import pl.grzeslowski.jsupla.api.generated.model.ChannelExecuteActionRequest;
Expand All @@ -57,6 +60,7 @@
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.openhab.binding.supla.SuplaBindingConstants.SUPLA_DEVICE_CLOUD_ID;
import static org.openhab.binding.supla.internal.cloud.AdditionalChannelType.LED_BRIGHTNESS;
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum.CLOSE;
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum.OPEN;
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionActionEnum.REVEAL;
Expand All @@ -71,14 +75,16 @@
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionEnumNames.LIGHTSWITCH;
import static pl.grzeslowski.jsupla.api.generated.model.ChannelFunctionEnumNames.RGBLIGHTING;

@SuppressWarnings("WeakerAccess")
@SuppressWarnings({"WeakerAccess", "unused"})
@ExtendWith(MockitoExtension.class)
@ExtendWith(RandomBeansExtension.class)
class CloudDeviceHandlerTest {
CloudDeviceHandler handler;
@Mock Thing thing;
@Mock Bridge bridge;
@Mock ThingUID bridgeUid;
@Mock LedCommandExecutorFactory ledCommandExecutorFactory;
@Mock LedCommandExecutor ledCommandExecutor;
@Mock ThingRegistry thingRegistry;
@Mock CloudBridgeHandler bridgeHandler;
@Mock Configuration configuration;
Expand Down Expand Up @@ -109,7 +115,6 @@ class CloudDeviceHandlerTest {
@Random
@Min(1) @Max(100) int garageDoorChannelId;


List<Channel> allChannels;

@Random String oAuthToken;
Expand Down Expand Up @@ -172,6 +177,7 @@ void setUpInternalInitialize() throws ApiException, IllegalAccessException {
given(device.isEnabled()).willReturn(true);
given(device.getChannels()).willReturn(allChannels);
given(thing.getUID()).willReturn(thingUID);
given(ledCommandExecutorFactory.newLedCommandExecutor(channelsCloudApi)).willReturn(ledCommandExecutor);
given(channelsCloudApi.getChannel(anyInt(), any())).willAnswer(invocationOnMock -> {
int channelId = invocationOnMock.getArgument(0);
return allChannels.stream()
Expand All @@ -180,7 +186,7 @@ void setUpInternalInitialize() throws ApiException, IllegalAccessException {
.orElseThrow(IllegalArgumentException::new);
});

handler = new CloudDeviceHandler(thing, channelsCloudApiFactory, ioDevicesCloudApiFactory);
handler = new CloudDeviceHandler(thing, channelsCloudApiFactory, ioDevicesCloudApiFactory, ledCommandExecutorFactory);
writeField(handler, "thingRegistry", thingRegistry, true);
writeField(handler, "callback", callback, true);
doAnswer(__ -> {
Expand Down Expand Up @@ -345,6 +351,38 @@ void revealPartiallyRollerShutter() throws Exception {
assertThat(value.getPercentage()).isEqualTo(percentage);
}

@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {"rgbChannelId", "dimmerAndRgbChannelId"})
@DisplayName("should send request to LedExecutor to change color brightness")
void revealPartiallyRollerShutter(String idFieldName) throws Exception {

// given
final int id = (int) FieldUtils.readDeclaredField(this, idFieldName, true);
final ChannelUID channelUID = buildChannelUID(id);
final PercentType command = new PercentType(33);

// when
handler.handlePercentCommand(channelUID, command);

// then
verify(ledCommandExecutor).changeColorBrightness(id, channelUID, command);
}

@Test
@DisplayName("should send request to Supla Cloud to reveal partially roller shutter")
void changeBrightness() throws Exception {

// given
ChannelUID dimmerAndRgbChannelUID = findDimmerAndRgbChannelUID(LED_BRIGHTNESS);
final PercentType command = new PercentType(33);

// when
handler.handlePercentCommand(dimmerAndRgbChannelUID, command);

// then
verify(ledCommandExecutor).changeBrightness(dimmerAndRgbChannelId, dimmerAndRgbChannelUID, command);
}

ChannelUID buildChannelUID(int id) {
return new ChannelUID(thingUID, valueOf(id));
}
Expand All @@ -356,4 +394,9 @@ ChannelUID findLightChannelUID() {
ChannelUID findRollerShutterChannelUID() {
return buildChannelUID(rollerShutterChannelId);
}

@SuppressWarnings("SameParameterValue")
ChannelUID findDimmerAndRgbChannelUID(AdditionalChannelType channelType) {
return new ChannelUID(thingUID, dimmerAndRgbChannelId + channelType.getSuffix());
}
}

0 comments on commit 9da45d4

Please sign in to comment.