From dbdaa30e5c7313bec8a736fc2b6479e2d355d9a6 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Thu, 25 Jun 2020 21:59:11 +0200 Subject: [PATCH 1/2] fix NPE Signed-off-by: Jan N. Klug --- .../conf/transform/scale/spektrum.scale | 4 ++++ .../scale/internal/ScaleTransformationService.java | 4 ++-- .../scale/internal/ScaleTransformServiceTest.java | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale diff --git a/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale b/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale new file mode 100644 index 0000000000000..0ccb21f400dab --- /dev/null +++ b/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale @@ -0,0 +1,4 @@ +[..10]=Kaltweiß +]10..90[=Gemischt +[90..100]=Warmweiß +NaN=Unbekannt \ No newline at end of file diff --git a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java index 5e3a5b16095b8..e5afba155c4e0 100644 --- a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java +++ b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/ScaleTransformationService.java @@ -93,7 +93,7 @@ public Object put(Object key, Object value) { * The method transforms the input source by matching searching * the range where it fits i.e. [min..max]=value or ]min..max]=value * - * @param properties the list of properties defining all the available ranges + * @param data the list of properties defining all the available ranges * @param source the input to transform * */ @@ -129,7 +129,7 @@ private String formatResult(Map data, String source, final BigDec private String getScaleResult(Map data, String source, final BigDecimal value) throws TransformationException { - return data.entrySet().stream().filter(entry -> entry.getKey().contains(value)).findFirst() + return data.entrySet().stream().filter(entry -> entry.getKey() != null && entry.getKey().contains(value)).findFirst() .map(Map.Entry::getValue) .orElseThrow(() -> new TransformationException("No matching range for '" + source + "'")); } diff --git a/bundles/org.openhab.transform.scale/src/test/java/org/openhab/transform/scale/internal/ScaleTransformServiceTest.java b/bundles/org.openhab.transform.scale/src/test/java/org/openhab/transform/scale/internal/ScaleTransformServiceTest.java index a32a786417b67..999c77c772ce8 100644 --- a/bundles/org.openhab.transform.scale/src/test/java/org/openhab/transform/scale/internal/ScaleTransformServiceTest.java +++ b/bundles/org.openhab.transform.scale/src/test/java/org/openhab/transform/scale/internal/ScaleTransformServiceTest.java @@ -146,4 +146,12 @@ public void testTransformAndFormat() throws TransformationException { String transformedResponse = processor.transform(existingscale, source); Assert.assertEquals("Correcte (992) !", transformedResponse); } + + @Test + public void testValueExceedsRange() throws TransformationException { + String existingscale = "scale/spektrum.scale"; + String source = "2409"; + String transformedResponse = processor.transform(existingscale, source); + Assert.assertEquals("", transformedResponse); + } } From 8bf566e77403d39e0ba44b7979fa9924f8b6e71d Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Fri, 26 Jun 2020 16:47:10 +0200 Subject: [PATCH 2/2] address review comments Signed-off-by: Jan N. Klug --- .../conf/transform/scale/spektrum.scale | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale b/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale index 0ccb21f400dab..c305b82a18add 100644 --- a/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale +++ b/bundles/org.openhab.transform.scale/conf/transform/scale/spektrum.scale @@ -1,4 +1,4 @@ [..10]=Kaltweiß ]10..90[=Gemischt [90..100]=Warmweiß -NaN=Unbekannt \ No newline at end of file +NaN=Unbekannt