SOL003 API ALign
[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
17 import mock
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
21 from lcm.nf.const import vnfpackage_info
22 from lcm.pub.database.models import NfInstModel, JobStatusModel
23 from lcm.pub.utils import restcall
24 from lcm.pub.utils.timeutil import now_time
25
26
27 class TestNFInstantiate(TestCase):
28     def setUp(self):
29         self.client = APIClient()
30         self.grant_result = {
31             "vimid": 'vimid_1',
32             "tenant": 'tenantname_1'
33         }
34
35     def tearDown(self):
36         pass
37
38     def assert_job_result(self, job_id, job_progress, job_detail):
39         jobs = JobStatusModel.objects.filter(jobid=job_id,
40                                              progress=job_progress,
41                                              descp=job_detail)
42         self.assertEqual(1, len(jobs))
43
44     def test_create_vnf_identifier_when_vnf_is_exist(self):
45         NfInstModel.objects.create(nfinstid='1111',
46                                    nf_name='vFW_01',
47                                    package_id='222',
48                                    version='',
49                                    vendor='',
50                                    netype='',
51                                    vnfd_model='',
52                                    status='NOT_INSTANTIATED',
53                                    nf_desc='vFW in Nanjing TIC Edge',
54                                    vnfdid='111',
55                                    create_time=now_time())
56         data = {
57             "vnfdId": "111",
58             "vnfInstanceName": "vFW_01",
59             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
60         }
61         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
62         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
63         context = json.loads(response.content)
64         self.assertEqual({'error': 'VNF is already exist.'}, context)
65
66     @mock.patch.object(restcall, 'call_req')
67     def test_create_vnf_identifier(self, mock_call_req):
68         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
69         mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
70         data = {
71             "vnfdId": "111",
72             "vnfInstanceName": "vFW_01",
73             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
74         }
75         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
76         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
77         context = json.loads(response.content)
78         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())