]> git.basschouten.com Git - openhab-addons.git/blob
00383b1e2452b0638e78e66a4968b99f5a1eff99
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.io.PrintStream;
22 import java.util.Locale;
23 import java.util.Properties;
24 import java.util.concurrent.Callable;
25
26 import org.apache.commons.io.FileUtils;
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
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 public class MapTransformationServiceTest {
43
44     private static final String SOURCE_CLOSED = "CLOSED";
45     private static final String SOURCE_UNKNOWN = "UNKNOWN";
46     private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
47     private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
48     private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
49     private static final String INEXISTING_FILENAME = "map/de.map";
50     private static final String BASE_FOLDER = "target";
51     private static final String SRC_FOLDER = "conf";
52     private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
53     private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
54
55     private @Mock BundleContext bundleContext;
56
57     private TestableMapTransformationService processor;
58
59     private class TestableMapTransformationService extends MapTransformationService {
60         @Override
61         protected String getSourcePath() {
62             return BASE_FOLDER + File.separator + super.getSourcePath();
63         }
64
65         @Override
66         protected Locale getLocale() {
67             return Locale.US;
68         }
69
70         @Override
71         public void activate(BundleContext context) {
72             super.activate(context);
73         }
74
75         @Override
76         public void deactivate() {
77             super.deactivate();
78         }
79     };
80
81     @BeforeEach
82     public void setUp() throws IOException {
83         processor = new TestableMapTransformationService();
84         processor.activate(bundleContext);
85         FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
86     }
87
88     @AfterEach
89     public void tearDown() throws IOException {
90         processor.deactivate();
91         FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
92     }
93
94     @Test
95     public void testTransformByMap() throws Exception {
96         // Test that we find a translation in an existing file
97         String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
98         assertEquals("zu", transformedResponse);
99
100         Properties properties = new Properties();
101         try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
102             properties.load(reader);
103             properties.setProperty(SOURCE_CLOSED, "changevalue");
104             properties.store(writer, "");
105
106             // This tests that the requested transformation file has been removed from
107             // the cache
108             waitForAssert(new Callable<Void>() {
109                 @Override
110                 public Void call() throws Exception {
111                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
112                     assertEquals("changevalue", transformedResponse);
113                     return null;
114                 }
115             }, 10000, 100);
116
117             properties.setProperty(SOURCE_CLOSED, "zu");
118             properties.store(writer, "");
119
120             waitForAssert(new Callable<Void>() {
121                 @Override
122                 public Void call() throws Exception {
123                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
124                     assertEquals("zu", transformedResponse);
125                     return null;
126                 }
127             }, 10000, 100);
128         } catch (IOException e1) {
129             PrintStream err = System.err;
130             if (err != null) {
131                 e1.printStackTrace(err);
132             }
133         }
134
135         // Checks that an unknown input in an existing file give the expected
136         // transformed response that shall be empty string (Issue #1107) if not found in the file
137         transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
138         assertEquals("", transformedResponse);
139
140         // Test that an inexisting file raises correct exception as expected
141         try {
142             transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
143             fail();
144         } catch (Exception e) {
145             // That's what we expect.
146         }
147
148         // Test that we find a localized version of desired file
149         transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
150         // as we don't know the real locale at the moment the
151         // test is run, we test that the string has actually been transformed
152         assertNotEquals(SOURCE_CLOSED, transformedResponse);
153         transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
154         assertNotEquals(SOURCE_CLOSED, transformedResponse);
155     }
156
157     @Test
158     public void testTransformByMapWithDefault() throws Exception {
159         // Standard behaviour with no default value
160         String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
161         assertEquals("", transformedResponse);
162         // Modified behaviour with a file containing default value definition
163         transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
164         assertEquals("Default Value", transformedResponse);
165     }
166
167     protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
168         int waitingTime = 0;
169         while (waitingTime < timeout) {
170             try {
171                 assertion.call();
172                 return;
173             } catch (AssertionError error) {
174                 waitingTime += sleepTime;
175                 try {
176                     Thread.sleep(sleepTime);
177                 } catch (InterruptedException e) {
178                     throw new RuntimeException(e);
179                 }
180             }
181         }
182         assertion.call();
183     }
184 }