forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[js-transform] Introduced support for additional parameters (openhab#…
…10901) * [js-transform] Introduced support for additional parameters Signed-off-by: Pauli Anttila <[email protected]> * Added junit tests and updated readme Signed-off-by: Pauli Anttila <[email protected]> * Typo fixes Signed-off-by: Pauli Anttila <[email protected]> * Typo fix Signed-off-by: Pauli Anttila <[email protected]> * Fixed junit test Signed-off-by: Pauli Anttila <[email protected]>
- Loading branch information
1 parent
71d9526
commit 02ca84d
Showing
8 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
bundles/org.openhab.transform.javascript/conf/transform/js/readme/readme.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(function(i) { | ||
var array = i.split(" "); | ||
return array[array.length - 1].length; | ||
})(input) |
4 changes: 4 additions & 0 deletions
4
bundles/org.openhab.transform.javascript/conf/transform/readme.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(function(i) { | ||
var array = i.split(" "); | ||
return array[array.length - 1].length; | ||
})(input) |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.transform.javascript/conf/transform/returntest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(function(i, a, b) { | ||
return b; | ||
})(input, a, test) |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.transform.javascript/conf/transform/scale.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(function(data, cf, d) { | ||
return parseFloat(data) * parseFloat(cf) / parseFloat(d); | ||
})(input, correctionFactor, divider) |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.transform.javascript/conf/transform/sum.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(function(i, a, b) { | ||
return parseInt(i) + parseInt(a) + parseInt(b); | ||
})(input, a, b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...t/java/org/openhab/transform/javascript/internal/JavaScriptTransformationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.transform.javascript.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Comparator; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
import org.openhab.core.transform.TransformationException; | ||
import org.osgi.framework.BundleContext; | ||
|
||
/** | ||
* @author Pauli Anttila - Initial contribution | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.WARN) | ||
public class JavaScriptTransformationServiceTest { | ||
|
||
private static final String BASE_FOLDER = "target"; | ||
private static final String SRC_FOLDER = "conf"; | ||
private static final String CONFIG_FOLDER = BASE_FOLDER + File.separator + SRC_FOLDER; | ||
|
||
private @Mock BundleContext bundleContext; | ||
|
||
private TestableJavaScriptTransformationService processor; | ||
|
||
private class TestableJavaScriptTransformationService extends JavaScriptTransformationService { | ||
public TestableJavaScriptTransformationService(JavaScriptEngineManager manager) { | ||
super(manager); | ||
} | ||
}; | ||
|
||
@BeforeEach | ||
public void setUp() throws IOException { | ||
JavaScriptEngineManager manager = new JavaScriptEngineManager(); | ||
processor = new TestableJavaScriptTransformationService(manager); | ||
copyDirectory(SRC_FOLDER, CONFIG_FOLDER); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() throws IOException { | ||
try (Stream<Path> walk = Files.walk(Path.of(CONFIG_FOLDER))) { | ||
walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); | ||
} | ||
} | ||
|
||
private void copyDirectory(String from, String to) throws IOException { | ||
Files.walk(Paths.get(from)).forEach(fromPath -> { | ||
Path toPath = Paths.get(to, fromPath.toString().substring(from.length())); | ||
try { | ||
Files.copy(fromPath, toPath); | ||
} catch (IOException e) { | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testReadmeExampleWithoutSubFolder() throws Exception { | ||
final String DATA = "foo bar baz"; | ||
final String SCRIPT = "readme.js"; | ||
|
||
String transformedResponse = processor.transform(SCRIPT, DATA); | ||
assertEquals("3", transformedResponse); | ||
} | ||
|
||
@Test | ||
public void testReadmeExampleWithSubFolders() throws Exception { | ||
final String DATA = "foo bar baz"; | ||
final String SCRIPT = "js/readme/readme.js"; | ||
|
||
String transformedResponse = processor.transform(SCRIPT, DATA); | ||
assertEquals("3", transformedResponse); | ||
} | ||
|
||
@Test | ||
public void testReadmeScaleExample() throws Exception { | ||
final String DATA = "214"; | ||
final String SCRIPT = "scale.js?correctionFactor=1.1÷r=10.js"; | ||
|
||
String transformedResponse = processor.transform(SCRIPT, DATA); | ||
assertEquals("23.54", transformedResponse); | ||
} | ||
|
||
@Test | ||
public void testAdditionalVariables() throws Exception { | ||
final String DATA = "100"; | ||
final String SCRIPT = "sum.js?a=10&b=1"; | ||
|
||
String transformedResponse = processor.transform(SCRIPT, DATA); | ||
assertEquals("111", transformedResponse); | ||
} | ||
|
||
@Test | ||
public void testIllegalVariableName() throws Exception { | ||
final String DATA = "100"; | ||
final String SCRIPT = "sum.js?a=10&input=fail&b=1"; | ||
|
||
Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA)); | ||
assertEquals("'input' word is reserved and can't be used in additional parameters", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testIllegalQuestionmarkSequence() throws Exception { | ||
final String DATA = "100"; | ||
final String SCRIPT = "sum.js?a=1&test=ab?d&b=2"; | ||
|
||
Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA)); | ||
assertEquals("Questionmark should be defined only once in the filename", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testIllegalAmbersandSequence() throws Exception { | ||
final String DATA = "foo"; | ||
final String SCRIPT = "returntest.js?a=1&test=ab&d&b=2"; | ||
|
||
Exception exception = assertThrows(TransformationException.class, () -> processor.transform(SCRIPT, DATA)); | ||
assertEquals("Illegal filename syntax", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testEncodedSpecialCharacters() throws Exception { | ||
final String DATA = "100"; | ||
final String SCRIPT = "returntest.js?a=1&test=ab%3Fd%26f&b=2"; | ||
|
||
String transformedResponse = processor.transform(SCRIPT, DATA); | ||
assertEquals("ab?d&f", transformedResponse); | ||
} | ||
} |