2 * Copyright (c) 2010-2021 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.*;
18 import java.io.FileReader;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.util.Locale;
23 import java.util.Properties;
24 import java.util.concurrent.Callable;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.jupiter.api.AfterEach;
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.junit.jupiter.MockitoExtension;
33 import org.mockito.junit.jupiter.MockitoSettings;
34 import org.mockito.quality.Strictness;
35 import org.osgi.framework.BundleContext;
38 * @author Gaƫl L'hopital - Initial contribution
40 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.WARN)
42 public class MapTransformationServiceTest {
44 private static final String SOURCE_CLOSED = "CLOSED";
45 private static final String SOURCE_UNKNOWN = "UNKNOWN";
46 private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
47 private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
48 private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
49 private static final String INEXISTING_FILENAME = "map/de.map";
50 private static final String BASE_FOLDER = "target";
51 private static final String SRC_FOLDER = "conf";
52 private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
53 private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
55 private @Mock BundleContext bundleContext;
57 private TestableMapTransformationService processor;
59 private class TestableMapTransformationService extends MapTransformationService {
61 protected String getSourcePath() {
62 return BASE_FOLDER + File.separator + super.getSourcePath();
66 protected Locale getLocale() {
71 public void activate(BundleContext context) {
72 super.activate(context);
76 public void deactivate() {
82 public void setUp() throws IOException {
83 processor = new TestableMapTransformationService();
84 processor.activate(bundleContext);
85 FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
89 public void tearDown() throws IOException {
90 processor.deactivate();
91 FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
95 public void testTransformByMap() throws Exception {
96 // Test that we find a translation in an existing file
97 String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
98 assertEquals("zu", transformedResponse);
100 Properties properties = new Properties();
101 try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
102 properties.load(reader);
103 properties.setProperty(SOURCE_CLOSED, "changevalue");
104 properties.store(writer, "");
106 // This tests that the requested transformation file has been removed from
108 waitForAssert(new Callable<Void>() {
110 public Void call() throws Exception {
111 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
112 assertEquals("changevalue", transformedResponse);
117 properties.setProperty(SOURCE_CLOSED, "zu");
118 properties.store(writer, "");
120 waitForAssert(new Callable<Void>() {
122 public Void call() throws Exception {
123 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
124 assertEquals("zu", transformedResponse);
128 } catch (IOException e1) {
129 PrintStream err = System.err;
131 e1.printStackTrace(err);
135 // Checks that an unknown input in an existing file give the expected
136 // transformed response that shall be empty string (Issue #1107) if not found in the file
137 transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
138 assertEquals("", transformedResponse);
140 // Test that an inexisting file raises correct exception as expected
142 transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
144 } catch (Exception e) {
145 // That's what we expect.
148 // Test that we find a localized version of desired file
149 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
150 // as we don't know the real locale at the moment the
151 // test is run, we test that the string has actually been transformed
152 assertNotEquals(SOURCE_CLOSED, transformedResponse);
153 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
154 assertNotEquals(SOURCE_CLOSED, transformedResponse);
158 public void testTransformByMapWithDefault() throws Exception {
159 // Standard behaviour with no default value
160 String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
161 assertEquals("", transformedResponse);
162 // Modified behaviour with a file containing default value definition
163 transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
164 assertEquals("Default Value", transformedResponse);
167 protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
169 while (waitingTime < timeout) {
173 } catch (AssertionError error) {
174 waitingTime += sleepTime;
176 Thread.sleep(sleepTime);
177 } catch (InterruptedException e) {
178 throw new RuntimeException(e);