diff options
Diffstat (limited to 'onap-client/onap_client/tests')
-rw-r--r-- | onap-client/onap_client/tests/__init__.py | 36 | ||||
-rw-r--r-- | onap-client/onap_client/tests/test.zip | bin | 0 -> 221 bytes | |||
-rw-r--r-- | onap-client/onap_client/tests/test_catalog.py | 125 | ||||
-rw-r--r-- | onap-client/onap_client/tests/test_payload.jinja | 1 | ||||
-rw-r--r-- | onap-client/onap_client/tests/test_request.py | 187 | ||||
-rw-r--r-- | onap-client/onap_client/tests/test_resource.py | 68 | ||||
-rw-r--r-- | onap-client/onap_client/tests/testdata.py | 92 | ||||
-rw-r--r-- | onap-client/onap_client/tests/utils.py | 79 |
8 files changed, 588 insertions, 0 deletions
diff --git a/onap-client/onap_client/tests/__init__.py b/onap-client/onap_client/tests/__init__.py new file mode 100644 index 0000000..5519a84 --- /dev/null +++ b/onap-client/onap_client/tests/__init__.py @@ -0,0 +1,36 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ diff --git a/onap-client/onap_client/tests/test.zip b/onap-client/onap_client/tests/test.zip Binary files differnew file mode 100644 index 0000000..2bd8d04 --- /dev/null +++ b/onap-client/onap_client/tests/test.zip diff --git a/onap-client/onap_client/tests/test_catalog.py b/onap-client/onap_client/tests/test_catalog.py new file mode 100644 index 0000000..944c115 --- /dev/null +++ b/onap-client/onap_client/tests/test_catalog.py @@ -0,0 +1,125 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ + +from onap_client.client.clients import Client +from onap_client.tests.testdata import TestClient, CATALOG_RESOURCES, THIS_DIR # noqa: F401 + + +def test_catalog_items(): + c = Client() + + assert "MAKE_TEST_REQUEST" in c.test.catalog_items + + +def test_client_namespace(): + c = Client() + + assert hasattr(c, "test") + + +def test_client_catalog_resources(): + c = Client() + + assert c.test.catalog_resources == CATALOG_RESOURCES + + +def test_catalog_item_verb(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert resource.verb == "POST" + + +def test_catalog_item_description(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert resource.description == "Test Catalog Request" + + +def test_catalog_item_payload(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert resource.payload == "{}/test_payload.jinja".format(THIS_DIR) + + +def test_catalog_item_uri_parameter(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert "test_uri_parameter" in resource.uri_parameters + + +def test_catalog_item_payload_parameter(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert "test_item_parameter" in resource.payload_parameters + + +def test_catalog_item_headers(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + headers = {"Accept": "application/json", "Content-Type": "application/json"} + + assert resource.headers == headers + + +def test_catalog_item_auth(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + auth = ("abc", "123") + + assert resource.auth == auth + + +def test_catalog_item_return_data(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_TEST_REQUEST") + + assert "return_data_1" in resource.return_data diff --git a/onap-client/onap_client/tests/test_payload.jinja b/onap-client/onap_client/tests/test_payload.jinja new file mode 100644 index 0000000..0038703 --- /dev/null +++ b/onap-client/onap_client/tests/test_payload.jinja @@ -0,0 +1 @@ +{"test_item_parameter": "{{test_item_parameter}}"}
\ No newline at end of file diff --git a/onap-client/onap_client/tests/test_request.py b/onap-client/onap_client/tests/test_request.py new file mode 100644 index 0000000..5c7e019 --- /dev/null +++ b/onap-client/onap_client/tests/test_request.py @@ -0,0 +1,187 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ + +import responses +import json +import os +import sys + +from io import StringIO + +from onap_client.client.clients import Client +from onap_client.client.request import APICatalogRequestObject + +from onap_client.tests.testdata import ( + THIS_DIR, + RETURN_DATA, +) +from onap_client.tests.testdata import TestClient # noqa: F401 +from onap_client.cli import main + +DUMMY_PARAM = "dummy_param" + + +def test_request_uri(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_FILES_REQUEST") + + request_object = create_request_object(resource) + + assert request_object.uri == "http://this.is.a.test.com/{}".format(DUMMY_PARAM) + + +def test_payload(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_FILES_REQUEST") + + request_object = create_request_object(resource) + + assert json.loads(request_object.payload) == {"test_item_parameter": DUMMY_PARAM} + + +def test_files(): + c = Client() + + resource = c.test.catalog_items.get("MAKE_FILES_REQUEST") + + request_object = create_request_object(resource) + + file_path = "{}/test.zip".format(THIS_DIR) + with open(file_path, "rb") as f: + data = f.read() + file_name = os.path.basename(file_path) + files = {"upload": [file_name, data, "application/zip"]} + assert request_object.files == files + + +@responses.activate +def test_make_request(): + responses.add( + responses.POST, + "http://this.is.a.test.com/{}".format(DUMMY_PARAM), + json=RETURN_DATA, + ) + + c = Client() + + params = {"test_item_parameter": DUMMY_PARAM, "test_uri_parameter": DUMMY_PARAM} + + resp = c.test.make_test_request(**params) + + assert resp.response_data == RETURN_DATA + + +def test_cli(): + cli_string = ["test", "--help"] + temp_out = StringIO() + sys.stdout = temp_out + + main(*cli_string) + + sys.stdout.seek(0) + output = sys.stdout.read() + + sys.stdout = sys.__stdout__ + + assert output.find("make-test-request") != -1 + + +def test_cli_request_help(): + cli_string = ["test", "make-test-request", "--help"] + temp_out = StringIO() + sys.stdout = temp_out + + main(*cli_string) + sys.stdout.seek(0) + output = sys.stdout.read() + + sys.stdout = sys.__stdout__ + + assert output.find("--test-item-parameter") != -1 + + +@responses.activate +def test_cli_request(): + temp_out = StringIO() + sys.stdout = temp_out + + responses.add( + responses.POST, + "http://this.is.a.test.com/{}".format(DUMMY_PARAM), + json=RETURN_DATA, + ) + + cli_string = [ + "test", + "make-test-request", + "--test-item-parameter", + DUMMY_PARAM, + "--test-uri-parameter", + DUMMY_PARAM, + ] + + main(*cli_string) + + sys.stdout.seek(0) + output = sys.stdout.read() + + sys.stdout = sys.__stdout__ + + assert json.loads(output) == RETURN_DATA + + +def create_request_object(catalog_item): + payload_input = {} + uri_input = {} + file_input = {} + + for param in catalog_item.payload_parameters: + payload_input[param] = DUMMY_PARAM + + for param in catalog_item.uri_parameters: + uri_input[param] = DUMMY_PARAM + + file_input["file_path"] = "{}/test.zip".format(THIS_DIR) + + return APICatalogRequestObject( + catalog_item, + payload_parameters=payload_input, + uri_parameters=uri_input, + file_parameters=file_input, + ) diff --git a/onap-client/onap_client/tests/test_resource.py b/onap-client/onap_client/tests/test_resource.py new file mode 100644 index 0000000..5b4124a --- /dev/null +++ b/onap-client/onap_client/tests/test_resource.py @@ -0,0 +1,68 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ + +from onap_client.resource import Resource +from onap_client.lib import generate_dummy_string, generate_dummy_date + + +class TestResource(Resource): + def __init__(self, test_param): + test_input = {} + + test_input["param1"] = test_param + test_input["param2"] = generate_dummy_string() + test_input["param3"] = generate_dummy_date() + + super().__init__(test_input) + + def _create(self, create_input): + return create_input + + def _post_create(self): + pass + + def _submit(self): + pass + + +def test_resource(): + test_param = "abc" + + r = TestResource(test_param) + r.print() + + assert hasattr(r, "param1") and r.param1 == test_param diff --git a/onap-client/onap_client/tests/testdata.py b/onap-client/onap_client/tests/testdata.py new file mode 100644 index 0000000..ade4713 --- /dev/null +++ b/onap-client/onap_client/tests/testdata.py @@ -0,0 +1,92 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ + +from functools import partial +from os.path import dirname, abspath + +from onap_client.client.clients import Client + +TEST_URI = "http://this.is.a.test.com" +THIS_DIR = dirname(abspath(__file__)) + + +class TestClient(Client): + @property + def namespace(self): + return "test" + + @property + def catalog_resources(self): + return CATALOG_RESOURCES + + +CATALOG_RESOURCES = { + "MAKE_TEST_REQUEST": { + "verb": "POST", + "description": "Test Catalog Request", + "uri": partial("{endpoint}/{test_uri_parameter}".format, endpoint=TEST_URI), + "uri-parameters": ["test_uri_parameter"], + "payload": "{}/test_payload.jinja".format(THIS_DIR), + "payload-parameters": ["test_item_parameter"], + "success_code": 200, + "headers": {"Accept": "application/json", "Content-Type": "application/json"}, + "return_data": { + "return_data_1": ("return_parameter_1",), + "return_data_2": ("return_parameter_2",), + }, + "auth": ("abc", "123"), + }, + "MAKE_FILES_REQUEST": { + "verb": "POST", + "description": "Test Catalog Request With Files", + "uri": partial("{endpoint}/{test_uri_parameter}".format, endpoint=TEST_URI), + "uri-parameters": ["test_uri_parameter"], + "files-parameters": ["file_path", "file_type"], + "payload": "{}/test_payload.jinja".format(THIS_DIR), + "payload-parameters": ["test_item_parameter"], + "success_code": 200, + "headers": {"Accept": "application/json", "Content-Type": "application/json"}, + "return_data": { + "return_data_1": ("return_parameter_1",), + "return_data_2": ("return_parameter_2",), + }, + "auth": ("abc", "123"), + }, +} + + +RETURN_DATA = {"return_parameter_1": "abc", "return_parameter_2": "123"} diff --git a/onap-client/onap_client/tests/utils.py b/onap-client/onap_client/tests/utils.py new file mode 100644 index 0000000..ddcca9e --- /dev/null +++ b/onap-client/onap_client/tests/utils.py @@ -0,0 +1,79 @@ +# -*- coding: utf8 -*- +# ============LICENSE_START======================================================= +# org.onap.vvp/validation-scripts +# =================================================================== +# Copyright © 2020 AT&T Intellectual Property. All rights reserved. +# =================================================================== +# +# Unless otherwise specified, all software contained herein is licensed +# under the Apache License, Version 2.0 (the "License"); +# you may not use this software 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. +# +# +# +# Unless otherwise specified, all documentation contained herein is licensed +# under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +# you may not use this documentation except in compliance with the License. +# You may obtain a copy of the License at +# +# https://creativecommons.org/licenses/by/4.0/ +# +# Unless required by applicable law or agreed to in writing, documentation +# 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. +# +# ============LICENSE_END============================================ + +import responses +import functools + + +def mockup_client(client): + for k, v in client.catalog_items.items(): + mockup_catalog_item(v) + + +def mockup_return_item(item, item_list): + items = list(item_list) + if not items: + return {item: item} + new_item = items.pop(0) + return {item: mockup_return_item(new_item, items)} + + +def mockup_catalog_item( + catalog_resource, override_return_data=None, override_uri_params={} +): + uri = catalog_resource.uri + if isinstance(uri, functools.partial): + params = {} + for param in catalog_resource.uri_parameters: + params[param] = param + params.update(override_uri_params) + uri = uri(**params) + + return_data = catalog_resource.return_data + return_items = {} + for k, v in return_data.items(): + return_items.update(mockup_return_item(k, v)) + + if override_return_data: + return_items = override_return_data + + responses.add( + getattr(responses, catalog_resource.verb), + uri, + json=return_items, + status=catalog_resource.success_code, + ) |