Merge "move json from code to indepandent file"
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_create.py
1 # Copyright 2017 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 mock
16 import os
17 import uuid
18
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
22
23 from lcm.ns.biz.ns_create import CreateNSService
24 from lcm.pub.database.models import NSInstModel
25 from lcm.pub.utils import restcall, fileutil
26
27
28 class TestNsInstantiate(TestCase):
29     def setUp(self):
30         self.client = APIClient()
31         self.nsd_id = str(uuid.uuid4())
32         self.ns_package_id = str(uuid.uuid4())
33         self.cur_path = os.path.dirname(os.path.abspath(__file__))
34         self.create_ns_json = fileutil.read_json_file(self.cur_path + '/data/create_ns.json')
35
36     def tearDown(self):
37         NSInstModel.objects.all().delete()
38
39     @mock.patch.object(restcall, 'call_req')
40     def test_create_ns(self, mock_call_req):
41         nspackage_info = {
42             "csarId": self.ns_package_id,
43             "packageInfo": {}
44         }
45         r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201']
46         r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
47         mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai]
48         self.create_ns_json["csarId"] = self.nsd_id
49         response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json')
50         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
51
52     @mock.patch.object(CreateNSService, "do_biz")
53     def test_create_ns_empty_data(self, mock_do_biz):
54         mock_do_biz.side_effect = Exception("Exception in CreateNS.")
55         response = self.client.post("/api/nslcm/v1/ns", data={})
56         self.assertEqual(response.data["error"], "Exception in CreateNS.")
57         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
58         self.assertIn("error", response.data)
59
60     @mock.patch.object(CreateNSService, "do_biz")
61     def test_create_ns_non_existing_nsd(self, mock_do_biz):
62         mock_do_biz.side_effect = Exception("nsd not exists.")
63         self.create_ns_json["csarId"] = "1"
64         response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json')
65         self.assertEqual(response.data["error"], "nsd not exists.")
66         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
67         self.assertIn("error", response.data)
68
69     @mock.patch.object(restcall, 'call_req')
70     def test_create_ns_when_fail_to_get_nsd(self, mock_call_req):
71         mock_call_req.return_value = [1, "Failed to get nsd.", '500']
72         self.create_ns_json["csarId"] = "1"
73         response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json')
74         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
75         self.assertIn("error", response.data)
76
77     @mock.patch.object(restcall, 'call_req')
78     def test_create_ns_when_ns_name_exist(self, mock_call_req):
79         nspackage_info = json.JSONEncoder().encode({
80             "csarId": self.ns_package_id,
81             "packageInfo": {}
82         })
83         mock_call_req.return_value = [0, nspackage_info, '200']
84         NSInstModel(id="1", name="ns").save()
85         self.create_ns_json["csarId"] = "1"
86         response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json')
87         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
88         self.assertIn("error", response.data)