Adoption of base framework code for azure plugin
[multicloud/azure.git] / azure / azure / tests / test_restcall.py
1 # Copyright (c) 2018 Amdocs
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 import urllib2
14
15 from azure.pub.utils import restcall
16
17
18 class TestRestCall(unittest.TestCase):
19
20     def test_combine_url(self):
21         url = ["http://a.com/test/", "http://a.com/test/",
22                "http://a.com/test", "http://a.com/test"]
23         res = ["/resource", "resource", "/resource", "resource"]
24         expected = "http://a.com/test/resource"
25         for i in range(len(url)):
26             self.assertEqual(expected, restcall.combine_url(url[i], res[i]))
27
28     @mock.patch.object(restcall, "call_req")
29     def test_get_res_from_aai(self, mock_call):
30         res = "cloud-regions"
31         content = ""
32         expect_url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v13"
33         expect_user = "AAI"
34         expect_pass = "AAI"
35         expect_headers = {
36             'X-FromAppId': 'MultiCloud',
37             'X-TransactionId': '9001',
38             'content-type': 'application/json',
39             'accept': 'application/json'
40         }
41         restcall.get_res_from_aai(res, content=content)
42         mock_call.assert_called_once_with(
43             expect_url, expect_user, expect_pass, restcall.rest_no_auth,
44             res, "GET", content, expect_headers)
45
46     @mock.patch.object(restcall, "call_req")
47     def test_req_by_msb(self, mock_call):
48         res = "multicloud"
49         method = "GET"
50         content = "no content"
51         restcall.req_by_msb(res, method, content=content)
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)
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
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)
68
69     @mock.patch("httplib2.Http.request")
70     def test_call_req_not_200(self, mock_req):
71         mock_resp = {
72             "status": "404"
73         }
74         resp_content = "hello"
75         mock_req.return_value = mock_resp, resp_content
76         expect_ret = [1, resp_content, "404", mock_resp]
77         ret = restcall.call_req("http://onap.org/", "user", "pass",
78                                 restcall.rest_no_auth, "vim", "GET")
79         self.assertEqual(expect_ret, ret)
80
81     @mock.patch("traceback.format_exc")
82     @mock.patch("sys.exc_info")
83     @mock.patch("httplib2.Http.request")
84     def test_call_req_response_not_ready(self, mock_req, mock_sys,
85                                          mock_traceback):
86         mock_sys.return_value = "httplib.ResponseNotReady"
87         mock_req.side_effect = [Exception("httplib.ResponseNotReady")] * 3
88         expect_ret = [1, "Unable to connect to http://onap.org/vim", "", ""]
89         ret = restcall.call_req("http://onap.org/", "user", "pass",
90                                 restcall.rest_no_auth, "vim", "GET")
91         self.assertEqual(expect_ret, ret)
92         self.assertEqual(3, mock_req.call_count)
93
94     @mock.patch("httplib2.Http.request")
95     def test_call_req_url_err(self, mock_req):
96         urlerr = urllib2.URLError("urlerror")
97         mock_req.side_effect = [urlerr]
98         expect_ret = [2, str(urlerr), "", ""]
99         ret = restcall.call_req("http://onap.org/", "user", "pass",
100                                 restcall.rest_no_auth, "vim", "GET")
101         self.assertEqual(expect_ret, ret)