aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_vendor.py
blob: f791b8e4fedae7ee44bcafef2c9e9cd11289a4cc (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
"""Test vendor 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 RequestError

from onapsdk.sdc.vendor import Vendor
import onapsdk.constants as const
from onapsdk.sdc.sdc_element import SdcElement

@mock.patch.object(Vendor, 'send_message_json')
def test_get_all_no_vendors(mock_send):
    """Returns empty array if no vendors."""
    mock_send.return_value = {}
    assert Vendor.get_all() == []
    mock_send.assert_called_once_with("GET", 'get Vendors', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/items?itemType=vlm')

@mock.patch.object(Vendor, 'send_message_json')
def test_get_all_some_vendors(mock_send):
    """Returns a list of vendors."""
    mock_send.return_value = {'results':[
        {'name': 'one', 'id': '1234'},
        {'name': 'two', 'id': '1235'}]}
    assert len(Vendor.get_all()) == 2
    vendor_1 = Vendor.get_all()[0]
    assert vendor_1.name == "one"
    assert vendor_1.identifier == "1234"
    assert vendor_1.created() == True
    vendor_2 = Vendor.get_all()[1]
    assert vendor_2.name == "two"
    assert vendor_2.identifier == "1235"
    assert vendor_2.created()
    mock_send.assert_called_with("GET", 'get Vendors', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/items?itemType=vlm')

@mock.patch.object(Vendor, 'exists')
def test_init_no_name(mock_exists):
    """Check init with no names."""
    mock_exists.return_value = False
    vendor = Vendor()
    assert isinstance(vendor, SdcElement)
    assert vendor._identifier == None
    assert vendor._version == None
    assert vendor.name == "Generic-Vendor"
    assert vendor.created() == False
    assert vendor.headers["USER_ID"] == "cs0008"
    assert isinstance(vendor._base_url(), str)
    assert "sdc1/feProxy/onboarding-api/v1.0" in vendor._base_url()

def test_init_with_name():
    """Check init with no names."""
    vendor = Vendor(name="YOLO")
    assert vendor._identifier == None
    assert vendor._version == None
    assert vendor.name == "YOLO"
    assert vendor.headers["USER_ID"] == "cs0008"
    assert isinstance(vendor._base_url(), str)
    assert "sdc1/feProxy/onboarding-api/v1.0" in vendor._base_url()

def test_equality_really_equals():
    """Check two Vendors are equals if name is the same."""
    vendor_1 = Vendor(name="equal")
    vendor_1.identifier  = "1234"
    vendor_2 = Vendor(name="equal")
    vendor_2.identifier  = "1235"
    assert vendor_1 == vendor_2

def test_equality_not_equals():
    """Check two Vendors are not equals if name is not the same."""
    vendor_1 = Vendor(name="equal")
    vendor_1.identifier  = "1234"
    vendor_2 = Vendor(name="not_equal")
    vendor_2.identifier  = "1234"
    assert vendor_1 != vendor_2

def test_equality_not_equals_not_same_object():
    """Check a Vendor and something different are not equals."""
    vendor_1 = Vendor(name="equal")
    vendor_1.identifier  = "1234"
    vendor_2 = "equal"
    assert vendor_1 != vendor_2

@mock.patch.object(Vendor, 'get_all')
def test_exists_not_exists(mock_get_all):
    """Return False if vendor doesn't exist in SDC."""
    vendor_1 = Vendor(name="one")
    vendor_1.identifier = "1234"
    mock_get_all.return_value = [vendor_1]
    vendor = Vendor(name="two")
    assert not vendor.exists()

@mock.patch.object(Vendor, 'get_all')
def test_exists_exists(mock_get_all):
    """Return True if vendor exists in SDC."""
    vendor_1 = Vendor(name="one")
    vendor_1.identifier = "1234"
    vendor_1.version = "1.1"
    mock_get_all.return_value = [vendor_1]
    vendor = Vendor(name="one")
    assert vendor.exists()

@mock.patch.object(Vendor, 'get_all')
@mock.patch.object(Vendor, 'send_message_json')
def test_load_created(mock_send, mock_get_all):
    mock_send.return_value = {'results':
        [{'status': 'state_one', 'id': "5678", "name": "1.0"}], "listCount": 1}
    vendor = Vendor(name="one")
    vendor.identifier = "1234"
    vendor.load()
    mock_get_all.assert_not_called()
    mock_send.assert_called_once_with('GET', 'get item', 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/items/1234/versions')
    assert vendor.status == "state_one"
    assert vendor.version == "5678"

@mock.patch.object(Vendor, 'get_all')
@mock.patch.object(Vendor, 'send_message_json')
def test_load_not_created(mock_send, mock_get_all):
    mock_send.return_value = {'results':
        [{'status': 'state_one', 'id': "5678", "name": "1.0"}], "listCount": 1}
    vendor = Vendor(name="one")
    vendor.load()
    mock_get_all.return_value = []
    mock_send.assert_not_called()
    assert vendor._status == None
    assert vendor.version == None
    assert vendor._identifier == None

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'send_message_json')
def test_create_already_exists(mock_send, mock_exists):
    """Do nothing if already created in SDC."""
    vendor = Vendor()
    mock_exists.return_value = True
    vendor.create()
    mock_send.assert_not_called()

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'send_message_json')
def test_create_issue_in_creation(mock_send, mock_exists):
    """Do nothing if not created but issue during creation."""
    vendor = Vendor()
    expected_data = '{\n  "iconRef": "icon",\n  "vendorName": "Generic-Vendor",\n  "description": "vendor"\n}'
    mock_exists.return_value = False
    mock_send.side_effect = RequestError
    with pytest.raises(RequestError) as exc:
        vendor.create()
    mock_send.assert_called_once_with("POST", "create Vendor", mock.ANY, data=expected_data)
    assert vendor.created() == False

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'send_message_json')
def test_create_OK(mock_send, mock_exists):
    """Create and update object."""
    vendor = Vendor()
    expected_data = '{\n  "iconRef": "icon",\n  "vendorName": "Generic-Vendor",\n  "description": "vendor"\n}'
    mock_exists.return_value = False
    mock_send.return_value = {
        'itemId': "1234",
        'version': {'id': "5678", 'status': 'state_created'}}
    vendor.create()
    mock_send.assert_called_once_with("POST", "create Vendor", mock.ANY, data=expected_data)
    assert vendor.status == const.DRAFT
    assert vendor.identifier == "1234"
    assert vendor.version == "5678"

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'load')
@mock.patch.object(Vendor, 'send_message')
def test_submit_already_certified(mock_send, mock_load, mock_exists):
    """Do nothing if already certified."""
    mock_exists.return_value = True
    vendor = Vendor()
    vendor._status = const.CERTIFIED
    vendor.submit()
    mock_send.assert_not_called()

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'load')
@mock.patch.object(Vendor, 'send_message')
def test_submit_not_created(mock_send, mock_load, mock_exists):
    """Do nothing if not created."""
    mock_exists.return_value = False
    vendor = Vendor()
    vendor.submit()
    mock_send.assert_not_called()

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'load')
@mock.patch.object(Vendor, 'send_message')
def test_submit_certified_NOK(mock_send, mock_load, mock_exists):
    """Don't update status if submission NOK."""
    mock_exists.return_value = True
    vendor = Vendor()
    vendor._identifier = "12345"
    mock_send.side_effect = RequestError
    expected_data = '{\n\n  "action": "Submit"\n}'
    vendor._status = "Draft"
    vendor._version = "1234"
    with pytest.raises(RequestError) as err:
        vendor.submit()
    assert err.type == RequestError
    mock_send.assert_called_once_with("PUT", "Submit Vendor", 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/vendor-license-models/12345/versions/1234/actions', data=expected_data)
    assert vendor._status != const.CERTIFIED

@mock.patch.object(Vendor, 'exists')
@mock.patch.object(Vendor, 'load')
@mock.patch.object(Vendor, 'send_message')
def test_submit_certified_OK(mock_send, mock_load, mock_exists):
    """Set status to CERTIFIED if submission OK."""
    mock_exists.return_value = True
    vendor = Vendor()
    vendor._status = "Draft"
    vendor._version = "1234"
    vendor.identifier = "12345"
    mock_send.return_value = mock.Mock()
    expected_data = '{\n\n  "action": "Submit"\n}'
    vendor.submit()
    mock_send.assert_called_once_with("PUT", "Submit Vendor", 'https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/vendor-license-models/12345/versions/1234/actions', data=expected_data)
    assert vendor.status == const.CERTIFIED

@mock.patch.object(Vendor, 'created')
@mock.patch.object(Vendor, 'load')
def test_version_no_load_no_created(mock_load, mock_created):
    mock_created.return_value = False
    vendor = Vendor()
    assert vendor.version == None
    mock_load.assert_not_called()

@mock.patch.object(Vendor, 'created')
@mock.patch.object(Vendor, 'load')
def test_version_no_load_created(mock_load, mock_created):
    mock_created.return_value = True
    vendor = Vendor()
    vendor._version = "64"
    assert vendor.version == "64"
    mock_load.assert_not_called()

@mock.patch.object(Vendor, 'load')
def test_version_with_load(mock_load):
    vendor = Vendor()
    vendor.identifier = "12345"
    assert vendor.version == None
    mock_load.assert_called_once()

@mock.patch.object(Vendor, 'created')
@mock.patch.object(Vendor, 'load')
def test_status_no_load_no_created(mock_load, mock_created):
    mock_created.return_value = False
    vendor = Vendor()
    assert vendor.status == None
    mock_load.assert_not_called()

@mock.patch.object(Vendor, 'created')
@mock.patch.object(Vendor, 'load')
def test_status_no_load_created(mock_load, mock_created):
    mock_created.return_value = True
    vendor = Vendor()
    vendor.identifier = "12345"
    vendor._status = "Draft"
    assert vendor.status == "Draft"
    mock_load.assert_not_called()

@mock.patch.object(Vendor, 'load')
def test_status_with_load(mock_load):
    vendor = Vendor()
    vendor.identifier = "12345"
    assert vendor.status == None
    mock_load.assert_called_once()

@mock.patch.object(Vendor, 'submit')
@mock.patch.object(Vendor, 'create')
def test_onboard_new_vendor(mock_create, mock_submit):
    getter_mock = mock.Mock(wraps=Vendor.status.fget)
    mock_status = Vendor.status.getter(getter_mock)
    with mock.patch.object(Vendor, 'status', mock_status):
        getter_mock.side_effect = [None, const.CERTIFIED, const.CERTIFIED]
        vendor = Vendor()
        vendor.onboard()
        mock_create.assert_called_once()
        mock_submit.assert_not_called()

@mock.patch.object(Vendor, 'submit')
@mock.patch.object(Vendor, 'create')
def test_onboard_created_vendor(mock_create, mock_submit):
    getter_mock = mock.Mock(wraps=Vendor.status.fget)
    mock_status = Vendor.status.getter(getter_mock)
    with mock.patch.object(Vendor, 'status', mock_status):
        getter_mock.side_effect = [const.DRAFT, const.DRAFT, None]
        vendor = Vendor()
        vendor.onboard()
        mock_submit.assert_called_once()
        mock_create.assert_not_called()

@mock.patch.object(Vendor, 'submit')
@mock.patch.object(Vendor, 'create')
def test_onboard_whole_vendor(mock_create, mock_submit):
    getter_mock = mock.Mock(wraps=Vendor.status.fget)
    mock_status = Vendor.status.getter(getter_mock)
    with mock.patch.object(Vendor, 'status', mock_status):
        getter_mock.side_effect = [None, const.DRAFT, const.DRAFT, None]
        vendor = Vendor()
        vendor.onboard()
        mock_submit.assert_called_once()
        mock_create.assert_called_once()

@mock.patch.object(Vendor, 'load')
@mock.patch.object(Vendor, "send_message")
def test_vendor_archive(mock_send, mock_load):
    vendor = Vendor()
    vendor.identifier = "12345"
    vendor.archive()
    mock_send.assert_called_once_with("PUT",
                                      "ARCHIVE Vendor",
                                      "https://sdc.api.fe.simpledemo.onap.org:30207/sdc1/feProxy/onboarding-api/v1.0/items/12345/actions",
                                      data='{\n\n  "action": "ARCHIVE"\n}')