]> git.basschouten.com Git - openhab-addons.git/blob
33751571c666b1245ca5f6ce35eac053a241567d
[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.javascript.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21 import java.nio.file.Paths;
22 import java.util.Comparator;
23 import java.util.stream.Stream;
24
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.mockito.junit.jupiter.MockitoSettings;
32 import org.mockito.quality.Strictness;
33 import org.openhab.core.transform.TransformationException;
34 import org.osgi.framework.BundleContext;
35
36 /**
37  * @author Pauli Anttila - Initial contribution
38  */
39 @ExtendWith(MockitoExtension.class)
40 @MockitoSettings(strictness = Strictness.WARN)
41 public class JavaScriptTransformationServiceTest {
42
43     private static final String BASE_FOLDER = "target";
44     private static final String SRC_FOLDER = "conf";
45     private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER;
46
47     private @Mock BundleContext bundleContext;
48
49     private TestableJavaScriptTransformationService processor;
50
51     private class TestableJavaScriptTransformationService extends JavaScriptTransformationService {
52         public TestableJavaScriptTransformationService(JavaScriptEngineManager manager) {
53             super(manager);
54         }
55     };
56
57     @BeforeEach
58     public void setUp() throws IOException {
59         JavaScriptEngineManager manager = new JavaScriptEngineManager();
60         processor = new TestableJavaScriptTransformationService(manager);
61         copyDirectory(SRC_FOLDER, CONFIG_FOLDER);
62     }
63
64     @AfterEach
65     public void tearDown() throws IOException {
66         try (Stream<Path> walk = Files.walk(Path.of(CONFIG_FOLDER))) {
67             walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
68         }
69     }
70
71     private void copyDirectory(String from, String to) throws IOException {
72         Files.walk(Paths.get(from)).forEach(fromPath -> {
73             Path toPath = Paths.get(to, fromPath.toString().substring(from.length()));
74             try {
75                 Files.copy(fromPath, toPath);
76             } catch (IOException e) {
77             }
78         });
79     }
80
81     @Test
82     public void testInlineScript() throws Exception {
83         final String DATA = "100";
84         final String SCRIPT = "| input / 10";
85
86         String transformedResponse = processor.transform(SCRIPT, DATA);
87         assertEquals("10.0", transformedResponse);
88     }
89
90     @Test
91     public void testInlineScriptIncludingPipe() throws Exception {
92         final String DATA = "1";
93         final String SCRIPT = "| false || (input == '1')";
94
95         String transformedResponse = processor.transform(SCRIPT, DATA);
96         assertEquals("true", transformedResponse);
97     }
98
99     @Test
100     public void testReadmeExampleWithoutSubFolder() throws Exception {
101         final String DATA = "foo bar baz";
102         final String SCRIPT = "readme.js";
103
104         String transformedResponse = processor.transform(SCRIPT, DATA);
105         assertEquals("3", transformedResponse);
106     }
107
108     @Test
109     public void testReadmeExampleWithSubFolders() throws Exception {
110         final String DATA = "foo bar baz";
111         final String SCRIPT = "js/readme/readme.js";
112
113         String transformedResponse = processor.transform(SCRIPT, DATA);
114         assertEquals("3", transformedResponse);
115     }
116
117     @Test
118     public void testReadmeScaleExample() throws Exception {
119         final String DATA = "214";
120         final String SCRIPT = "scale.js?correctionFactor=1.1&divider=10.js";
121
122         String transformedResponse = processor.transform(SCRIPT, DATA);
123         assertEquals("23.54", transformedResponse);
124     }
125
126     @Test
127     public void testAdditionalVariables() throws Exception {
128         final String DATA = "100";
129         final String SCRIPT = "sum.js?a=10&b=1";
130
131         String transformedResponse = processor.transform(SCRIPT, DATA);
132         assertEquals("111", transformedResponse);
133     }
134
135     @Test
136     public void testIllegalVariableName() throws Exception {
137         final String DATA = "100";
138         final String SCRIPT = "sum.js?a=10&input=fail&b=1";
139
140         Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA));
141         assertEquals("'input' word is reserved and can't be used in additional parameters", exception.getMessage());
142     }
143
144     @Test
145     public void testIllegalQuestionmarkSequence() throws Exception {
146         final String DATA = "100";
147         final String SCRIPT = "sum.js?a=1&test=ab?d&b=2";
148
149         Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA));
150         assertEquals("Questionmark should be defined only once in the filename", exception.getMessage());
151     }
152
153     @Test
154     public void testIllegalAmbersandSequence() throws Exception {
155         final String DATA = "foo";
156         final String SCRIPT = "returntest.js?a=1&test=ab&d&b=2";
157
158         Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA));
159         assertEquals("Illegal filename syntax", exception.getMessage());
160     }
161
162     @Test
163     public void testEncodedSpecialCharacters() throws Exception {
164         final String DATA = "100";
165         final String SCRIPT = "returntest.js?a=1&test=ab%3Fd%26f&b=2";
166
167         String transformedResponse = processor.transform(SCRIPT, DATA);
168         assertEquals("ab?d&f", transformedResponse);
169     }
170 }