Fix restcall bug of decode http response content
[multicloud/framework.git] / multivimbroker / multivimbroker / tests / test_restcall.py
1 # Copyright (c) 2017-2018 VMware, Inc.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
11 import mock
12 import unittest
13
14 from multivimbroker.pub.utils import restcall
15
16
17 class TestRestCall(unittest.TestCase):
18
19     def test_combine_url(self):
20         url = ["http://a.com/test/", "http://a.com/test/",
21                "http://a.com/test", "http://a.com/test"]
22         res = ["/resource", "resource", "/resource", "resource"]
23         expected = "http://a.com/test/resource"
24         for i in range(len(url)):
25             self.assertEqual(expected, restcall.combine_url(url[i], res[i]))
26
27     @mock.patch.object(restcall, "call_req")
28     def test_get_res_from_aai(self, mock_call):
29         res = "cloud-regions"
30         content = ""
31         expect_url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v13"
32         expect_user = "AAI"
33         expect_pass = "AAI"
34         expect_headers = {
35             'X-FromAppId': 'MultiCloud',
36             'X-TransactionId': '9001',
37             'content-type': 'application/json',
38             'accept': 'application/json'
39         }
40         restcall.get_res_from_aai(res, content=content)
41         mock_call.assert_called_once_with(
42             expect_url, expect_user, expect_pass, restcall.rest_no_auth,
43             res, "GET", content, expect_headers)
44
45     @mock.patch.object(restcall, "call_req")
46     def test_req_by_msb(self, mock_call):
47         res = "multicloud"
48         method = "GET"
49         content = "no content"
50         headers = None
51         restcall.req_by_msb(res, method, content=content, headers=headers)
52         expect_url = "http://msb.onap.org:10080/"
53         mock_call.assert_called_once_with(
54             expect_url, "", "", restcall.rest_no_auth, res, method,
55             content, headers)
56
57     @mock.patch("httplib2.Http.request")
58     def test_call_req_success(self, mock_req):
59         mock_resp = {
60             "status": "200"
61         }
62         resp_content = "hello"
63         mock_req.return_value = mock_resp, resp_content.encode("utf-8")
64         expect_ret = [0, resp_content, "200", mock_resp]
65         ret = restcall.call_req("http://onap.org/", "user", "pass",
66                                 restcall.rest_no_auth, "vim", "GET")
67         self.assertEqual(expect_ret, ret)