aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_cps.py
blob: 9be0940e46227df2c198c5c895aa98e4e13cac85 (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
#   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.

import requests
from unittest import mock
from typing import List

import pytest
from onapsdk.cps import Anchor, Dataspace, SchemaSet, SchemaSetModuleReference
from onapsdk.exceptions import (APIError, ResourceNotFound, SDKException)

DATASPACE_ANCHOR = {
    "name": "anchor1",
    "schemaSetName": "schemaSet1"
}

DATASPACE_ANCHORS = [
    DATASPACE_ANCHOR,
    {
        "name": "anchor2",
        "schemaSetName": "schemaSet2"
    }
]

DATASPACE_SCHEMA_SET = {
    "name": "schemaSet1",
    "moduleReferences": [
        {
            "name": "mr1",
            "namespace": "mr1_namespace",
            "revision": "mr1_revision",
        },
        {
            "name": "mr2",
            "namespace": "mr2_namespace",
            "revision": "mr2_revision",
        }
    ]
}

# Dataspace tests
def test_dataspace():
    ds = Dataspace(name="test_ds")
    assert ds.name == "test_ds"
    assert f"cps/api/v2/dataspaces/{ds.name}" in ds.url

@mock.patch("onapsdk.cps.Dataspace.send_message")
def test_dataspace_create_anchor(mock_send_message):
    ds = Dataspace(name="test_ds")
    anchor = ds.create_anchor(mock.MagicMock(), "test_anchor")
    mock_send_message.assert_called_once()
    assert anchor.name == "test_anchor"

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_anchors(mock_send_message_json):
    mock_send_message_json.return_value = DATASPACE_ANCHORS
    ds = Dataspace(name="test_ds")
    anchors = list(ds.get_anchors())
    assert len(anchors) == 2
    anchor_1, anchor_2 = anchors
    assert isinstance(anchor_1, Anchor)
    assert isinstance(anchor_2, Anchor)
    assert anchor_1.name == "anchor1"
    assert isinstance(anchor_1.schema_set, SchemaSet)
    assert anchor_1.schema_set.name == "schemaSet1"
    assert anchor_1.schema_set.dataspace == ds
    assert anchor_2.name == "anchor2"
    assert isinstance(anchor_2.schema_set, SchemaSet)
    assert anchor_2.schema_set.name == "schemaSet2"
    assert anchor_2.schema_set.dataspace == ds

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_anchor(mock_send_message_json):
    mock_send_message_json.return_value = DATASPACE_ANCHOR
    ds = Dataspace(name="test_ds")
    anchor = ds.get_anchor("anything")
    assert anchor.name == "anchor1"
    assert anchor.schema_set.name == "schemaSet1"
    assert anchor.schema_set.dataspace == ds

@mock.patch("onapsdk.cps.Dataspace.send_message")
def test_dataspace_delete_anchor(mock_send_message):
    ds = Dataspace(name="test_ds")
    ds.delete_anchor("some-anchor")
    mock_send_message.assert_called_once()
    args = mock_send_message.call_args
    assert args[0][0] == "DELETE"
    assert args[0][2].split('/')[-1] == "some-anchor"

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_schema_set(mock_send_message_json):
    mock_send_message_json.return_value = DATASPACE_SCHEMA_SET
    ds = Dataspace(name="test_ds")
    schema_set = ds.get_schema_set("anything")
    assert isinstance(schema_set, SchemaSet)
    assert schema_set.dataspace == ds
    assert schema_set.name == "schemaSet1"
    assert len(schema_set.module_refences) == 2
    mr_1, mr_2 = schema_set.module_refences
    assert mr_1.name == "mr1"
    assert mr_1.namespace == "mr1_namespace"
    assert mr_1.revision == "mr1_revision"
    assert mr_2.name == "mr2"
    assert mr_2.namespace == "mr2_namespace"
    assert mr_2.revision == "mr2_revision"

@mock.patch("onapsdk.cps.Dataspace.send_message")
@mock.patch("onapsdk.cps.Dataspace.get_schema_set")
def test_dataspace_create_schema_set(mock_get_chema_set, mock_send_message):
    ds = Dataspace(name="test_ds")
    _ = ds.create_schema_set("test_schema_set_name", b"fake_file")
    mock_send_message.assert_called_once()
    mock_get_chema_set.assert_called_once_with("test_schema_set_name")

@mock.patch("onapsdk.cps.Dataspace.send_message")
def test_dataspace_delete(mock_send_message):
    ds = Dataspace(name="test_ds")
    ds.delete()
    mock_send_message.assert_called_once()

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_dataspace(mock_send_message_json):
    mock_send_message_json.return_value = {"name": "test_ds"}
    dataspace_name = "test_ds"
    dataspace = Dataspace.get_dataspace(dataspace_name)
    assert dataspace.name == dataspace_name

# Schemaset tests
def test_schema_set():
    schema_set = SchemaSet(name="test", dataspace=mock.MagicMock())
    assert schema_set.name == "test"
    assert isinstance(schema_set.module_refences, List)
    assert not len(schema_set.module_refences)

    schema_set = SchemaSet(name="test_with_mr", dataspace=mock.MagicMock(),
                           module_references=[SchemaSetModuleReference(name="mr1", namespace="mr1_n", revision="mr1_rev"),
                                              SchemaSetModuleReference(name="mr2", namespace="mr2_n", revision="mr2_rev")])
    assert schema_set.name == "test_with_mr"
    assert isinstance(schema_set.module_refences, List)
    assert len(schema_set.module_refences) == 2
    mr_1, mr_2 = schema_set.module_refences
    assert isinstance(mr_1, SchemaSetModuleReference)
    assert isinstance(mr_2, SchemaSetModuleReference)
    assert mr_1.name == "mr1"
    assert mr_1.namespace == "mr1_n"
    assert mr_1.revision == "mr1_rev"
    assert mr_2.name == "mr2"
    assert mr_2.namespace == "mr2_n"
    assert mr_2.revision == "mr2_rev"

@mock.patch("onapsdk.cps.SchemaSet.send_message")
def test_schemaset_delete(mock_send_message):
    schema_set = SchemaSet(name="test", dataspace=mock.MagicMock())
    schema_set.delete()
    mock_send_message.assert_called_once()

# Anchor tests
def test_anchor():
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    assert anchor.name == "test_anchor"
    assert "test_anchor" in anchor.url

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_delete(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.delete()
    mock_send_message.assert_called_once()
    url = mock_send_message.call_args[0][2]
    assert anchor.url.rstrip("/") in url

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_create_node(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.create_node('{"test": "data"}')
    mock_send_message.assert_called_once()
    data = mock_send_message.call_args[1]["data"]
    assert data == '{"test": "data"}'

@mock.patch("onapsdk.cps.Anchor.send_message_json")
def test_anchor_get_node(mock_send_message_json):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.get_node("test-xpath")
    mock_send_message_json.assert_called_once()
    params = mock_send_message_json.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath"
    assert "descendants" in params and params["descendants"] == 0

    mock_send_message_json.reset_mock()
    anchor.get_node("test-xpath-2", descendants=-1)
    mock_send_message_json.assert_called_once()
    params = mock_send_message_json.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath-2"
    assert "descendants" in params and params["descendants"] == -1

    mock_send_message_json.reset_mock()
    anchor.get_node("test-xpath-3", descendants=2)
    mock_send_message_json.assert_called_once()
    params = mock_send_message_json.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath-3"
    assert "descendants" in params and params["descendants"] == 2

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_update_node(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.update_node("test-xpath", '{"test": "data"}')
    mock_send_message.assert_called_once()
    params = mock_send_message.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath"

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_replace_node(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.replace_node("test-xpath", '{"test": "data"}')
    mock_send_message.assert_called_once()
    params = mock_send_message.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath"

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_add_list_node(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.add_list_node("test-xpath", '{"test": "data"}')
    mock_send_message.assert_called_once()
    params = mock_send_message.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath"

@mock.patch("onapsdk.cps.Anchor.send_message_json")
def test_anchor_query_node(mock_send_message_json):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.query_node("/test-query")
    mock_send_message_json.assert_called_once()
    params = mock_send_message_json.call_args.kwargs["params"]
    assert "cps-path" in params and params["cps-path"] == "/test-query"
    assert "include-descendants" in params and params["include-descendants"] is False

    mock_send_message_json.reset_mock()
    anchor.query_node("/test-query1", include_descendants=True)
    mock_send_message_json.assert_called_once()
    params = mock_send_message_json.call_args.kwargs["params"]
    assert "cps-path" in params and params["cps-path"] == "/test-query1"
    assert "include-descendants" in params and params["include-descendants"] is True

@mock.patch("onapsdk.cps.Anchor.send_message")
def test_anchor_delete_nodes(mock_send_message):
    anchor = Anchor(name="test_anchor", schema_set=mock.MagicMock())
    anchor.delete_nodes("test-xpath")
    mock_send_message.assert_called_once()
    params = mock_send_message.call_args.kwargs["params"]
    assert "xpath" in params and params["xpath"] == "test-xpath"

@mock.patch("onapsdk.cps.Dataspace.send_message")
def test_dataspace_create_anchor_except(mock_send_message_json):
    ds = Dataspace(name="test_creating_anchor")
    mock_send_message_json.exceptions = requests.exceptions
    mock_send_message_json.side_effect = APIError('Dataspace not found', 400)
    with pytest.raises(ResourceNotFound):
        ds.create_anchor(mock.MagicMock(), "test_creating_anchor")
    mock_send_message_json.side_effect = APIError()
    with pytest.raises(SDKException):
        ds.create_anchor(mock.MagicMock(), "test_creating_anchor")

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_anchors_except(mock_send_message_json):
    ds = Dataspace(name="test_ds")
    mock_send_message_json.exceptions = requests.exceptions
    mock_send_message_json.side_effect = APIError('Dataspace not found', 400)
    with pytest.raises(ResourceNotFound):
        list(ds.get_anchors())
    mock_send_message_json.side_effect = APIError()
    with pytest.raises(SDKException):
        list(ds.get_anchors())

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_anchor_except(mock_send_message_json):
    ds = Dataspace(name="test_ds")
    mock_send_message_json.exceptions = requests.exceptions
    mock_send_message_json.side_effect = APIError('Dataspace not found', 400)
    with pytest.raises(ResourceNotFound):
        ds.get_anchor(mock.MagicMock())
    mock_send_message_json.side_effect = APIError()
    with pytest.raises(SDKException):
        ds.get_anchor(mock.MagicMock())

@mock.patch("onapsdk.cps.Dataspace.send_message_json")
def test_dataspace_get_schema_set_except(mock_send_message_json):
    mock_send_message_json.return_value = DATASPACE_SCHEMA_SET
    ds = Dataspace(name="test_ds")
    mock_send_message_json.exceptions = requests.exceptions
    mock_send_message_json.side_effect = APIError('Dataspace not found', 400)
    with pytest.raises(ResourceNotFound):
        ds.get_schema_set(mock.MagicMock())
    mock_send_message_json.side_effect = APIError()
    with pytest.raises(SDKException):
        ds.get_schema_set(mock.MagicMock())


@mock.patch("onapsdk.cps.Dataspace.send_message")
@mock.patch("onapsdk.cps.Dataspace.get_schema_set")
def test_dataspace_create_schema_set_except(mock_get_chema_set, mock_send_message):
    ds = Dataspace(name="test_ds")
    _ = ds.create_schema_set("test_schema_set_name", b"fake_file")
    mock_send_message.exceptions = requests.exceptions
    mock_send_message.side_effect = APIError('Dataspace not found', 400)
    mock_get_chema_set.exceptions = requests.exceptions
    mock_get_chema_set.side_effect = APIError('Dataspace not found', 400)
    with pytest.raises(ResourceNotFound):
        ds.create_schema_set(mock.MagicMock(), 8)
    mock_send_message.side_effect = APIError()
    mock_get_chema_set.side_effect = APIError()
    with pytest.raises(SDKException):
        ds.create_schema_set(mock.MagicMock(), 9)