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

[transform.chain] Use core transformation helpers #551

Merged
merged 1 commit into from
Dec 28, 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 @@ -16,6 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.binding.generic.ChannelTransformation;
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.ProfileContext;
import org.openhab.core.thing.profiles.ProfileTypeUID;
Expand All @@ -26,9 +27,6 @@
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.commons.transform.NoOpValueTransformation;
import org.smarthomej.commons.transform.ValueTransformation;
import org.smarthomej.commons.transform.ValueTransformationProvider;

/**
* Profile to offer the ChainTransformation on a ItemChannelLink
Expand All @@ -37,29 +35,25 @@
*/
@NonNullByDefault
public class ChainTransformationProfile implements StateProfile {

public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
TransformationService.TRANSFORM_PROFILE_SCOPE, "CHAIN");

private final Logger logger = LoggerFactory.getLogger(ChainTransformationProfile.class);
private final ProfileCallback callback;

private final ValueTransformationProvider valueTransformationProvider;
private final ChainTransformationProfileConfiguration configuration;

private ValueTransformation toItem;
private ValueTransformation toChannel;
private ChannelTransformation toItem;
private ChannelTransformation toChannel;

public ChainTransformationProfile(ProfileCallback callback, ProfileContext context,
ValueTransformationProvider valueTransformationProvider) {
public ChainTransformationProfile(ProfileCallback callback, ProfileContext context) {
this.callback = callback;
this.valueTransformationProvider = valueTransformationProvider;

configuration = context.getConfiguration().as(ChainTransformationProfileConfiguration.class);
logger.debug("Profile configured with: '{}'", configuration);

toItem = valueTransformationProvider.getValueTransformation(configuration.toItem);
toChannel = valueTransformationProvider.getValueTransformation(configuration.toChannel);
toItem = new ChannelTransformation(configuration.toItem);
toChannel = new ChannelTransformation(configuration.toChannel);
}

@Override
Expand Down Expand Up @@ -96,15 +90,15 @@ public void onStateUpdateFromHandler(State state) {
}

private Optional<StringType> transformToItem(String input) {
if (!NoOpValueTransformation.getInstance().equals(toItem) && !configuration.toItem.isEmpty()) {
toItem = valueTransformationProvider.getValueTransformation(configuration.toItem);
if (!configuration.toItem.isEmpty()) {
toItem = new ChannelTransformation(configuration.toItem);
}
return toItem.apply(input).map(StringType::new);
}

private Optional<StringType> transformToChannel(String input) {
if (!NoOpValueTransformation.getInstance().equals(toChannel) && !configuration.toChannel.isEmpty()) {
toChannel = valueTransformationProvider.getValueTransformation(configuration.toChannel);
if (!configuration.toChannel.isEmpty()) {
toChannel = new ChannelTransformation(configuration.toChannel);
}
return toChannel.apply(input).map(StringType::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import org.openhab.core.thing.profiles.ProfileTypeBuilder;
import org.openhab.core.thing.profiles.ProfileTypeProvider;
import org.openhab.core.thing.profiles.ProfileTypeUID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.smarthomej.commons.transform.ValueTransformationProvider;

/**
*
Expand All @@ -40,12 +37,6 @@
@NonNullByDefault
@Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
public class ChainTransformationProfileFactory implements ProfileFactory, ProfileTypeProvider {
private final ValueTransformationProvider valueTransformationProvider;

@Activate
public ChainTransformationProfileFactory(@Reference ValueTransformationProvider valueTransformationProvider) {
this.valueTransformationProvider = valueTransformationProvider;
}

@Override
public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
Expand All @@ -57,7 +48,7 @@ public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
ProfileContext profileContext) {
return ChainTransformationProfile.PROFILE_TYPE_UID.equals(profileTypeUID)
? new ChainTransformationProfile(callback, profileContext, valueTransformationProvider)
? new ChainTransformationProfile(callback, profileContext)
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.*;

import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -36,10 +34,14 @@
import org.openhab.core.thing.profiles.ProfileCallback;
import org.openhab.core.thing.profiles.ProfileContext;
import org.openhab.core.thing.profiles.StateProfile;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.openhab.core.transform.TransformationService;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.smarthomej.transform.chain.internal.test.TestValueTransformationProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
* The {@link ChainTransformationProfileTest} are test cases for the ChainTransformationProfile
Expand All @@ -57,15 +59,42 @@ public class ChainTransformationProfileTest {

public static final String FAIL_TRANSFORMATION_PATTERN = "APPEND:foo∩FAIL:noparam∩DUPLICATE:noparam";

@Mock
private @NonNullByDefault({}) ProfileCallback callback;
private static final ServiceReference<TransformationService> appendTransformationServiceRefMock = mock(
ServiceReference.class);
private static final ServiceReference<TransformationService> duplicateTransformationServiceRefMock = mock(
ServiceReference.class);
private static final ServiceReference<TransformationService> failTransformationServiceRefMock = mock(
ServiceReference.class);
private static final BundleContext bundleContextMock = mock(BundleContext.class);

@Mock
private @NonNullByDefault({}) ProfileContext context;
private @Mock @NonNullByDefault({}) ProfileCallback callback;
private @Mock @NonNullByDefault({}) ProfileContext context;

private @NonNullByDefault({}) ArgumentCaptor<State> stateCaptor;
private @NonNullByDefault({}) ArgumentCaptor<Command> commandCaptor;

@BeforeAll
public static void beforeAll() {
when(duplicateTransformationServiceRefMock.getProperty(TransformationService.SERVICE_PROPERTY_NAME))
.thenReturn("DUPLICATE");
when(appendTransformationServiceRefMock.getProperty(TransformationService.SERVICE_PROPERTY_NAME))
.thenReturn("APPEND");
when(failTransformationServiceRefMock.getProperty(TransformationService.SERVICE_PROPERTY_NAME))
.thenReturn("FAIL");

when(bundleContextMock.getService(duplicateTransformationServiceRefMock))
.thenReturn(new DuplicateTransformationService());
when(bundleContextMock.getService(appendTransformationServiceRefMock))
.thenReturn(new AppendTransformationService());
when(bundleContextMock.getService(failTransformationServiceRefMock))
.thenReturn(new FailTransformationService());

TransformationHelper helper = new TransformationHelper(bundleContextMock);
helper.setTransformationService(duplicateTransformationServiceRefMock);
helper.setTransformationService(appendTransformationServiceRefMock);
helper.setTransformationService(failTransformationServiceRefMock);
}

@BeforeEach
public void setup() {
stateCaptor = ArgumentCaptor.forClass(State.class);
Expand Down Expand Up @@ -230,6 +259,30 @@ private StateProfile getProfile(String toItem, String toChannel, boolean undefOn
Map<String, Object> configuration = Map.of("toItem", toItem, "toChannel", toChannel, "undefOnError",
undefOnError);
doReturn(new Configuration(configuration)).when(context).getConfiguration();
return new ChainTransformationProfile(callback, context, new TestValueTransformationProvider());
return new ChainTransformationProfile(callback, context);
}

private static class FailTransformationService implements TransformationService {

@Override
public @Nullable String transform(String s, String s1) throws TransformationException {
return null;
}
}

private static class AppendTransformationService implements TransformationService {

@Override
public @Nullable String transform(String s, String s1) throws TransformationException {
return s1 + s;
}
}

private static class DuplicateTransformationService implements TransformationService {

@Override
public @Nullable String transform(String s, String s1) throws TransformationException {
return s1 + s1;
}
}
}

This file was deleted.

Loading