-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_lib.py
1417 lines (1302 loc) · 65.3 KB
/
test_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2020 VMware, Inc.
# SPDX-License-Identifier: MIT
import unittest
from unittest import mock
from lib import utils
from lib import establish_connection as connection
from lib import dictionary_processing as dict_processing
from lib.dictionary_processing import ServiceType
from lib.path_processing import PathProcessing
from lib.rest_endpoint.rest_navigation_handler import RestNavigationHandler
from lib.metadata_processor import MetadataProcessor
from lib.type_handler_common import TypeHandlerCommon
class TestInputs(unittest.TestCase):
def test_get_input_params(self):
# case 1.1: SSL is secure
test_args = ['vmsgen', '-vc', 'v_url']
ssl_verify_expected = True
with mock.patch('sys.argv', test_args):
_, _, _, ssl_verify_actual, _, _, _, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(ssl_verify_expected, ssl_verify_actual)
# case 1.2: SSL is insecure
test_args = ['vmsgen', '-vc', 'v_url', '-k']
ssl_verify_expected = False
with mock.patch('sys.argv', test_args):
_, _, _, ssl_verify_actual, _, _, _, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(ssl_verify_expected, ssl_verify_actual)
# case 2.1: tag separator option (default)
test_args = ['vmsgen', '-vc', 'v_url', '-k']
tag_separator_expected = '/'
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, tag_separator_actual, _, _, _, = connection.get_input_params()
self.assertEqual(tag_separator_expected, tag_separator_actual)
# case 2.2: tag separator option
expected = '_'
test_args = ['vmsgen', '-vc', 'v_url', '-s', expected]
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, tag_separator_actual, _, _, _, = connection.get_input_params()
self.assertEqual(expected, tag_separator_actual)
# case 3.1: operation id option is FALSE
test_args = ['vmsgen', '-vc', 'v_url', '-k']
generate_op_id_expected = False
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, generate_op_id_actual, _, _, _, _, = connection.get_input_params()
self.assertEqual(generate_op_id_expected, generate_op_id_actual)
# case 3.1: operation id option is TRUE
test_args = ['vmsgen', '-vc', 'v_url', '-k', '-uo']
generate_op_id_expected = True
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, generate_op_id_actual, _, _, _, _, = connection.get_input_params()
self.assertEqual(generate_op_id_expected, generate_op_id_actual)
# case 4.1: generate metamodel option is FALSE
test_args = ['vmsgen', '-vc', 'v_url', '-k']
generate_metamodel_expected = False
with mock.patch('sys.argv', test_args):
_, _, _, _, _, generate_metamodel_actual, _, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(generate_metamodel_expected, generate_metamodel_actual)
# case 4.1: generate metamodel option is TRUE
test_args = ['vmsgen', '-vc', 'v_url', '-k', '-c']
generate_metamodel_expected = True
with mock.patch('sys.argv', test_args):
_, _, _, _, _, generate_metamodel_actual, _, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(generate_metamodel_expected, generate_metamodel_actual)
# case 5.1: swagger specification is default i.e openAPI 3.0
test_args = ['vmsgen', '-vc', 'v_url', '-k']
swagger_specification_expected = '3'
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, swagger_specification_actual, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(swagger_specification_expected, swagger_specification_actual)
# case 5.2: swagger specification is swagger 2.0
test_args = ['vmsgen', '-vc', 'v_url', '-k', '-oas' , '2']
swagger_specification_expected = '2'
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, swagger_specification_actual, _, _, _, _, _, = connection.get_input_params()
self.assertEqual(swagger_specification_expected, swagger_specification_actual)
# case 6.1: deprecated option is TRUE
test_args = ['vmsgen', '-vc', 'v_url', '-k', '--deprecate-slash-rest']
deprecated_expected = True
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, _, deprecated_actual, _, _, = connection.get_input_params()
self.assertEqual(deprecated_expected, deprecated_actual)
# case 6.2: deprecated option is FALSE
test_args = ['vmsgen', '-vc', 'v_url', '-k']
deprecated_expected = False
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, _, deprecated_actual, _, _, = connection.get_input_params()
self.assertEqual(deprecated_expected, deprecated_actual)
# case 7: fetch security
test_args = ['vmsgen', '-vc', 'v_url', '-k', '-fam']
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, _, _, fetch_security, _, = connection.get_input_params()
self.assertEqual(True, fetch_security)
# case 8: auto rest services
test_args = ['vmsgen', '-vc', 'v_url', '-k', '-ars', 'com.vmware.vcenter.ovf.import_flag',
'com.vmware.content.library.item.storage']
with mock.patch('sys.argv', test_args):
_, _, _, _, _, _, _, _, _, _, _, auto_rest_services = connection.get_input_params()
self.assertEqual(['com.vmware.vcenter.ovf.import_flag', 'com.vmware.content.library.item.storage'],
auto_rest_services)
class TestDictionaryProcessing(unittest.TestCase):
def test_get_service_url_from_service_id(self):
base_url = "https://vcip/rest"
service_id = "com.vmware.vcenter.ovf.import_flag"
service_url_expected = "https://vcip/rest/com/vmware/vcenter/ovf/import-flag"
service_url_actual = dict_processing.get_service_url_from_service_id(base_url, service_id)
self.assertEqual(service_url_expected, service_url_actual)
def test_get_service_path_from_service_url(self):
base_url = "https://vcip/rest"
# case 1 : service url starts with base url
service_url = "https://vcip/rest/com/vmware/package-mock/mock"
service_url_expected = "/com/vmware/package-mock/mock"
service_url_actual = dict_processing.get_service_path_from_service_url(service_url, base_url)
self.assertEqual(service_url_expected, service_url_actual)
# case 2 : service url does not start with base url
service_url = "https://vcip/endpoint/com/vmware/package-mock/mock"
service_url_expected = "https://vcip/endpoint/com/vmware/package-mock/mock"
service_url_actual = dict_processing.get_service_path_from_service_url(service_url, base_url)
self.assertEqual(service_url_expected, service_url_actual)
def test_get_paths_inside_metamodel(self):
auto_rest_services = ['com.vmware.vcenter.iso.image']
#case 1: https methods ('put', 'post', 'patch', 'get', 'delete') not in metadata.keys
operation_info_mock = mock.Mock()
operation_info_mock.metadata = {
'Mock-key-1.1' : {}
}
service_info_mock = mock.Mock()
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service = 'com.vmware.package-mock-1.mock.mock'
service_dict = {
'com.vmware.package-mock-1.mock.mock': service_info_mock
}
'''
Structure of key-value pair in service_dict
service_dict = {
'com.vmware.package-mock-1.mock.mock':
ServiceInfo(
operations = {
'mock-key-1': OperationInfo(metadata = {
'Mock-key-1.1' : {}
})
})
}
'''
path_list_expected = []
service_type_actual, path_list_actual = dict_processing.get_paths_inside_metamodel(service,
service_dict,
auto_rest_services)
self.assertEqual(ServiceType.SLASH_REST, service_type_actual)
self.assertEqual(path_list_expected, path_list_actual)
#case 2: https methods ('put', 'post', 'patch', 'get', 'delete') in metadata.keys
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock_string_value'
element_info_mock = mock.Mock()
element_info_mock.elements = {
'path' : element_value_mock
}
operation_info_mock.metadata = {
'put' : element_info_mock
}
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service = 'com.vmware.package-mock-1.mock.mock'
service_dict = {
'com.vmware.package-mock-1.mock.mock': service_info_mock
}
'''
Structure of key-value pair in service_dict
service_dict = {
'com.vmware.package-mock-1.mock.mock':
ServiceInfo(
operations = {
'mock-key-1': OperationInfo(metadata = {
'put' : ElementInfo(elemets = {
'path' : ElementValue(string_value = 'mock_string_value')
})
})
})
}
'''
path_list_expected = ['mock_string_value']
service_type_actual, path_list_actual = dict_processing.get_paths_inside_metamodel(service,
service_dict,
auto_rest_services)
self.assertEqual(ServiceType.SLASH_API, service_type_actual)
self.assertEqual(path_list_expected, path_list_actual)
# case 3: https methods ('put', 'post', 'patch', 'get', 'delete') in metadata.keys with deprecated applied
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock_string_value'
element_info_mock = mock.Mock()
element_info_mock.elements = {
'path': element_value_mock
}
operation_info_mock.metadata = {
'put': element_info_mock
}
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service = 'com.vmware.package-mock-1.mock.mock'
service_dict = {
'com.vmware.package-mock-1.mock.mock': service_info_mock
}
'''
Structure of key-value pair in service_dict
service_dict = {
'com.vmware.package-mock-1.mock.mock':
ServiceInfo(
operations = {
'mock-key-1': OperationInfo(metadata = {
'put' : ElementInfo(elemets = {
'path' : ElementValue(string_value = 'mock_string_value')
})
})
})
}
'''
path_list_expected = ['mock_string_value']
service_type_actual, path_list_actual = dict_processing.get_paths_inside_metamodel(service,
service_dict,
auto_rest_services,
True)
self.assertEqual(ServiceType.SLASH_API, service_type_actual)
self.assertEqual(path_list_expected, path_list_actual)
# case 3.1: https methods ('put', 'post', 'patch', 'get', 'delete') in metadata.keys with deprecated applied
# and RequestMapping apparent in the metadata
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock_string_value'
element_info_mock = mock.Mock()
element_info_mock.elements = {
'path': element_value_mock
}
operation_info_mock.metadata = {
'put': element_info_mock,
'RequestMapping': {}
}
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service = 'com.vmware.package-mock-1.mock.mock'
service_dict = {
'com.vmware.package-mock-1.mock.mock': service_info_mock
}
'''
Structure of key-value pair in service_dict
service_dict = {
'com.vmware.package-mock-1.mock.mock':
ServiceInfo(
operations = {
'mock-key-1': OperationInfo(metadata = {
'put' : ElementInfo(elemets = {
'path' : ElementValue(string_value = 'mock_string_value')
}),
'RequestMapping' : {})
})
})
}
'''
path_list_expected = ['mock_string_value']
replacement_dict_expected = {service: {"mock-key-1": ("put", "mock_string_value")}}
replacement_dict_actual = {}
service_type_actual, path_list_actual = dict_processing.get_paths_inside_metamodel(service,
service_dict,
auto_rest_services,
True,
replacement_dict_actual)
self.assertEqual(ServiceType.SLASH_REST_AND_API, service_type_actual)
self.assertEqual(path_list_expected, path_list_actual)
self.assertEqual(replacement_dict_expected, replacement_dict_actual)
# case 3.2: https methods ('put', 'post', 'patch', 'get', 'delete') in metadata.keys with deprecated applied
# and apparent in Auto Rest
rest_navigation_handler = RestNavigationHandler("")
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock_string_value'
element_params_value_mock = mock.Mock()
element_params_value_mock.list_value = ['action=filter', 'action=cancel']
element_info_mock = mock.Mock()
element_info_mock.elements = {
'path': element_value_mock,
'params': element_params_value_mock
}
operation_info_mock.metadata = {
'put': element_info_mock,
}
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service = 'com.vmware.vcenter.iso.image'
service_dict = {
service: service_info_mock
}
'''
Structure of key-value pair in service_dict
service_dict = {
'com.vmware.vcenter.iso.image':
ServiceInfo(
operations = {
'mock-key-1': OperationInfo(metadata = {
'put' : ElementInfo(elemets = {
'path' : ElementValue(string_value = 'mock_string_value'),
'params'(
list_value = 'action=cancel'
)
}))
})
})
}
'''
path_list_expected = ['mock_string_value']
replacement_dict_expected = {service: {"mock-key-1": ("put", "mock_string_value?action=filter&action=cancel")}}
replacement_dict_actual = {}
service_type_actual, path_list_actual = dict_processing.get_paths_inside_metamodel(service,
service_dict,
auto_rest_services,
True,
replacement_dict_actual)
self.assertEqual(ServiceType.SLASH_REST_AND_API, service_type_actual)
self.assertEqual(path_list_expected, path_list_actual)
self.assertEqual(replacement_dict_expected, replacement_dict_actual)
def test_add_service_urls_using_metamodel(self):
auto_rest_services = ['com.vmware.vcenter.iso.image']
# case 1: /api operation only
service_urls_map = {'https://vcip/rest/com/vmware/package/mock' : 'com.vmware.package.mock'}
rest_navigation_url = 'https://vcip/rest'
rest_navigation_handler = RestNavigationHandler(rest_navigation_url)
element_value_mock = mock.Mock()
element_value_mock.string_value = '/package/mock'
element_info_mock = mock.Mock()
element_info_mock.elements = {
'path' : element_value_mock
}
operation_info_mock = mock.Mock()
operation_info_mock.metadata = {
'put' : element_info_mock
}
service_info_mock = mock.Mock()
service_info_mock.operations = {
'mock-key-1': operation_info_mock
}
service_dict = {
'com.vmware.package.mock': service_info_mock
}
# case 1.1: --deprecate-slash-rest off
package_dict_api_expected = {'package': ['/package/mock']}
package_dict_api_actual, \
package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
self.assertEqual(package_dict_api_expected, package_dict_api_actual)
self.assertEqual({}, package_dict_actual)
# case 1.2: --deprecate-slash-rest on
package_dict_api_actual, \
package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services,
True)
self.assertEqual(package_dict_api_expected, package_dict_api_actual)
self.assertEqual({}, package_dict_actual)
# case 2: /api operation and /rest equivalent
# Service is not apparent in auto_rest_services
rest_navigation_handler.get_service_operations = mock.MagicMock(return_value={})
package_dict_api_actual, \
package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
# Service is apparent in auto_rest_services
service_urls_map = {'https://vcip/rest/com/vmware/package/mock': 'com.vmware.vcenter.iso.image'}
service_dict = {
'com.vmware.vcenter.iso.image': service_info_mock
}
# case 2.1: --deprecate-slash-rest off
package_dict_api_actual, \
package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
package_dict_expected = {'package': ['/com/vmware/package/mock']}
self.assertEqual({}, package_dict_api_actual)
self.assertEqual(package_dict_expected, package_dict_actual)
# case 2.2 --deprecate-slash-rest on
package_dict_api_actual, \
package_dict_actual, \
package_dict_deprecated_actual, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services,
True)
package_dict_expected = {'package': ['/com/vmware/package/mock']}
package_dict_api_expected = {'package': ['/package/mock']}
self.assertEqual(package_dict_api_expected, package_dict_api_actual)
self.assertEqual(package_dict_expected, package_dict_deprecated_actual)
self.assertEqual({}, package_dict_actual)
# case 3: /rest operation only
service_urls_map = {'https://vcip/rest/vmware/com/package/mock': 'com.vmware.package.mock'}
service_dict = {
'com.vmware.package.mock': service_info_mock
}
element_value_mock.string_value = 'mock_string_value'
operation_info_mock.metadata = {
'mock_element_key': element_info_mock
}
# case 3.1: --deprecate-slash-rest off
package_dict_expected = {'package': ['/vmware/com/package/mock']}
_, package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
# case 3.2: --deprecate-slash-rest on
self.assertEqual(package_dict_expected, package_dict_actual)
_, package_dict_actual, _, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services,
True)
self.assertEqual(package_dict_expected, package_dict_actual)
# case 4: /api operation and /rest equivalent (RequestMapping)
service_urls_map = {'https://vcip/rest/vmware/com/package/mock': 'com.vmware.package.mock'}
element_value_mock.string_value = '/package/mock'
operation_info_mock.metadata = {
'put': element_info_mock,
'RequestMapping': {}
}
package_dict_expected = {'package': ['/vmware/com/package/mock']}
# case 4.1: --deprecate-slash-rest off
package_dict_api_actual, \
package_dict_actual, \
package_dict_deprecated_actual, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
self.assertEqual({}, package_dict_deprecated_actual)
self.assertEqual({}, package_dict_api_actual)
self.assertEqual(package_dict_expected, package_dict_actual)
# case 4.2: --deprecate-slash-rest on
package_dict_deprecated_expected = {'package': ['/vmware/com/package/mock']}
package_dict_api_expected = {'package': ['/package/mock']}
package_dict_api_actual, \
package_dict_actual, \
package_dict_deprecated_actual, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services,
True)
self.assertEqual(package_dict_deprecated_expected, package_dict_deprecated_actual)
self.assertEqual(package_dict_api_expected, package_dict_api_actual)
self.assertEqual({}, package_dict_actual)
#case 5: checking for two operation - one with /api and /rest equivalet, other only /rest
operation_info_mock_second = mock.Mock()
operation_info_mock_second.metadata = {
'RequestMapping': {}
}
service_info_mock.operations['mock-key-2'] = operation_info_mock_second
# case 5.1: --deprecate-slash-rest off
package_dict_api_actual, \
package_dict_actual, \
package_dict_deprecated_actual, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services)
package_dict_expected = {'package': ['/vmware/com/package/mock']}
self.assertEqual(package_dict_expected, package_dict_actual)
# case 5.2: --deprecate-slash-rest on
package_dict_deprecated_expected = {'package': ['/vmware/com/package/mock']}
package_dict_api_expected = {'package': ['/package/mock']}
package_dict_api_actual, \
package_dict_actual, \
package_dict_deprecated_actual, _, = dict_processing.add_service_urls_using_metamodel(service_urls_map,
service_dict,
rest_navigation_handler,
auto_rest_services,
True)
self.assertEqual(package_dict_deprecated_expected, package_dict_deprecated_actual)
self.assertEqual(package_dict_api_expected, package_dict_api_actual)
self.assertEqual({}, package_dict_actual)
def test_objectTodict(self):
# case 1: object is of type interger
obj_mock = 1
expected_object = dict_processing.objectTodict(obj_mock)
actual_object = 1
self.assertEqual(expected_object, actual_object)
# case 2: object is of type dict
obj_mock = {
'key-1': {
'key-1.1': 'value-1.1'
}
}
expected_object = dict_processing.objectTodict(obj_mock)
self.assertEqual(expected_object, obj_mock)
# case 3: object is of type list
obj_mock = ['item-1', { 'key': 'value'}]
expected_object = dict_processing.objectTodict(obj_mock)
self.assertEqual(expected_object, obj_mock)
class TestUtils(unittest.TestCase):
def test_is_filtered(self):
'''create a mock object for service info'''
ServiceInfoMock = mock.Mock()
ServiceInfoMock.metadata = { 'TechPreview' : {} }
#case 1: non-empty metadata
bool_value_expected = False
bool_value_actual = utils.is_filtered(ServiceInfoMock.metadata)
self.assertEqual(bool_value_expected, bool_value_actual)
#case 2: metadata is empty
ServiceInfoMock.metadata = {}
bool_value_expected = False
bool_value_actual = utils.is_filtered(ServiceInfoMock.metadata)
self.assertEqual(bool_value_expected, bool_value_actual)
#case 3: metadata contains changing
ServiceInfoMock.metadata = {
'Changing' : {},
'Proposed' : {}
}
bool_value_expected = True
bool_value_actual = utils.is_filtered(ServiceInfoMock.metadata)
self.assertEqual(bool_value_expected, bool_value_actual)
# case 4: metadata does not contain 'TechPreview', 'Changing' or 'Proposed'
ServiceInfoMock.metadata = {
'mock metadata key' : {}
}
bool_value_expected = False
bool_value_actual = utils.is_filtered(ServiceInfoMock.metadata)
self.assertEqual(bool_value_expected, bool_value_actual)
def test_tags_from_service_name(self):
# case 1: tags generation from short name
expected = ['']
utils.TAG_SEPARATOR = ''
actual = utils.tags_from_service_name('three.levels.deep')
self.assertEqual(expected, actual)
# case 2: test tags generation from proper name
expected = ['levels_deep']
utils.TAG_SEPARATOR = '_'
actual = utils.tags_from_service_name('more.than.three.levels.deep')
self.assertEqual(expected, actual)
def test_is_param_path_variable(self):
#case 1: parameter name equals the path url placeholder
path_param_placeholder = 'mock'
field_info_mock = mock.Mock()
field_info_mock.name = 'mock'
bool_value_actual = True
bool_value_expected = utils.is_param_path_variable(field_info_mock, path_param_placeholder)
self.assertEqual(bool_value_expected, bool_value_actual)
#case 2: parameter name not equals the path url placeholder
# && parameter metadata does not contain path variable
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock_string_value'
element_map_mock = mock.Mock()
element_map_mock.elements = {'value': element_value_mock}
field_info_mock.name = 'mock_name'
field_info_mock.metadata = {'mock_element_key' : element_map_mock}
'''
The field info structure looks like :
FieldInfo(name = 'mock_name', metadata = {
'mock_element_key' : ElementMap(elements = {
'value' : ElementValue(string_value = 'mock_string_value')
})
})
'''
bool_value_actual = False
bool_value_expected = utils.is_param_path_variable(field_info_mock, path_param_placeholder)
self.assertEqual(bool_value_expected, bool_value_actual)
#case 3: comparing placeholder with value of parameter elements
path_param_placeholder = 'mock_string_value'
field_info_mock.metadata = {'PathVariable' : element_map_mock}
bool_value_actual = True
bool_value_expected = utils.is_param_path_variable(field_info_mock, path_param_placeholder)
self.assertEqual(bool_value_expected, bool_value_actual)
def test_extract_path_parameters(self):
#case 1: parameter is a path variable
element_value_mock = mock.Mock()
element_value_mock.string_value = 'mock'
element_map_mock = mock.Mock()
element_map_mock.elements = {'value': element_value_mock}
field_info_mock = mock.Mock()
field_info_mock.name = 'mock_name'
field_info_mock.metadata = {'PathVariable' : element_map_mock}
url = '/package/mock-1/{mock}'
params = [field_info_mock]
''''
params = [FieldInfo(name = 'mock_name', metadata = {
'PathVariable' : ElementMap(elements = {
'value' : ElementValue(string_value = 'mock')
})
})]
'''
path_params_expected = [field_info_mock]
other_params_expected = []
new_url_expected = '/package/mock-1/{mock_name}'
path_params_actual, other_params_actual, new_url_actual = utils.extract_path_parameters(params, url)
self.assertEqual(path_params_expected, path_params_actual)
self.assertEqual(other_params_expected, other_params_actual)
self.assertEqual(new_url_expected, new_url_actual)
#case 2: parameter is not a path variable
field_info_mock.metadata = {'metadata_key' : element_map_mock}
path_params_expected = []
other_params_expected = [field_info_mock]
new_url_expected = '/package/mock-1/{mock}'
path_params_actual, other_params_actual, new_url_actual = utils.extract_path_parameters(params, url)
self.assertEqual(path_params_expected, path_params_actual)
self.assertEqual(other_params_expected, other_params_actual)
self.assertEqual(new_url_expected, new_url_actual)
def test_build_path(self):
# generic mock path object creation
path_obj_expected = {
'tags': ['mock_tag'],
'method': 'get',
'path': '/com/vmware/mock_package/mock_tag',
'summary': 'mock documentation',
'responses': {'response code 1': {}, 'response code 2' : {} },
'consumes': 'mock consumes',
'produces': 'mock produces',
'operationId': 'mock id',
'parameters': [{'mock params':'params 1'}]
}
path_obj_actual = utils.build_path('com.vmware.mock_package.mock_tag', 'get', '/com/vmware/mock_package/mock_tag',
'mock documentation', [{'mock params':'params 1'}], 'mock id',
{'response code 1': {}, 'response code 2' : {} },'mock consumes', 'mock produces')
self.assertEqual(path_obj_expected, path_obj_actual)
def test_add_basic_auth(self):
# check for adding security security attribute related to '/com/vmware/cis/session'
path_obj = {
'tags': ['session'],
'method': 'post',
'path': '/com/vmware/cis/session',
'summary': 'mock documentation',
'responses': {'response code 1': {}, 'response code 2' : {} },
'consumes': 'mock consumes',
'produces': 'mock produces',
'operationId': 'mock id',
'parameters': [{'mock params':'params 1'}]
}
path_obj_actual = utils.add_basic_auth(path_obj)
path_obj_expected = {
'tags': ['session'],
'method': 'post',
'path': '/com/vmware/cis/session',
'summary': 'mock documentation',
'responses': {'response code 1': {}, 'response code 2' : {} },
'consumes': 'mock consumes',
'produces': 'mock produces',
'operationId': 'mock id',
'security': [{'basic_auth': []}],
'parameters': [{'mock params':'params 1'}]
}
self.assertEqual(path_obj_expected, path_obj_actual)
def test_extract_body_parameters(self):
# check for presence of body parameters inside params list
# parameter 1
field_info_mock_1 = mock.Mock()
element_map_mock_1 = mock.Mock()
field_info_mock_1.metadata = {'Body' : element_map_mock_1}
#parameter 2
field_info_mock_2 = mock.Mock()
element_map_mock_2 = mock.Mock()
field_info_mock_2.metadata = {'element_map_key' : element_map_mock_2}
params = [field_info_mock_1, field_info_mock_2]
body_params_actual, other_params_actual = utils.extract_body_parameters(params)
body_params_expected = [field_info_mock_1]
other_params_expected = [field_info_mock_2]
self.assertEqual(body_params_expected, body_params_actual)
self.assertEqual(other_params_expected, other_params_actual)
def test_extract_query_parameters(self):
# check for presence of query parameters inside params list
# parameter 1
field_info_mock_1 = mock.Mock()
element_map_mock_1 = mock.Mock()
field_info_mock_1.metadata = {'element_map_key' : element_map_mock_1}
#parameter 2
field_info_mock_2 = mock.Mock()
element_map_mock_2 = mock.Mock()
field_info_mock_2.metadata = {'Query' : element_map_mock_2}
params = [field_info_mock_1, field_info_mock_2]
query_params_actual, other_params_actual = utils.extract_query_parameters(params)
query_params_expected = [field_info_mock_2]
other_params_expected = [field_info_mock_1]
self.assertEqual(query_params_expected, query_params_actual)
self.assertEqual(other_params_expected, other_params_actual)
def test_metamodel_to_swagger_type_converter(self):
input_type_cases = ['date_time', 'secret', 'any_error', 'opaque',
'dynamic_structure', 'uri', 'id',
'long', 'double', 'binary', 'notValidType']
input_type_out_expected = [('string','date-time'), ('string', 'password'), ('string', None),
('object', None), ('object', None), ('string', 'uri'), ('string', None),
('integer', 'int64'), ('number', 'double'), ('string', 'binary'),
('notvalidtype', None)]
input_type_out_actual = []
for val in input_type_cases:
input_type_out_actual.append(utils.metamodel_to_swagger_type_converter(val))
self.assertEqual(input_type_out_actual, input_type_out_expected)
def test_is_type_builtin(self):
typeset_cases = ['binary', 'boolean', 'datetime',
'double', 'dynamicstructure', 'exception',
'id', 'long', 'opaque', 'secret', 'string', 'uri']
typeset_cases_out_expected = [True]*len(typeset_cases)
typeset_cases_out_actual = []
for val in typeset_cases:
typeset_cases_out_actual.append(utils.is_type_builtin(val))
self.assertEqual(typeset_cases_out_actual, typeset_cases_out_expected)
def test_add_query_param(self):
#case 1: url already contains vmw-task=true query parameter
url = '/package/mock-1/{mock}/mock?vmw-task=true'
param = 'vmw-task=true'
url_actual = utils.add_query_param(url,param)
url_expected = '/package/mock-1/{mock}/mock?vmw-task=true'
self.assertEqual(url_expected, url_actual)
#case 2: url contains a query parameter, so append vmw-task=true using '&'
url = '/package/mock-1/{mock}/mock?key=value'
param = 'vmw-task=true'
url_actual = utils.add_query_param(url,param)
url_expected = '/package/mock-1/{mock}/mock?key=value&vmw-task=true'
self.assertEqual(url_expected, url_actual)
#case 3: query index not find
url = '/package/mock-1/{mock}/mock'
param = 'vmw-task=true'
url_actual = utils.add_query_param(url,param)
url_expected = '/package/mock-1/{mock}/mock?vmw-task=true'
self.assertEqual(url_expected, url_actual)
def test_create_req_body_from_params_list(self):
# case 1: Parameters array is empty
path_obj = {
'parameters':[]
}
path_obj_expected = path_obj
utils.create_req_body_from_params_list(path_obj)
self.assertEqual(path_obj, path_obj_expected)
# case 2: Parameters array contains dictionary with key as '$ref' and
# value starting with '#/components/requestBodies' (only for put, post and patch operations)
path_obj = {
'parameters':[
{'mock-param-1':{}},
{'$ref':'#/components/requestBodies/mock-path'},
{'mock-param-2':{}},
]
}
utils.create_req_body_from_params_list(path_obj)
path_obj_expected = {
'parameters': [
{'mock-param-1': {}},
{'mock-param-2': {}}
],
'requestBody': {'$ref': '#/components/requestBodies/mock-path'}
}
self.assertEqual(path_obj, path_obj_expected)
# case 3: No dictionary with key as '$ref' inside parameters array
path_obj = {
'parameters':[
{'mock-param-1':{}},
{'mock-param-2':{}},
]
}
utils.create_req_body_from_params_list(path_obj)
path_obj_expected = {
'parameters':[
{'mock-param-1':{}},
{'mock-param-2':{}},
]
}
self.assertEqual(path_obj, path_obj_expected)
class TestPathProcessing(unittest.TestCase):
path_process = PathProcessing()
def test_remove_com_vmware_from_dict(self):
'''
case 1 (path dict processing): removing com.vmware. from every key-value pair
which contains it and $ from every ref value of definition.
case 1.1: remove com.vmware from value when key is either of ('$ref', 'summary', 'description')
case 1.2: removing $ sign from ref value's, example: { "$ref" : "#/definitions/vcenter.vcha.cluster.failover$task_result" }
case 1.3: removing attribute required when it is with '$ref'
'''
path_dict = {
'com/vmware/mock/path':{
'get': {
'tags': ['mock'],
'summary': 'com.vmware.mock', # 1.1: example 1
'parameters': [
{
"in" : "path",
"description" : "com.vmware.somemockparam description" # 1.1: example 2
},
{
"$ref": '#/parameters/com.vmware.somemockparam' # 1.1 : example 3
}
],
'responses': {
'mock 200': {
'description': 'description about com.vmware.mock_response',
'schema': {
'$ref': '#/definitions/com.vmware.mock_response$result', # 1.2
'required': False # 1.3
}
}
},
'operationId': 'get'
}
}
}
path_dict_expected = {
'com/vmware/mock/path': {
'get': {
'tags': ['mock'],
'summary': 'mock',
'parameters': [
{
'in': 'path',
'description': 'somemockparam description'
},
{
'$ref': '#/parameters/somemockparam'
}
],
'responses': {
'mock 200': {
'description': 'description about mock_response',
'schema': {
'$ref': '#/definitions/mock_response_result'
}
}
},
'operationId': 'get'
}
}
}
self.path_process.remove_com_vmware_from_dict(path_dict)
self.assertEqual(path_dict, path_dict_expected)
'''
case 2 (type dict processing)
case 2.1 : remove com.vmware and replace '$' with '_' from key's
case 2.2 : removing attribute required when it is with '$ref'
'''
type_dict = {
'com.vmware.mock.mock_check$list' : { # 2.1 : example 1
'type': 'object',
'properties': {
'value': {
'description': ' value desc.',
'type': 'array',
'items': {
'$ref': '#/definitions/com.vmware.mock_check_item', # 2.1 : example 2
'required': True # 2.2
}
},
'data': {
'description': ' data desc ',
'type': 'object'
},
'required': ['value']
}
}
}
type_dict_expected = {
'mock.mock_check_list': {
'type': 'object',
'properties': {
'value': {
'description':
' value desc.',
'type': 'array',
'items': {
'$ref': '#/definitions/mock_check_item'
}
},
'data': {
'description': ' data desc ',
'type': 'object'
},
'required': ['value']
}
}
}
self.path_process.remove_com_vmware_from_dict(type_dict)
self.assertEqual(type_dict, type_dict_expected)
def test_create_camelized_op_id(self):
# Note: create_unique_op_ids(path_dict) test cases are handled in test cases provided for create_camelized_op_id
# case 1: without query parameter: removes com/vmware/ and replaces '/' & '-' with '_'
# also converts the first letter of all the words except the first one from lower to upper before concatenating to form unique op id
path = "com/vmware/mock-path"
http_method = "post"
operations_dict = {
'operationId' : 'post'
}
op_id_expected = 'postMockPath'
op_id_actual = self.path_process.create_camelized_op_id(path, http_method, operations_dict)
self.assertEqual(op_id_actual, op_id_expected)
# case 2 : similar to case 1 with added query param to path
path = "com/vmware/mock-path?action=value"
http_method = "post"
operations_dict = {
'operationId' : 'mock-operation-id'
}
op_id_expected = 'mockOperationIdMockPath'
op_id_actual = self.path_process.create_camelized_op_id(path, http_method, operations_dict)
self.assertEqual(op_id_actual, op_id_expected)
# case 3 : similar to case 1 with path variable
path = "com/vmware/mock-path/{mock}/test"
http_method = "post"
operations_dict = {
'operationId' : 'post'
}
op_id_expected = 'postMockPathTest'
op_id_actual = self.path_process.create_camelized_op_id(path, http_method, operations_dict)
self.assertEqual(op_id_actual, op_id_expected)
def test_create_unique_op_ids(self):
# update path dictionary with unique operation id
path_dict = {
'com/vmware/mock-path':{
'post': {
'operationId' : 'post'
}
}
}
self.path_process.create_unique_op_ids(path_dict)
path_dict_expected = {
'com/vmware/mock-path':{
'post': {
'operationId' : 'postMockPath'
}