2 * Copyright (c) 2010-2022 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.jupiter.api.Assertions.*;
17 import java.util.Locale;
19 import javax.measure.quantity.Dimensionless;
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;
27 * @author Gaƫl L'hopital - Initial contribution
29 public class ScaleTransformServiceTest {
30 private ScaleTransformationService processor;
34 processor = new ScaleTransformationService() {
36 protected Locale getLocale() {
43 public void testTransformByScale() throws TransformationException {
44 // need to be sure we'll have the german version
45 String existingscale = "scale/humidex_de.scale";
47 String transformedResponse = processor.transform(existingscale, source);
48 assertEquals("nicht wesentlich", transformedResponse);
50 existingscale = "scale/limits.scale";
52 transformedResponse = processor.transform(existingscale, source);
53 assertEquals("middle", transformedResponse);
57 public void testTransformByScaleLimits() throws TransformationException {
58 String existingscale = "scale/limits.scale";
60 // Testing upper bound opened range
61 String source = "500";
62 String transformedResponse = processor.transform(existingscale, source);
63 assertEquals("extreme", transformedResponse);
65 // Testing lower bound opened range
67 transformedResponse = processor.transform(existingscale, source);
68 assertEquals("low", transformedResponse);
70 // Testing unfinite up and down range
71 existingscale = "scale/catchall.scale";
73 transformedResponse = processor.transform(existingscale, source);
74 assertEquals("catchall", transformedResponse);
78 public void testTransformByScaleUndef() throws TransformationException {
79 // check that for undefined/non numeric value we return empty string
81 String existingscale = "scale/humidex_fr.scale";
83 String transformedResponse = processor.transform(existingscale, source);
84 assertEquals("", transformedResponse);
88 public void testTransformByScaleErrorInBounds() throws TransformationException {
89 // the tested file contains inputs that generate a conversion error of the bounds
91 String existingscale = "scale/erroneous.scale";
94 @SuppressWarnings("unused")
95 String transformedResponse = processor.transform(existingscale, source);
97 } catch (TransformationException e) {
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);
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";
118 String transformedResponse = processor.transform(evaluationOrder, source);
119 assertEquals("first", transformedResponse);
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) !";
128 String transformedResponse = processor.transform(aqScaleFile, airQuality.toString());
129 assertEquals(expected, transformedResponse);
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);
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);
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);