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.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
53 self.assertIn("error", response.data)
55 @mock.patch.object(CreateNSService, "do_biz")
56 def test_create_ns_non_existing_nsd(self, mock_do_biz):
57 mock_do_biz.side_effect = Exception("nsd not exists.")
58 self.create_ns_dict["csarId"] = "1"
59 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
60 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
61 self.assertIn("error", response.data)
63 @mock.patch.object(restcall, 'call_req')
64 def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
65 mock_call_req.return_value = [1, "Failed to get nsd.", '500']
66 self.create_ns_dict["csarId"] = "1"
67 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
68 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
69 self.assertIn("error", response.data)
71 @mock.patch.object(restcall, 'call_req')
72 def test_create_ns_when_ns_name_exist(self, mock_call_req):
73 nspackage_info = json.JSONEncoder().encode({
74 "csarId": str(uuid.uuid4()),
77 mock_call_req.return_value = [0, nspackage_info, '200']
78 NSInstModel(id="1", name="ns").save()
79 self.create_ns_dict["csarId"] = "1"
80 response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_dict, format='json')
81 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
82 self.assertIn("error", response.data)
84 @mock.patch.object(restcall, 'call_req')
85 def test_create_ns_when_ns_csar_no_exist(self, mock_call_req):
86 mock_call_req.return_value = [1, "Failed query ns csar file", 500]
87 response = self.client.post("/api/nslcm/v1/ns", data={"csarId": ''}, format='json')
88 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
89 self.assertIn("error", response.data)