Merge "add NS create test usecase"
[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 import json
15 import uuid
16
17 import mock
18 from django.test import TestCase, Client
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
24
25 class TestNsInstanceApi(TestCase):
26     def setUp(self):
27         self.client = Client()
28         self.apiClient = APIClient()
29         self.format = 'json'
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())
33
34     def tearDown(self):
35         NSInstModel.objects.all().delete()
36
37     @mock.patch.object(restcall, 'call_req')
38     def test_create_ns(self, mock_call_req):
39         nspackage_info = {
40             "csarId": self.ns_package_id,
41             "packageInfo": {
42                 "nsPackageId": self.ns_package_id,
43                 "nsdId": self.nsd_id
44             }
45         }
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]
49
50         header = {
51             'HTTP_GLOBALCUSTOMERID': 'global-customer-id-test1',
52             'HTTP_SERVICETYPE': 'service-type-test1'
53         }
54
55         data = {
56             "nsdId": self.nsd_id,
57             "nsName": "ns",
58             "nsDescription": "description"
59         }
60         response = self.apiClient.post(self.ns_instances_url, data=data, format=self.format, **header)
61
62         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)