diff --git a/lang/rust/avro/examples/test_interop_data.rs b/lang/rust/avro/examples/test_interop_data.rs index f62bf345b76..39c97d0649e 100644 --- a/lang/rust/avro/examples/test_interop_data.rs +++ b/lang/rust/avro/examples/test_interop_data.rs @@ -18,10 +18,10 @@ use apache_avro::Reader; use std::{ collections::HashMap, + error::Error, 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..be2a31f4b7d --- /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, + validator::{ + set_enum_symbol_name_validator, set_record_field_name_validator, set_schema_name_validator, + set_schema_namespace_validator, EnumSymbolNameValidator, RecordFieldNameValidator, + SchemaNameValidator, SchemaNamespaceValidator, + }, + 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(()) +}