978d5f6a1541b09e79467577b782aded578c76ae
[vfc/nfvo/lcm.git] / lcm / ns_sfcs / tests / test_create_port_chain.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 mock
15 import json
16 from test_data import nsd_model
17 from rest_framework import status
18 from lcm.pub.utils import restcall
19 from lcm.pub.database.models import FPInstModel
20 from django.test import Client
21 from django.test import TestCase
22
23
24 class TestSfc(TestCase):
25     def setUp(self):
26         self.client = Client()
27         FPInstModel.objects.all().delete()
28         FPInstModel(fpinstid="fp_inst_1", sdncontrollerid="test_sdncontrollerid",
29                     symmetric=1, flowclassifiers="test_flowclassifiers",
30                     portpairgroups=json.JSONEncoder().encode([{"groupid": "1"}])).save()
31
32     def tearDown(self):
33         FPInstModel.objects.all().delete()
34
35     @mock.patch.object(restcall, 'call_req')
36     def test_create_port_chain_success(self, mock_call_req):
37         data = {
38             "fpinstid": "fp_inst_1",
39             "context": json.dumps(nsd_model)
40         }
41         mock_vals = {
42             "/external-system/esr-thirdparty-sdnc-list/esr-thirdparty-sdnc/test_sdncontrollerid?depth=all":
43                 [0, json.JSONEncoder().encode({
44                     "thirdparty-sdnc-id": "1",
45                     "esr-system-info-list": {
46                         "esr-system-info": [{
47                             "service-url": "url_1",
48                             "thirdparty-sdnc-id": "1",
49                             "user-name": "aa",
50                             "password": "123",
51                             "vendor": "zte",
52                             "version": "v1.0",
53                             "protocal": "http",
54                             "product-name": "bbb",
55                             "type": "11"
56                         }]
57                     }
58                 }), '200'],
59             "/api/ztesdncdriver/v1/createportchain":
60                 [0, json.JSONEncoder().encode({"id": "test_id_1"}), '200'],
61             "/api/microservices/v1/services":
62                 [0, None, '200']
63         }
64
65         def side_effect(*args):
66             return mock_vals[args[4]]
67
68         mock_call_req.side_effect = side_effect
69         resp = self.client.post("/api/nslcm/v1/ns/create_port_chain", data)
70         ret = FPInstModel.objects.get(fpinstid="fp_inst_1")
71         self.assertEqual(resp.status_code, status.HTTP_200_OK)
72         self.assertEqual("test_id_1", ret.sfcid)