]> git.basschouten.com Git - openhab-addons.git/blob
c12e6db610a54ee15322430fd563bacdfcf79844
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.automation.groovyscripting;
14
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.net.URL;
18
19 import javax.script.ScriptEngine;
20 import javax.script.ScriptException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.automation.module.script.ScriptEngineContainer;
26 import org.openhab.core.automation.module.script.ScriptEngineManager;
27 import org.openhab.core.test.java.JavaOSGiTest;
28
29 /**
30  * This tests the JSON, XML and YAML slurpers using the Groovy scripting engine.
31  *
32  * @author Wouter Born - Initial contribution
33  */
34 @NonNullByDefault
35 public class SlurperOSGiTest extends JavaOSGiTest {
36
37     private @NonNullByDefault({}) ScriptEngine engine;
38
39     private final String path = "OH-INF/automation/jsr223/";
40
41     @BeforeEach
42     public void init() {
43         ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
44         ScriptEngineContainer container = scriptManager.createScriptEngine("groovy", "myGroovyEngine");
45         engine = container.getScriptEngine();
46     }
47
48     private void evalScript(String fileName) throws ScriptException, IOException {
49         URL url = bundleContext.getBundle().getResource(path + fileName);
50         engine.eval(new InputStreamReader(url.openStream()));
51     }
52
53     @Test
54     public void jsonSlurper() throws ScriptException, IOException {
55         evalScript("json-slurper.groovy");
56     }
57
58     @Test
59     public void xmlSlurper() throws ScriptException, IOException {
60         evalScript("xml-slurper.groovy");
61     }
62
63     @Test
64     public void yamlSlurper() throws ScriptException, IOException {
65         evalScript("yaml-slurper.groovy");
66     }
67 }