diff options
-rw-r--r-- | catalog/packages/tests/test_nsdm_subscription.py | 25 | ||||
-rw-r--r-- | catalog/packages/views/nsdm_subscription_views.py | 188 |
2 files changed, 38 insertions, 175 deletions
diff --git a/catalog/packages/tests/test_nsdm_subscription.py b/catalog/packages/tests/test_nsdm_subscription.py index d2db19cb..1eb4c0dd 100644 --- a/catalog/packages/tests/test_nsdm_subscription.py +++ b/catalog/packages/tests/test_nsdm_subscription.py @@ -100,8 +100,7 @@ class TestNsdmSubscription(TestCase): expected_data = { 'status': 500, 'detail': "callbackUri http://callbackuri.com didn't" - " return 204 statuscode.", - 'title': 'Creating Subscription Failed!' + " return 204 statuscode." } response = self.client.post("/api/nsd/v1/subscriptions", data=self.subscription, format='json') @@ -148,8 +147,7 @@ class TestNsdmSubscription(TestCase): expected_data = { 'status': 303, 'detail': 'Already Subscription exists with' - ' the same callbackUri and filter', - 'title': 'Creating Subscription Failed!' + ' the same callbackUri and filter' } response = self.client.post("/api/nsd/v1/subscriptions", data=self.subscription, format='json') @@ -191,8 +189,7 @@ class TestNsdmSubscription(TestCase): mock_requests.get.return_value.status_code = 204 expected_data = { 'status': 400, - 'detail': 'Auth type should be BASIC', - 'title': 'Creating Subscription Failed!' + 'detail': 'Auth type should be BASIC' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -217,8 +214,7 @@ class TestNsdmSubscription(TestCase): mock_requests.get.return_value.status_code = 204 expected_data = { 'status': 400, - 'detail': 'Auth type should be OAUTH2_CLIENT_CREDENTIALS', - 'title': 'Creating Subscription Failed!' + 'detail': 'Auth type should be OAUTH2_CLIENT_CREDENTIALS' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -240,8 +236,7 @@ class TestNsdmSubscription(TestCase): mock_requests.get.return_value.status_code = 204 expected_data = { 'status': 400, - 'detail': 'userName and password needed for BASIC', - 'title': 'Creating Subscription Failed!' + 'detail': 'userName and password needed for BASIC' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -266,8 +261,7 @@ class TestNsdmSubscription(TestCase): expected_data = { 'status': 400, 'detail': 'clientId, clientPassword and tokenEndpoint' - ' required for OAUTH2_CLIENT_CREDENTIALS', - 'title': 'Creating Subscription Failed!' + ' required for OAUTH2_CLIENT_CREDENTIALS' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -295,8 +289,7 @@ class TestNsdmSubscription(TestCase): expected_data = { 'status': 400, 'detail': 'Notification Filter should contain' - ' either nsdId or nsdInfoId', - 'title': 'Creating Subscription Failed!' + ' either nsdId or nsdInfoId' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -324,8 +317,7 @@ class TestNsdmSubscription(TestCase): expected_data = { 'status': 400, 'detail': 'Notification Filter should contain' - ' either pnfdId or pnfdInfoIds', - 'title': 'Creating Subscription Failed!' + ' either pnfdId or pnfdInfoIds' } response = self.client.post("/api/nsd/v1/subscriptions", data=dummy_subscription, format='json') @@ -459,7 +451,6 @@ class TestNsdmSubscription(TestCase): def test_nsdm_get_subscription_failure(self): expected_data = { - "title": "Query Subscription Failed!", "status": 404, "detail": "Subscription(" + self.subscription_id + ") " "doesn't exists" diff --git a/catalog/packages/views/nsdm_subscription_views.py b/catalog/packages/views/nsdm_subscription_views.py index 336a6c18..a3ceeae2 100644 --- a/catalog/packages/views/nsdm_subscription_views.py +++ b/catalog/packages/views/nsdm_subscription_views.py @@ -13,7 +13,6 @@ # limitations under the License. import logging -import traceback from drf_yasg.utils import swagger_auto_schema, no_body from rest_framework import status @@ -27,11 +26,9 @@ from catalog.packages.serializers.nsdm_subscription import NsdmSubscriptionSeria from catalog.packages.serializers.nsdm_subscription import NsdmSubscriptionRequestSerializer from catalog.packages.serializers.response import ProblemDetailsSerializer -from catalog.pub.exceptions import ResourceNotFoundException from catalog.pub.exceptions import NsdmBadRequestException -from catalog.pub.exceptions import NsdmDuplicateSubscriptionException - from catalog.packages.biz.nsdm_subscription import NsdmSubscription +from .common import view_safe_call_with_log logger = logging.getLogger(__name__) @@ -44,17 +41,6 @@ def validate_data(data, serializer): return serialized_data -def get_problem_details_serializer(title, status_code, error_message): - problem_details = { - "title": title, - "status": status_code, - "detail": error_message - } - problem_details_serializer = ProblemDetailsSerializer(data=problem_details) - problem_details_serializer.is_valid() - return problem_details_serializer - - @swagger_auto_schema( method='POST', operation_description="Create Subscription for NSD Management", @@ -78,84 +64,33 @@ def get_problem_details_serializer(title, status_code, error_message): } ) @api_view(http_method_names=['POST', 'GET']) +@view_safe_call_with_log(logger=logger) def nsd_subscription_rc(request): if request.method == 'POST': logger.debug("SubscribeNotification--post::> %s" % request.data) - try: - title = 'Creating Subscription Failed!' - nsdm_subscription_request = \ - validate_data(request.data, - NsdmSubscriptionRequestSerializer) - subscription = NsdmSubscription().create( - nsdm_subscription_request.data) - subscription_resp = validate_data(subscription, - NsdmSubscriptionSerializer) - return Response(data=subscription_resp.data, - status=status.HTTP_201_CREATED) - except NsdmDuplicateSubscriptionException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_303_SEE_OTHER, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_303_SEE_OTHER) - except NsdmBadRequestException as e: - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_400_BAD_REQUEST, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_400_BAD_REQUEST) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - problem_details_serializer = \ - get_problem_details_serializer( - title, - status.HTTP_500_INTERNAL_SERVER_ERROR, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + nsdm_subscription_request = \ + validate_data(request.data, + NsdmSubscriptionRequestSerializer) + subscription = NsdmSubscription().create( + nsdm_subscription_request.data) + subscription_resp = validate_data(subscription, + NsdmSubscriptionSerializer) + return Response(data=subscription_resp.data, + status=status.HTTP_201_CREATED) + if request.method == 'GET': logger.debug("Subscription Notification GET %s" % request.query_params) - try: - title = 'Query Subscription Failed!' - request_query_params = {} - if request.query_params: - request_query_params = \ - validate_data(request.query_params, - NsdmNotificationsFilter).data - subscription_data = \ - NsdmSubscription().query_multi_subscriptions( - request_query_params) - subscriptions = validate_data(subscription_data, - NsdmSubscriptionsSerializer) - return Response(data=subscriptions.data, status=status.HTTP_200_OK) - except NsdmBadRequestException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_400_BAD_REQUEST, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_400_BAD_REQUEST) - except ResourceNotFoundException as e: - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_404_NOT_FOUND, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_404_NOT_FOUND) - except Exception as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer( - title, - status.HTTP_500_INTERNAL_SERVER_ERROR, - traceback.format_exc()) - return Response(data=problem_details_serializer.data, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + request_query_params = {} + if request.query_params: + request_query_params = \ + validate_data(request.query_params, + NsdmNotificationsFilter).data + subscription_data = \ + NsdmSubscription().query_multi_subscriptions( + request_query_params) + subscriptions = validate_data(subscription_data, + NsdmSubscriptionsSerializer) + return Response(data=subscriptions.data, status=status.HTTP_200_OK) @swagger_auto_schema( @@ -181,77 +116,14 @@ def nsd_subscription_rc(request): } ) @api_view(http_method_names=['GET', 'DELETE']) +@view_safe_call_with_log(logger=logger) def nsd_subscription_rd(request, **kwargs): subscription_id = kwargs.get("subscriptionId") + validate_data({'subscription_id': subscription_id}, NsdmSubscriptionIdSerializer) if request.method == 'GET': - try: - title = 'Query Subscription Failed!' - validate_data({'subscription_id': subscription_id}, - NsdmSubscriptionIdSerializer) - subscription_data = \ - NsdmSubscription().query_single_subscription(subscription_id) - subscription = validate_data(subscription_data, - NsdmSubscriptionSerializer) - return Response(data=subscription.data, status=status.HTTP_200_OK) - except NsdmBadRequestException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_400_BAD_REQUEST, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_400_BAD_REQUEST) - except ResourceNotFoundException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_404_NOT_FOUND, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_404_NOT_FOUND) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - problem_details_serializer = \ - get_problem_details_serializer( - title, - status.HTTP_500_INTERNAL_SERVER_ERROR, - "Query of subscriptioni(%s) Failed" - % subscription_id) - return Response(data=problem_details_serializer.data, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + subscription_data = NsdmSubscription().query_single_subscription(subscription_id) + subscription = validate_data(subscription_data, NsdmSubscriptionSerializer) + return Response(data=subscription.data, status=status.HTTP_200_OK) elif request.method == 'DELETE': - try: - title = 'Delete Subscription Failed!' - validate_data({'subscription_id': subscription_id}, - NsdmSubscriptionIdSerializer) - subscription_data = NsdmSubscription().\ - delete_single_subscription(subscription_id) - return Response(status=status.HTTP_204_NO_CONTENT) - except NsdmBadRequestException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_400_BAD_REQUEST, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_400_BAD_REQUEST) - except ResourceNotFoundException as e: - logger.error(e.message) - problem_details_serializer = \ - get_problem_details_serializer(title, - status.HTTP_404_NOT_FOUND, - e.message) - return Response(data=problem_details_serializer.data, - status=status.HTTP_404_NOT_FOUND) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - problem_details_serializer = \ - get_problem_details_serializer( - title, - status.HTTP_500_INTERNAL_SERVER_ERROR, - "Delete of subscription(%s) Failed" - % subscription_id) - return Response(data=problem_details_serializer.data, - status=status.HTTP_500_INTERNAL_SERVER_ERROR) + subscription_data = NsdmSubscription().delete_single_subscription(subscription_id) + return Response(status=status.HTTP_204_NO_CONTENT) |