Update project maturity status
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / utils / test_exceptions.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import jsonpickle
17
18 from aria.utils import exceptions
19
20 _ARG1 = 'arg-1'
21 _ARG2 = 'arg-2'
22
23
24 class TestWrapIfNeeded(object):
25
26     def test_no_wrapping_required1(self):
27         e = JsonPickleableException1(_ARG1, _ARG2)
28         assert exceptions.wrap_if_needed(e) is e
29
30     def test_no_wrapping_required2(self):
31         e = JsonPickleableException1(arg1=_ARG1, arg2=_ARG2)
32         assert exceptions.wrap_if_needed(e) is e
33
34     def test_no_wrapping_required3(self):
35         e = JsonPickleableException2(arg1=_ARG1, arg2=_ARG2)
36         assert exceptions.wrap_if_needed(e) is e
37
38     def test_wrapping_required1(self):
39         e = NonJsonPickleableException(_ARG1, _ARG2)
40         wrapped_e = exceptions.wrap_if_needed(e)
41         wrapped_e = jsonpickle.loads(jsonpickle.dumps(wrapped_e))
42         assert isinstance(wrapped_e, exceptions._WrappedException)
43         assert wrapped_e.exception_type == type(e).__name__
44         assert wrapped_e.exception_str == str(e)
45
46     def test_wrapping_required2(self):
47         e = NonJsonPickleableException(arg1=_ARG1, arg2=_ARG2)
48         wrapped_e = exceptions.wrap_if_needed(e)
49         wrapped_e = jsonpickle.loads(jsonpickle.dumps(wrapped_e))
50         assert isinstance(wrapped_e, exceptions._WrappedException)
51         assert wrapped_e.exception_type == type(e).__name__
52         assert wrapped_e.exception_str == str(e)
53
54
55 class JsonPickleableException1(Exception):
56     def __init__(self, arg1, arg2):
57         super(JsonPickleableException1, self).__init__(arg1, arg2)
58         self.arg1 = arg1
59         self.arg2 = arg2
60
61
62 class JsonPickleableException2(Exception):
63     def __init__(self, arg1=None, arg2=None):
64         super(JsonPickleableException2, self).__init__()
65         self.arg1 = arg1
66         self.arg2 = arg2
67
68
69 class NonJsonPickleableException(Exception):
70     def __init__(self, arg1, arg2):
71         super(NonJsonPickleableException, self).__init__()
72         self.arg1 = arg1
73         self.arg2 = arg2