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