Skip to content

Commit

Permalink
openhab#15 Use reflection to load all channels
Browse files Browse the repository at this point in the history
  • Loading branch information
magx2 committed May 16, 2019
1 parent ddfe8d7 commit eeb9743
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.glytching.junit.extension.random.Random;
import io.github.glytching.junit.extension.random.RandomBeansExtension;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.thing.Bridge;
Expand Down Expand Up @@ -33,11 +34,12 @@

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.lang.String.valueOf;
import static java.util.Arrays.asList;
import static org.apache.commons.lang3.reflect.FieldUtils.writeField;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -106,7 +108,12 @@ void setUp() throws IllegalAccessException, ApiException {
}

void setUpChannels() {
allChannels = asList(lightChannel, rgbChannel, dimmerAndRgbChannel, rollerShutterChannel, gateChannel);
allChannels = FieldUtils.getAllFieldsList(CloudDeviceHandlerTest.class)
.stream()
.filter(field -> Channel.class.isAssignableFrom(field.getType()))
.map(this::readField)
.map(channel -> (Channel) channel)
.collect(Collectors.toList());
allChannels.forEach(channel -> given(channel.isHidden()).willReturn(false));
given(lightChannel.getFunction()).willReturn(new ChannelFunction().name(LIGHTSWITCH));
given(rgbChannel.getFunction()).willReturn(new ChannelFunction().name(RGBLIGHTING));
Expand All @@ -121,6 +128,14 @@ void setUpChannels() {
given(gateChannel.getId()).willReturn(gateChannelId);
}

private Object readField(final Field field) {
try {
return FieldUtils.readDeclaredField(this, field.getName(), true);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
}

void setUpInternalInitialize() throws ApiException, IllegalAccessException {
given(thing.getBridgeUID()).willReturn(bridgeUid);
given(thingRegistry.get(bridgeUid)).willReturn(bridge);
Expand Down

0 comments on commit eeb9743

Please sign in to comment.