3767242f130556e40ea319f83154d5f168218c02
[dcaegen2/platform.git] / mod / distributorapi / tests / test_registry_client.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019 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 import distributor
17 from distributor import registry_client as rc
18
19
20 def test_add_url_from_link():
21     test = {"link": {"href": "bar"}, "name": "jane", "age": 33,
22             "innerTest": {"link": {"href": "baz"}, "name": "bob"}
23             }
24     result = rc._add_url_from_link("http://foo", test)
25
26     assert result["selfUrl"] == "http://foo/bar"
27     assert result["innerTest"]["selfUrl"] == "http://foo/baz"
28
29
30 def test_get_buckets(monkeypatch):
31     def fake_get_json(url):
32         if url == "http://registry/buckets":
33             return []
34         return None
35
36     monkeypatch.setattr(distributor.registry_client, "_get_json", fake_get_json)
37
38     assert [] == rc.get_buckets("http://registry")
39     assert [] == rc.get_buckets("http://registry/")
40
41
42 def test_find_flow(monkeypatch):
43     def fake_get_buckets(url):
44         return [{"selfUrl": "{0}/buckets/123".format(url)}]
45
46     monkeypatch.setattr(distributor.registry_client, "get_buckets", fake_get_buckets)
47
48     def fake_get_flows(registry_url, url):
49         if url == "http://registry/buckets/123":
50             return [{"identifier": "abc"}]
51         return None
52
53     monkeypatch.setattr(distributor.registry_client, "get_flows", fake_get_flows)
54
55     assert rc.find_flow("http://registry", "abc")["identifier"] == "abc"
56
57
58 def test_flow_versions(monkeypatch):
59     def fake_get_json_many(url):
60         if url == "http://registry/buckets/123/flows/abc/versions":
61             return [{"version": 1}, {"version": 3}, {"version": 2}]
62         print(url)
63         return []
64
65     monkeypatch.setattr(distributor.registry_client, "_get_json",
66             fake_get_json_many)
67
68     assert [3, 2, 1] == rc.get_flow_versions("http://registry/buckets/123/flows/abc/")
69
70
71 def test_get_flow_diff_latest(monkeypatch):
72     def fake_get_flow_versions(url):
73         return ["1"]
74
75     monkeypatch.setattr(distributor.registry_client, "get_flow_versions",
76             fake_get_flow_versions)
77
78     assert None == rc.get_flow_diff_latest("http://registry", "http://registry/buckets/123/flows/abc/")