#include #include #include #include #include #include using valijson::Schema; using valijson::SchemaParser; using valijson::adapters::RapidJsonAdapter; using valijson::Validator; typedef SchemaParser::FetchDocumentFunction::Type FetchDocumentFunction; namespace { static rapidjson::MemoryPoolAllocator allocator; static rapidjson::Value fetchedRoot; static RapidJsonAdapter fetchedRootAdapter; } class TestFetchDocumentCallback : public ::testing::Test { }; boost::shared_ptr fetchDocument(const std::string &uri) { EXPECT_STREQ("test", uri.c_str()); 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(fetchedRoot); } TEST_F(TestFetchDocumentCallback, Basics) { // Define schema rapidjson::Document schemaDocument; RapidJsonAdapter schemaDocumentAdapter(schemaDocument); schemaDocument.SetObject(); schemaDocument.AddMember("$ref", "test#/", allocator); // Parse schema document Schema schema; SchemaParser schemaParser; schemaParser.populateSchema(schemaDocumentAdapter, schema, boost::make_optional(fetchDocument)); // Test resulting schema with a valid document rapidjson::Document validDocument; validDocument.SetObject(); validDocument.AddMember("test", "valid", allocator); Validator validator(schema); EXPECT_TRUE(validator.validate(RapidJsonAdapter(validDocument), NULL)); // Test resulting schema with an invalid document rapidjson::Document invalidDocument; invalidDocument.SetObject(); invalidDocument.AddMember("test", 123, allocator); EXPECT_FALSE(validator.validate(RapidJsonAdapter(invalidDocument), NULL)); }