blob: f2ed9994b31cd48570ce83611c3a423eb96377a5 [file] [log] [blame]
Marc Kodererfafcc4f2014-03-17 13:20:40 +01001# Copyright 2014 Deutsche Telekom AG
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import jsonschema
17import mock
18
19import tempest.common.generator.base_generator as base_generator
20from tempest.tests import base
21
22
23class TestNegativeBasicGenerator(base.TestCase):
24 valid_desc = {
25 "name": "list-flavors-with-detail",
26 "http-method": "GET",
27 "url": "flavors/detail",
28 "json-schema": {
29 "type": "object",
30 "properties": {
31 "minRam": {"type": "integer"},
32 "minDisk": {"type": "integer"}
33 }
34 },
35 "resources": ["flavor", "volume", "image"]
36 }
37
38 minimal_desc = {
39 "name": "list-flavors-with-detail",
40 "http-method": "GET",
41 "url": "flavors/detail",
42 }
43
44 add_prop_desc = {
45 "name": "list-flavors-with-detail",
46 "http-method": "GET",
47 "url": "flavors/detail",
48 "unknown_field": [12]
49 }
50
51 invalid_json_schema_desc = {
52 "name": "list-flavors-with-detail",
53 "http-method": "GET",
54 "url": "flavors/detail",
55 "json-schema": {"type": "NotExistingType"}
56 }
57
58 def setUp(self):
59 super(TestNegativeBasicGenerator, self).setUp()
60 self.generator = base_generator.BasicGeneratorSet()
61
62 def _assert_valid_jsonschema_call(self, jsonschema_mock, desc):
63 self.assertEqual(jsonschema_mock.call_count, 1)
64 jsonschema_mock.assert_called_with(desc, self.generator.schema)
65
66 @mock.patch('jsonschema.validate', wraps=jsonschema.validate)
67 def test_validate_schema_with_valid_input(self, jsonschema_mock):
68 self.generator.validate_schema(self.valid_desc)
69 self._assert_valid_jsonschema_call(jsonschema_mock, self.valid_desc)
70
71 @mock.patch('jsonschema.validate', wraps=jsonschema.validate)
72 def test_validate_schema_with_minimal_input(self, jsonschema_mock):
73 self.generator.validate_schema(self.minimal_desc)
74 self._assert_valid_jsonschema_call(jsonschema_mock, self.minimal_desc)
75
76 def test_validate_schema_with_invalid_input(self):
77 self.assertRaises(jsonschema.ValidationError,
78 self.generator.validate_schema, self.add_prop_desc)
79 self.assertRaises(jsonschema.SchemaError,
80 self.generator.validate_schema,
81 self.invalid_json_schema_desc)