Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_instances_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
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.pub.database.models import NSInstModel
22 from lcm.pub.utils import restcall
23 from lcm.ns.biz.ns_create import CreateNSService
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.ns.tests import SOL_CREATE_NS_DICT, SOL_REST_HEADER_DICT, NS_PACKAGE_INFO_DICT, NS_INFO_AAI_DICT
26
27
28 class TestNsInstanceApi(TestCase):
29
30     def setUp(self):
31         self.apiClient = APIClient()
32         self.format = 'json'
33         self.ns_instances_url = '/api/nslcm/v1/ns_instances'
34         self.nsd_id = "c9f0a95e-dea0-4698-96e5-5a79bc5a233d"
35         self.ns_package_id = "c9f0a95e-dea0-4698-96e5-5a79bc5a233d"
36
37     def tearDown(self):
38         NSInstModel.objects.all().delete()
39
40     @mock.patch.object(restcall, 'call_req')
41     def test_create_ns(self, mock_call_req):
42         r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(NS_PACKAGE_INFO_DICT), '201']
43         r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
44         mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
45         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
46         self.assertEqual(status.HTTP_201_CREATED, response.status_code)
47         return response.data['id']
48
49     @mock.patch.object(restcall, 'call_req')
50     def test_create_ns_cpe(self, mock_call_req):
51         r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(NS_PACKAGE_INFO_DICT), '201']
52         r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
53         mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
54         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
55         self.assertEqual(status.HTTP_201_CREATED, response.status_code)
56
57     @mock.patch.object(restcall, 'call_req')
58     def test_create_ns_when_ns_name_exist(self, mock_call_req):
59         NSInstModel.objects.all().delete()
60         NSInstModel(id="1", name="ns").save()
61         nspackage_info = json.JSONEncoder().encode(NS_PACKAGE_INFO_DICT)
62         mock_call_req.return_value = [0, nspackage_info, '200']
63         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
64         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
65
66     @mock.patch.object(CreateNSService, "do_biz")
67     def test_create_ns_empty_data(self, mock_do_biz):
68         mock_do_biz.side_effect = Exception("Exception in CreateNS.")
69         response = self.apiClient.post(self.ns_instances_url, data={}, format=self.format, **SOL_REST_HEADER_DICT)
70         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
71
72     @mock.patch.object(CreateNSService, "do_biz")
73     def test_create_ns_no_header(self, mock_do_biz):
74         mock_do_biz.side_effect = Exception("Exception in CreateNS.")
75         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format)
76         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
77
78     @mock.patch.object(CreateNSService, "do_biz")
79     def test_create_ns_non_existing_nsd(self, mock_do_biz):
80         mock_do_biz.side_effect = NSLCMException("nsd not exists.")
81         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
82         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
83
84     @mock.patch.object(restcall, 'call_req')
85     def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
86         mock_call_req.return_value = [1, "Failed to get nsd.", '500']
87         response = self.apiClient.post(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
88         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
89
90     def test_ns_instances_method_not_allowed(self):
91         response = self.apiClient.delete(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
92         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
93         response = self.apiClient.put(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
94         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
95         response = self.apiClient.patch(self.ns_instances_url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
96         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
97
98     def test_invidual_ns_instance_method_not_allowed(self):
99         url = self.ns_instances_url + '/1'
100         response = self.apiClient.post(url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
101         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
102         response = self.apiClient.put(url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
103         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
104         response = self.apiClient.patch(url, data=SOL_CREATE_NS_DICT, format=self.format, **SOL_REST_HEADER_DICT)
105         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
106
107     def test_query_ns(self):
108         NSInstModel.objects.all().delete()
109         self.test_create_ns()
110         response = self.apiClient.get(self.ns_instances_url)
111         self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
112         self.assertIsNotNone(response.data)
113         self.assertEqual(1, len(response.data))
114         self.assertEqual(self.nsd_id, response.data[0]['nsdId'])
115         self.assertEqual('ns', response.data[0]['nsInstanceName'])
116         self.assertEqual('NOT_INSTANTIATED', response.data[0]['nsState'])
117
118     def test_query_one_ns(self):
119         NSInstModel.objects.all().delete()
120         id = self.test_create_ns()
121         url = self.ns_instances_url + '/' + id
122         response = self.apiClient.get(url)
123         self.assertEqual(status.HTTP_200_OK, response.status_code, response.data)
124         self.assertIsNotNone(response.data)
125         self.assertEqual(self.nsd_id, response.data['nsdId'])
126         self.assertEqual('ns', response.data['nsInstanceName'])
127         self.assertEqual('NOT_INSTANTIATED', response.data['nsState'])
128
129     @mock.patch.object(restcall, 'call_req')
130     def test_delete_ns(self, mock_call_req):
131         NSInstModel(id="1", nspackage_id="7", nsd_id="2").save()
132         r1_query_ns_to_aai = [0, json.JSONEncoder().encode(NS_INFO_AAI_DICT), '200']
133         r2_delete_ns_to_aai = [0, json.JSONEncoder().encode({}), '200']
134         mock_call_req.side_effect = [r1_query_ns_to_aai, r2_delete_ns_to_aai]
135         url = self.ns_instances_url + '/1'
136         response = self.apiClient.delete(url)
137         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)