]> git.basschouten.com Git - openhab-addons.git/blob
28fd8ead8739189ab1f5dad94c737a061bbeb5c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.map.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17
18 import java.io.File;
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;
24 import java.util.Map;
25 import java.util.Objects;
26
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;
41
42 /**
43  * @author Gaël L'hopital - Initial contribution
44  * @author Jan N. Klug - Refactored to use {@link TransformationRegistry}
45  */
46 @ExtendWith(MockitoExtension.class)
47 @MockitoSettings(strictness = Strictness.LENIENT)
48 @NonNullByDefault
49 public class MapTransformationServiceTest extends JavaTest {
50     private static final String SOURCE_CLOSED = "CLOSED";
51     private static final String SOURCE_UNKNOWN = "UNKNOWN";
52
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";
58
59     private static final String SRC_FOLDER = "conf" + File.separator + "transform";
60
61     @Mock
62     private @NonNullByDefault({}) TransformationRegistry transformationRegistry;
63
64     private @NonNullByDefault({}) MapTransformationService processor;
65     private final Map<String, Transformation> configurationMap = new HashMap<>();
66
67     @BeforeEach
68     public void setUp() throws IOException {
69         configurationMap.clear();
70         Files.walk(Path.of(SRC_FOLDER)).filter(Files::isRegularFile).forEach(file -> {
71             try {
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) {
78             }
79         });
80
81         Mockito.when(transformationRegistry.get(anyString(), eq(null)))
82                 .thenAnswer((Answer<Transformation>) invocation -> {
83                     Object[] args = invocation.getArguments();
84                     return configurationMap.get(args[0]);
85                 });
86
87         processor = new MapTransformationService(transformationRegistry);
88     }
89
90     @Test
91     public void testTransformSucceeds() throws TransformationException {
92         String transformedResponse = processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED);
93         assertEquals("zu", transformedResponse);
94     }
95
96     @Test
97     public void testTransformFailsWithoutDefault() {
98         assertThrows(TransformationException.class,
99                 () -> processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_UNKNOWN));
100     }
101
102     @Test
103     public void testTransformSucceedsWithDefault() throws TransformationException {
104         assertEquals("Default Value", processor.transform(DEFAULTED_TRANSFORMATION, SOURCE_UNKNOWN));
105     }
106
107     @Test
108     public void testTransformSucceedsWithFallbackDefault() throws TransformationException {
109         assertEquals(SOURCE_UNKNOWN, processor.transform(FALLBACK_TRANSFORMATION, SOURCE_UNKNOWN));
110     }
111
112     @Test
113     public void testTransformFailsOnUnknownTransformation() {
114         assertThrows(TransformationException.class, () -> processor.transform(UNKNOWN_TRANSFORMATION, SOURCE_CLOSED));
115     }
116
117     @Test
118     public void setTransformationIsRemoved() throws TransformationException {
119         assertEquals("zu", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
120
121         Transformation transformation = configurationMap.remove(NON_DEFAULTED_TRANSFORMATION_DE);
122         processor.removed(Objects.requireNonNull(transformation));
123
124         assertThrows(TransformationException.class,
125                 () -> processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
126     }
127
128     @Test
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);
135
136         // assert there is no modified cached version
137         assertEquals("zu", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
138     }
139
140     @Test
141     public void setTransformationIsUpdatedIfOldElementPresent() throws TransformationException {
142         // ensure old transformation is cached
143         processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED);
144
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);
151
152         // ensure modified configuration is applied
153         assertEquals("fermé", processor.transform(NON_DEFAULTED_TRANSFORMATION_DE, SOURCE_CLOSED));
154     }
155
156     @Test
157     public void oneLineInlineMapTest() throws TransformationException {
158         String transformation = "|key1=semicolons_are_the_separators ; key2 = value2";
159         assertEquals("value2", processor.transform(transformation, "key2"));
160     }
161
162     @Test
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"));
166     }
167
168     @Test
169     public void defaultInlineTest() throws TransformationException {
170         String transformation = "|key1=value1;key2=value;=default";
171         assertEquals("default", processor.transform(transformation, "nonexistent"));
172     }
173
174     @Test
175     public void defaultSourceInlineTest() throws TransformationException {
176         String transformation = "|key1=value1;key2=value;=_source_";
177         assertEquals("nonexistent", processor.transform(transformation, "nonexistent"));
178     }
179 }