resp.content->resp.text in Should Contain
[integration/test.git] / csit / libraries / backuprestore / backuprestoretest.py
1 import unittest
2 import sys
3 import JsonDiffTool
4
5 """
6 Unit tests for the Json Diff Tool library
7 Updated: 2017-04-24
8 """
9
10 __author__ = "Diego Granados"
11 __copyright__ = "Copyright(c) 2017, Ericsson."
12 __license__ = "New-style BSD"
13 __email__ = "diego.jesus.granados.lopez@ericsson.com"
14
15
16 class PathConversionTest(unittest.TestCase):
17     """
18     Conversions from jsonpath paths to jsonpatch patches used to remove the element pointed by the path
19     """
20
21     def testArrayElementConversion(self):
22         self.assertEquals('[{"op":"remove","path":"/ietf-yang-library:modules-state/module/56"}]',
23                           JsonDiffTool.from_path_to_jsonpatch('/ietf-yang-library:modules-state/module/56'),
24                           "Array element conversion failed!")
25
26     def testMapValueElementConversion(self):
27         self.assertEquals('[{"op":"remove","path":"/ietf-yang-library:modules-state/module/blablah"}]',
28                           JsonDiffTool.from_path_to_jsonpatch('/ietf-yang-library:modules-state/module/blablah'),
29                           "Array element conversion failed!")
30
31
32 class JsonDiffToolTest(unittest.TestCase):
33     """
34     Tests for the tool itself, including both command-line and RIDE keyword invokation style
35     """
36
37     def testSimpleDifferenceCountingWithoutFiltering(self):
38         """
39         Identical documents
40         """
41         self.assertEquals(0,
42                           JsonDiffTool.Json_Diff_Check_Keyword('testinput/arrayTwoNames.json',
43                                                                'testinput/arrayTwoNamesCopy.json',
44                                                                '',
45                                                                ''),
46                           'failed! (expected 0 differences)')
47
48     def testEqualFilesWithScrambledArrayOrder(self):
49         """
50         This is moving an array element from one position to other. RFC 6902 describes this as "moving
51         a value", but this jsonpatch implementation constructs a patch using remove + add. Acceptable though
52         """
53         self.assertEquals(2,
54                           JsonDiffTool.Json_Diff_Check_Keyword('testinput/arrayTwoNames.json',
55                                                                'testinput/arrayTwoNamesReversed.json',
56                                                                '',
57                                                                ''),
58                           'failed! (expected 2 differences)')
59
60     def testEqualFilesWithChangedAttributeOrder(self):
61         """
62         Attributes in different order. It's not a difference
63         """
64         self.assertEquals(0,
65                           JsonDiffTool.Json_Diff_Check_Keyword('testinput/setTwoNames.json',
66                                                                'testinput/setTwoNamesReversed.json',
67                                                                '',
68                                                                ''),
69                           'failed! (expected 0 differences)')
70
71     def testSimpleDifferenceSecondFileWithExtraAttrib(self):
72         self.assertEquals(1,
73                           JsonDiffTool.Json_Diff_Check_Keyword('testinput/setTwoNames.json',
74                                                                'testinput/setTwoNamesExtraAttrib.json',
75                                                                '',
76                                                                ''),
77                           'failed! (expected 1 differences)')
78
79     def testSimpleDifferenceCountingWithoutFiltering(self):
80         """
81         Example coming from a true daexim export. No prefilters used
82         """
83         input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
84                       '-f', 'testinput/mainTestCase/odl_backup_operational_after.json']
85         sys.argv[1:] = input_argv
86         self.assertEquals(16,
87                           JsonDiffTool.Json_Diff_Check(),
88                           "main failed! expected 16 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
89
90     def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
91         """
92         Using a prefilter for the initial file The prefilter contains one expression only
93         """
94         input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
95                       '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
96                       '-ipf', 'testinput/mainTestCase/json_prefilter.conf', '-v']
97         sys.argv[1:] = input_argv
98         self.assertEquals(15,
99                           JsonDiffTool.Json_Diff_Check(),
100                           "main failed! expected 15 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
101
102     def testSimpleDifferenceCountingUsingMatchingBeforeFilterMatchingTwoEntries(self):
103         """
104         Using a prefilter for the initial file The prefilter contains two expressions
105         """
106         input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
107                       '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
108                       '-ipf', 'testinput/mainTestCase/json_prefilter_two_matches.conf', '-v']
109         sys.argv[1:] = input_argv
110         self.assertEquals(14,
111                           JsonDiffTool.Json_Diff_Check(),
112                           "main failed! expected 14 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
113
114     def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
115         """
116         Using a prefilter for both initial and final files
117         """
118         input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
119                       '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
120                       '-ipf', 'testinput/mainTestCase/json_prefilter.conf',
121                       '-fpf', 'testinput/mainTestCase/json_postfilter.conf',
122                       '-v']
123         sys.argv[1:] = input_argv
124         self.assertEquals(16,
125                           JsonDiffTool.Json_Diff_Check(),
126                           "main failed! expected 16 differences, result was: " + str(JsonDiffTool.Json_Diff_Check()))
127
128     def testUsingANonExistingFile(self):
129         """
130         The second file does not exist. Exception expected
131         """
132         self.assertRaises(IOError,
133                           JsonDiffTool.Json_Diff_Check_Keyword,
134                           'testinput/arrayTwoNames.json',
135                           'testinput/thisFileDoesNotExist.json',
136                           '',
137                           '')
138
139     def testNotPassingAMandatoryParameter(self):
140         """
141         Both initial and final json files are mandatory
142         """
143         input_argv = ['-f', 'testinput/mainTestCase/odl_backup_operational_after.json']
144         # parser = JsonDiffTool.parseArgs(input_argv)
145
146         with self.assertRaises(SystemExit) as cm:
147             JsonDiffTool.parse_args(input_argv)
148
149         # 2 for missing argument
150         self.assertEqual(cm.exception.code, 2)
151
152     def testUsingNotMatchingFilterExpressions(self):
153         """
154         Using prefilter files whose expressions match nothing
155         """
156         input_argv = ['-i', 'testinput/mainTestCase/odl_backup_operational_before.json',
157                       '-f', 'testinput/mainTestCase/odl_backup_operational_after.json',
158                       '-ipf', 'testinput/mainTestCase/json_prefilter_zero_matches.conf',
159                       '-fpf', 'testinput/mainTestCase/json_prefilter_zero_matches.conf',
160                       '-v']
161         sys.argv[1:] = input_argv
162         self.assertEquals(16,
163                           JsonDiffTool.Json_Diff_Check(),
164                           "main failed! expected 16 differences, result was: " + str(
165                               JsonDiffTool.Json_Diff_Check()))
166
167
168 if __name__ == "__main__":
169     unittest.main()