aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_sdc_category_management.py
blob: 59ca5f5897cf5253ee2bea05ab36169e240bb431 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Test SdcElement module."""
#   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

import pytest
from onapsdk.exceptions import APIError, ResourceNotFound

from onapsdk.sdc.category_management import ResourceCategory, ServiceCategory


CATEGORIES = {
    "categories": {
        'resourceCategories': [
            {
                'name': 'Network L4+',
                'normalizedName': 'network l4+',
                'uniqueId': 'resourceNewCategory.network l4+',
                'icons': None,
                'subcategories': [
                    {
                        'name': 'Common Network Resources',
                        'normalizedName': 'common network resources',
                        'uniqueId': 'resourceNewCategory.network l4+.common network resources',
                        'icons': ['network'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    }
                ],
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Network L2-3',
                'normalizedName': 'network l2-3',
                'uniqueId': 'resourceNewCategory.network l2-3',
                'icons': None,
                'subcategories': [
                    {
                        'name': 'Router',
                        'normalizedName': 'router',
                        'uniqueId': 'resourceNewCategory.network l2-3.router',
                        'icons': ['router', 'vRouter'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    },
                    {
                        'name': 'LAN Connectors',
                        'normalizedName': 'lan connectors',
                        'uniqueId': 'resourceNewCategory.network l2-3.lan connectors',
                        'icons': ['network', 'connector', 'port'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    },
                    {
                        'name': 'Infrastructure',
                        'normalizedName': 'infrastructure',
                        'uniqueId': 'resourceNewCategory.network l2-3.infrastructure',
                        'icons': ['ucpe'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    },
                    {
                        'name': 'Gateway',
                        'normalizedName': 'gateway',
                        'uniqueId': 'resourceNewCategory.network l2-3.gateway',
                        'icons': ['gateway'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    },
                    {
                        'name': 'WAN Connectors',
                        'normalizedName': 'wan connectors',
                        'uniqueId': 'resourceNewCategory.network l2-3.wan connectors',
                        'icons': ['network', 'connector', 'port'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    }
                ],
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Network Connectivity',
                'normalizedName': 'network connectivity',
                'uniqueId': 'resourceNewCategory.network connectivity',
                'icons': None,
                'subcategories': [
                    {
                        'name': 'Connection Points',
                        'normalizedName': 'connection points',
                        'uniqueId': 'resourceNewCategory.network connectivity.connection points',
                        'icons': ['cp'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    },
                    {
                        'name': 'Virtual Links',
                        'normalizedName': 'virtual links',
                        'uniqueId': 'resourceNewCategory.network connectivity.virtual links',
                        'icons': ['vl'],
                        'groupings': None,
                        'version': None,
                        'ownerId': None,
                        'empty': False,
                        'type': None
                    }
                ],
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Configuration',
                'normalizedName': 'configuration',
                'uniqueId': 'resourceNewCategory.configuration',
                'icons': None,
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
        ],
        'serviceCategories': [
            {
                'name': 'Partner Domain Service',
                'normalizedName': 'partner domain service',
                'uniqueId': 'serviceNewCategory.partner domain service',
                'icons': ['partner_domain_service'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Mobility',
                'normalizedName': 'mobility',
                'uniqueId': 'serviceNewCategory.mobility',
                'icons': ['mobility'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'VoIP Call Control',
                'normalizedName': 'voip call control',
                'uniqueId': 'serviceNewCategory.voip call control',
                'icons': ['call_controll'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'E2E Service',
                'normalizedName': 'e2e service',
                'uniqueId': 'serviceNewCategory.e2e service',
                'icons': ['network_l_1-3'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Network L4+',
                'normalizedName': 'network l4+',
                'uniqueId': 'serviceNewCategory.network l4+',
                'icons': ['network_l_4'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Network L1-3',
                'normalizedName': 'network l1-3',
                'uniqueId': 'serviceNewCategory.network l1-3',
                'icons': ['network_l_1-3'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
            {
                'name': 'Network Service',
                'normalizedName': 'network service',
                'uniqueId': 'serviceNewCategory.network service',
                'icons': ['network_l_1-3'],
                'subcategories': None,
                'version': None,
                'ownerId': None,
                'empty': False,
                'type': None
            },
        ],
        'productCategories': []
    }
}


@mock.patch.object(ResourceCategory, "send_message_json")
def test_resource_category_exists(mock_send_message_json):

    rc = ResourceCategory(name="test_name")
    mock_send_message_json.return_value = {}
    assert not rc.exists()
    mock_send_message_json.return_value = CATEGORIES
    assert not rc.exists()
    rc = ResourceCategory(name="Network Connectivity")
    assert rc.exists()

@mock.patch.object(ResourceCategory, "send_message_json")
def test_resource_category_get(mock_send_message_json):

    mock_send_message_json.return_value = CATEGORIES
    rc = ResourceCategory.get(name="Network Connectivity")
    assert rc.name == "Network Connectivity"
    assert rc.normalized_name == "network connectivity"
    assert rc.unique_id == "resourceNewCategory.network connectivity"
    assert not rc.icons
    assert not rc.version
    assert not rc.owner_id
    assert not rc.empty
    assert not rc.type
    assert len(rc.subcategories) == 2

    with pytest.raises(ResourceNotFound):
        ResourceCategory.get(name="Network Connectivity", subcategory="Toto")
    rc = ResourceCategory.get(name="Network Connectivity", subcategory="Connection Points")
    assert rc.name == "Network Connectivity"
    assert rc.normalized_name == "network connectivity"
    assert rc.unique_id == "resourceNewCategory.network connectivity"
    assert not rc.icons
    assert not rc.version
    assert not rc.owner_id
    assert not rc.empty
    assert not rc.type
    assert len(rc.subcategories) == 1

    mock_send_message_json.side_effect = APIError
    with pytest.raises(ResourceNotFound):
        ResourceCategory.get(name="Network Connectivity")

    mock_send_message_json.side_effect = KeyError
    with pytest.raises(ResourceNotFound):
        ResourceCategory.get(name="Network Connectivity")

@mock.patch.object(ResourceCategory, "send_message_json")
def test_resource_category_create(mock_send_message_json):

    mock_send_message_json.return_value = CATEGORIES
    rc = ResourceCategory.create(name="Network Connectivity")
    assert rc.name == "Network Connectivity"
    assert rc.normalized_name == "network connectivity"
    assert rc.unique_id == "resourceNewCategory.network connectivity"
    assert not rc.icons
    assert not rc.version
    assert not rc.owner_id
    assert not rc.empty
    assert not rc.type
    ResourceCategory.create(name="New category")

@mock.patch.object(ServiceCategory, "send_message_json")
def test_service_category_exists(mock_send_message_json):

    sc = ServiceCategory(name="test_name")
    mock_send_message_json.return_value = CATEGORIES
    assert not sc.exists()
    sc = ServiceCategory(name="Partner Domain Service")
    assert sc.exists()
    mock_send_message_json.side_effect = APIError
    assert not sc.exists()
    mock_send_message_json.side_effect = KeyError
    assert not sc.exists()

@mock.patch.object(ServiceCategory, "send_message_json")
def test_service_category_get(mock_send_message_json):

    mock_send_message_json.return_value = {}
    with pytest.raises(ResourceNotFound):
        ServiceCategory.get(name="Partner Domain Service")
    mock_send_message_json.return_value = CATEGORIES
    sc = ServiceCategory.get(name="Partner Domain Service")
    assert sc.name == "Partner Domain Service"
    assert sc.normalized_name == "partner domain service"
    assert sc.unique_id == "serviceNewCategory.partner domain service"
    assert not sc.version
    assert not sc.owner_id
    assert not sc.empty
    assert not sc.type

@mock.patch.object(ServiceCategory, "send_message_json")
def test_service_category_create(mock_send_message_json):

    mock_send_message_json.return_value = CATEGORIES
    sc = ServiceCategory.create(name="Partner Domain Service")
    assert sc.name == "Partner Domain Service"
    assert sc.normalized_name == "partner domain service"
    assert sc.unique_id == "serviceNewCategory.partner domain service"
    assert not sc.version
    assert not sc.owner_id
    assert not sc.empty
    assert not sc.type
    ServiceCategory.create(name="New category")