1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from django.test import TestCase, Client
19 from rest_framework import status
20 from rest_framework.test import APIClient
22 from lcm.ns.biz.ns_create import CreateNSService
23 from lcm.pub.database.models import NSInstModel
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.pub.utils import restcall
28 class TestNsInstantiate(TestCase):
30 self.client = Client()
31 self.client1 = APIClient()
32 self.nsd_id = str(uuid.uuid4())
33 self.ns_package_id = str(uuid.uuid4())
36 NSInstModel.objects.all().delete()
38 @mock.patch.object(restcall, 'call_req')
39 def test_create_ns(self, mock_call_req):
41 "csarId": self.ns_package_id,
44 r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201']
45 r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
46 mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
49 "globalCustomerId": "global-customer-id-test1",
50 "serviceType": "service-type-test1"
52 "csarId": self.nsd_id,
54 "description": "description"
56 response = self.client1.post("/api/nslcm/v1/ns", data=data, format='json')
57 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
59 @mock.patch.object(CreateNSService, "do_biz")
60 def test_create_ns_empty_data(self, mock_do_biz):
61 mock_do_biz.side_effect = Exception("Exception in CreateNS.")
63 response = self.client.post("/api/nslcm/v1/ns", data=data)
64 self.assertEqual(response.data["error"], "Exception in CreateNS.")
65 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
66 self.assertIn("error", response.data)
68 @mock.patch.object(CreateNSService, "do_biz")
69 def test_create_ns_non_existing_nsd(self, mock_do_biz):
70 mock_do_biz.side_effect = NSLCMException("nsd not exists.")
75 'description': 'description'
77 response = self.client.post("/api/nslcm/v1/ns", data=data)
78 self.assertEqual(response.data["error"], "nsd not exists.")
79 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
80 self.assertIn("error", response.data)
82 @mock.patch.object(restcall, 'call_req')
83 def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
84 mock_call_req.return_value = [1, "Failed to get nsd.", '500']
88 'description': 'description'
90 response = self.client.post("/api/nslcm/v1/ns", data=data)
91 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
92 self.assertIn("error", response.data)
94 @mock.patch.object(restcall, 'call_req')
95 def test_create_ns_when_ns_name_exist(self, mock_call_req):
96 nspackage_info = json.JSONEncoder().encode({
97 "csarId": self.ns_package_id,
100 mock_call_req.return_value = [0, nspackage_info, '200']
101 NSInstModel(id="1", name="ns1").save()
105 'description': 'description'
107 response = self.client.post("/api/nslcm/v1/ns", data=data)
108 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
109 self.assertIn("error", response.data)