summaryrefslogtreecommitdiffstats
path: root/tests/test_customer_resource.py
blob: c82b01650fb5758782ab19425d70dac99a05a725 (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
"""
   Copyright 2022 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.mock import MagicMock, patch, PropertyMock

from onap_data_provider.resources.customer_resource import CustomerResource

from onapsdk.exceptions import ResourceNotFound

CUSTOMER_DATA = {
    "global-customer-id": "test_id",
    "subscriber-name": "test_name",
    "subscriber-type": "Customer",
    "service-subscriptions": [{"service-type": "test_voip"}],
}

SERVICE_SUBSCRIPTION_WITH_TENANTS_DATA = {
    "service-type": "test-service-subscription",
    "tenants": [
        {
            "tenant-id": "1234",
            "cloud-owner": "test-cloud-owner",
            "cloud-region-id": "test-cloud-region",
        }
    ],
}


@patch(
    "onap_data_provider.resources.customer_resource.Customer.get_by_global_customer_id"
)
def test_customer_resource_customer(mock_customer_get_by_global_customer_id):
    mock_customer_get_by_global_customer_id.side_effect = ResourceNotFound
    customer_resource = CustomerResource(CUSTOMER_DATA)
    assert customer_resource.customer is None
    mock_customer_get_by_global_customer_id.side_effect = None
    mock_customer_get_by_global_customer_id.return_value = 1
    assert customer_resource.customer == 1


@patch(
    "onap_data_provider.resources.customer_resource.CustomerResource.customer",
    new_callable=PropertyMock,
)
def test_customer_exists(mock_customer):
    mock_customer.return_value = None
    customer_resource = CustomerResource(CUSTOMER_DATA)
    assert customer_resource.exists is False
    mock_customer.return_value = 1  # Anything but not None
    assert customer_resource.exists is True


@patch(
    "onap_data_provider.resources.customer_resource.CustomerResource.exists",
    new_callable=PropertyMock,
)
@patch("onap_data_provider.resources.customer_resource.Customer.create")
def test_customer_create(mock_customer_create, mock_exists):
    customer_resource = CustomerResource(CUSTOMER_DATA)
    assert customer_resource.data == CUSTOMER_DATA
    mock_exists.return_value = False
    customer_resource.create()
    assert mock_customer_create.called_once_with(
        global_customer_id="test_id",
        subscriber_name="test_name",
        subscriber_type="Customer",
    )


@patch(
    "onap_data_provider.resources.customer_resource.CustomerResource.ServiceSubscriptionResource.service_subscription",
    new_callable=PropertyMock,
)
@patch("onap_data_provider.resources.customer_resource.CloudRegion")
def test_service_subscription_with_tenants(
    mock_cloud_region, _
):
    cloud_region_mock = MagicMock()
    mock_cloud_region.get_by_id.return_value = cloud_region_mock
    service_subscription_resource = CustomerResource.ServiceSubscriptionResource(
        SERVICE_SUBSCRIPTION_WITH_TENANTS_DATA, MagicMock()
    )
    service_subscription_resource.create()
    mock_cloud_region.get_by_id.assert_called_once_with(
        "test-cloud-owner", "test-cloud-region"
    )
    cloud_region_mock.get_tenant.assert_called_once_with("1234")