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.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.jupiter.api.AfterEach;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32 import org.mockito.junit.jupiter.MockitoSettings;
33 import org.mockito.quality.Strictness;
34 import org.osgi.framework.BundleContext;
37 * @author Gaƫl L'hopital - Initial contribution
39 @ExtendWith(MockitoExtension.class)
40 @MockitoSettings(strictness = Strictness.WARN)
41 public class MapTransformationServiceTest {
43 private static final String SOURCE_CLOSED = "CLOSED";
44 private static final String SOURCE_UNKNOWN = "UNKNOWN";
45 private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
46 private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
47 private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
48 private static final String INEXISTING_FILENAME = "map/de.map";
49 private static final String BASE_FOLDER = "target";
50 private static final String SRC_FOLDER = "conf";
51 private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
52 private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
54 private @Mock BundleContext bundleContext;
56 private TestableMapTransformationService processor;
58 private class TestableMapTransformationService extends MapTransformationService {
60 protected String getSourcePath() {
61 return BASE_FOLDER + File.separator + super.getSourcePath();
65 protected Locale getLocale() {
70 public void activate(BundleContext context) {
71 super.activate(context);
75 public void deactivate() {
81 public void setUp() throws IOException {
82 processor = new TestableMapTransformationService();
83 processor.activate(bundleContext);
84 FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
88 public void tearDown() throws IOException {
89 processor.deactivate();
90 FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
94 public void testTransformByMap() throws Exception {
95 // Test that we find a translation in an existing file
96 String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
97 assertEquals("zu", transformedResponse);
99 Properties properties = new Properties();
100 try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
101 properties.load(reader);
102 properties.setProperty(SOURCE_CLOSED, "changevalue");
103 properties.store(writer, "");
105 // This tests that the requested transformation file has been removed from
107 waitForAssert(new Callable<Void>() {
109 public Void call() throws Exception {
110 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
111 assertEquals("changevalue", transformedResponse);
116 properties.setProperty(SOURCE_CLOSED, "zu");
117 properties.store(writer, "");
119 waitForAssert(new Callable<Void>() {
121 public Void call() throws Exception {
122 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
123 assertEquals("zu", transformedResponse);
127 } catch (IOException e1) {
128 e1.printStackTrace(System.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);