]> git.basschouten.com Git - openhab-addons.git/blob
7189186b0a98148bbd4d6dd743b1c85d4f94c5ec
[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.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;
35
36 /**
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @ExtendWith(MockitoExtension.class)
40 @MockitoSettings(strictness = Strictness.WARN)
41 public class MapTransformationServiceTest {
42
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;
53
54     private @Mock BundleContext bundleContext;
55
56     private TestableMapTransformationService processor;
57
58     private class TestableMapTransformationService extends MapTransformationService {
59         @Override
60         protected String getSourcePath() {
61             return BASE_FOLDER + File.separator + super.getSourcePath();
62         }
63
64         @Override
65         protected Locale getLocale() {
66             return Locale.US;
67         }
68
69         @Override
70         public void activate(BundleContext context) {
71             super.activate(context);
72         }
73
74         @Override
75         public void deactivate() {
76             super.deactivate();
77         }
78     };
79
80     @BeforeEach
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));
85     }
86
87     @AfterEach
88     public void tearDown() throws IOException {
89         processor.deactivate();
90         FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
91     }
92
93     @Test
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);
98
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, "");
104
105             // This tests that the requested transformation file has been removed from
106             // the cache
107             waitForAssert(new Callable<Void>() {
108                 @Override
109                 public Void call() throws Exception {
110                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
111                     assertEquals("changevalue", transformedResponse);
112                     return null;
113                 }
114             }, 10000, 100);
115
116             properties.setProperty(SOURCE_CLOSED, "zu");
117             properties.store(writer, "");
118
119             waitForAssert(new Callable<Void>() {
120                 @Override
121                 public Void call() throws Exception {
122                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
123                     assertEquals("zu", transformedResponse);
124                     return null;
125                 }
126             }, 10000, 100);
127         } catch (IOException e1) {
128             e1.printStackTrace(System.err);
129         }
130
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);
135
136         // Test that an inexisting file raises correct exception as expected
137         try {
138             transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
139             fail();
140         } catch (Exception e) {
141             // That's what we expect.
142         }
143
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);
151     }
152
153     @Test
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);
161     }
162
163     protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
164         int waitingTime = 0;
165         while (waitingTime < timeout) {
166             try {
167                 assertion.call();
168                 return;
169             } catch (AssertionError error) {
170                 waitingTime += sleepTime;
171                 try {
172                     Thread.sleep(sleepTime);
173                 } catch (InterruptedException e) {
174                     throw new RuntimeException(e);
175                 }
176             }
177         }
178         assertion.call();
179     }
180 }