Refactor codes for create vnf ut
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_create_vnf.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
15 import json
16 import uuid
17 import mock
18
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
22
23 from .const import vnfpackage_info
24 from lcm.pub.database.models import NfInstModel
25 from lcm.pub.database.models import JobStatusModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.timeutil import now_time
28
29
30 class TestNFInstantiate(TestCase):
31     def setUp(self):
32         self.client = APIClient()
33
34         self.grant_result = {
35             "vimid": 'vimid_1',
36             "tenant": 'tenantname_1'
37         }
38
39     def tearDown(self):
40         pass
41
42     def assert_job_result(self, job_id, job_progress, job_detail):
43         jobs = JobStatusModel.objects.filter(
44             jobid=job_id,
45             progress=job_progress,
46             descp=job_detail
47         )
48         self.assertEqual(1, len(jobs))
49
50     def test_create_vnf_identifier_when_vnf_is_exist(self):
51         NfInstModel.objects.create(
52             nfinstid='1111',
53             nf_name='vFW_01',
54             package_id='222',
55             version='',
56             vendor='',
57             netype='',
58             vnfd_model='',
59             status='NOT_INSTANTIATED',
60             nf_desc='vFW in Nanjing TIC Edge',
61             vnfdid='111',
62             create_time=now_time()
63         )
64         data = {
65             "vnfdId": "111",
66             "vnfInstanceName": "vFW_01",
67             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
68         }
69         response = self.client.post(
70             "/api/vnflcm/v1/vnf_instances",
71             data=data,
72             format='json'
73         )
74         self.failUnlessEqual(
75             status.HTTP_500_INTERNAL_SERVER_ERROR,
76             response.status_code
77         )
78         context = json.loads(response.content)
79         self.assertEqual({
80             'detail': 'VNF is already exist.',
81             'status': 500
82         }, context)
83
84     @mock.patch.object(restcall, 'call_req')
85     @mock.patch.object(uuid, 'uuid4')
86     def test_create_vnf_identifier(self, mock_uuid4, mock_call_req):
87         r2_get_vnfpackage_from_catalog = [
88             0,
89             json.JSONEncoder().encode(vnfpackage_info),
90             '200'
91         ]
92         mock_call_req.return_value = r2_get_vnfpackage_from_catalog
93         mock_uuid4.return_value = "1"
94         data = {
95             "vnfdId": "111",
96             "vnfInstanceName": "vFW_01",
97             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
98         }
99         response = self.client.post(
100             "/api/vnflcm/v1/vnf_instances",
101             data=data,
102             format='json'
103         )
104         expect_data = {
105             "id": "1",
106             "vnfProvider": "huawei",
107             "vnfdVersion": "1.0",
108             "vnfPkgId": "111",
109             "instantiationState": "NOT_INSTANTIATED"
110         }
111         self.assertEqual(expect_data, response.data)