1 # Copyright 2019 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 lcm.pub.database.models import NSInstModel
23 from lcm.pub.utils import restcall
26 class TestNsInstanceApi(TestCase):
28 self.apiClient = APIClient()
30 self.ns_instances_url = '/api/nslcm/v1/ns_instances'
31 self.nsd_id = str(uuid.uuid4())
32 self.ns_package_id = str(uuid.uuid4())
35 NSInstModel.objects.all().delete()
37 @mock.patch.object(restcall, 'call_req')
38 def test_create_ns(self, mock_call_req):
40 "csarId": self.ns_package_id,
42 "nsPackageId": self.ns_package_id,
46 r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201']
47 r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
48 mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
51 'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1',
52 'HTTP_SERVICETYPE': 'service-type-test1'
58 "nsDescription": "description"
60 response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header)
61 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
63 def test_query_ns(self):
64 NSInstModel.objects.all().delete()
66 response = self.apiClient.get(self.ns_instances_url)
67 self.failUnlessEqual(status.HTTP_200_OK, response.status_code, response.data)
68 self.assertIsNotNone(response.data)
69 self.assertEqual(1, len(response.data))
70 self.assertEquals(self.nsd_id, response.data[0]['nsdId'])
71 self.assertEquals('ns', response.data[0]['nsInstanceName'])
72 self.assertEquals('NOT_INSTANTIATED', response.data[0]['nsState'])
74 @mock.patch.object(restcall, 'call_req')
75 def test_delete_ns(self, mock_call_req):
76 NSInstModel(id="1", nspackage_id="7", nsd_id="2").save()
78 "service-instance-id": "service-instance-id-9b9348f2-f75d-4559-823d-db7ac138ed34",
79 "service-instance-name": "service-instance-name-9b9348f2-f75d-4559-823d-db7ac138ed34",
80 "service-type": "service-type-9b9348f2-f75d-4559-823d-db7ac138ed34",
81 "service-role": "service-role-9b9348f2-f75d-4559-823d-db7ac138ed34",
82 "resource-version": "1505350720009"
84 r1_query_ns_to_aai = [0, json.JSONEncoder().encode(ns_info), '200']
85 r2_delete_ns_to_aai = [0, json.JSONEncoder().encode({}), '200']
86 mock_call_req.side_effect = [r1_query_ns_to_aai, r2_delete_ns_to_aai]
87 url = self.ns_instances_url + '/1'
88 response = self.apiClient.delete(url)
89 self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)