Rm resmgr.grant releated contents
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / tests / test_sfcdetailview.py
1 # Copyright 2016 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 from lcm.pub.msapi import extsys
17 import mock
18 from django.test import TestCase, Client
19 from rest_framework import status
20 from lcm.pub.msapi import sdncdriver
21 from lcm.pub.database.models import FPInstModel
22
23
24 class TestSfcDetailViews(TestCase):
25     def setUp(self):
26         self.client = Client()
27         self.ns_inst_id = str(uuid.uuid4())
28         self.sfc_inst_id = str(uuid.uuid4())
29         self.status = "active"
30         self.sdn_controler_id = str(uuid.uuid4())
31
32     def tearDown(self):
33         pass
34
35     def test_sfc_delete_failed(self):
36         response = self.client.delete("/api/nslcm/v1/ns/sfcs/%s" % "notExist")
37         expect_resp_data = {"result": 0, "detail": "sfc is not exist or has been already deleted"}
38         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
39         self.assertEqual(expect_resp_data, response.data)
40
41     @mock.patch.object(extsys, "get_sdn_controller_by_id")
42     @mock.patch.object(sdncdriver, "delete_port_chain")
43     @mock.patch.object(sdncdriver, "delete_flow_classifier")
44     @mock.patch.object(sdncdriver, "delete_port_pair_group")
45     @mock.patch.object(sdncdriver, "delete_port_pair")
46     def test_sfc_delete_success(self, mock_delete_port_pair, mock_delete_port_pair_group, mock_delete_flow_classifier, mock_delete_port_chain, mock_get_sdn_controller_by_id):
47         mock_delete_port_chain.return_value = None
48         mock_delete_flow_classifier.return_value = None
49         mock_delete_port_pair_group.return_value = None
50         mock_delete_port_pair.return_value = None
51         mock_get_sdn_controller_by_id.return_value = json.loads('{"test":"test_name","url":"url_add"}')
52         sfc_inst_id = "10"
53
54         FPInstModel(fpid="1", fpinstid="10", fpname="2", nsinstid="3", vnffginstid="4",
55                     symmetric="5", policyinfo="6", forworderpaths="7", status="8", sdncontrollerid="9",
56                     sfcid="10", flowclassifiers="11",
57                     portpairgroups=json.JSONEncoder().encode([{"groupid": "98", "portpair": "99"}])
58                     ).save()
59         response = self.client.delete("/api/nslcm/v1/ns/sfcs/%s" % sfc_inst_id)
60         expect_resp_data = {"result": 0, "detail": "delete sfc success"}
61         self.assertEqual(expect_resp_data, response.data)
62
63     def test_sfc_get_failed(self):
64         sfc_inst_id = "10"
65         response = self.client.get("/api/nslcm/v1/ns/sfcs/%s" % sfc_inst_id)
66         self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code)
67
68     def test_sfc_get_success(self):
69         sfc_inst_id = "10"
70         FPInstModel(fpid="1", fpinstid="10", fpname="2", nsinstid="3", vnffginstid="4",
71                     symmetric="5", policyinfo="6", forworderpaths="7", status="8", sdncontrollerid="9",
72                     sfcid="10", flowclassifiers="11",
73                     portpairgroups="12").save()
74         response = self.client.get("/api/nslcm/v1/ns/sfcs/%s" % sfc_inst_id)
75         expect_resp_data = {'sfcName': 'xxx', 'sfcInstId': '10', 'sfcStatus': '8'}
76         self.assertEqual(expect_resp_data, response.data)