]> git.basschouten.com Git - openhab-addons.git/blob
1a6095df8cd07d2d8588b85e368235ffe36ebb8c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.scale.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.ArgumentMatchers.eq;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.measure.quantity.Dimensionless;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.mockito.junit.jupiter.MockitoSettings;
37 import org.mockito.quality.Strictness;
38 import org.mockito.stubbing.Answer;
39 import org.openhab.core.library.types.QuantityType;
40 import org.openhab.core.transform.Transformation;
41 import org.openhab.core.transform.TransformationException;
42 import org.openhab.core.transform.TransformationRegistry;
43
44 /**
45  * @author GaĆ«l L'hopital - Initial contribution
46  */
47 @ExtendWith(MockitoExtension.class)
48 @MockitoSettings(strictness = Strictness.LENIENT)
49 @NonNullByDefault
50 public class ScaleTransformServiceTest {
51     private static final String SRC_FOLDER = "conf" + File.separator + "transform";
52
53     @Mock
54     private @NonNullByDefault({}) TransformationRegistry transformationConfigurationRegistry;
55     private final Map<String, Transformation> configurationMap = new HashMap<>();
56     private @NonNullByDefault({}) ScaleTransformationService processor;
57
58     @BeforeEach
59     public void init() throws IOException {
60         configurationMap.clear();
61         Files.walk(Path.of(SRC_FOLDER)).filter(Files::isRegularFile).forEach(file -> {
62             try {
63                 String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
64                 String uid = Path.of(SRC_FOLDER).relativize(file).toString();
65                 Transformation transformationConfiguration = new Transformation(uid, uid, "scale",
66                         Map.of(Transformation.FUNCTION, content));
67                 configurationMap.put(uid, transformationConfiguration);
68             } catch (IOException ignored) {
69             }
70         });
71
72         Mockito.when(transformationConfigurationRegistry.get(anyString(), eq(null)))
73                 .thenAnswer((Answer<Transformation>) invocation -> {
74                     Object[] args = invocation.getArguments();
75                     return configurationMap.get(args[0]);
76                 });
77         processor = new ScaleTransformationService(transformationConfigurationRegistry);
78     }
79
80     @Test
81     public void testTransformByScale() throws TransformationException {
82         // need to be sure we'll have the german version
83         String existingscale = "scale/humidex_de.scale";
84         String source = "10";
85         String transformedResponse = processor.transform(existingscale, source);
86         assertEquals("nicht wesentlich", transformedResponse);
87
88         existingscale = "scale/limits.scale";
89         source = "10";
90         transformedResponse = processor.transform(existingscale, source);
91         assertEquals("middle", transformedResponse);
92     }
93
94     @Test
95     public void testTransformByScaleLimits() throws TransformationException {
96         String existingscale = "scale/limits.scale";
97
98         // Testing upper bound opened range
99         String source = "500";
100         String transformedResponse = processor.transform(existingscale, source);
101         assertEquals("extreme", transformedResponse);
102
103         // Testing lower bound opened range
104         source = "-10";
105         transformedResponse = processor.transform(existingscale, source);
106         assertEquals("low", transformedResponse);
107
108         // Testing unfinite up and down range
109         existingscale = "scale/catchall.scale";
110         source = "-10";
111         transformedResponse = processor.transform(existingscale, source);
112         assertEquals("catchall", transformedResponse);
113     }
114
115     @Test
116     public void testTransformByScaleUndef() throws TransformationException {
117         // check that for undefined/non numeric value we return empty string
118         // Issue #1107
119         String existingscale = "scale/humidex_fr.scale";
120         String source = "-";
121         assertThrows(TransformationException.class, () -> processor.transform(existingscale, source));
122     }
123
124     @Test
125     public void testTransformByScaleErrorInBounds() throws TransformationException {
126         // the tested file contains inputs that generate a conversion error of the bounds
127         // of range
128         String existingscale = "scale/erroneous.scale";
129         String source = "15";
130         try {
131             @SuppressWarnings("unused")
132             String transformedResponse = processor.transform(existingscale, source);
133             fail();
134         } catch (TransformationException e) {
135             // awaited result
136         }
137     }
138
139     @Test
140     public void testTransformByScaleErrorInValue() throws TransformationException {
141         // checks that an error is raised when trying to scale an erroneous value
142         String existingscale = "scale/evaluationorder.scale";
143         String source = "azerty";
144         assertThrows(TransformationException.class, () -> processor.transform(existingscale, source));
145     }
146
147     @Test
148     public void testEvaluationOrder() throws TransformationException {
149         // Ensures that only first matching scale as presented in the file is taken in account
150         String evaluationOrder = "scale/evaluationorder.scale";
151         // This value matches two lines of the scale file
152         String source = "12";
153
154         String transformedResponse = processor.transform(evaluationOrder, source);
155         assertEquals("first", transformedResponse);
156     }
157
158     @Test
159     public void testTransformQuantityType() throws TransformationException {
160         QuantityType<Dimensionless> airQuality = new QuantityType<>("992 ppm");
161         String aqScaleFile = "scale/netatmo_aq.scale";
162         String expected = "Correcte (992 ppm) !";
163
164         String transformedResponse = processor.transform(aqScaleFile, airQuality.toString());
165         assertEquals(expected, transformedResponse);
166     }
167
168     @Test
169     public void testCatchNonNumericValue() throws TransformationException {
170         // checks that an error is raised when trying to scale an erroneous value
171         String existingscale = "scale/catchnonnumeric.scale";
172         String source = "azerty";
173         String transformedResponse = processor.transform(existingscale, source);
174         assertEquals("Non Numeric", transformedResponse);
175     }
176
177     @Test
178     public void testTransformAndFormat() throws TransformationException {
179         String existingscale = "scale/netatmo_aq.scale";
180         String source = "992";
181         String transformedResponse = processor.transform(existingscale, source);
182         assertEquals("Correcte (992) !", transformedResponse);
183     }
184
185     @Test
186     public void testValueExceedsRange() throws TransformationException {
187         String existingscale = "scale/humidex.scale";
188         String source = "200";
189         assertThrows(TransformationException.class, () -> processor.transform(existingscale, source));
190     }
191 }