Fix vfc-lcm/ns pep8 issue
[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
21 from lcm.ns.ns_create import CreateNSService
22 from lcm.pub.database.models import NSInstModel
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.utils import restcall
25
26
27 class TestNsInstantiate(TestCase):
28     def setUp(self):
29         self.client = Client()
30         self.nsd_id = str(uuid.uuid4())
31         self.ns_package_id = str(uuid.uuid4())
32
33     def tearDown(self):
34         NSInstModel.objects.all().delete()
35
36     @mock.patch.object(restcall, 'call_req')
37     def test_create_ns(self, mock_call_req):
38         nspackage_info = {
39             "csarId": self.ns_package_id,
40             "packageInfo": {}
41         }
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         data = {
46             'nsdid': self.nsd_id,
47             'nsname': 'ns',
48             'description': 'description'}
49         response = self.client.post("/api/nslcm/v1/ns", data=data)
50         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
51
52     @mock.patch.object(CreateNSService, "do_biz")
53     def test_create_ns_empty_data(self, mock_do_biz):
54         mock_do_biz.side_effect = Exception("Exception in CreateNS.")
55
56         data = {}
57
58         response = self.client.post("/api/nslcm/v1/ns", data=data)
59         self.assertEqual(response.data["error"], "Exception in CreateNS.")
60         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
61         self.assertIn("error", response.data)
62
63     @mock.patch.object(CreateNSService, "do_biz")
64     def test_create_ns_non_existing_nsd(self, mock_do_biz):
65         mock_do_biz.side_effect = NSLCMException("nsd not exists.")
66         new_nsd_id = '1'
67
68         data = {
69             'nsdid': new_nsd_id,
70             'nsname': 'ns',
71             'description': 'description'}
72
73         response = self.client.post("/api/nslcm/v1/ns", data=data)
74         self.assertEqual(response.data["error"], "nsd not exists.")
75         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
76         self.assertIn("error", response.data)