]> git.basschouten.com Git - openhab-addons.git/blob
c5b8eec189b22c2b94a477ddfde415efac6641fa
[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.*;
18 import java.util.Locale;
19 import java.util.Properties;
20 import java.util.concurrent.Callable;
21
22 import org.apache.commons.io.FileUtils;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.mockito.junit.jupiter.MockitoSettings;
30 import org.mockito.quality.Strictness;
31 import org.osgi.framework.BundleContext;
32
33 /**
34  * @author GaĆ«l L'hopital - Initial contribution
35  */
36 @ExtendWith(MockitoExtension.class)
37 @MockitoSettings(strictness = Strictness.WARN)
38 public class MapTransformationServiceTest {
39
40     private static final String SOURCE_CLOSED = "CLOSED";
41     private static final String SOURCE_UNKNOWN = "UNKNOWN";
42     private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
43     private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
44     private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
45     private static final String INEXISTING_FILENAME = "map/de.map";
46     private static final String BASE_FOLDER = "target";
47     private static final String SRC_FOLDER = "conf";
48     private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
49     private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
50
51     private @Mock BundleContext bundleContext;
52
53     private TestableMapTransformationService processor;
54
55     private class TestableMapTransformationService extends MapTransformationService {
56         @Override
57         protected String getSourcePath() {
58             return BASE_FOLDER + File.separator + super.getSourcePath();
59         }
60
61         @Override
62         protected Locale getLocale() {
63             return Locale.US;
64         }
65
66         @Override
67         public void activate(BundleContext context) {
68             super.activate(context);
69         }
70
71         @Override
72         public void deactivate() {
73             super.deactivate();
74         }
75     };
76
77     @BeforeEach
78     public void setUp() throws IOException {
79         processor = new TestableMapTransformationService();
80         processor.activate(bundleContext);
81         FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
82     }
83
84     @AfterEach
85     public void tearDown() throws IOException {
86         processor.deactivate();
87         FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
88     }
89
90     @Test
91     public void testTransformByMap() throws Exception {
92         // Test that we find a translation in an existing file
93         String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
94         assertEquals("zu", transformedResponse);
95
96         Properties properties = new Properties();
97         try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
98             properties.load(reader);
99             properties.setProperty(SOURCE_CLOSED, "changevalue");
100             properties.store(writer, "");
101
102             // This tests that the requested transformation file has been removed from
103             // the cache
104             waitForAssert(new Callable<Void>() {
105                 @Override
106                 public Void call() throws Exception {
107                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
108                     assertEquals("changevalue", transformedResponse);
109                     return null;
110                 }
111             }, 10000, 100);
112
113             properties.setProperty(SOURCE_CLOSED, "zu");
114             properties.store(writer, "");
115
116             waitForAssert(new Callable<Void>() {
117                 @Override
118                 public Void call() throws Exception {
119                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
120                     assertEquals("zu", transformedResponse);
121                     return null;
122                 }
123             }, 10000, 100);
124         } catch (IOException e1) {
125             PrintStream err = System.err;
126             if (err != null) {
127                 e1.printStackTrace(err);
128             }
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 }