Skip to content

Commit

Permalink
[modbus] Take cascaded transformation into use
Browse files Browse the repository at this point in the history
Signed-off-by: Sami Salonen <ssalonen@gmail.com>
  • Loading branch information
ssalonen committed Jan 26, 2021
1 parent b63f168 commit f4d22e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
package org.openhab.binding.modbus.internal;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.osgi.framework.BundleContext;

/**
Expand All @@ -30,9 +32,15 @@
public class CascadedValueTransformationImpl implements ValueTransformation {
private final List<SingleValueTransformation> transformations;

public CascadedValueTransformationImpl(final String transformationString) {
transformations = Arrays.stream(transformationString.split("∩")).filter(s -> !s.isEmpty())
.map(transformation -> new SingleValueTransformation(transformation)).collect(Collectors.toList());
public CascadedValueTransformationImpl(@Nullable String transformationString) {
String transformationNonNull = transformationString == null ? "" : transformationString;
List<SingleValueTransformation> localTransformations = Arrays.stream(transformationNonNull.split("∩"))
.filter(s -> !s.isEmpty()).map(transformation -> new SingleValueTransformation(transformation))
.collect(Collectors.toList());
if (localTransformations.isEmpty()) {
localTransformations = Collections.singletonList(new SingleValueTransformation(transformationString));
}
transformations = localTransformations;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
import org.openhab.binding.modbus.handler.ModbusPollerThingHandler;
import org.openhab.binding.modbus.internal.CascadedValueTransformationImpl;
import org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal;
import org.openhab.binding.modbus.internal.ModbusConfigurationException;
import org.openhab.binding.modbus.internal.SingleValueTransformation;
Expand Down Expand Up @@ -130,8 +131,8 @@ public class ModbusDataThingHandler extends BaseThingHandler {
private volatile @Nullable ModbusDataConfiguration config;
private volatile @Nullable ValueType readValueType;
private volatile @Nullable ValueType writeValueType;
private volatile @Nullable ValueTransformation readTransformation;
private volatile @Nullable ValueTransformation writeTransformation;
private volatile @Nullable CascadedValueTransformationImpl readTransformation;
private volatile @Nullable CascadedValueTransformationImpl writeTransformation;
private volatile Optional<Integer> readIndex = Optional.empty();
private volatile Optional<Integer> readSubIndex = Optional.empty();
private volatile @Nullable Integer writeStart;
Expand Down Expand Up @@ -505,7 +506,7 @@ private void validateAndParseReadParameters(ModbusDataConfiguration config) thro
throw new ModbusConfigurationException(errmsg);
}
}
readTransformation = new SingleValueTransformation(config.getReadTransform());
readTransformation = new CascadedValueTransformationImpl(config.getReadTransform());
validateReadIndex();
}

Expand All @@ -514,7 +515,7 @@ private void validateAndParseWriteParameters(ModbusDataConfiguration config) thr
boolean writeStartMissing = StringUtils.isBlank(config.getWriteStart());
boolean writeValueTypeMissing = StringUtils.isBlank(config.getWriteValueType());
boolean writeTransformationMissing = StringUtils.isBlank(config.getWriteTransform());
writeTransformation = new SingleValueTransformation(config.getWriteTransform());
writeTransformation = new CascadedValueTransformationImpl(config.getWriteTransform());
boolean writingCoil = WRITE_TYPE_COIL.equals(config.getWriteType());
writeParametersHavingTransformationOnly = (writeTypeMissing && writeStartMissing && writeValueTypeMissing
&& !writeTransformationMissing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/
package org.openhab.binding.modbus.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.osgi.framework.BundleContext;

/**
* @author Sami Salonen - Initial contribution
Expand All @@ -37,4 +39,40 @@ public void testTransformation() {

assertEquals(3, transformation.toString().split("∩").length);
}

@Test
public void testTransformationEmpty() {
CascadedValueTransformationImpl transformation = new CascadedValueTransformationImpl("");
assertFalse(transformation.isIdentityTransform());
assertEquals("", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationNull() {
CascadedValueTransformationImpl transformation = new CascadedValueTransformationImpl(null);
assertFalse(transformation.isIdentityTransform());
assertEquals("", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationDefault() {
CascadedValueTransformationImpl transformation = new CascadedValueTransformationImpl("deFault");
assertTrue(transformation.isIdentityTransform());
assertEquals("xx", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationDefaultChained() {
CascadedValueTransformationImpl transformation = new CascadedValueTransformationImpl("deFault∩DEFAULT∩default");
assertTrue(transformation.isIdentityTransform());
assertEquals("xx", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationDefaultChainedWithStatic() {
CascadedValueTransformationImpl transformation = new CascadedValueTransformationImpl(
"deFault∩DEFAULT∩default∩static");
assertFalse(transformation.isIdentityTransform());
assertEquals("static", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/
package org.openhab.binding.modbus.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.osgi.framework.BundleContext;

/**
* @author Sami Salonen - Initial contribution
Expand Down Expand Up @@ -48,4 +50,32 @@ public void testTransformationNewStyle2() {
assertEquals("REGEX", transformation.transformationServiceName);
assertEquals(":myregex(.*)", transformation.transformationServiceParam);
}

@Test
public void testTransformationEmpty() {
SingleValueTransformation transformation = new SingleValueTransformation("");
assertFalse(transformation.isIdentityTransform());
assertEquals("", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationNull() {
SingleValueTransformation transformation = new SingleValueTransformation(null);
assertFalse(transformation.isIdentityTransform());
assertEquals("", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationDefault() {
SingleValueTransformation transformation = new SingleValueTransformation("deFault");
assertTrue(transformation.isIdentityTransform());
assertEquals("xx", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}

@Test
public void testTransformationDefaultChainedWithStatic() {
SingleValueTransformation transformation = new SingleValueTransformation("static");
assertFalse(transformation.isIdentityTransform());
assertEquals("static", transformation.transform(Mockito.mock(BundleContext.class), "xx"));
}
}

0 comments on commit f4d22e3

Please sign in to comment.