diff options
-rw-r--r-- | lcm/ns/serializers/sol/create_ns_serializers.py | 4 | ||||
-rw-r--r-- | lcm/ns/tests/test_sol_ns_instances_api.py | 78 | ||||
-rw-r--r-- | lcm/ns/views/sol/ns_instances_views.py | 1 |
3 files changed, 80 insertions, 3 deletions
diff --git a/lcm/ns/serializers/sol/create_ns_serializers.py b/lcm/ns/serializers/sol/create_ns_serializers.py index c335ee24..d3f7832d 100644 --- a/lcm/ns/serializers/sol/create_ns_serializers.py +++ b/lcm/ns/serializers/sol/create_ns_serializers.py @@ -17,5 +17,5 @@ from rest_framework import serializers class CreateNsRequestSerializer(serializers.Serializer): nsdId = serializers.CharField(help_text="Identifier of the NSD that defines the NS instance to be created.", required=True, allow_null=False) - nsName = serializers.CharField(help_text="Name of NS", required=False, allow_null=True) - nsDescription = serializers.CharField(help_text="Description of NS", required=False, allow_null=True) + nsName = serializers.CharField(help_text="Name of NS", required=True, allow_null=True) + nsDescription = serializers.CharField(help_text="Description of NS", required=True, allow_null=True) diff --git a/lcm/ns/tests/test_sol_ns_instances_api.py b/lcm/ns/tests/test_sol_ns_instances_api.py index 94d60bb6..bf207702 100644 --- a/lcm/ns/tests/test_sol_ns_instances_api.py +++ b/lcm/ns/tests/test_sol_ns_instances_api.py @@ -21,6 +21,8 @@ from rest_framework import status from rest_framework.test import APIClient from lcm.pub.database.models import NSInstModel from lcm.pub.utils import restcall +from lcm.ns.biz.ns_create import CreateNSService +from lcm.pub.exceptions import NSLCMException class TestNsInstanceApi(TestCase): @@ -62,6 +64,82 @@ class TestNsInstanceApi(TestCase): self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code) return response.data['id'] + @mock.patch.object(restcall, 'call_req') + def test_create_ns_when_ns_name_exist(self, mock_call_req): + NSInstModel.objects.all().delete() + NSInstModel(id="1", name="ns").save() + nspackage_info = json.JSONEncoder().encode({ + "csarId": self.ns_package_id, + "packageInfo": {} + }) + mock_call_req.return_value = [0, nspackage_info, '200'] + header = { + 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1', + 'HTTP_SERVICETYPE': 'service-type-test1' + } + + data = { + "nsdId": self.nsd_id, + "nsName": "ns", + "nsDescription": "description" + } + response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(CreateNSService, "do_biz") + def test_create_ns_empty_data(self, mock_do_biz): + mock_do_biz.side_effect = Exception("Exception in CreateNS.") + data = { + 'nsdId': 'nsdId' + } + header = { + 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1', + 'HTTP_SERVICETYPE': 'service-type-test1' + } + response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + @mock.patch.object(CreateNSService, "do_biz") + def test_create_ns_no_header(self, mock_do_biz): + mock_do_biz.side_effect = Exception("Exception in CreateNS.") + data = { + "nsdId": self.nsd_id, + "nsName": "ns", + "nsDescription": "description" + } + response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + @mock.patch.object(CreateNSService, "do_biz") + def test_create_ns_non_existing_nsd(self, mock_do_biz): + mock_do_biz.side_effect = NSLCMException("nsd not exists.") + data = { + "nsdId": self.nsd_id, + "nsName": "ns", + "nsDescription": "description" + } + header = { + 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1', + 'HTTP_SERVICETYPE': 'service-type-test1' + } + response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + + @mock.patch.object(restcall, 'call_req') + def test_create_ns_when_fail_to_get_nsd(self, mock_call_req): + mock_call_req.return_value = [1, "Failed to get nsd.", '500'] + data = { + "nsdId": self.nsd_id, + "nsName": "ns", + "nsDescription": "description" + } + header = { + 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1', + 'HTTP_SERVICETYPE': 'service-type-test1' + } + response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + def test_ns_instances_method_not_allowed(self): header = { 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1', diff --git a/lcm/ns/views/sol/ns_instances_views.py b/lcm/ns/views/sol/ns_instances_views.py index dbbdbb53..a81e20f6 100644 --- a/lcm/ns/views/sol/ns_instances_views.py +++ b/lcm/ns/views/sol/ns_instances_views.py @@ -95,7 +95,6 @@ class NSInstancesView(APIView): response = Response(data=resp_serializer.data, status=status.HTTP_201_CREATED) response["Location"] = NS_INSTANCE_BASE_URI % nsInstance['id'] return response - except BadRequestException as e: logger.error("Exception in CreateNS: %s", e.message) data = {'status': status.HTTP_400_BAD_REQUEST, 'detail': e.message} |