From eeb2b47ee2c039abaa5d1e3fd6424f4c5e07831a Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Fri, 22 Dec 2023 10:40:20 +0200 Subject: [PATCH] AVRO-3900: Merge the validators integration tests in one test Just make sure the setup of the custom validators is done before the parsing of any schema to prevent registering the default validator (SpecificationValidator) Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/avro/examples/test_interop_data.rs | 2 +- .../avro/tests/validator_enum_symbol_name.rs | 44 ---------- .../avro/tests/validator_record_field_name.rs | 49 ----------- lang/rust/avro/tests/validator_schema_name.rs | 44 ---------- .../avro/tests/validator_schema_namespace.rs | 44 ---------- lang/rust/avro/tests/validators.rs | 84 +++++++++++++++++++ 6 files changed, 85 insertions(+), 182 deletions(-) delete mode 100644 lang/rust/avro/tests/validator_enum_symbol_name.rs delete mode 100644 lang/rust/avro/tests/validator_record_field_name.rs delete mode 100644 lang/rust/avro/tests/validator_schema_name.rs delete mode 100644 lang/rust/avro/tests/validator_schema_namespace.rs create mode 100644 lang/rust/avro/tests/validators.rs diff --git a/lang/rust/avro/examples/test_interop_data.rs b/lang/rust/avro/examples/test_interop_data.rs index f62bf345b76..352c427fff9 100644 --- a/lang/rust/avro/examples/test_interop_data.rs +++ b/lang/rust/avro/examples/test_interop_data.rs @@ -16,12 +16,12 @@ // under the License. use apache_avro::Reader; +use std::error::Error; use std::{ collections::HashMap, ffi::OsStr, io::{BufReader, Read}, }; -use std::error::Error; fn main() -> Result<(), Box> { let mut expected_user_metadata: HashMap> = HashMap::new(); diff --git a/lang/rust/avro/tests/validator_enum_symbol_name.rs b/lang/rust/avro/tests/validator_enum_symbol_name.rs deleted file mode 100644 index 7420e4c0975..00000000000 --- a/lang/rust/avro/tests/validator_enum_symbol_name.rs +++ /dev/null @@ -1,44 +0,0 @@ -// 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. - -use apache_avro::{ - validator::{set_enum_symbol_name_validator, EnumSymbolNameValidator}, - AvroResult, -}; -use apache_avro_test_helper::TestResult; - -struct CustomValidator; - -#[test] -fn avro_3900_custom_enum_symbol_validator_with_spec_invalid_enum_symbol_names() -> TestResult { - impl EnumSymbolNameValidator for CustomValidator { - fn validate(&self, _ns: &str) -> AvroResult<()> { - Ok(()) - } - } - - assert!(set_enum_symbol_name_validator(Box::new(CustomValidator)).is_ok()); - - let schema = r#"{ - "type": "enum", - "name": "Test", - "symbols": ["A-B", "B-A"] - }"#; - apache_avro::Schema::parse_str(schema)?; - - Ok(()) -} diff --git a/lang/rust/avro/tests/validator_record_field_name.rs b/lang/rust/avro/tests/validator_record_field_name.rs deleted file mode 100644 index d13283de6a1..00000000000 --- a/lang/rust/avro/tests/validator_record_field_name.rs +++ /dev/null @@ -1,49 +0,0 @@ -// 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. - -use apache_avro::{ - validator::{set_record_field_name_validator, RecordFieldNameValidator}, - AvroResult, -}; -use apache_avro_test_helper::TestResult; - -struct CustomValidator; - -#[test] -fn avro_3900_custom_record_field_validator_with_spec_invalid_field_name() -> TestResult { - impl RecordFieldNameValidator for CustomValidator { - fn validate(&self, _ns: &str) -> AvroResult<()> { - Ok(()) - } - } - - assert!(set_record_field_name_validator(Box::new(CustomValidator)).is_ok()); - - let schema = r#"{ - "type": "record", - "name": "Test", - "fields": [ - { - "name": "A-B", - "type": "int" - } - ] - }"#; - apache_avro::Schema::parse_str(schema)?; - - Ok(()) -} diff --git a/lang/rust/avro/tests/validator_schema_name.rs b/lang/rust/avro/tests/validator_schema_name.rs deleted file mode 100644 index 3ceac1fb314..00000000000 --- a/lang/rust/avro/tests/validator_schema_name.rs +++ /dev/null @@ -1,44 +0,0 @@ -// 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. - -use apache_avro::{ - schema::Namespace, - validator::{set_schema_name_validator, SchemaNameValidator}, - AvroResult, -}; -use apache_avro_test_helper::TestResult; - -struct CustomValidator; - -#[test] -fn avro_3900_custom_schema_name_validator_with_spec_invalid_name() -> TestResult { - impl SchemaNameValidator for CustomValidator { - fn validate(&self, schema_name: &str) -> AvroResult<(String, Namespace)> { - Ok((schema_name.to_string(), None)) - } - } - - assert!(set_schema_name_validator(Box::new(CustomValidator)).is_ok()); - - let schema = r#"{ - "name": "com-example", - "type": "int"} - "#; - apache_avro::Schema::parse_str(schema)?; - - Ok(()) -} diff --git a/lang/rust/avro/tests/validator_schema_namespace.rs b/lang/rust/avro/tests/validator_schema_namespace.rs deleted file mode 100644 index e0cfbc04d77..00000000000 --- a/lang/rust/avro/tests/validator_schema_namespace.rs +++ /dev/null @@ -1,44 +0,0 @@ -// 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. - -use apache_avro::{ - validator::{set_schema_namespace_validator, SchemaNamespaceValidator}, - AvroResult, -}; -use apache_avro_test_helper::TestResult; - -struct CustomValidator; - -#[test] -fn avro_3900_custom_namespace_validator_with_spec_invalid_ns() -> TestResult { - impl SchemaNamespaceValidator for CustomValidator { - fn validate(&self, _ns: &str) -> AvroResult<()> { - Ok(()) - } - } - - assert!(set_schema_namespace_validator(Box::new(CustomValidator)).is_ok()); - - let schema = r#"{ - "name": "name", - "namespace": "com-example", - "type": "int"} - "#; - apache_avro::Schema::parse_str(schema)?; - - Ok(()) -} diff --git a/lang/rust/avro/tests/validators.rs b/lang/rust/avro/tests/validators.rs new file mode 100644 index 00000000000..1cc5df6f4dd --- /dev/null +++ b/lang/rust/avro/tests/validators.rs @@ -0,0 +1,84 @@ +// 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. + +use apache_avro::schema::Namespace; +use apache_avro::validator::{ + set_record_field_name_validator, set_schema_name_validator, set_schema_namespace_validator, + RecordFieldNameValidator, SchemaNameValidator, SchemaNamespaceValidator, +}; +use apache_avro::{ + validator::{set_enum_symbol_name_validator, EnumSymbolNameValidator}, + AvroResult, +}; +use apache_avro_test_helper::TestResult; + +struct CustomValidator; + +#[test] +fn avro_3900_custom_validator_with_spec_invalid_names() -> TestResult { + // Setup the custom validators before the schema is parsed + // because the parsing will trigger the validation and will + // setup the default validator (SpecificationValidator)! + impl SchemaNameValidator for CustomValidator { + fn validate(&self, schema_name: &str) -> AvroResult<(String, Namespace)> { + Ok((schema_name.to_string(), None)) + } + } + impl SchemaNamespaceValidator for CustomValidator { + fn validate(&self, _ns: &str) -> AvroResult<()> { + Ok(()) + } + } + + impl EnumSymbolNameValidator for CustomValidator { + fn validate(&self, _ns: &str) -> AvroResult<()> { + Ok(()) + } + } + + impl RecordFieldNameValidator for CustomValidator { + fn validate(&self, _ns: &str) -> AvroResult<()> { + Ok(()) + } + } + + assert!(set_schema_name_validator(Box::new(CustomValidator)).is_ok()); + assert!(set_schema_namespace_validator(Box::new(CustomValidator)).is_ok()); + assert!(set_enum_symbol_name_validator(Box::new(CustomValidator)).is_ok()); + assert!(set_record_field_name_validator(Box::new(CustomValidator)).is_ok()); + + let schema = r#"{ + "name": "invalid-schema-name", + "namespace": "invalid-namespace", + "type": "record", + "fields": [ + { + "name": "invalid-field-name", + "type": "int" + }, + { + "type": "enum", + "name": "Test", + "symbols": ["A-B", "B-A"] + } + ] + }"#; + + apache_avro::Schema::parse_str(schema)?; + + Ok(()) +}