X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=lcm%2Fns%2Ftests%2Ftest_ns_create.py;h=e32d7e4883b9d1b0e4943cb3d58922b059dff04c;hb=60712a9ab45cdf5db1f498e1ab80da68876073cb;hp=0f2305657e1c8ae8ac2f68425aa2cd03b6a56b42;hpb=cc42d7ab095dd50a791a7673e94e524f007ded60;p=vfc%2Fnfvo%2Flcm.git diff --git a/lcm/ns/tests/test_ns_create.py b/lcm/ns/tests/test_ns_create.py index 0f230565..e32d7e48 100644 --- a/lcm/ns/tests/test_ns_create.py +++ b/lcm/ns/tests/test_ns_create.py @@ -1,4 +1,4 @@ -# Copyright 2016 ZTE Corporation. +# Copyright 2017 ZTE Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -11,29 +11,103 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import json import uuid +import mock from django.test import TestCase, Client from rest_framework import status +from rest_framework.test import APIClient -from lcm.pub.database.models import NSInstModel, NSDModel +from lcm.ns.biz.ns_create import CreateNSService +from lcm.pub.database.models import NSInstModel +from lcm.pub.exceptions import NSLCMException +from lcm.pub.utils import restcall class TestNsInstantiate(TestCase): def setUp(self): self.client = Client() + self.client1 = APIClient() self.nsd_id = str(uuid.uuid4()) self.ns_package_id = str(uuid.uuid4()) - NSDModel(id=self.ns_package_id, nsd_id=self.nsd_id, name='name').save() def tearDown(self): - NSDModel.objects.all().delete() NSInstModel.objects.all().delete() - def test_create_ns(self): + @mock.patch.object(restcall, 'call_req') + def test_create_ns(self, mock_call_req): + nspackage_info = { + "csarId": self.ns_package_id, + "packageInfo": {} + } + r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201'] + r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201'] + mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai] data = { - 'nsdid': self.nsd_id, - 'nsname': 'ns', - 'description': 'description'} - response = self.client.post("/api/nslcm/v1/ns", data=data) + "context": { + "globalCustomerId": "global-customer-id-test1", + "serviceType": "service-type-test1" + }, + "csarId": self.nsd_id, + "nsName": "ns", + "nsDescription": "description", + 'nsdId': 'nsdId' + } + response = self.client1.post("/api/nslcm/v1/ns", data=data, format='json') self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code) + + @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' + } + response = self.client.post("/api/nslcm/v1/ns", data=data) + self.assertEqual(response.data["error"], "Exception in CreateNS.") + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + self.assertIn("error", response.data) + + @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.") + new_nsd_id = '1' + data = { + 'csarId': new_nsd_id, + 'nsName': 'ns', + 'nsDescription': 'description', + 'nsdId': 'nsdId' + } + response = self.client.post("/api/nslcm/v1/ns", data=data) + self.assertEqual(response.data["error"], "nsd not exists.") + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + self.assertIn("error", response.data) + + @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 = { + 'csarId': '1', + 'nsName': 'ns', + 'nsDescription': 'description' + } + response = self.client.post("/api/nslcm/v1/ns", data=data) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + self.assertIn("error", response.data) + + @mock.patch.object(restcall, 'call_req') + def test_create_ns_when_ns_name_exist(self, mock_call_req): + nspackage_info = json.JSONEncoder().encode({ + "csarId": self.ns_package_id, + "packageInfo": {} + }) + mock_call_req.return_value = [0, nspackage_info, '200'] + NSInstModel(id="1", name="ns1").save() + data = { + 'csarId': '1', + 'nsName': 'ns1', + 'nsDescription': 'description' + } + response = self.client.post("/api/nslcm/v1/ns", data=data) + self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) + self.assertIn("error", response.data)