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
8 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
17 from distributor.http import _app as app
18 from distributor import config
23 def __init__(self, code, json = None):
24 self.status_code = code
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))
36 def __init__(self, op, url, resp):
41 def check(self, op, url):
42 if op != self.op or url != self.url:
46 def _match(answers, op, url):
47 for choice in answers:
48 ret = choice.check(op, url)
51 message = 'Unexpected request {} {}'.format(op, url)
53 raise Exception(message)
56 def mockrequests(monkeypatch):
58 def get(url, headers = None):
59 return _match(answers, 'GET', url)
61 def post(url, json, headers = None):
62 return _match(answers, 'POST', url)
64 def put(url, json, headers = None):
65 return _match(answers, 'PUT', url)
67 def delete(url, headers = None):
68 return _match(answers, 'DELETE', url)
70 monkeypatch.setattr(requests, 'get', get)
71 monkeypatch.setattr(requests, 'post', post)
72 monkeypatch.setattr(requests, 'put', put)
73 monkeypatch.setattr(requests, 'delete', delete)
78 app.config['TESTING'] = True
79 with app.test_client() as client:
84 def test_api(client, mockrequests):
85 dummyflow = {'link': {'href': 'buckets/link1/flows/flow1'}, 'name': 'flowname'}
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'}))
94 for rule in app.url_map.iter_rules():
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)