diff --git a/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfile.java b/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfile.java index 3f6b74c82b..1765890055 100644 --- a/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfile.java +++ b/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfile.java @@ -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; @@ -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 @@ -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 @@ -96,15 +90,15 @@ public void onStateUpdateFromHandler(State state) { } private Optional 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 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); } diff --git a/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileFactory.java b/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileFactory.java index 140795e197..b21433538a 100644 --- a/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileFactory.java +++ b/bundles/org.smarthomej.transform.chain/src/main/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileFactory.java @@ -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; /** * @@ -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 getProfileTypes(@Nullable Locale locale) { @@ -57,7 +48,7 @@ public Collection 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; } diff --git a/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileTest.java b/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileTest.java index 6ca870eb95..64f27a97a6 100644 --- a/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileTest.java +++ b/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/ChainTransformationProfileTest.java @@ -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; @@ -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 @@ -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 appendTransformationServiceRefMock = mock( + ServiceReference.class); + private static final ServiceReference duplicateTransformationServiceRefMock = mock( + ServiceReference.class); + private static final ServiceReference 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 stateCaptor; private @NonNullByDefault({}) ArgumentCaptor 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); @@ -230,6 +259,30 @@ private StateProfile getProfile(String toItem, String toChannel, boolean undefOn Map 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; + } } } diff --git a/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/test/TestValueTransformationProvider.java b/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/test/TestValueTransformationProvider.java deleted file mode 100644 index 0c2623fb28..0000000000 --- a/bundles/org.smarthomej.transform.chain/src/test/java/org/smarthomej/transform/chain/internal/test/TestValueTransformationProvider.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright (c) 2021-2023 Contributors to the SmartHome/J project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.smarthomej.transform.chain.internal.test; - -import java.util.Map; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.transform.TransformationException; -import org.openhab.core.transform.TransformationService; -import org.smarthomej.commons.transform.CascadedValueTransformation; -import org.smarthomej.commons.transform.NoOpValueTransformation; -import org.smarthomej.commons.transform.ValueTransformation; -import org.smarthomej.commons.transform.ValueTransformationProvider; - -/** - * The {@link TestValueTransformationProvider} is a test transformation provider providing "APPEND", "DUPLICATE" and - * "FAIL" transformations - * - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class TestValueTransformationProvider implements ValueTransformationProvider { - private Map transformationServiceMap = Map.of("FAIL", - new FailTransformationService(), "APPEND", new AppendTransformationService(), "DUPLICATE", - new DuplicateTransformationService()); - - @Override - public ValueTransformation getValueTransformation(@Nullable String pattern) { - if (pattern == null || pattern.isEmpty()) { - return NoOpValueTransformation.getInstance(); - } - - return new CascadedValueTransformation(pattern, transformationServiceMap::get); - } - - 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; - } - } -}