d7f543e87fd4daa8342fa8a39d5ddf12a8e0f67e
[dcaegen2/platform.git] / mod / distributorapi / tests / test_api.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16
17 from distributor.http import _app as app
18 from distributor import config
19 import pytest
20 import requests
21
22 class _resp(object):
23     def __init__(self, code, json = None):
24         self.status_code = code
25         if json is not None:
26             self._json = json
27     
28     def json(self):
29         return self._json
30
31     def raise_for_status(self):
32         if self.status_code < 200 or self.status_code >= 300:
33             raise Exception('Error response {}'.format(self.status_code))
34
35 class _req(object):
36     def __init__(self, op, url, resp):
37         self.op = op
38         self.url = url;
39         self.resp = resp
40
41     def check(self, op, url):
42         if op != self.op or url != self.url:
43             return None
44         return self.resp
45
46 def _match(answers, op, url):
47     for choice in answers:
48         ret = choice.check(op, url)
49         if ret is not None:
50             return ret
51     message = 'Unexpected request {} {}'.format(op, url)
52     print(message)
53     raise Exception(message)
54
55 @pytest.fixture
56 def mockrequests(monkeypatch):
57     answers = []
58     def get(url, headers = None):
59         return _match(answers, 'GET', url)
60     
61     def post(url, json, headers = None):
62         return _match(answers, 'POST', url)
63
64     def put(url, json, headers = None):
65         return _match(answers, 'PUT', url)
66
67     def delete(url, headers = None):
68         return _match(answers, 'DELETE', url)
69
70     monkeypatch.setattr(requests, 'get', get)
71     monkeypatch.setattr(requests, 'post', post)
72     monkeypatch.setattr(requests, 'put', put)
73     monkeypatch.setattr(requests, 'delete', delete)
74     return answers
75
76 @pytest.fixture
77 def client():
78     app.config['TESTING'] = True
79     with app.test_client() as client:
80         yield client
81
82 config.init()
83
84 def test_api(client, mockrequests):
85     dummyflow = {'link': {'href': 'buckets/link1/flows/flow1'}, 'name': 'flowname'}
86     mockrequests.extend([
87         _req('GET', 'http://nifi-registry:18080/nifi-registry-api/buckets',
88             _resp(200, [{'link': {'href':'buckets/link1'}}])),
89         _req('GET', 'http://nifi-registry:18080/nifi-registry-api/buckets/link1/flows',
90             _resp(200, [dummyflow])),
91         _req('POST', 'http://newtarget1/url/api/graph/main',
92             _resp(200, {'id':'group1'}))
93     ])
94     for rule in app.url_map.iter_rules():
95       print(rule)
96     url = '/distributor/distribution-targets'
97     url2 = url + '/notfound'
98     url3 = url2 + '/process-groups'
99     assert(len(client.get(url).get_json()['distributionTargets']) == 0)
100     assert(client.get(url2).status_code == 404)
101     assert(client.put(url2, json={'name': 'notfound1', 'runtimeApiUrl': 'http://notfound/url'}).status_code == 404)
102     assert(client.delete(url2).status_code == 404)
103     assert(client.post(url3, json={'processGroupId': 'group1'}).status_code == 404)
104     resp = client.post(url, json={'name': 'target1', 'runtimeApiUrl': 'http://target/url'})
105     assert(resp.status_code == 200)
106     print(resp.get_json())
107     url2 = '/distributor/distribution-targets/' + resp.get_json()['id']
108     url3 = url2 + '/process-groups'
109     assert(len(client.get(url).get_json()['distributionTargets']) == 1)
110     assert(client.get(url2).status_code == 200)
111     assert(client.put(url2, json={'name': 'newtarget1', 'runtimeApiUrl': 'http://newtarget1/url'}).status_code == 200)
112     assert(client.post(url3, json={'processGroupId': 'group1'}).status_code == 404)
113     dummyflow['identifier'] = 'group1'
114     assert(client.post(url3, json={'processGroupId': 'group1'}).status_code == 501)
115     assert(client.delete(url2).status_code == 200)
116     assert(client.delete(url2).status_code == 404)