separate ns-create and test create function
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_create.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import json
15 import uuid
16
17 import mock
18 from django.test import TestCase, Client
19 from rest_framework import status
20 from rest_framework.test import APIClient
21
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
26
27
28 class TestNsInstantiate(TestCase):
29     def setUp(self):
30         self.client = Client()
31         self.client1 = APIClient()
32         self.nsd_id = str(uuid.uuid4())
33         self.ns_package_id = str(uuid.uuid4())
34
35     def tearDown(self):
36         NSInstModel.objects.all().delete()
37
38     @mock.patch.object(restcall, 'call_req')
39     def test_create_ns(self, mock_call_req):
40         nspackage_info = {
41             "csarId": self.ns_package_id,
42             "packageInfo": {}
43         }
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]
47         data = {
48             "context": {
49                 "globalCustomerId": "global-customer-id-test1",
50                 "serviceType": "service-type-test1"
51             },
52             "csarId": self.nsd_id,
53             "nsName": "ns",
54             "nsDescription": "description",
55             'nsdId': 'nsdId'
56         }
57         response = self.client1.post("/api/nslcm/v1/ns", data=data, format='json')
58         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
59
60     @mock.patch.object(CreateNSService, "do_biz")
61     def test_create_ns_empty_data(self, mock_do_biz):
62         mock_do_biz.side_effect = Exception("Exception in CreateNS.")
63         data = {
64             'nsdId': 'nsdId'
65         }
66         response = self.client.post("/api/nslcm/v1/ns", data=data)
67         self.assertEqual(response.data["error"], "Exception in CreateNS.")
68         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
69         self.assertIn("error", response.data)
70
71     @mock.patch.object(CreateNSService, "do_biz")
72     def test_create_ns_non_existing_nsd(self, mock_do_biz):
73         mock_do_biz.side_effect = NSLCMException("nsd not exists.")
74         new_nsd_id = '1'
75         data = {
76             'csarId': new_nsd_id,
77             'nsName': 'ns',
78             'nsDescription': 'description',
79             'nsdId': 'nsdId'
80         }
81         response = self.client.post("/api/nslcm/v1/ns", data=data)
82         self.assertEqual(response.data["error"], "nsd not exists.")
83         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
84         self.assertIn("error", response.data)
85
86     @mock.patch.object(restcall, 'call_req')
87     def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
88         mock_call_req.return_value = [1, "Failed to get nsd.", '500']
89         data = {
90             'csarId': '1',
91             'nsName': 'ns',
92             'nsDescription': 'description'
93         }
94         response = self.client.post("/api/nslcm/v1/ns", data=data)
95         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
96         self.assertIn("error", response.data)
97
98     @mock.patch.object(restcall, 'call_req')
99     def test_create_ns_when_ns_name_exist(self, mock_call_req):
100         nspackage_info = json.JSONEncoder().encode({
101             "csarId": self.ns_package_id,
102             "packageInfo": {}
103         })
104         mock_call_req.return_value = [0, nspackage_info, '200']
105         NSInstModel(id="1", name="ns1").save()
106         data = {
107             'csarId': '1',
108             'nsName': 'ns1',
109             'nsDescription': 'description'
110         }
111         response = self.client.post("/api/nslcm/v1/ns", data=data)
112         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
113         self.assertIn("error", response.data)