/** * Copyright (c) 2021 OceanBase * OceanBase CE is licensed under Mulan PubL v2. * You can use this software according to the terms and conditions of the Mulan PubL v2. * You may obtain a copy of Mulan PubL v2 at: * http://license.coscl.org.cn/MulanPubL-2.0 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PubL v2 for more details. */ #include #define private public #include "lib/json_type/ob_json_tree.h" #include "lib/json_type/ob_json_parse.h" #include "lib/timezone/ob_timezone_info.h" #include "lib/json_type/ob_json_schema.h" #undef private #include using namespace std; namespace oceanbase { namespace common { class TestJsonSchema : public ::testing::Test { public: TestJsonSchema() {} ~TestJsonSchema() {} virtual void SetUp() {} virtual void TearDown() {} static void SetUpTestCase() {} static void TearDownTestCase() {} private: // disallow copy DISALLOW_COPY_AND_ASSIGN(TestJsonSchema); }; ObString j_schema_to_str("{\"$id\": \"httpexample.com/schemas/customer\", \"type\": \"object\", \"$defs\": {\"name\": {\"type\": \"string\"}}, \"required\": [\"first_name\", \"last_name\"], \"properties\": {\"last_name\": {\"$ref\": \"#/$defs/name\"}, \"first_name\": {\"$ref\": \"#/$defs/name\"}}}"); TEST_F(TestJsonSchema, test_parse_json_schema_ref) { ObArenaAllocator allocator(ObModIds::TEST); ObIJsonBase *j_tree = NULL; ObIJsonBase *j_bin = NULL; ObJsonSeekResult hit; ObJsonBuffer j_buf(&allocator); common::ObString j_text("{\"$id\": \"httpexample.com/schemas/customer\"," "\"type\": \"object\"," "\"properties\": {\"first_name\": { \"$ref\": \"#/$defs/name\" }," "\"last_name\": { \"$ref\": \"#/$defs/name\" }}," "\"required\": [\"first_name\", \"last_name\"]," "\"$defs\": {\"name\": { \"type\": \"string\" }}}"); ASSERT_EQ(OB_SUCCESS, ObJsonBaseFactory::get_json_base(&allocator, j_text, ObJsonInType::JSON_TREE, ObJsonInType::JSON_TREE, j_tree, ObJsonParser::JSN_SCHEMA_FLAG)); ASSERT_EQ(OB_SUCCESS, j_tree->print(j_buf, false)); ASSERT_EQ(j_schema_to_str, ObString(j_buf.length(), j_buf.ptr())); common::ObString j_text_wrong("{\"$id\": \"httpexample.com/schemas/customer\"," "\"type\": \"object\"," "\"properties\": {\"first_name\": { \"$ref\": \"#/$defs/name\" }," "\"last_name\": { \"$ref\": \"/$defs/name\" }}," "\"required\": [\"first_name\", \"last_name\"]," "\"$defs\": {\"name\": { \"type\": \"string\" }}}"); ASSERT_EQ(OB_ERR_UNSUPPROTED_REF_IN_JSON_SCHEMA, ObJsonBaseFactory::get_json_base(&allocator, j_text_wrong, ObJsonInType::JSON_TREE, ObJsonInType::JSON_TREE, j_tree, ObJsonParser::JSN_SCHEMA_FLAG)); } TEST_F(TestJsonSchema, test_parse_json_schema_dup_key) { ObArenaAllocator allocator(ObModIds::TEST); ObIJsonBase *j_tree = NULL; ObIJsonBase *j_bin = NULL; ObJsonSeekResult hit; ObJsonBuffer j_buf(&allocator); common::ObString j_text("{\"$id\": \"httpexample.com/schemas/customer\"," "\"type\": \"object\"," "\"type\": \"number\"," "\"properties\": {\"first_name\": { \"$ref\": \"#/$defs/name\" }," "\"last_name\": { \"$ref\": \"#/$defs/name\" }}," "\"required\": [\"first_name\", \"last_name\"]," "\"required\": [\"test_name\"]," "\"$defs\": {\"name\": { \"type\": \"string\" }}}"); ASSERT_EQ(OB_SUCCESS, ObJsonBaseFactory::get_json_base(&allocator, j_text, ObJsonInType::JSON_TREE, ObJsonInType::JSON_TREE, j_tree, ObJsonParser::JSN_SCHEMA_FLAG)); ASSERT_EQ(OB_SUCCESS,j_tree->print(j_buf, false)); ASSERT_EQ(j_schema_to_str, ObString(j_buf.length(), j_buf.ptr())); common::ObString j_text_wrong("{\"$id\": \"httpexample.com/schemas/customer\"," "\"type\": \"object\"," "\"properties\": {\"first_name\": { \"$ref\": \"#/$defs/name\" }," "\"last_name\": { \"$ref\": \"#/$defs/name\" }}," "\"last_name\": { \"$ref\": \"/$defs/name\" }}," "\"required\": [\"first_name\", \"last_name\"]," "\"$defs\": {\"name\": { \"type\": \"string\" }}}"); ASSERT_EQ(OB_ERR_UNSUPPROTED_REF_IN_JSON_SCHEMA, ObJsonBaseFactory::get_json_base(&allocator, j_text_wrong, ObJsonInType::JSON_TREE, ObJsonInType::JSON_TREE, j_tree, ObJsonParser::JSN_SCHEMA_FLAG)); } # define STRING_TYPE_COUNT 2 ObString string_type_str[STRING_TYPE_COUNT] = { "{\"schema\": {\"type\": 4, \"pattern\": \"^S_\", \"maxLength\": 3, \"minLength\": 2}}", "{}" }; TEST_F(TestJsonSchema, test_parse_string_type) { ObArenaAllocator allocator(ObModIds::TEST); ObIJsonBase *j_tree = NULL; ObIJsonBase *j_bin = NULL; ObJsonSeekResult hit; ObJsonBuffer j_buf(&allocator); common::ObString j_text("{\"type\": \"string\",\"pattern\":\"^S_\",\"minLength\": 2,\"maxLength\": 3}"); ASSERT_EQ(OB_SUCCESS, ObJsonBaseFactory::get_json_base(&allocator, j_text, ObJsonInType::JSON_TREE, ObJsonInType::JSON_TREE, j_tree, ObJsonParser::JSN_SCHEMA_FLAG)); ObJsonSchemaTree json_schema(&allocator); ASSERT_EQ(OB_SUCCESS, json_schema.build_schema_tree(j_tree)); int schema_count = json_schema.schema_map_->element_count(); ASSERT_EQ(STRING_TYPE_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(NUMBER_TYPE_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(NUMBER_TYPE_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(NULL_TYPE_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(NULL_TYPE_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(PRO_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(DEP_REUQUIRED_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(DEP_UNNESTED_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(DEP_NESTED_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(PATTERN_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(PATTERN_AND_PRO, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(ADD_PRO_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(PATTERN_PRO_ADD, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(ITEMS_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(TUPLE_ITEMS_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(ADD_ITEMS_COUNT, schema_count); for (int i = 0; i < schema_count; ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(ALLOF_UNNESTED_COUNT, schema_count); for (int i = 0; i < schema_count; i++) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(ALLOF_NESTED_COUNT, schema_count); for (int i = 0; i < schema_count; i++) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(NOT_NESTED_COUNT, schema_count); for (int i = 0; i < schema_count; i++) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<element_count(); ASSERT_EQ(COUNT_COMPLEX_SCHEMA, schema_count); for (int i = 0; i < schema_count; i++) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); j_buf.reset(); j_value->print(j_buf, false); // cout<element_count(); ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); cout<<"json_schema "<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS,schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<element_count(); ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); //cout<<"json_schema "<get_array_element(i, j_value)); ASSERT_EQ(OB_SUCCESS, schema_validator.schema_validator(j_value, is_valid)); //cout<element_count(); ++i) { ObIJsonBase *value = nullptr; ASSERT_EQ(OB_SUCCESS, json_schema.schema_map_->get_array_element(i, value)); j_buf.reset(); ASSERT_EQ(OB_SUCCESS, value->print(j_buf, false)); //cout<<"json_schema "<