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.Disabled;
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 @Disabled("Needs to be updated for OH3")
43 public class MapTransformationServiceTest {
45 private static final String SOURCE_CLOSED = "CLOSED";
46 private static final String SOURCE_UNKNOWN = "UNKNOWN";
47 private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
48 private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
49 private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
50 private static final String INEXISTING_FILENAME = "map/de.map";
51 private static final String BASE_FOLDER = "target";
52 private static final String SRC_FOLDER = "conf";
53 private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
54 private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
56 private @Mock BundleContext bundleContext;
58 private TestableMapTransformationService processor;
60 private class TestableMapTransformationService extends MapTransformationService {
62 protected String getSourcePath() {
63 return BASE_FOLDER + File.separator + super.getSourcePath();
67 protected Locale getLocale() {
72 public void activate(BundleContext context) {
73 super.activate(context);
77 public void deactivate() {
83 public void setUp() throws IOException {
84 processor = new TestableMapTransformationService();
85 processor.activate(bundleContext);
86 FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
90 public void tearDown() throws IOException {
91 processor.deactivate();
92 FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
96 public void testTransformByMap() throws Exception {
97 // Test that we find a translation in an existing file
98 String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
99 assertEquals("zu", transformedResponse);
101 Properties properties = new Properties();
102 try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
103 properties.load(reader);
104 properties.setProperty(SOURCE_CLOSED, "changevalue");
105 properties.store(writer, "");
107 // This tests that the requested transformation file has been removed from
109 waitForAssert(new Callable<Void>() {
111 public Void call() throws Exception {
112 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
113 assertEquals("changevalue", transformedResponse);
118 properties.setProperty(SOURCE_CLOSED, "zu");
119 properties.store(writer, "");
121 waitForAssert(new Callable<Void>() {
123 public Void call() throws Exception {
124 final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
125 assertEquals("zu", transformedResponse);
129 } catch (IOException e1) {
130 e1.printStackTrace(System.err);
133 // Checks that an unknown input in an existing file give the expected
134 // transformed response that shall be empty string (Issue #1107) if not found in the file
135 transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
136 assertEquals("", transformedResponse);
138 // Test that an inexisting file raises correct exception as expected
140 transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
142 } catch (Exception e) {
143 // That's what we expect.
146 // Test that we find a localized version of desired file
147 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
148 // as we don't know the real locale at the moment the
149 // test is run, we test that the string has actually been transformed
150 assertNotEquals(SOURCE_CLOSED, transformedResponse);
151 transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
152 assertNotEquals(SOURCE_CLOSED, transformedResponse);
156 public void testTransformByMapWithDefault() throws Exception {
157 // Standard behaviour with no default value
158 String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
159 assertEquals("", transformedResponse);
160 // Modified behaviour with a file containing default value definition
161 transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
162 assertEquals("Default Value", transformedResponse);
165 protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
167 while (waitingTime < timeout) {
171 } catch (AssertionError error) {
172 waitingTime += sleepTime;
174 Thread.sleep(sleepTime);
175 } catch (InterruptedException e) {
176 throw new RuntimeException(e);