2015-05-04 03:01:03 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <valijson/adapters/rapidjson_adapter.hpp>
|
|
|
|
|
|
|
|
#include <valijson/schema.hpp>
|
|
|
|
#include <valijson/schema_parser.hpp>
|
|
|
|
#include <valijson/validator.hpp>
|
|
|
|
|
|
|
|
using valijson::Schema;
|
|
|
|
using valijson::SchemaParser;
|
|
|
|
using valijson::adapters::RapidJsonAdapter;
|
|
|
|
using valijson::Validator;
|
|
|
|
|
|
|
|
typedef SchemaParser::FetchDocumentFunction<RapidJsonAdapter>::Type
|
|
|
|
FetchDocumentFunction;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> allocator;
|
|
|
|
static rapidjson::Value fetchedRoot;
|
|
|
|
static RapidJsonAdapter fetchedRootAdapter;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestFetchDocumentCallback : public ::testing::Test
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::shared_ptr<const RapidJsonAdapter> fetchDocument(const std::string &uri)
|
|
|
|
{
|
2015-05-05 08:18:21 +02:00
|
|
|
EXPECT_STREQ("test", uri.c_str());
|
2015-05-04 03:01:03 +02:00
|
|
|
|
|
|
|
rapidjson::Value valueOfTypeAttribute;
|
|
|
|
valueOfTypeAttribute.SetString("string", allocator);
|
|
|
|
|
|
|
|
rapidjson::Value schemaOfTestProperty;
|
|
|
|
schemaOfTestProperty.SetObject();
|
|
|
|
schemaOfTestProperty.AddMember("type", valueOfTypeAttribute, allocator);
|
|
|
|
|
|
|
|
rapidjson::Value propertiesConstraint;
|
|
|
|
propertiesConstraint.SetObject();
|
|
|
|
propertiesConstraint.AddMember("test", schemaOfTestProperty, allocator);
|
|
|
|
|
|
|
|
fetchedRoot.SetObject();
|
|
|
|
fetchedRoot.AddMember("properties", propertiesConstraint, allocator);
|
|
|
|
|
|
|
|
// Have to ensure that fetchedRoot exists for at least as long as the
|
|
|
|
// shared pointer that we return here
|
|
|
|
return boost::make_shared<RapidJsonAdapter>(fetchedRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TestFetchDocumentCallback, Basics)
|
|
|
|
{
|
|
|
|
// Define schema
|
|
|
|
rapidjson::Document schemaDocument;
|
|
|
|
RapidJsonAdapter schemaDocumentAdapter(schemaDocument);
|
|
|
|
schemaDocument.SetObject();
|
2015-05-05 08:18:21 +02:00
|
|
|
schemaDocument.AddMember("$ref", "test#/", allocator);
|
2015-05-04 03:01:03 +02:00
|
|
|
|
|
|
|
// Parse schema document
|
|
|
|
Schema schema;
|
|
|
|
SchemaParser schemaParser;
|
|
|
|
schemaParser.populateSchema(schemaDocumentAdapter, schema,
|
|
|
|
boost::make_optional<FetchDocumentFunction>(fetchDocument));
|
|
|
|
|
|
|
|
// Test resulting schema with a valid document
|
|
|
|
rapidjson::Document validDocument;
|
|
|
|
validDocument.SetObject();
|
|
|
|
validDocument.AddMember("test", "valid", allocator);
|
2015-12-23 04:34:04 +01:00
|
|
|
Validator validator;
|
|
|
|
EXPECT_TRUE(validator.validate(schema, RapidJsonAdapter(validDocument), NULL));
|
2015-05-04 03:01:03 +02:00
|
|
|
|
|
|
|
// Test resulting schema with an invalid document
|
|
|
|
rapidjson::Document invalidDocument;
|
|
|
|
invalidDocument.SetObject();
|
|
|
|
invalidDocument.AddMember("test", 123, allocator);
|
2015-12-23 04:34:04 +01:00
|
|
|
EXPECT_FALSE(validator.validate(schema, RapidJsonAdapter(invalidDocument), NULL));
|
2015-05-04 03:01:03 +02:00
|
|
|
}
|