]> git.basschouten.com Git - openhab-addons.git/blob
e1974b2006d9a9f9bedfca8afbab6b88064f7a8f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.map.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.File;
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;
24
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;
36
37 /**
38  * @author GaĆ«l L'hopital - Initial contribution
39  */
40 @ExtendWith(MockitoExtension.class)
41 @MockitoSettings(strictness = Strictness.WARN)
42 @Disabled("Needs to be updated for OH3")
43 public class MapTransformationServiceTest {
44
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;
55
56     private @Mock BundleContext bundleContext;
57
58     private TestableMapTransformationService processor;
59
60     private class TestableMapTransformationService extends MapTransformationService {
61         @Override
62         protected String getSourcePath() {
63             return BASE_FOLDER + File.separator + super.getSourcePath();
64         }
65
66         @Override
67         protected Locale getLocale() {
68             return Locale.US;
69         }
70
71         @Override
72         public void activate(BundleContext context) {
73             super.activate(context);
74         }
75
76         @Override
77         public void deactivate() {
78             super.deactivate();
79         }
80     };
81
82     @BeforeEach
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));
87     }
88
89     @AfterEach
90     public void tearDown() throws IOException {
91         processor.deactivate();
92         FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
93     }
94
95     @Test
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);
100
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, "");
106
107             // This tests that the requested transformation file has been removed from
108             // the cache
109             waitForAssert(new Callable<Void>() {
110                 @Override
111                 public Void call() throws Exception {
112                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
113                     assertEquals("changevalue", transformedResponse);
114                     return null;
115                 }
116             }, 10000, 100);
117
118             properties.setProperty(SOURCE_CLOSED, "zu");
119             properties.store(writer, "");
120
121             waitForAssert(new Callable<Void>() {
122                 @Override
123                 public Void call() throws Exception {
124                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
125                     assertEquals("zu", transformedResponse);
126                     return null;
127                 }
128             }, 10000, 100);
129         } catch (IOException e1) {
130             e1.printStackTrace(System.err);
131         }
132
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);
137
138         // Test that an inexisting file raises correct exception as expected
139         try {
140             transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
141             fail();
142         } catch (Exception e) {
143             // That's what we expect.
144         }
145
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);
153     }
154
155     @Test
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);
163     }
164
165     protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
166         int waitingTime = 0;
167         while (waitingTime < timeout) {
168             try {
169                 assertion.call();
170                 return;
171             } catch (AssertionError error) {
172                 waitingTime += sleepTime;
173                 try {
174                     Thread.sleep(sleepTime);
175                 } catch (InterruptedException e) {
176                     throw new RuntimeException(e);
177                 }
178             }
179         }
180         assertion.call();
181     }
182 }