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
19 from lcm.pub.database.models import NSInstModel
20 from lcm.ns.biz.ns_create import CreateNSService
21 from lcm.pub.utils import restcall
22 from rest_framework import status
23 from rest_framework.test import APIClient
24 from lcm.ns.tests import CREATE_NS_DICT
27 class TestNsInstantiate(TestCase):
29 self.client = APIClient()
30 self.create_ns_dict = CREATE_NS_DICT
33 NSInstModel.objects.all().delete()
35 @mock.patch.object(restcall, 'call_req')
36 def test_create_ns(self, mock_call_req):
38 "csarId": str(uuid.uuid4()),
41 r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201']
42 r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
43 mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
44 self.create_ns_dict["csarId"] = str(uuid.uuid4())
45 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
46 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
48 @mock.patch.object(CreateNSService, "do_biz")
49 def test_create_ns_empty_data(self, mock_do_biz):
50 mock_do_biz.side_effect = Exception("Exception in CreateNS.")
51 response = self.client.post("/api/nslcm/v1/ns", data={})
52 self.assertEqual(response.data["error"], "Exception in CreateNS.")
53 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
54 self.assertIn("error", response.data)
56 @mock.patch.object(CreateNSService, "do_biz")
57 def test_create_ns_non_existing_nsd(self, mock_do_biz):
58 mock_do_biz.side_effect = Exception("nsd not exists.")
59 self.create_ns_dict["csarId"] = "1"
60 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
61 self.assertEqual(response.data["error"], "nsd not exists.")
62 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
63 self.assertIn("error", response.data)
65 @mock.patch.object(restcall, 'call_req')
66 def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
67 mock_call_req.return_value = [1, "Failed to get nsd.", '500']
68 self.create_ns_dict["csarId"] = "1"
69 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
70 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
71 self.assertIn("error", response.data)
73 @mock.patch.object(restcall, 'call_req')
74 def test_create_ns_when_ns_name_exist(self, mock_call_req):
75 nspackage_info = json.JSONEncoder().encode({
76 "csarId": str(uuid.uuid4()),
79 mock_call_req.return_value = [0, nspackage_info, '200']
80 NSInstModel(id="1", name="ns").save()
81 self.create_ns_dict["csarId"] = "1"
82 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
83 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
84 self.assertIn("error", response.data)