diff options
author | 2018-08-31 14:23:54 +0800 | |
---|---|---|
committer | 2018-08-31 14:33:43 +0800 | |
commit | d2ec8b67204818f8786e9bd90ed145938c84bf4c (patch) | |
tree | 120adf21d015d9d142c80cc67974b0fec6ec2313 | |
parent | 0dabf5f49b1e969052f32c76e8b4bede5e8245a8 (diff) |
Add test for ns descriptor
Add test for ns pnf and modify views
Change-Id: I7ff3c15b1899ffe0272698e3d67395273e7cf254
Issue-ID: VFC-1038
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r-- | catalog/packages/tests/test_ns_descriptor.py | 47 | ||||
-rw-r--r-- | catalog/packages/tests/test_pnf_descriptor.py | 22 | ||||
-rw-r--r-- | catalog/packages/views/ns_descriptor_views.py | 6 | ||||
-rw-r--r-- | catalog/packages/views/pnf_descriptor_views.py | 15 | ||||
-rw-r--r-- | catalog/packages/views/vnf_package_views.py | 9 |
5 files changed, 70 insertions, 29 deletions
diff --git a/catalog/packages/tests/test_ns_descriptor.py b/catalog/packages/tests/test_ns_descriptor.py index 341e39be..d9349236 100644 --- a/catalog/packages/tests/test_ns_descriptor.py +++ b/catalog/packages/tests/test_ns_descriptor.py @@ -25,6 +25,7 @@ from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.utils import toscaparser from catalog.packages.const import PKG_STATUS from catalog.packages.tests.const import nsd_data +from catalog.packages.biz.ns_descriptor import NsDescriptor class TestNsDescriptor(TestCase): @@ -79,6 +80,11 @@ class TestNsDescriptor(TestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(expected_reponse_data, response.data) + def test_nsd_create_failed(self): + reqest_data = {'username': "111"} + response = self.client.post('/api/nsd/v1/ns_descriptors', data=reqest_data, format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + def test_query_multiple_nsds_normal(self): expected_reponse_data = [ copy.deepcopy(self.expected_nsd_info), @@ -238,3 +244,44 @@ class TestNsDescriptor(TestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual('test2', partial_file_content) os.remove('nsd_content.txt') + + @mock.patch.object(NsDescriptor, 'create') + def test_nsd_create_when_catch_exception(self, mock_create): + reqest_data = {'userDefinedData': self.user_defined_data} + mock_create.side_effect = TypeError("integer type") + response = self.client.post('/api/nsd/v1/ns_descriptors', data=reqest_data, format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(NsDescriptor, 'query_single') + def test_query_single_when_catch_exception(self, mock_query_single): + mock_query_single.side_effect = TypeError("integer type") + response = self.client.get('/api/nsd/v1/ns_descriptors/22', format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(NsDescriptor, 'query_multiple') + def test_query_multiple_when_catch_exception(self, mock_query_multipe): + mock_query_multipe.side_effect = TypeError("integer type") + response = self.client.get('/api/nsd/v1/ns_descriptors', format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(NsDescriptor, 'delete_single') + def test_delete_when_catch_exception(self, mock_delete_single): + mock_delete_single.side_effect = TypeError("integer type") + response = self.client.delete("/api/nsd/v1/ns_descriptors/21", format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(NsDescriptor, 'upload') + def test_upload_when_catch_exception(self, mock_upload): + mock_upload.side_effect = TypeError("integer type") + with open('nsd_content.txt', 'wb') as fp: + fp.write('test') + with open('nsd_content.txt', 'rb') as fp: + response = self.client.put("/api/nsd/v1/ns_descriptors/22/nsd_content", {'file': fp}) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + os.remove('nsd_content.txt') + + @mock.patch.object(NsDescriptor, 'download') + def test_download_when_catch_exception(self, mock_download): + mock_download.side_effect = TypeError("integer type") + response = self.client.get("/api/nsd/v1/ns_descriptors/23/nsd_content", format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/catalog/packages/tests/test_pnf_descriptor.py b/catalog/packages/tests/test_pnf_descriptor.py index 6d02bc14..60460df5 100644 --- a/catalog/packages/tests/test_pnf_descriptor.py +++ b/catalog/packages/tests/test_pnf_descriptor.py @@ -75,6 +75,11 @@ class TestPnfDescriptor(TestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(expected_reponse_data, response.data) + def test_pnfd_create_failed(self): + request_data = {'username': "111"} + response = self.client.post('/api/nsd/v1/pnf_descriptors', data=request_data, format='json') + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + def test_query_multiple_pnfds_normal(self): expected_reponse_data = [ copy.deepcopy(self.expected_pnfd_info), @@ -208,6 +213,23 @@ class TestPnfDescriptor(TestCase): response = self.client.get("/api/nsd/v1/pnf_descriptors/22/pnfd_content") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + def test_pnfd_download_when_not_on_boarded(self): + with open('pnfd_content.txt', 'wb') as fp: + fp.writelines('test1') + fp.writelines('test2') + user_defined_data = json.JSONEncoder().encode(self.user_defined_data) + PnfPackageModel( + pnfPackageId='22', + usageState=PKG_STATUS.NOT_IN_USE, + onboardingState=PKG_STATUS.CREATED, + userDefinedData=user_defined_data, + localFilePath="pnfd_content.txt", + pnfdModel='test' + ).save() + response = self.client.get("/api/nsd/v1/pnf_descriptors/22/pnfd_content") + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + os.remove('pnfd_content.txt') + @mock.patch.object(PnfPackage, "create") def test_pnfd_create_when_catch_exception(self, mock_create): request_data = {'userDefinedData': self.user_defined_data} diff --git a/catalog/packages/views/ns_descriptor_views.py b/catalog/packages/views/ns_descriptor_views.py index aa99637d..a82b55b4 100644 --- a/catalog/packages/views/ns_descriptor_views.py +++ b/catalog/packages/views/ns_descriptor_views.py @@ -58,9 +58,6 @@ def ns_info_rd(request, nsdInfoId): # TODO except ResourceNotFoundException as e: logger.error(e.message) return Response(data={'error': 'NSDs do not exist.'}, status=status.HTTP_404_NOT_FOUND) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query of a NSD failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -121,9 +118,6 @@ def ns_descriptors_rc(request, *args, **kwargs): data = NsDescriptor().query_multiple() nsd_infos = validate_data(data, NsdInfosSerializer) return Response(data=nsd_infos.data, status=status.HTTP_200_OK) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query of multiple NSDs failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) diff --git a/catalog/packages/views/pnf_descriptor_views.py b/catalog/packages/views/pnf_descriptor_views.py index d6600923..f5430e6a 100644 --- a/catalog/packages/views/pnf_descriptor_views.py +++ b/catalog/packages/views/pnf_descriptor_views.py @@ -26,6 +26,7 @@ from drf_yasg.utils import no_body, swagger_auto_schema from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response +from catalog.packages.views.ns_descriptor_views import validate_data logger = logging.getLogger(__name__) @@ -60,9 +61,6 @@ def pnfd_info_rd(request, pnfdInfoId): # TODO except ResourceNotFoundException as e: logger.error(e.message) return Response(data={'error': "PNFD does not exist."}, status=status.HTTP_404_NOT_FOUND) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query of a PNFD failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -124,9 +122,6 @@ def pnf_descriptors_rc(request, *args, **kwargs): data = PnfPackage().query_multiple() pnfd_infos = validate_data(data, PnfdInfosSerializer) return Response(data=pnfd_infos.data, status=status.HTTP_200_OK) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query of multiple PNFDs failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -191,11 +186,3 @@ def pnfd_content_ru(request, *args, **kwargs): logger.error(traceback.format_exc()) error_msg = {'error': 'Downloading PNFD content failed.'} return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - -def validate_data(data, serializer): - serialized_data = serializer(data=data) - if not serialized_data.is_valid(): - logger.error('Data validation failed.') - raise CatalogException(serialized_data.error) - return serialized_data diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py index d72b71a3..d77f02eb 100644 --- a/catalog/packages/views/vnf_package_views.py +++ b/catalog/packages/views/vnf_package_views.py @@ -60,9 +60,6 @@ def vnf_packages_rc(request): res = VnfPackage().query_multiple() query_serializer = validate_data(res, VnfPkgInfosSerializer) return Response(data=query_serializer.data, status=status.HTTP_200_OK) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query VNF package failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -76,9 +73,6 @@ def vnf_packages_rc(request): res = VnfPackage().create_vnf_pkg(req_serializer.data) create_vnf_pkg_resp_serializer = validate_data(res, VnfPkgInfoSerializer) return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Create VNF package failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -210,9 +204,6 @@ def vnf_package_rd(request, vnfPkgId): except ResourceNotFoundException as e: logger.error(e.message) return Response(data={'error': "VNF package does not exist"}, status=status.HTTP_404_NOT_FOUND) - except CatalogException as e: - logger.error(e.message) - error_msg = {'error': 'Query an individual VNF package failed.'} except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) |