You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cs...@apache.org on 2017/08/16 16:06:49 UTC
[incubator-openwhisk] branch master updated: Use Java based
JsonSchema validator (#2565)
This is an automated email from the ASF dual-hosted git repository.
csantanapr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git
The following commit(s) were added to refs/heads/master by this push:
new ee2830f Use Java based JsonSchema validator (#2565)
ee2830f is described below
commit ee2830f5a89f10b5cbc40a92560c28e9671f0fb6
Author: Markus Thömmes <ma...@me.com>
AuthorDate: Wed Aug 16 18:06:46 2017 +0200
Use Java based JsonSchema validator (#2565)
---
tests/build.gradle | 2 +
tests/src/test/scala/system/rest/JsonSchema.scala | 17 +++++----
.../test/scala/system/rest/JsonSchemaTests.scala | 2 +-
tools/jenkins/apache/dockerhub.groovy | 1 -
tools/json/validate.py | 43 ----------------------
tools/macos/README.md | 2 +-
tools/macos/docker-machine/README.md | 2 +-
tools/travis/setup.sh | 1 -
tools/ubuntu-setup/pip.sh | 1 -
9 files changed, 14 insertions(+), 57 deletions(-)
diff --git a/tests/build.gradle b/tests/build.gradle
index 1f94b14..a3bb047 100644
--- a/tests/build.gradle
+++ b/tests/build.gradle
@@ -52,6 +52,8 @@ dependencies {
compile 'org.scalamock:scalamock-scalatest-support_2.11:3.4.2'
compile 'com.typesafe.akka:akka-testkit_2.11:2.4.16'
compile 'com.typesafe.akka:akka-http-testkit_2.11:10.0.9'
+ compile 'com.github.java-json-tools:json-schema-validator:2.2.8';
+
compile project(':common:scala')
compile project(':core:controller')
compile project(':core:invoker')
diff --git a/tests/src/test/scala/system/rest/JsonSchema.scala b/tests/src/test/scala/system/rest/JsonSchema.scala
index 265a013..b9c2485 100644
--- a/tests/src/test/scala/system/rest/JsonSchema.scala
+++ b/tests/src/test/scala/system/rest/JsonSchema.scala
@@ -17,25 +17,26 @@
package system.rest
-import common.TestUtils
-import common.WhiskProperties
+import com.github.fge.jsonschema.main.JsonSchemaFactory
+import com.fasterxml.jackson.databind.ObjectMapper
/**
* Utilities for dealing with JSON schema
*
*/
-trait JsonSchema extends RestUtil {
-
- def validatorDir = WhiskProperties.getFileRelativeToWhiskHome("tools/json")
+trait JsonSchema {
/**
* Check whether a JSON document (represented as a String) conforms to a JSON schema (also a String).
- * Invokes a python utility.
*
* @return true if the document is valid, false otherwise
*/
def check(doc: String, schema: String): Boolean = {
- def result = TestUtils.runCmd(TestUtils.DONTCARE_EXIT, validatorDir, WhiskProperties.python, "validate.py", doc, schema)
- return result.stdout.trim() == "true"
+ val mapper = new ObjectMapper()
+ val docNode = mapper.readTree(doc)
+ val schemaNode = mapper.readTree(schema)
+
+ val validator = JsonSchemaFactory.byDefault().getValidator
+ validator.validate(schemaNode, docNode).isSuccess
}
}
diff --git a/tests/src/test/scala/system/rest/JsonSchemaTests.scala b/tests/src/test/scala/system/rest/JsonSchemaTests.scala
index 119c670..58fe41f 100644
--- a/tests/src/test/scala/system/rest/JsonSchemaTests.scala
+++ b/tests/src/test/scala/system/rest/JsonSchemaTests.scala
@@ -26,7 +26,7 @@ import org.scalatest.junit.JUnitRunner
* Basic tests of API calls for actions
*/
@RunWith(classOf[JUnitRunner])
-class JsonSchemaTests extends FlatSpec with Matchers with JsonSchema {
+class JsonSchemaTests extends FlatSpec with Matchers with JsonSchema with RestUtil {
def TEST_SCHEMA = """{
"type" : "object",
diff --git a/tools/jenkins/apache/dockerhub.groovy b/tools/jenkins/apache/dockerhub.groovy
index e4fc9a8..d99efa4 100644
--- a/tools/jenkins/apache/dockerhub.groovy
+++ b/tools/jenkins/apache/dockerhub.groovy
@@ -8,7 +8,6 @@ node('xenial && !H22') {
sh "pip install --user --upgrade pip"
withEnv(['PATH+LOCAL_JENKINS=/home/jenkins/.local/bin']) {
sh "pip install --user markupsafe"
- sh "pip install --user jsonschema"
sh "pip install --user couchdb"
sh "pip install --user ansible==2.3.0.0"
sh "pip install --user requests==2.10.0"
diff --git a/tools/json/validate.py b/tools/json/validate.py
deleted file mode 100755
index 0a20009..0000000
--- a/tools/json/validate.py
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/usr/bin/python
-"""Validate that a string conforms to a json schema.
-
- usage: validate.py obj schema
- where obj and schema are both strings.
- prints 'true' if validate succeeds, 'false' otherwise
-
-/*
- * 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.
- */
-"""
-import sys
-import json
-from jsonschema import validate
-
-
-if len(sys.argv) != 3:
- print('usage: validate.py obj schema')
- sys.exit(-1)
-
-a1 = sys.argv[1].replace('\n', '')
-a2 = sys.argv[2].replace('\n', '')
-obj = json.loads(a1)
-schema = json.loads(a2)
-
-try:
- validate(obj, schema)
- print('true')
-except:
- print('false')
diff --git a/tools/macos/README.md b/tools/macos/README.md
index 24eeb7d..7eb3a5b 100644
--- a/tools/macos/README.md
+++ b/tools/macos/README.md
@@ -32,7 +32,7 @@ sudo easy_install pip
# install docker for python
/usr/local/bin/pip install docker==2.2.1
# install script prerequisites
-sudo -H pip install ansible==2.3.0.0 jinja2==2.9.6 jsonschema couchdb' | bash
+sudo -H pip install ansible==2.3.0.0 jinja2==2.9.6 couchdb' | bash
```
# Build
diff --git a/tools/macos/docker-machine/README.md b/tools/macos/docker-machine/README.md
index 831edf3..7513bd5 100644
--- a/tools/macos/docker-machine/README.md
+++ b/tools/macos/docker-machine/README.md
@@ -37,7 +37,7 @@ brew install scala
# install pip
sudo easy_install pip
# install script prerequisites
-sudo -H pip install ansible==2.3.0.0 jinja2==2.9.6 jsonschema couchdb' | bash
+sudo -H pip install ansible==2.3.0.0 jinja2==2.9.6 couchdb' | bash
```
# Create and configure Docker machine
diff --git a/tools/travis/setup.sh b/tools/travis/setup.sh
index 2dfd4cd..f4ec7b9 100755
--- a/tools/travis/setup.sh
+++ b/tools/travis/setup.sh
@@ -13,7 +13,6 @@ echo "Docker Info:"
docker info
# Python
-pip install --user jsonschema
pip install --user couchdb
# Ansible
diff --git a/tools/ubuntu-setup/pip.sh b/tools/ubuntu-setup/pip.sh
index e73618a..caa9a95 100755
--- a/tools/ubuntu-setup/pip.sh
+++ b/tools/ubuntu-setup/pip.sh
@@ -3,6 +3,5 @@ set -e
set -x
sudo apt-get install -y python-pip
-sudo pip install jsonschema
sudo pip install argcomplete
sudo pip install couchdb
--
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].