1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
22 from catalog.pub.database.models import NSPackageModel
23 from catalog.pub.config.config import CATALOG_ROOT_PATH
26 class TestNsDescriptor(TestCase):
28 self.client = APIClient()
29 self.user_defined_data = {
38 def test_nsd_create_normal(self):
40 'userDefinedData': self.user_defined_data
42 expected_reponse_data = {
43 'nsdOnboardingState': 'CREATED',
44 'nsdOperationalState': 'DISABLED',
45 'nsdUsageState': 'NOT_IN_USE',
46 'userDefinedData': self.user_defined_data,
49 response = self.client.post(
50 '/api/nsd/v1/ns_descriptors',
54 response.data.pop('id')
55 self.assertEqual(response.status_code, status.HTTP_201_CREATED)
56 self.assertEqual(expected_reponse_data, response.data)
58 def test_query_multiple_nsds_normal(self):
59 expected_reponse_data = [
66 'nsdInvariantId': None,
69 'nestedNsdInfoIds': [],
70 'nsdOnboardingState': 'CREATED',
71 'onboardingFailureDetails': None,
72 'nsdOperationalState': 'DISABLED',
73 'nsdUsageState': 'NOT_IN_USE',
74 'userDefinedData': self.user_defined_data,
83 'nsdInvariantId': None,
86 'nestedNsdInfoIds': [],
87 'nsdOnboardingState': 'CREATED',
88 'onboardingFailureDetails': None,
89 'nsdOperationalState': 'DISABLED',
90 'nsdUsageState': 'NOT_IN_USE',
91 'userDefinedData': self.user_defined_data,
95 user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
99 onboardingState='CREATED',
100 operationalState='DISABLED',
101 usageState='NOT_IN_USE',
102 userDefinedData=user_defined_data
104 response = self.client.get('/api/nsd/v1/ns_descriptors', format='json')
105 self.assertEqual(response.status_code, status.HTTP_200_OK)
106 self.assertEqual(expected_reponse_data, response.data)
108 def test_query_single_nsd_normal(self):
109 expected_reponse_data = {
115 'nsdInvariantId': None,
118 'nestedNsdInfoIds': [],
119 'nsdOnboardingState': 'CREATED',
120 'onboardingFailureDetails': None,
121 'nsdOperationalState': 'DISABLED',
122 'nsdUsageState': 'NOT_IN_USE',
123 'userDefinedData': self.user_defined_data,
126 user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
129 onboardingState='CREATED',
130 operationalState='DISABLED',
131 usageState='NOT_IN_USE',
132 userDefinedData=user_defined_data
135 response = self.client.get('/api/nsd/v1/ns_descriptors/22', format='json')
136 self.assertEqual(response.status_code, status.HTTP_200_OK)
137 self.assertEqual(expected_reponse_data, response.data)
139 def test_delete_single_nsd_normal(self):
140 user_defined_data = json.JSONEncoder().encode(self.user_defined_data)
143 operationalState='DISABLED',
144 usageState='NOT_IN_USE',
145 userDefinedData=user_defined_data,
148 resp = self.client.delete("/api/nsd/v1/ns_descriptors/21", format='json')
149 self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
150 self.assertEqual({}, resp.data)
152 def test_nsd_content_upload_normal(self):
153 user_defined_data_json = json.JSONEncoder().encode(self.user_defined_data)
156 operationalState='DISABLED',
157 usageState='NOT_IN_USE',
158 userDefinedData=user_defined_data_json,
160 with open('nsd_content.txt', 'wb') as fp:
162 with open('nsd_content.txt', 'rb') as fp:
163 resp = self.client.put(
164 "/api/nsd/v1/ns_descriptors/22/nsd_content",
168 with open(os.path.join(CATALOG_ROOT_PATH, '22/nsd_content.txt')) as fp:
170 file_content = '%s%s' % (file_content, data)
171 self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
172 self.assertEqual({}, resp.data)
173 self.assertEqual(file_content, 'test')
174 os.remove('nsd_content.txt')
176 def test_nsd_content_upload_failure(self):
179 def test_nsd_content_download_normal(self):
182 def test_nsd_content_partial_download_normal(self):
183 with open('nsd_content.txt', 'wb') as fp:
184 fp.writelines('test1')
185 fp.writelines('test2')
188 onboardingState='ONBOARDED',
189 localFilePath='nsd_content.txt'
191 response = self.client.get(
192 "/api/nsd/v1/ns_descriptors/23/nsd_content",
196 partial_file_content = ''
197 for data in response.streaming_content:
198 partial_file_content = '%s%s' % (partial_file_content, data)
199 self.assertEqual(response.status_code, status.HTTP_200_OK)
200 self.assertEqual('test2', partial_file_content)
201 os.remove('nsd_content.txt')