]> git.basschouten.com Git - openhab-addons.git/blob
7047f6e98eb94c38b55b3f9d0e0071aedfcd5a46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
17 import java.util.Locale;
18
19 import javax.measure.quantity.Dimensionless;
20
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.transform.TransformationException;
25
26 /**
27  * @author GaĆ«l L'hopital - Initial contribution
28  */
29 public class ScaleTransformServiceTest {
30     private ScaleTransformationService processor;
31
32     @BeforeEach
33     public void init() {
34         processor = new ScaleTransformationService() {
35             @Override
36             protected Locale getLocale() {
37                 return Locale.US;
38             }
39         };
40     }
41
42     @Test
43     public void testTransformByScale() throws TransformationException {
44         // need to be sure we'll have the german version
45         String existingscale = "scale/humidex_de.scale";
46         String source = "10";
47         String transformedResponse = processor.transform(existingscale, source);
48         assertEquals("nicht wesentlich", transformedResponse);
49
50         existingscale = "scale/limits.scale";
51         source = "10";
52         transformedResponse = processor.transform(existingscale, source);
53         assertEquals("middle", transformedResponse);
54     }
55
56     @Test
57     public void testTransformByScaleLimits() throws TransformationException {
58         String existingscale = "scale/limits.scale";
59
60         // Testing upper bound opened range
61         String source = "500";
62         String transformedResponse = processor.transform(existingscale, source);
63         assertEquals("extreme", transformedResponse);
64
65         // Testing lower bound opened range
66         source = "-10";
67         transformedResponse = processor.transform(existingscale, source);
68         assertEquals("low", transformedResponse);
69
70         // Testing unfinite up and down range
71         existingscale = "scale/catchall.scale";
72         source = "-10";
73         transformedResponse = processor.transform(existingscale, source);
74         assertEquals("catchall", transformedResponse);
75     }
76
77     @Test
78     public void testTransformByScaleUndef() throws TransformationException {
79         // check that for undefined/non numeric value we return empty string
80         // Issue #1107
81         String existingscale = "scale/humidex_fr.scale";
82         String source = "-";
83         String transformedResponse = processor.transform(existingscale, source);
84         assertEquals("", transformedResponse);
85     }
86
87     @Test
88     public void testTransformByScaleErrorInBounds() throws TransformationException {
89         // the tested file contains inputs that generate a conversion error of the bounds
90         // of range
91         String existingscale = "scale/erroneous.scale";
92         String source = "15";
93         try {
94             @SuppressWarnings("unused")
95             String transformedResponse = processor.transform(existingscale, source);
96             fail();
97         } catch (TransformationException e) {
98             // awaited result
99         }
100     }
101
102     @Test
103     public void testTransformByScaleErrorInValue() throws TransformationException {
104         // checks that an error is raised when trying to scale an erroneous value
105         String existingscale = "scale/evaluationorder.scale";
106         String source = "azerty";
107         String transformedResponse = processor.transform(existingscale, source);
108         assertEquals("", transformedResponse);
109     }
110
111     @Test
112     public void testEvaluationOrder() throws TransformationException {
113         // Ensures that only first matching scale as presented in the file is taken in account
114         String evaluationOrder = "scale/evaluationorder.scale";
115         // This value matches two lines of the scale file
116         String source = "12";
117
118         String transformedResponse = processor.transform(evaluationOrder, source);
119         assertEquals("first", transformedResponse);
120     }
121
122     @Test
123     public void testTransformQuantityType() throws TransformationException {
124         QuantityType<Dimensionless> airQuality = new QuantityType<>("992 ppm");
125         String aqScaleFile = "scale/netatmo_aq.scale";
126         String expected = "Correcte (992 ppm) !";
127
128         String transformedResponse = processor.transform(aqScaleFile, airQuality.toString());
129         assertEquals(expected, transformedResponse);
130     }
131
132     @Test
133     public void testCatchNonNumericValue() throws TransformationException {
134         // checks that an error is raised when trying to scale an erroneous value
135         String existingscale = "scale/catchnonnumeric.scale";
136         String source = "azerty";
137         String transformedResponse = processor.transform(existingscale, source);
138         assertEquals("Non Numeric", transformedResponse);
139     }
140
141     @Test
142     public void testTransformAndFormat() throws TransformationException {
143         String existingscale = "scale/netatmo_aq.scale";
144         String source = "992";
145         String transformedResponse = processor.transform(existingscale, source);
146         assertEquals("Correcte (992) !", transformedResponse);
147     }
148
149     @Test
150     public void testValueExceedsRange() throws TransformationException {
151         String existingscale = "scale/humidex.scale";
152         String source = "200";
153         String transformedResponse = processor.transform(existingscale, source);
154         assertEquals("", transformedResponse);
155     }
156 }