Restore Apex tests
[it/dep.git] / smo-install / test / pythonsdk / unit-tests / test_policy.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: Apache-2.0
3 """Test Policy module."""
4
5 from unittest import mock
6 from oransdk.policy.policy import OranPolicy, PolicyType
7
8 HEADER = {"Accept": "application/json", "Content-Type": "application/json"}
9 API_URL = "https://localhost:6969"
10 PAP_URL = "https://localhost:6969"
11 BASIC_AUTH = {'username': 'dcae@dcae.onap.org', 'password': 'demo123456!'}
12
13 def test_initialization():
14     """Class initialization test."""
15     policy = OranPolicy()
16     assert isinstance(policy, OranPolicy)
17
18
19 @mock.patch.object(OranPolicy, 'send_message_json')
20 def test_get_components_status(mock_send_message_json):
21     """Test Policy's class method."""
22     OranPolicy.get_components_status(BASIC_AUTH)
23     mock_send_message_json.assert_called_once_with('GET',
24                                                    'Get status of Policy component',
25                                                    (f"{PAP_URL}/policy/pap/v1/components/healthcheck"),
26                                                    basic_auth=BASIC_AUTH)
27
28 @mock.patch.object(OranPolicy, 'send_message_json')
29 def test_get_policy_status(mock_send_message_json):
30     """Test Policy's class method."""
31     OranPolicy.get_policy_status(BASIC_AUTH)
32     mock_send_message_json.assert_called_once_with('GET',
33                                                    'Get status of all the policies',
34                                                    (f"{PAP_URL}/policy/pap/v1/policies/status"),
35                                                    basic_auth=BASIC_AUTH)
36
37
38 @mock.patch.object(OranPolicy, 'send_message')
39 def test_get_policy(mock_send_message):
40     """Test Policy's class method."""
41     OranPolicy.get_policy(PolicyType(type="test_type", version="type_version"),
42                           "policy_name", "policy_version", BASIC_AUTH)
43     mock_send_message.assert_called_once_with('GET',
44                                                    'Get the policy',
45                                                    (f"{API_URL}/policy/api/v1/policytypes/test_type/versions/"\
46                                                    + "type_version/policies/policy_name/versions/policy_version"),
47                                                    basic_auth=BASIC_AUTH)
48
49
50 @mock.patch.object(OranPolicy, 'send_message')
51 def test_create_policy(mock_send_message):
52     """Test Policy's class method."""
53     OranPolicy.create_policy(PolicyType(type="test_type", version="type_version"), {}, BASIC_AUTH)
54     mock_send_message.assert_called_once_with('POST',
55                                               'Create Policy',
56                                               (f"{API_URL}/policy/api/v1/policytypes/test_type/"\
57                                               + "versions/type_version/policies"),
58                                               data={},
59                                               headers=HEADER,
60                                               basic_auth=BASIC_AUTH)
61
62 @mock.patch.object(OranPolicy, 'send_message')
63 def test_deploy_policy(mock_send_message):
64     """Test Policy's class method."""
65     OranPolicy.deploy_policy({}, BASIC_AUTH)
66     mock_send_message.assert_called_once_with('POST',
67                                               'Deploy Policy',
68                                               (f"{API_URL}/policy/pap/v1/pdps/policies"),
69                                               data={},
70                                               headers=HEADER,
71                                               basic_auth=BASIC_AUTH)
72
73 @mock.patch.object(OranPolicy, 'send_message')
74 def test_undeploy_policy(mock_send_message):
75     """Test Policy's class method."""
76     OranPolicy.undeploy_policy("policy_id","1.0.0", BASIC_AUTH)
77     mock_send_message.assert_called_once_with('DELETE',
78                                               'Undeploy Policy',
79                                               (f"{PAP_URL}/policy/pap/v1/pdps/policies/policy_id/versions/1.0.0"),
80                                               headers=HEADER,
81                                               basic_auth=BASIC_AUTH)
82
83 @mock.patch.object(OranPolicy, 'send_message')
84 def test_delete_policy(mock_send_message):
85     """Test Policy's class method."""
86     OranPolicy.delete_policy(PolicyType(type="test_type", version="type_version"), "policy_id","1.0.0", BASIC_AUTH)
87     mock_send_message.assert_called_once_with('DELETE',
88                                               'Delete Policy',
89                                               (f"{API_URL}/policy/api/v1/policytypes/test_type/versions/type_version/policies/policy_id/versions/1.0.0"),
90                                               headers=HEADER,
91                                               basic_auth=BASIC_AUTH)