summaryrefslogtreecommitdiffstats
path: root/tests/test_customer_resource.py
blob: 23b51ef0fbeef140f64913ef4ee46d31efef63e1 (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
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")