2015-05-20 08:42:21 +00:00
|
|
|
%%%=============================================================================
|
|
|
|
%% Copyright 2014 Klarna AB
|
|
|
|
%%
|
|
|
|
%% Licensed 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.
|
|
|
|
%%
|
|
|
|
%%
|
|
|
|
%% @doc jesse test suite which covers Draft 04. It uses JSON-Schema-Test-Suite
|
|
|
|
%% (https://github.com/json-schema/JSON-Schema-Test-Suite) as the test data.
|
|
|
|
%% @end
|
|
|
|
%%%=============================================================================
|
|
|
|
|
|
|
|
-module(jesse_tests_draft4_SUITE).
|
|
|
|
|
2015-12-14 15:31:33 +00:00
|
|
|
-compile([ export_all
|
|
|
|
]).
|
|
|
|
|
|
|
|
-define(EXCLUDED_FUNS, [ module_info
|
|
|
|
, all
|
|
|
|
, init_per_suite
|
|
|
|
, end_per_suite
|
|
|
|
, do_test
|
|
|
|
, run_tests
|
|
|
|
, run_test_set
|
|
|
|
, load_test_specs
|
|
|
|
, filename_to_key
|
|
|
|
, get_path
|
|
|
|
, load_schema
|
|
|
|
]).
|
2015-05-20 08:42:21 +00:00
|
|
|
|
|
|
|
-include_lib("common_test/include/ct.hrl").
|
|
|
|
|
|
|
|
%% JSON-Schema-Test-Suite attributes definitions
|
|
|
|
-define(DATA, <<"data">>).
|
|
|
|
-define(DESCRIPTION, <<"description">>).
|
|
|
|
-define(SCHEMA, <<"schema">>).
|
|
|
|
-define(TESTS, <<"tests">>).
|
|
|
|
-define(VALID, <<"valid">>).
|
|
|
|
|
|
|
|
all() ->
|
2015-12-14 15:31:33 +00:00
|
|
|
Exports = ?MODULE:module_info(exports),
|
|
|
|
[F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)].
|
2015-05-20 08:42:21 +00:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
2015-05-22 13:38:21 +00:00
|
|
|
inets:start(),
|
2015-05-20 08:42:21 +00:00
|
|
|
load_test_specs() ++ Config.
|
|
|
|
|
|
|
|
end_per_suite(_Config) ->
|
2015-05-22 13:38:21 +00:00
|
|
|
inets:stop().
|
2015-05-20 08:42:21 +00:00
|
|
|
|
|
|
|
%%% Testcases
|
2015-12-14 15:31:33 +00:00
|
|
|
|
2015-05-20 08:42:21 +00:00
|
|
|
additionalItems(Config) ->
|
|
|
|
do_test("additionalItems", Config).
|
|
|
|
|
|
|
|
additionalProperties(Config) ->
|
|
|
|
do_test("additionalProperties", Config).
|
|
|
|
|
|
|
|
allOf(Config) ->
|
|
|
|
do_test("allOf", Config).
|
|
|
|
|
|
|
|
anyOf(Config) ->
|
|
|
|
do_test("anyOf", Config).
|
|
|
|
|
2015-05-20 14:34:15 +00:00
|
|
|
default(Config) ->
|
|
|
|
do_test("default", Config).
|
|
|
|
|
|
|
|
definitions(Config) ->
|
|
|
|
do_test("definitions", Config).
|
|
|
|
|
2015-05-20 08:42:21 +00:00
|
|
|
dependencies(Config) ->
|
|
|
|
do_test("dependencies", Config).
|
|
|
|
|
|
|
|
enum(Config) ->
|
|
|
|
do_test("enum", Config).
|
|
|
|
|
|
|
|
items(Config) ->
|
|
|
|
do_test("items", Config).
|
|
|
|
|
|
|
|
maximum(Config) ->
|
|
|
|
do_test("maximum", Config).
|
|
|
|
|
|
|
|
maxItems(Config) ->
|
|
|
|
do_test("maxItems", Config).
|
|
|
|
|
|
|
|
maxLength(Config) ->
|
|
|
|
do_test("maxLength", Config).
|
|
|
|
|
|
|
|
maxProperties(Config) ->
|
|
|
|
do_test("maxProperties", Config).
|
|
|
|
|
|
|
|
minimum(Config) ->
|
|
|
|
do_test("minimum", Config).
|
|
|
|
|
|
|
|
minItems(Config) ->
|
|
|
|
do_test("minItems", Config).
|
|
|
|
|
|
|
|
minLength(Config) ->
|
|
|
|
do_test("minLength", Config).
|
|
|
|
|
|
|
|
minProperties(Config) ->
|
|
|
|
do_test("minProperties", Config).
|
|
|
|
|
|
|
|
multipleOf(Config) ->
|
|
|
|
do_test("multipleOf", Config).
|
|
|
|
|
|
|
|
'not'(Config) ->
|
|
|
|
do_test("not", Config).
|
|
|
|
|
|
|
|
oneOf(Config) ->
|
|
|
|
do_test("oneOf", Config).
|
|
|
|
|
|
|
|
pattern(Config) ->
|
|
|
|
do_test("pattern", Config).
|
|
|
|
|
|
|
|
patternProperties(Config) ->
|
|
|
|
do_test("patternProperties", Config).
|
|
|
|
|
|
|
|
properties(Config) ->
|
|
|
|
do_test("properties", Config).
|
|
|
|
|
2015-05-22 13:38:21 +00:00
|
|
|
ref(Config) ->
|
|
|
|
do_test("ref", Config).
|
|
|
|
|
|
|
|
refRemote(Config) ->
|
2015-05-25 13:58:23 +00:00
|
|
|
TestDir = os:getenv("TEST_DIR"),
|
|
|
|
DocumentRoot = filename:join(TestDir, "JSON-Schema-Test-Suite/remotes"),
|
2016-02-28 22:30:28 +00:00
|
|
|
ServerOpts = [ {port, 1234}
|
|
|
|
, {server_name, "localhost"}
|
|
|
|
, {server_root, "."}
|
|
|
|
, {document_root, DocumentRoot}
|
|
|
|
],
|
2015-05-22 13:38:21 +00:00
|
|
|
inets:start(httpd, ServerOpts),
|
|
|
|
do_test("refRemote", Config).
|
2015-05-20 08:42:21 +00:00
|
|
|
|
|
|
|
required(Config) ->
|
|
|
|
do_test("required", Config).
|
|
|
|
|
|
|
|
type(Config) ->
|
|
|
|
do_test("type", Config).
|
|
|
|
|
|
|
|
uniqueItems(Config) ->
|
|
|
|
do_test("uniqueItems", Config).
|
|
|
|
|
|
|
|
%%% Internal functions
|
2015-12-14 15:31:33 +00:00
|
|
|
|
2015-05-20 08:42:21 +00:00
|
|
|
do_test(Key, Config) ->
|
2015-05-22 13:38:21 +00:00
|
|
|
run_tests(?config(Key, Config)).
|
2015-05-20 08:42:21 +00:00
|
|
|
|
|
|
|
run_tests(Specs) ->
|
|
|
|
lists:foreach( fun(Spec) ->
|
|
|
|
Description = get_path(?DESCRIPTION, Spec),
|
|
|
|
Schema = get_path(?SCHEMA, Spec),
|
|
|
|
TestSet = get_path(?TESTS, Spec),
|
2015-12-14 15:31:33 +00:00
|
|
|
ct:pal( "** Test set: ~s~n"
|
|
|
|
"** Schema: ~p~n"
|
|
|
|
, [Description, Schema]
|
|
|
|
),
|
2015-05-20 08:42:21 +00:00
|
|
|
run_test_set(Schema, TestSet)
|
|
|
|
end
|
|
|
|
, Specs
|
|
|
|
).
|
|
|
|
|
|
|
|
run_test_set(Schema, TestSet) ->
|
|
|
|
lists:foreach( fun(Test) ->
|
|
|
|
Description = get_path(?DESCRIPTION, Test),
|
|
|
|
TestData = get_path(?DATA, Test),
|
2015-05-20 14:34:15 +00:00
|
|
|
ct:pal("* Test case: ~s~n", [Description]),
|
2015-12-14 15:31:33 +00:00
|
|
|
Opts = [ { default_schema_ver
|
|
|
|
, <<"http://json-schema.org/draft-04/schema#">>
|
|
|
|
}
|
|
|
|
, {schema_loader_fun, fun load_schema/1}
|
|
|
|
],
|
2015-05-20 14:34:15 +00:00
|
|
|
try jesse:validate_with_schema(Schema, TestData, Opts) of
|
|
|
|
Result ->
|
|
|
|
ct:pal("Result: ~p~n", [Result]),
|
|
|
|
case get_path(?VALID, Test) of
|
|
|
|
true -> {ok, TestData} = Result;
|
|
|
|
false -> {error, _} = Result
|
|
|
|
end
|
|
|
|
catch C:E ->
|
|
|
|
ct:pal("Error: ~p:~p~nStacktrace: ~p~n",
|
|
|
|
[C, E, erlang:get_stacktrace()])
|
2015-05-20 08:42:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
, TestSet
|
|
|
|
).
|
|
|
|
|
|
|
|
load_test_specs() ->
|
2015-12-14 15:31:33 +00:00
|
|
|
TestsDir = filename:join( os:getenv("TEST_DIR")
|
|
|
|
, "JSON-Schema-Test-Suite/tests/draft4"
|
|
|
|
),
|
2015-05-20 08:42:21 +00:00
|
|
|
FileList = filelib:wildcard(TestsDir ++ "/*.json"),
|
|
|
|
lists:map( fun(Filename) ->
|
|
|
|
{ok, Bin} = file:read_file(Filename),
|
2015-12-14 14:56:31 +00:00
|
|
|
JsonTest = jsx:decode(Bin),
|
2015-05-20 08:42:21 +00:00
|
|
|
{filename_to_key(Filename), JsonTest}
|
|
|
|
end
|
|
|
|
, FileList
|
|
|
|
).
|
|
|
|
|
|
|
|
filename_to_key(Filename) ->
|
|
|
|
filename:rootname(filename:basename(Filename)).
|
|
|
|
|
|
|
|
get_path(Key, Schema) ->
|
|
|
|
jesse_json_path:path(Key, Schema).
|
|
|
|
|
2015-05-22 13:38:21 +00:00
|
|
|
load_schema(URI) ->
|
2015-05-25 13:58:23 +00:00
|
|
|
{ok, Response} = httpc:request(get, {URI, []}, [], [{body_format, binary}]),
|
|
|
|
{{_Line, 200, _}, _Headers, Body} = Response,
|
2015-12-14 14:56:31 +00:00
|
|
|
jsx:decode(Body).
|