2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.scale.internal;
15 import static org.junit.Assert.fail;
17 import java.util.Locale;
19 import javax.measure.quantity.Dimensionless;
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;
28 * @author Gaƫl L'hopital - Initial contribution
30 public class ScaleTransformServiceTest {
31 private ScaleTransformationService processor;
35 processor = new ScaleTransformationService() {
37 protected Locale getLocale() {
44 public void testTransformByScale() throws TransformationException {
45 // need to be sure we'll have the german version
46 String existingscale = "scale/humidex_de.scale";
48 String transformedResponse = processor.transform(existingscale, source);
49 Assert.assertEquals("nicht wesentlich", transformedResponse);
51 existingscale = "scale/limits.scale";
53 transformedResponse = processor.transform(existingscale, source);
54 Assert.assertEquals("middle", transformedResponse);
58 public void testTransformByScaleLimits() throws TransformationException {
59 String existingscale = "scale/limits.scale";
61 // Testing upper bound opened range
62 String source = "500";
63 String transformedResponse = processor.transform(existingscale, source);
64 Assert.assertEquals("extreme", transformedResponse);
66 // Testing lower bound opened range
68 transformedResponse = processor.transform(existingscale, source);
69 Assert.assertEquals("low", transformedResponse);
71 // Testing unfinite up and down range
72 existingscale = "scale/catchall.scale";
74 transformedResponse = processor.transform(existingscale, source);
75 Assert.assertEquals("catchall", transformedResponse);
79 public void testTransformByScaleUndef() throws TransformationException {
80 // check that for undefined/non numeric value we return empty string
82 String existingscale = "scale/humidex_fr.scale";
84 String transformedResponse = processor.transform(existingscale, source);
85 Assert.assertEquals("", transformedResponse);
89 public void testTransformByScaleErrorInBounds() throws TransformationException {
90 // the tested file contains inputs that generate a conversion error of the bounds
92 String existingscale = "scale/erroneous.scale";
95 @SuppressWarnings("unused")
96 String transformedResponse = processor.transform(existingscale, source);
98 } catch (TransformationException e) {
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);
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";
119 String transformedResponse = processor.transform(evaluationOrder, source);
120 Assert.assertEquals("first", transformedResponse);
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) !";
129 String transformedResponse = processor.transform(aqScaleFile, airQuality.toString());
130 Assert.assertEquals(expected, transformedResponse);
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);
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);
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);