1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# Copyright 2017 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import uuid
import mock
from django.test import TestCase, Client
from rest_framework import status
from lcm.ns.ns_create import CreateNSService
from lcm.pub.exceptions import NSLCMException
from lcm.pub.database.models import NSInstModel, NSDModel
from lcm.pub.utils import restcall
class TestNsInstantiate(TestCase):
def setUp(self):
self.client = Client()
self.nsd_id = str(uuid.uuid4())
self.ns_package_id = str(uuid.uuid4())
NSDModel(id=self.ns_package_id, nsd_id=self.nsd_id, name='name').save()
def tearDown(self):
NSDModel.objects.all().delete()
NSInstModel.objects.all().delete()
@mock.patch.object(restcall, 'call_req')
def test_create_ns(self, mock_call_req):
r1_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
mock_call_req.side_effect = [r1_create_ns_to_aai]
data = {
'nsdid': self.nsd_id,
'nsname': 'ns',
'description': 'description'}
response = self.client.post("/api/nslcm/v1/ns", data=data)
self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
@mock.patch.object(CreateNSService, "do_biz")
def test_create_ns_empty_data(self, mock_do_biz):
mock_do_biz.side_effect = Exception("Exception in CreateNS.")
data = {}
response = self.client.post("/api/nslcm/v1/ns", data=data)
self.assertEqual(response.data["error"], "Exception in CreateNS.")
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
self.assertIn("error", response.data)
@mock.patch.object(CreateNSService, "do_biz")
def test_create_ns_non_existing_nsd(self, mock_do_biz):
mock_do_biz.side_effect = NSLCMException("nsd not exists.")
new_nsd_id = '1'
data = {
'nsdid': new_nsd_id,
'nsname': 'ns',
'description': 'description'}
response = self.client.post("/api/nslcm/v1/ns", data=data)
self.assertEqual(response.data["error"], "nsd not exists.")
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
self.assertIn("error", response.data)
|