#-- # ============================================================================= # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # * The names of its contributors may not be used to endorse or promote # products derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ============================================================================= #++ $:.unshift "../lib" require 'copland/errors' require 'copland/configuration/yaml/schema.rb' require 'copland/default-schema-processor.rb' require 'test/unit' require 'mock' require 'yaml' class TC_DefaultSchemaProcessor < Test::Unit::TestCase def setup @parser = Copland::Configuration::YAML::SchemaParser.new @processor = Copland::DefaultSchemaProcessor.new @owner = MockServicePoint.new @client = MockServicePoint.new y = YAML::load( %Q{ definition: class: type: string required: true something: type: any integer: type: integer real: type: real service: type: service configuration: type: configuration log: type: log array: type: array hash: type: hash boolean: type: boolean string: type: string subschema: definition: user.name: type: string required: true auto.load: type: boolean services: type: array options: type: array definition: option.name: type: string required: true option.value: type: string required: true } ) @schema = @parser.process( @owner, y ) end def test_illegal_parameters data = { "class" => "hello", "bogus" => "value" } assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_missing_parameters data = { "something" => "X" } assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_wrong_type pairs = { "integer" => "hello", "real" => "hello", "service" => "hello", "configuration" => "hello", "array" => "hello", "hash" => "hello", "boolean" => "hello", "string" => 15 } pairs.each do |k,v| data = { "class" => "hello", k => v } assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end end def test_validate_any data = { "class" => "hello", "something" => "value" } assert_nothing_raised do @processor.validate( @owner, @client, @schema, data ) end data = { "class" => "hello", "something" => 15 } assert_nothing_raised do @processor.validate( @owner, @client, @schema, data ) end data = { "class" => "hello", "something" => [ "one", "two" ] } assert_nothing_raised do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_bad_subschema_missing_key data = YAML::load( %Q{ class: hello subschema: auto.load: true } ) assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_bad_subschema_extra_key data = YAML::load( %Q{ class: hello subschema: user.name: bob bogus: value } ) assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_bad_subschema_type_mismatch data = YAML::load( %Q{ class: hello subschema: user.name: bob auto.load: value } ) assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_array_subschema_missing_key data = YAML::load( %Q{ class: hello options: - option.name: bob } ) assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_array_subschema_wrong_type data = YAML::load( %Q{ class: hello options: - option.name: bob option.value: !!service some.service } ) assert_raise( Copland::ValidationException ) do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_array_subschema data = YAML::load( %Q{ class: hello options: - option.name: bob option.value: jim - option.name: tom option.value: carl - option.name: another option.value: pair } ) assert_nothing_raised do @processor.validate( @owner, @client, @schema, data ) end end def test_validate_success data = YAML::load( %Q{ class: hello something: :a_symbol integer: 15 real: 3.14 service: !!service copland.Something configuration: !!configuration copland.Something log: !!log ~ array: - one - two hash: one: two three: four boolean: true string: hello } ) assert_nothing_raised do @processor.validate( @owner, @client, @schema, data ) end end end