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.Assert.fail;
18 import java.io.FileReader;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.util.Locale;
22 import java.util.Properties;
23 import java.util.concurrent.Callable;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.osgi.framework.BundleContext;
35 * @author Gaƫl L'hopital - Initial contribution
37 public class MapTransformationServiceTest {
39 private static final String SOURCE_CLOSED = "CLOSED";
40 private static final String SOURCE_UNKNOWN = "UNKNOWN";
41 private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
42 private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
43 private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
44 private static final String INEXISTING_FILENAME = "map/de.map";
45 private static final String BASE_FOLDER = "target";
46 private static final String SRC_FOLDER = "conf";
47 private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
48 private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
51 private 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 MockitoAnnotations.initMocks(this);
81 processor = new TestableMapTransformationService();
82 processor.activate(bundleContext);
83 FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
87 public void tearDown() throws IOException {
88 processor.deactivate();
89 FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
93 public void testTransformByMap() throws Exception {
94 // Test that we find a translation in an existing file
95 String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
96 Assert.assertEquals("zu", transformedResponse);
98 Properties properties = new Properties();
99 try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
100 properties.load(reader);
101 properties.setProperty(SOURCE_CLOSED, "changevalue");
102 properties.store(writer, "");
104 // This tests that the requested transformation file has been removed from
106 waitForAssert(new Callable<Void>() {
108 public Void call() throws Exception {
109 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
110 Assert.assertEquals("changevalue", transformedResponse);
115 properties.setProperty(SOURCE_CLOSED, "zu");
116 properties.store(writer, "");
118 waitForAssert(new Callable<Void>() {
120 public Void call() throws Exception {
121 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
122 Assert.assertEquals("zu", transformedResponse);
126 } catch (IOException e1) {
127 e1.printStackTrace(System.err);
130 // Checks that an unknown input in an existing file give the expected
131 // transformed response that shall be empty string (Issue #1107) if not found in the file
132 transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
133 Assert.assertEquals("", transformedResponse);
135 // Test that an inexisting file raises correct exception as expected
137 transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
139 } catch (Exception e) {
140 // That's what we expect.
143 // Test that we find a localized version of desired file
144 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
145 // as we don't know the real locale at the moment the
146 // test is run, we test that the string has actually been transformed
147 Assert.assertNotEquals(SOURCE_CLOSED, transformedResponse);
148 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
149 Assert.assertNotEquals(SOURCE_CLOSED, transformedResponse);
153 public void testTransformByMapWithDefault() throws Exception {
154 // Standard behaviour with no default value
155 String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
156 Assert.assertEquals("", transformedResponse);
157 // Modified behaviour with a file containing default value definition
158 transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
159 Assert.assertEquals("Default Value", transformedResponse);
162 protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
164 while (waitingTime < timeout) {
168 } catch (AssertionError error) {
169 waitingTime += sleepTime;
171 Thread.sleep(sleepTime);
172 } catch (InterruptedException e) {
173 throw new RuntimeException(e);