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