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