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