ADD UT Issue-ID: VFC-1429 Signed-off-by: zhuerlei <zhu.erlei@zte.com.cn>
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_inst_ns_post_deal_view.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
17 from django.test import TestCase
18 from lcm.pub.database.models import NSInstModel, ServiceBaseInfoModel
19 from lcm.pub.utils import restcall
20 from rest_framework import status
21 from rest_framework.test import APIClient
22 from lcm.ns.tests import NSD_MODEL_DICT_INST_NS_POST_DEAL_VIEW
23
24
25 class NSInstPostDealView(TestCase):
26     def setUp(self):
27         self.client = APIClient()
28         self.url = "/api/nslcm/v1/ns/test_ns_instance_id/postdeal"
29         self.data = {"status": "true"}
30         NSInstModel(id="test_ns_instance_id", name="test_ns_instance_name", nsd_id="test_nsd_id",
31                     nsd_invariant_id="test_nsd_invariant_id",
32                     nsd_model=json.dumps(NSD_MODEL_DICT_INST_NS_POST_DEAL_VIEW)).save()
33         ServiceBaseInfoModel(service_id="test_ns_instance_id", service_name="service_name", service_type="service_type",
34                              active_status="active_status", status="status", creator="creator", create_time="0").save()
35
36     def tearDown(self):
37         NSInstModel.objects.all().delete()
38
39     @mock.patch.object(restcall, 'call_req')
40     def test_inst_ns_post_deal_view(self, mock_call_req):
41         ret = {"status": 1}
42         mock_vals = {
43             "api/polengine/v1/policyinfo":
44                 [0, json.JSONEncoder().encode(ret), "200"],
45         }
46
47         def side_effect(*args):
48             return mock_vals[args[4]]
49
50         mock_call_req.side_effect = side_effect
51
52         response = self.client.post(self.url, data=self.data, format='json')
53         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)