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.map.internal;
15 import static org.junit.jupiter.api.Assertions.*;
18 import java.util.Locale;
19 import java.util.Properties;
20 import java.util.concurrent.Callable;
22 import org.apache.commons.io.FileUtils;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.mockito.junit.jupiter.MockitoSettings;
30 import org.mockito.quality.Strictness;
31 import org.osgi.framework.BundleContext;
34 * @author Gaƫl L'hopital - Initial contribution
36 @ExtendWith(MockitoExtension.class)
37 @MockitoSettings(strictness = Strictness.WARN)
38 public class MapTransformationServiceTest {
40 private static final String SOURCE_CLOSED = "CLOSED";
41 private static final String SOURCE_UNKNOWN = "UNKNOWN";
42 private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
43 private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
44 private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
45 private static final String INEXISTING_FILENAME = "map/de.map";
46 private static final String BASE_FOLDER = "target";
47 private static final String SRC_FOLDER = "conf";
48 private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
49 private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
51 private @Mock BundleContext bundleContext;
53 private TestableMapTransformationService processor;
55 private class TestableMapTransformationService extends MapTransformationService {
57 protected String getSourcePath() {
58 return BASE_FOLDER + File.separator + super.getSourcePath();
62 protected Locale getLocale() {
67 public void activate(BundleContext context) {
68 super.activate(context);
72 public void deactivate() {
78 public void setUp() throws IOException {
79 processor = new TestableMapTransformationService();
80 processor.activate(bundleContext);
81 FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
85 public void tearDown() throws IOException {
86 processor.deactivate();
87 FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
91 public void testTransformByMap() throws Exception {
92 // Test that we find a translation in an existing file
93 String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
94 assertEquals("zu", transformedResponse);
96 Properties properties = new Properties();
97 try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
98 properties.load(reader);
99 properties.setProperty(SOURCE_CLOSED, "changevalue");
100 properties.store(writer, "");
102 // This tests that the requested transformation file has been removed from
104 waitForAssert(new Callable<Void>() {
106 public Void call() throws Exception {
107 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
108 assertEquals("changevalue", transformedResponse);
113 properties.setProperty(SOURCE_CLOSED, "zu");
114 properties.store(writer, "");
116 waitForAssert(new Callable<Void>() {
118 public Void call() throws Exception {
119 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
120 assertEquals("zu", transformedResponse);
124 } catch (IOException e1) {
125 PrintStream err = System.err;
127 e1.printStackTrace(err);
131 // Checks that an unknown input in an existing file give the expected
132 // transformed response that shall be empty string (Issue #1107) if not found in the file
133 transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
134 assertEquals("", transformedResponse);
136 // Test that an inexisting file raises correct exception as expected
138 transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
140 } catch (Exception e) {
141 // That's what we expect.
144 // Test that we find a localized version of desired file
145 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
146 // as we don't know the real locale at the moment the
147 // test is run, we test that the string has actually been transformed
148 assertNotEquals(SOURCE_CLOSED, transformedResponse);
149 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
150 assertNotEquals(SOURCE_CLOSED, transformedResponse);
154 public void testTransformByMapWithDefault() throws Exception {
155 // Standard behaviour with no default value
156 String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
157 assertEquals("", transformedResponse);
158 // Modified behaviour with a file containing default value definition
159 transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
160 assertEquals("Default Value", transformedResponse);
163 protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
165 while (waitingTime < timeout) {
169 } catch (AssertionError error) {
170 waitingTime += sleepTime;
172 Thread.sleep(sleepTime);
173 } catch (InterruptedException e) {
174 throw new RuntimeException(e);