summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormaopengzhang <zhang.maopeng1@zte.com.cn>2019-03-27 10:22:27 +0800
committermaopengzhang <zhang.maopeng1@zte.com.cn>2019-03-27 10:48:46 +0800
commit9981bcff0e63e76b74c19149ff9c327d5f910a29 (patch)
tree088a5111be8bfde87aeddf8ceb373aeb17f97338
parent13f71231dcf78a605ad93f6e180d9501fdde13a1 (diff)
add NS create exception usecase
add NS create exception usecase Change-Id: I7014fa9c4eb7ae729824dcf23ab508b24c5fdc81 Issue-ID: VFC-1211 Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
-rw-r--r--lcm/ns/serializers/sol/create_ns_serializers.py4
-rw-r--r--lcm/ns/tests/test_sol_ns_instances_api.py78
-rw-r--r--lcm/ns/views/sol/ns_instances_views.py1
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}