aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_esr_resource.py
blob: 0339d2b363d10809fbb1c61bc069a93f25aece70 (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
"""
   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 collections import namedtuple
from unittest.mock import MagicMock, patch, PropertyMock

from onap_data_provider.resources.esr_system_info_resource import (
    CloudRegion,
    EsrSystemInfoResource,
)


ESR_RESOURCE_DATA = {
    "esr-system-info-id": "Test ID",
    "user-name": "Test name",
    "password": "testpass",
    "system-type": "test type",
    "service-url": "test url",
    "cloud-domain": "test cloud domain",
}


EsrSystemInfoNamedtuple = namedtuple("EsrSystemInfo", ["esr_system_info_id"])


@patch(
    "onap_data_provider.resources.esr_system_info_resource.CloudRegion.esr_system_infos",
    new_callable=PropertyMock,
)
def test_esr_system_info_resource_esr_system_info(mock_cloud_region_esr_system_infos):
    cloud_region = CloudRegion(
        cloud_owner="test",
        cloud_region_id="test",
        orchestration_disabled=True,
        in_maint=True,
    )
    esr_resource = EsrSystemInfoResource(ESR_RESOURCE_DATA, cloud_region)
    mock_cloud_region_esr_system_infos.return_value = iter([])
    assert esr_resource.esr_system_info is None

    mock_cloud_region_esr_system_infos.return_value = iter(
        [EsrSystemInfoNamedtuple("Test ID")]
    )
    assert esr_resource.esr_system_info is not None


@patch(
    "onap_data_provider.resources.esr_system_info_resource.EsrSystemInfoResource.esr_system_info",
    new_callable=PropertyMock,
)
def test_esr_system_info_resource_exists(mock_esr_system_info):
    mock_esr_system_info.return_value = None
    cloud_region_mock = MagicMock()
    esr_resource = EsrSystemInfoResource(ESR_RESOURCE_DATA, cloud_region_mock)
    assert esr_resource.exists is False

    mock_esr_system_info.return_value = 1
    assert esr_resource.exists is True


@patch(
    "onap_data_provider.resources.esr_system_info_resource.EsrSystemInfoResource.exists",
    new_callable=PropertyMock,
)
def test_esr_system_info_resource_create(mock_exists):

    cloud_region_mock = MagicMock()
    esr_resource = EsrSystemInfoResource(ESR_RESOURCE_DATA, cloud_region_mock)

    mock_exists.return_value = True
    esr_resource.create()
    cloud_region_mock.add_esr_system_info.assert_not_called()

    mock_exists.return_value = False
    esr_resource.create()
    cloud_region_mock.add_esr_system_info.assert_called_once_with(
        esr_system_info_id="Test ID",
        user_name="Test name",
        password="testpass",
        system_type="test type",
        system_status="active",
        service_url="test url",
        cloud_domain="test cloud domain",
        default_tenant=None,
    )