aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_aai_service_subscription.py
blob: 3c7eb91b4597af9a2ad9b999f4f8e331ba77c09f (plain)
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
#   Copyright 2022 Orange, Deutsche Telekom AG
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from unittest import mock

from onapsdk.aai.business import Customer, ServiceSubscription, ServiceInstance
from onapsdk.aai.cloud_infrastructure import CloudRegion


SERVICE_INSTANCES = {
    "service-instance":[
        {
            "service-instance-id":"5410bf79-2aa3-450e-a324-ec5630dc18cf",
            "service-instance-name":"test",
            "environment-context":"General_Revenue-Bearing",
            "workload-context":"Production",
            "model-invariant-id":"2a51a89b-6f94-4417-8831-c468fb30ed02",
            "model-version-id":"92a82807-b483-4579-86b1-c79b1286aab4",
            "resource-version":"1589457727708",
            "orchestration-status":"Active",
            "relationship-list":{
                "relationship":[
                    {
                        "related-to":"owning-entity",
                        "relationship-label":"org.onap.relationships.inventory.BelongsTo",
                        "related-link":"/aai/v16/business/owning-entities/owning-entity/ff6c945f-89ab-4f14-bafd-0cdd6eac791a",
                        "relationship-data":[
                            {
                                "relationship-key":"owning-entity.owning-entity-id",
                                "relationship-value":"ff6c945f-89ab-4f14-bafd-0cdd6eac791a"
                            }
                        ]
                    },
                    {
                        "related-to":"project",
                        "relationship-label":"org.onap.relationships.inventory.Uses",
                        "related-link":"/aai/v16/business/projects/project/python_onap_sdk_project",
                        "relationship-data":[
                            {
                                "relationship-key":"project.project-name",
                                "relationship-value":"python_onap_sdk_project"
                            }
                        ]
                    }
                ]
            }
        }
    ]
}


MULTIPLE_CLOUD_REGIONS_AND_TENATS_RELATIONSHIP = {
    "relationship":[
        {
            "related-to":"tenant",
            "relationship-label":"org.onap.relationships.inventory.Uses",
            "related-link":"/aai/v19/cloud-infrastructure/cloud-regions/cloud-region/DT/RegionOne/tenants/tenant/8fa33ca96caa4172aeeeefd1dbf5c715",
            "relationship-data":[
                {
                    "relationship-key":"cloud-region.cloud-owner",
                    "relationship-value":"DT"
                },
                {
                    "relationship-key":"cloud-region.cloud-region-id",
                    "relationship-value":"RegionOne"
                },
                {
                    "relationship-key":"tenant.tenant-id",
                    "relationship-value":"8fa33ca96caa4172aeeeefd1dbf5c715"
                }
            ],
            "related-to-property":[
                {
                    "property-key":"tenant.tenant-name",
                    "property-value":"ci-onap-master-vnfs"
                }
            ]
        },
        {
            "related-to":"tenant",
            "relationship-label":"org.onap.relationships.inventory.Uses",
            "related-link":"/aai/v19/cloud-infrastructure/cloud-regions/cloud-region/test_cloud_owner/test_cloud_region_id/tenants/tenant/1234",
            "relationship-data":[
                {
                    "relationship-key":"cloud-region.cloud-owner",
                    "relationship-value":"test_cloud_owner"
                },
                {
                    "relationship-key":"cloud-region.cloud-region-id",
                    "relationship-value":"test_cloud_region_id"
                },
                {
                    "relationship-key":"tenant.tenant-id",
                    "relationship-value":"1234"
                }
            ],
            "related-to-property":[
                {
                    "property-key":"tenant.tenant-name",
                    "property-value":"test_tenant"
                }
            ]
        }
    ]
}


COUNT = {
    "results":[
        {
            "service-subscription":1
        }
    ]
}


@mock.patch.object(ServiceSubscription, "send_message_json")
def test_get_service_instance_by_filter_parameter(mock_send_message_json):
    """Test Service Subscription get_service_instance_by_filter_parameter method"""
    customer = Customer("generic", "generic", "INFRA")
    service_subscription = ServiceSubscription(customer=customer,
                                               service_type="test_service_type",
                                               resource_version="test_resource_version")
    mock_send_message_json.return_value = SERVICE_INSTANCES
    service_instance = service_subscription._get_service_instance_by_filter_parameter(filter_parameter_name="service-instance-id", filter_parameter_value="5410bf79-2aa3-450e-a324-ec5630dc18cf")
    assert service_instance.instance_name == "test"
    assert service_instance.instance_id == "5410bf79-2aa3-450e-a324-ec5630dc18cf"


@mock.patch.object(ServiceSubscription, "_get_service_instance_by_filter_parameter")
def test_get_service_instance_by_id(mock_get):
    """Test Service Subscription get_service_instance_by_id method"""
    service_subscription = ServiceSubscription(customer=None,
                                               service_type="test_service_type",
                                               resource_version="test_resource_version")
    mock_get.return_value = ServiceInstance(service_subscription="ServiceSubscription",
                                            instance_id="5410bf79-2aa3-450e-a324-ec5630dc18cf")
    service_instance = service_subscription.get_service_instance_by_id(service_instance_id="5410bf79-2aa3-450e-a324-ec5630dc18cf")
    assert service_instance.instance_id == "5410bf79-2aa3-450e-a324-ec5630dc18cf"


@mock.patch.object(ServiceSubscription, "_get_service_instance_by_filter_parameter")
def test_get_service_instance_by_name(mock_get):
    """Test Service Subscription get_service_instance_by_name method"""
    service_subscription = ServiceSubscription(customer=None,
                                               service_type="test_service_type",
                                               resource_version="test_resource_version")
    mock_get.return_value = ServiceInstance(service_subscription="ServiceSubscription",
                                            instance_id="5410bf79-2aa3-450e-a324-ec5630dc18cf",
                                            instance_name="test")
    service_instance = service_subscription.get_service_instance_by_name(service_instance_name="test")
    assert service_instance.instance_name == "test"


@mock.patch.object(ServiceSubscription, "send_message_json")
@mock.patch.object(CloudRegion, "get_by_id")
def test_cloud_regions(mock_cloud_region_get_by_id, mock_send_message_json):
    """Test service subscription `cloud_regions` property"""
    service_subscription = ServiceSubscription(customer=mock.MagicMock(),
                                               service_type="test_service_type",
                                               resource_version="test_resource_version")
    mock_send_message_json.return_value = MULTIPLE_CLOUD_REGIONS_AND_TENATS_RELATIONSHIP
    assert len(list(service_subscription.cloud_regions)) == 2
    assert len(mock_cloud_region_get_by_id.mock_calls) == 2


@mock.patch.object(ServiceSubscription, "send_message_json")
@mock.patch.object(CloudRegion, "get_by_id")
@mock.patch.object(CloudRegion, "get_tenant")
def test_tenants(mock_cloud_region_get_tenant, mock_cloud_region_get_by_id, mock_send_message_json):
    """Test service subscription `tenants` property"""
    service_subscription = ServiceSubscription(customer=mock.MagicMock(),
                                               service_type="test_service_type",
                                               resource_version="test_resource_version")
    mock_send_message_json.return_value = MULTIPLE_CLOUD_REGIONS_AND_TENATS_RELATIONSHIP
    assert len(list(service_subscription.tenants)) == 2

@mock.patch.object(ServiceSubscription, "send_message_json")
def test_service_subscription_count(mock_send_message_json):
    mock_send_message_json.return_value = COUNT
    assert ServiceSubscription.count(customer=mock.MagicMock()) == 1