Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_terminate_api.py
1 # Copyright 2019 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 import mock
17 from django.test import TestCase, Client
18 from rest_framework import status
19 from lcm.ns.biz.ns_terminate import TerminateNsService
20 from lcm.pub.database.models import NfInstModel, NSInstModel
21 from lcm.ns.tests import VNFD_MODEL_DICT
22
23
24 class TestTerminateNsApi(TestCase):
25     def setUp(self):
26         self.client = Client()
27         self.url = "/api/nslcm/v1/ns_instances/%s/terminate"
28         self.ns_inst_id = str(uuid.uuid4())
29         self.nf_inst_id = '1'
30         self.vnffg_id = str(uuid.uuid4())
31         self.vim_id = str(uuid.uuid4())
32         self.job_id = str(uuid.uuid4())
33         self.nf_uuid = '1-1-1'
34         self.tenant = "tenantname"
35         NSInstModel(
36             id=self.ns_inst_id,
37             name="ns_name",
38             status='null').save()
39         NfInstModel.objects.create(
40             nfinstid=self.nf_inst_id,
41             nf_name='name_1',
42             vnf_id='1',
43             vnfm_inst_id='1',
44             ns_inst_id='1-1-1,2-2-2',
45             max_cpu='14',
46             max_ram='12296',
47             max_hd='101',
48             max_shd="20",
49             max_net=10,
50             status='null',
51             mnfinstid=self.nf_uuid,
52             package_id='pkg1',
53             vnfd_model=json.dumps(VNFD_MODEL_DICT))
54
55     def tearDown(self):
56         NSInstModel.objects.all().delete()
57         NfInstModel.objects.all().delete()
58
59     @mock.patch.object(TerminateNsService, 'run')
60     def test_terminate_vnf(self, mock_run):
61         mock_run.re.return_value = "1"
62         req_data = {"terminationTime": "2019-03-25T09:10:35.610"}
63         response = self.client.post(self.url % self.ns_inst_id, data=req_data)
64         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
65         self.assertIsNotNone(response['Location'])
66         response = self.client.get(response['Location'], format='json')
67         self.assertEqual(response.status_code, status.HTTP_200_OK)
68
69     def test_method_not_allowed(self):
70         response = self.client.put(self.url % '1', data={}, format='json')
71         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
72         response = self.client.patch(self.url % '1', data={}, format='json')
73         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
74         response = self.client.delete(self.url % '1', data={}, format='json')
75         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
76         response = self.client.get(self.url % '1', data={}, format='json')
77         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)