diff options
-rw-r--r-- | catalog/packages/biz/nsdm_subscription.py | 11 | ||||
-rw-r--r-- | catalog/packages/tests/test_nsdm_subscription.py | 50 | ||||
-rwxr-xr-x | catalog/packages/urls.py | 2 | ||||
-rw-r--r-- | catalog/packages/views/nsdm_subscription_views.py | 54 |
4 files changed, 116 insertions, 1 deletions
diff --git a/catalog/packages/biz/nsdm_subscription.py b/catalog/packages/biz/nsdm_subscription.py index 6c5372f7..a94717ab 100644 --- a/catalog/packages/biz/nsdm_subscription.py +++ b/catalog/packages/biz/nsdm_subscription.py @@ -46,6 +46,17 @@ class NsdmSubscription: def __init__(self): pass + def query_single_subscription(self, subscription_id): + logger.debug("Start Query Subscription... ") + subscription = \ + NsdmSubscriptionModel.objects.filter( + subscriptionid=subscription_id) + if not subscription.exists(): + raise ResourceNotFoundException( + "Subscription(%s) doesn't exists" % subscription_id) + logger.debug("Subscription found... ") + return self.fill_resp_data(subscription[0]) + def query_multi_subscriptions(self, query_params): self.params = query_params query_data = {} diff --git a/catalog/packages/tests/test_nsdm_subscription.py b/catalog/packages/tests/test_nsdm_subscription.py index b31a4b40..104ec767 100644 --- a/catalog/packages/tests/test_nsdm_subscription.py +++ b/catalog/packages/tests/test_nsdm_subscription.py @@ -434,3 +434,53 @@ class TestNsdmSubscription(TestCase): response = self.client.get('/api/nsd/v1/subscriptions', format='json') self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + def test_nsdm_get_subscription(self): + NsdmSubscriptionModel(subscriptionid=self.subscription_id, + callback_uri="http://callbackuri.com", + auth_info={}, + notificationTypes=json.dumps( + ["NsdOnBoardingNotification"]), + nsdId=[], nsdVersion=[], + nsdInfoId=[], nsdDesigner=[], + nsdName=[], nsdInvariantId=[], + vnfPkgIds=[], pnfdInfoIds=[], + nestedNsdInfoIds=[], nsdOnboardingState=[], + nsdOperationalState=[], nsdUsageState=[], + pnfdId=[], pnfdVersion=[], pnfdProvider=[], + pnfdName=[], pnfdInvariantId=[], + pnfdOnboardingState=[], pnfdUsageState=[], + links=json.dumps(self.links)).save() + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_200_OK, response.status_code) + self.assertEqual(self.test_subscription, response.data) + + def test_nsdm_get_subscription_failure(self): + expected_data = { + "title": "Query Subscription Failed!", + "status": 404, + "detail": "Subscription(" + self.subscription_id + ") " + "doesn't exists" + } + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) + self.assertEqual(expected_data, response.data) + + def test_nsdm_get_subscription_failure_bad_request(self): + response = self.client.get("/api/nsd/v1/subscriptions/123", + format='json') + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + + @mock.patch.object(NsdmSubscription, 'query_single_subscription') + def test_nsdmsubscription_getsingle_when_catch_exception( + self, mock_create): + mock_create.side_effect = TypeError("Unicode type") + response = self.client.get('/api/nsd/v1/' + 'subscriptions/' + self.subscription_id, + format='json') + self.assertEqual(response.status_code, + status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py index 0add1d53..296f0512 100755 --- a/catalog/packages/urls.py +++ b/catalog/packages/urls.py @@ -37,7 +37,7 @@ urlpatterns = [ url(r'^api/nsd/v1/ns_descriptors/(?P<nsdInfoId>[0-9a-zA-Z\-\_]+)$', ns_descriptor_views.ns_info_rd, name='ns_info_rd'), url(r'^api/nsd/v1/ns_descriptors/(?P<nsdInfoId>[0-9a-zA-Z\-\_]+)/nsd_content$', ns_descriptor_views.nsd_content_ru, name='nsd_content_ru'), url(r'^api/nsd/v1/subscriptions$', nsdm_subscription_views.nsd_subscription_rc, name='nsd_subscription_rc'), - # url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsdm_subscription_views.nsd_subscription_rd, name='nsd_subscription_rd'), + url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsdm_subscription_views.nsd_subscription_rd, name='nsd_subscription_rd'), # ETSI SOL005 PNFD url(r'^api/nsd/v1/pnf_descriptors$', pnf_descriptor_views.pnf_descriptors_rc, name='pnf_descriptors_rc'), diff --git a/catalog/packages/views/nsdm_subscription_views.py b/catalog/packages/views/nsdm_subscription_views.py index 9b4abe51..32da8655 100644 --- a/catalog/packages/views/nsdm_subscription_views.py +++ b/catalog/packages/views/nsdm_subscription_views.py @@ -24,6 +24,7 @@ from catalog.packages.serializers.nsdm_filter_data \ import NsdmNotificationsFilter from catalog.packages.serializers.nsdm_subscription import \ NsdmSubscriptionsSerializer, \ + NsdmSubscriptionIdSerializer, \ NsdmSubscriptionSerializer, \ NsdmSubscriptionRequestSerializer from catalog.packages.serializers.response \ @@ -157,3 +158,56 @@ def nsd_subscription_rc(request): traceback.format_exc()) return Response(data=problem_details_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +@swagger_auto_schema( + method='GET', + operation_description="Query subscriptions for Nsd Management", + request_body=no_body, + responses={ + status.HTTP_200_OK: NsdmSubscriptionSerializer(), + status.HTTP_400_BAD_REQUEST: ProblemDetailsSerializer(), + status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer() + } +) +@api_view(http_method_names=['GET']) +def nsd_subscription_rd(request, **kwargs): + subscription_id = kwargs.get("subscriptionId") + 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) |