2 * Copyright (c) 2010-2024 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.map.internal;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
19 import java.io.IOException;
20 import java.nio.charset.StandardCharsets;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.HashMap;
25 import java.util.Objects;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.mockito.junit.jupiter.MockitoSettings;
35 import org.mockito.quality.Strictness;
36 import org.mockito.stubbing.Answer;
37 import org.openhab.core.test.java.JavaTest;
38 import org.openhab.core.transform.Transformation;
39 import org.openhab.core.transform.TransformationException;
40 import org.openhab.core.transform.TransformationRegistry;
43 * @author Gaël L'hopital - Initial contribution
44 * @author Jan N. Klug - Refactored to use {@link TransformationRegistry}
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.LENIENT)
49 public class MapTransformationServiceTest extends JavaTest {
50 private static final String SOURCE_CLOSED = "CLOSED";
51 private static final String SOURCE_UNKNOWN = "UNKNOWN";
53 private static final String NON_DEFAULTED_TRANSFORMATION_DE = "map" + File.separator + "doorstatus_de.map";
54 private static final String NON_DEFAULTED_TRANSFORMATION_FR = "map" + File.separator + "doorstatus_fr.map";
55 private static final String DEFAULTED_TRANSFORMATION = "map" + File.separator + "doorstatus_defaulted.map";
56 private static final String FALLBACK_TRANSFORMATION = "map" + File.separator + "doorstatus_fallback.map";
57 private static final String UNKNOWN_TRANSFORMATION = "map" + File.separator + "de.map";
59 private static final String SRC_FOLDER = "conf" + File.separator + "transform";
62 private @NonNullByDefault({}) TransformationRegistry transformationRegistry;
64 private @NonNullByDefault({}) MapTransformationService processor;
65 private final Map<String, Transformation> configurationMap = new HashMap<>();
68 public void setUp() throws IOException {
69 configurationMap.clear();
70 Files.walk(Path.of(SRC_FOLDER)).filter(Files::isRegularFile).forEach(file -> {
72 String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
73 String uid = Path.of(SRC_FOLDER).relativize(file).toString();
74 Transformation transformation = new Transformation(uid, uid, "map",
75 Map.of(Transformation.FUNCTION, content));
76 configurationMap.put(uid, transformation);
77 } catch (IOException ignored) {
81 Mockito.when(transformationRegistry.get(anyString(), eq(null)))
82 .thenAnswer((Answer<Transformation>) invocation -> {
83 Object[] args = invocation.getArguments();
84 return configurationMap.get(args[0]);
87 processor = new MapTransformationService(transformationRegistry);
91 public void testTransformSucceeds() throws TransformationException {
92 String transformedResponse = processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED);
93 assertEquals("zu", transformedResponse);
97 public void testTransformFailsWithoutDefault() {
98 assertThrows(TransformationException.class,
99 () -> processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_UNKNOWN));
103 public void testTransformSucceedsWithDefault() throws TransformationException {
104 assertEquals("Default Value", processor.transform(DEFAULTED_TRANSFORMATION, SOURCE_UNKNOWN));
108 public void testTransformSucceedsWithFallbackDefault() throws TransformationException {
109 assertEquals(SOURCE_UNKNOWN, processor.transform(FALLBACK_TRANSFORMATION, SOURCE_UNKNOWN));
113 public void testTransformFailsOnUnknownTransformation() {
114 assertThrows(TransformationException.class, () -> processor.transform(UNKNOWN_TRANSFORMATION, SOURCE_CLOSED));
118 public void setTransformationIsRemoved() throws TransformationException {
119 assertEquals("zu", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
121 Transformation transformation = configurationMap.remove(NON_DEFAULTED_TRANSFORMATION_DE);
122 processor.removed(Objects.requireNonNull(transformation));
124 assertThrows(TransformationException.class,
125 () -> processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
129 public void setTransformationIsNotUpdatedIfOldElementMissing() throws TransformationException {
130 // update configuration
131 Transformation transformationDE = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_DE));
132 Transformation transformationModified = new Transformation(transformationDE.getUID(),
133 transformationDE.getLabel(), transformationDE.getType(), transformationDE.getConfiguration());
134 processor.updated(transformationDE, transformationModified);
136 // assert there is no modified cached version
137 assertEquals("zu", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
141 public void setTransformationIsUpdatedIfOldElementPresent() throws TransformationException {
142 // ensure old transformation is cached
143 processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED);
145 // update configuration
146 Transformation transformationDE = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_DE));
147 Transformation transformationFR = Objects.requireNonNull(configurationMap.get(NON_DEFAULTED_TRANSFORMATION_FR));
148 Transformation transformationModified = new Transformation(transformationDE.getUID(),
149 transformationDE.getLabel(), transformationDE.getType(), transformationFR.getConfiguration());
150 processor.updated(transformationDE, transformationModified);
152 // ensure modified configuration is applied
153 assertEquals("fermé", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
157 public void oneLineInlineMapTest() throws TransformationException {
158 String transformation = "|key1=semicolons_are_the_separators ; key2 = value2";
159 assertEquals("value2", processor.transform(transformation, "key2"));
163 public void multiLineInlineMapTest() throws TransformationException {
164 String transformation = "|key1=semicolons_arent_separators;1 \n key2 = value;2";
165 assertEquals("value;2", processor.transform(transformation, "key2"));
169 public void defaultInlineTest() throws TransformationException {
170 String transformation = "|key1=value1;key2=value;=default";
171 assertEquals("default", processor.transform(transformation, "nonexistent"));
175 public void defaultSourceInlineTest() throws TransformationException {
176 String transformation = "|key1=value1;key2=value;=_source_";
177 assertEquals("nonexistent", processor.transform(transformation, "nonexistent"));