]> git.basschouten.com Git - openhab-addons.git/blob
0552a64a9f07d6e05dc3d06fb4f839df277f407c
[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.Assert.fail;
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.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.osgi.framework.BundleContext;
33
34 /**
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 public class MapTransformationServiceTest {
38
39     private static final String SOURCE_CLOSED = "CLOSED";
40     private static final String SOURCE_UNKNOWN = "UNKNOWN";
41     private static final String EXISTING_FILENAME_DE = "map/doorstatus_de.map";
42     private static final String SHOULD_BE_LOCALIZED_FILENAME = "map/doorstatus.map";
43     private static final String DEFAULTED_FILENAME = "map/doorstatus_defaulted.map";
44     private static final String INEXISTING_FILENAME = "map/de.map";
45     private static final String BASE_FOLDER = "target";
46     private static final String SRC_FOLDER = "conf";
47     private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
48     private static final String USED_FILENAME = CONFIG_FOLDER + File.separator + "transform/" + EXISTING_FILENAME_DE;
49
50     @Mock
51     private 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     @Before
78     public void setUp() throws IOException {
79         MockitoAnnotations.initMocks(this);
80
81         processor = new TestableMapTransformationService();
82         processor.activate(bundleContext);
83         FileUtils.copyDirectory(new File(SRC_FOLDER), new File(CONFIG_FOLDER));
84     }
85
86     @After
87     public void tearDown() throws IOException {
88         processor.deactivate();
89         FileUtils.deleteDirectory(new File(CONFIG_FOLDER));
90     }
91
92     @Test
93     public void testTransformByMap() throws Exception {
94         // Test that we find a translation in an existing file
95         String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
96         Assert.assertEquals("zu", transformedResponse);
97
98         Properties properties = new Properties();
99         try (FileReader reader = new FileReader(USED_FILENAME); FileWriter writer = new FileWriter(USED_FILENAME)) {
100             properties.load(reader);
101             properties.setProperty(SOURCE_CLOSED, "changevalue");
102             properties.store(writer, "");
103
104             // This tests that the requested transformation file has been removed from
105             // the cache
106             waitForAssert(new Callable<Void>() {
107                 @Override
108                 public Void call() throws Exception {
109                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
110                     Assert.assertEquals("changevalue", transformedResponse);
111                     return null;
112                 }
113             }, 10000, 100);
114
115             properties.setProperty(SOURCE_CLOSED, "zu");
116             properties.store(writer, "");
117
118             waitForAssert(new Callable<Void>() {
119                 @Override
120                 public Void call() throws Exception {
121                     final String transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_CLOSED);
122                     Assert.assertEquals("zu", transformedResponse);
123                     return null;
124                 }
125             }, 10000, 100);
126         } catch (IOException e1) {
127             e1.printStackTrace(System.err);
128         }
129
130         // Checks that an unknown input in an existing file give the expected
131         // transformed response that shall be empty string (Issue #1107) if not found in the file
132         transformedResponse = processor.transform(EXISTING_FILENAME_DE, SOURCE_UNKNOWN);
133         Assert.assertEquals("", transformedResponse);
134
135         // Test that an inexisting file raises correct exception as expected
136         try {
137             transformedResponse = processor.transform(INEXISTING_FILENAME, SOURCE_CLOSED);
138             fail();
139         } catch (Exception e) {
140             // That's what we expect.
141         }
142
143         // Test that we find a localized version of desired file
144         transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
145         // as we don't know the real locale at the moment the
146         // test is run, we test that the string has actually been transformed
147         Assert.assertNotEquals(SOURCE_CLOSED, transformedResponse);
148         transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, SOURCE_CLOSED);
149         Assert.assertNotEquals(SOURCE_CLOSED, transformedResponse);
150     }
151
152     @Test
153     public void testTransformByMapWithDefault() throws Exception {
154         // Standard behaviour with no default value
155         String transformedResponse = processor.transform(SHOULD_BE_LOCALIZED_FILENAME, "toBeDefaulted");
156         Assert.assertEquals("", transformedResponse);
157         // Modified behaviour with a file containing default value definition
158         transformedResponse = processor.transform(DEFAULTED_FILENAME, "toBeDefaulted");
159         Assert.assertEquals("Default Value", transformedResponse);
160     }
161
162     protected void waitForAssert(Callable<Void> assertion, int timeout, int sleepTime) throws Exception {
163         int waitingTime = 0;
164         while (waitingTime < timeout) {
165             try {
166                 assertion.call();
167                 return;
168             } catch (AssertionError error) {
169                 waitingTime += sleepTime;
170                 try {
171                     Thread.sleep(sleepTime);
172                 } catch (InterruptedException e) {
173                     throw new RuntimeException(e);
174                 }
175             }
176         }
177         assertion.call();
178     }
179 }