You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/03/27 11:40:06 UTC
[sling-whiteboard] branch master updated: GraphQLScriptEngine + IT
skeleton
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 1d5477f GraphQLScriptEngine + IT skeleton
1d5477f is described below
commit 1d5477fe7d4314833cdab7c6042a09bb549984d2
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri Mar 27 12:39:53 2020 +0100
GraphQLScriptEngine + IT skeleton
---
graphql-scripting/bnd.bnd | 2 +
graphql-scripting/pom.xml | 190 +++++++++++++++++++++
.../gql/internal/GraphQLScriptEngine.java | 65 +++++++
.../gql/internal/GraphQLScriptEngineFactory.java | 70 ++++++++
.../GraphQLScriptEngineFactoryConfiguration.java | 55 ++++++
.../graphql/it/GraphQLScriptEngineIT.java | 52 ++++++
.../graphql/it/GraphQLScriptingTestSupport.java | 74 ++++++++
7 files changed, 508 insertions(+)
diff --git a/graphql-scripting/bnd.bnd b/graphql-scripting/bnd.bnd
new file mode 100644
index 0000000..5ffd3d3
--- /dev/null
+++ b/graphql-scripting/bnd.bnd
@@ -0,0 +1,2 @@
+ScriptEngine-Name: ${project.name}
+ScriptEngine-Version: ${project.version}
\ No newline at end of file
diff --git a/graphql-scripting/pom.xml b/graphql-scripting/pom.xml
new file mode 100644
index 0000000..9ccfc86
--- /dev/null
+++ b/graphql-scripting/pom.xml
@@ -0,0 +1,190 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling-bundle-parent</artifactId>
+ <version>35</version>
+ <relativePath />
+ </parent>
+
+ <artifactId>org.apache.sling.scripting.graphql</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+
+ <name>Apache Sling Scripting GraphQL</name>
+ <description>Support for server-side GraphQL queries</description>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <sling.java.version>8</sling.java.version>
+ <org.ops4j.pax.exam.version>4.13.1</org.ops4j.pax.exam.version>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>integration-test</goal>
+ <goal>verify</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ <!-- pax exam bug, often times out at exit -->
+ <forkedProcessExitTimeoutInSeconds>1</forkedProcessExitTimeoutInSeconds>
+ <systemProperties>
+ <property>
+ <name>bundle.filename</name>
+ <value>${basedir}/target/${project.build.finalName}.jar</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.servicemix.tooling</groupId>
+ <artifactId>depends-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <!-- javax -->
+ <dependency>
+ <groupId>javax.inject</groupId>
+ <artifactId>javax.inject</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <!-- OSGi -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.cmpn</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.component.annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.metatype.annotations</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- Apache Felix -->
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.framework</artifactId>
+ <version>6.0.3</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- Apache Sling -->
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.7.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.resource.presence</artifactId>
+ <version>0.0.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.api</artifactId>
+ <version>2.1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.scripting.core</artifactId>
+ <version>2.0.30</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.paxexam</artifactId>
+ <version>3.1.0</version>
+ <scope>test</scope>
+ </dependency>
+ <!-- logging -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <!-- testing -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-cm</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-container-forked</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-junit4</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-link-mvn</artifactId>
+ <version>${org.ops4j.pax.exam.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngine.java b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngine.java
new file mode 100644
index 0000000..a7a5689
--- /dev/null
+++ b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngine.java
@@ -0,0 +1,65 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sling.scripting.gql.internal;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Date;
+
+import javax.script.AbstractScriptEngine;
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptException;
+
+public class GraphQLScriptEngine extends AbstractScriptEngine {
+
+ private final GraphQLScriptEngineFactory factory;
+
+ public GraphQLScriptEngine(GraphQLScriptEngineFactory factory) {
+ this.factory = factory;
+ }
+
+ @Override
+ public Object eval(String script, ScriptContext context) throws ScriptException {
+ try {
+ context.getWriter().write("Hey this is " + getClass().getName() + " at " + new Date());
+ } catch(IOException e) {
+ throw new ScriptException(e);
+ }
+ return null;
+ }
+
+ @Override
+ public Object eval(Reader reader, ScriptContext context) throws ScriptException {
+ return eval("", context);
+ }
+
+ @Override
+ public Bindings createBindings() {
+ return null;
+ }
+
+ @Override
+ public ScriptEngineFactory getFactory() {
+ return factory;
+ }
+}
diff --git a/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactory.java b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactory.java
new file mode 100644
index 0000000..8215be9
--- /dev/null
+++ b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactory.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sling.scripting.gql.internal;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+
+import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.metatype.annotations.Designate;
+
+@Component(
+ service = ScriptEngineFactory.class,
+ immediate = true,
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Apache Sling Scripting GraphQL ScriptEngineFactory",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+ }
+)
+
+@Designate(
+ ocd = GraphQLScriptEngineFactoryConfiguration.class
+)
+
+public class GraphQLScriptEngineFactory extends AbstractScriptEngineFactory {
+
+ public static final String LANGUAGE_NAME = "GraphQL";
+ public static final String LANGUAGE_VERSION = "Sling:GraphQL:0.1";
+
+ @Activate
+ private void activate(final GraphQLScriptEngineFactoryConfiguration config) {
+ setExtensions(config.extensions());
+ setMimeTypes(config.mimeTypes());
+ setNames(config.names());
+ }
+
+ @Override
+ public String getLanguageName() {
+ return LANGUAGE_NAME;
+ }
+
+ @Override
+ public String getLanguageVersion() {
+ return LANGUAGE_VERSION;
+ }
+
+ @Override
+ public ScriptEngine getScriptEngine() {
+ return new GraphQLScriptEngine(this);
+ }
+}
diff --git a/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactoryConfiguration.java b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactoryConfiguration.java
new file mode 100644
index 0000000..d759017
--- /dev/null
+++ b/graphql-scripting/src/main/java/org/apache/sling/scripting/gql/internal/GraphQLScriptEngineFactoryConfiguration.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.gql.internal;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(
+ name = "Apache Sling Scripting GraphQL ScriptEngineFactory",
+ description = "Scripting engine for GraphQL queries"
+)
+@interface GraphQLScriptEngineFactoryConfiguration {
+
+ @AttributeDefinition(
+ name = "extensions",
+ description = "extensions"
+ )
+ String[] extensions() default {
+ "gql"
+ };
+
+ @AttributeDefinition(
+ name = "mime types",
+ description = "mime types"
+ )
+ String[] mimeTypes() default {
+ // TODO just making this up, is there a standard value?
+ "text/x-graphql"
+ };
+
+ @AttributeDefinition(
+ name = "names",
+ description = "names"
+ )
+ String[] names() default {
+ "GraphQL",
+ "graphql"
+ };
+}
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptEngineIT.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptEngineIT.java
new file mode 100644
index 0000000..24666a1
--- /dev/null
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptEngineIT.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.graphql.it;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class GraphQLScriptEngineIT extends GraphQLScriptingTestSupport {
+
+ @Configuration
+ public Option[] configuration() {
+ return new Option[] { baseConfiguration() };
+ }
+
+ @Test
+ public void testEnginePresent() throws ScriptException {
+ assertNotNull("Expected configAdmin to be present", configAdmin);
+ assertNotNull("Expecting ScriptEngineFactory to be present", scriptEngineFactory);
+ final ScriptEngine engine = scriptEngineFactory.getScriptEngine();
+ assertNotNull("Expecting ScriptEngine to be provided", engine);
+ assertEquals("Expecting our GraphQLScriptEngine", "GraphQLScriptEngine", engine.getClass().getSimpleName());
+ }
+}
\ No newline at end of file
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java
new file mode 100644
index 0000000..57718c2
--- /dev/null
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.graphql.it;
+
+import javax.inject.Inject;
+import javax.script.ScriptEngineFactory;
+
+import org.apache.sling.testing.paxexam.TestSupport;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.ProbeBuilder;
+import org.ops4j.pax.exam.TestProbeBuilder;
+import org.ops4j.pax.exam.options.ModifiableCompositeOption;
+import org.ops4j.pax.exam.util.Filter;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingResourcePresence;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingScripting;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+
+public abstract class GraphQLScriptingTestSupport extends TestSupport {
+
+ @Inject
+ @Filter(value = "(names=graphql)")
+ protected ScriptEngineFactory scriptEngineFactory;
+
+ @Inject
+ protected ConfigurationAdmin configAdmin;
+
+ public ModifiableCompositeOption baseConfiguration() {
+ return composite(
+ super.baseConfiguration(),
+ slingQuickstart(),
+ testBundle("bundle.filename"),
+
+ // testing
+ slingResourcePresence(),
+ junitBundles()
+ );
+ }
+
+ @ProbeBuilder
+ public TestProbeBuilder probeConfiguration(final TestProbeBuilder testProbeBuilder) {
+ testProbeBuilder.setHeader("Sling-Initial-Content", "initial-content");
+ return testProbeBuilder;
+ }
+
+ protected Option slingQuickstart() {
+ final int httpPort = findFreePort();
+ final String workingDirectory = workingDirectory();
+ return composite(
+ slingQuickstartOakTar(workingDirectory, httpPort),
+ slingScripting()
+ );
+ }
+
+}