aboutsummaryrefslogtreecommitdiffstats
path: root/src/consul_interface_tests.erl
blob: cefa1fe33ded5412c8c059726c06ec25e3f3d9a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
% ============LICENSE_START=======================================================
% org.onap.dcae
% ================================================================================
% Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
% ================================================================================
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%      http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
% ============LICENSE_END=========================================================
%
% ECOMP is a trademark and service mark of AT&T Intellectual Property.

-module(consul_interface_tests).
-include_lib("eunit/include/eunit.hrl").

-import(consul_interface, [
          consul_get_service/3,
          consul_read_kv/3,
          consul_get_configuration/3,
          consul_get_preferences/3,
          consul_bind_config/3
        ]).

consul_bind_config_test() ->
    try meck:new(httpabs, [passthrough]) catch _:_ -> ok end,
    try meck:new(util, [passthrough]) catch _:_ -> ok end,
    FakeReturn = {[{<<"i am here">>, <<"my son">>}]},
    meck:expect(util, resolve_cbs, fun(_XER, _ConsulURL) -> "http://666.666.666.666:10000" end), %dont try to test the consul part here
    meck:expect(httpabs, get, fun(_XER, URL) -> case URL of
                                  "http://666.666.666.666:10000/service_component/salvationnotfound" -> {404, ""};
                                  "http://666.666.666.666:10000/service_component/salvation" -> {200, jiffy:encode(FakeReturn)}
                              end end),
    ?assert(consul_bind_config("", "salvation", "") == {200, FakeReturn}),
    ?assert(consul_bind_config("", "salvationnotfound", "") == {404, ""}),
    meck:unload(httpabs),
    meck:unload(util).

consul_read_kv_test() ->
    try meck:new(httpabs, [passthrough]) catch _:_ -> ok end,
    Val = base64:encode("salvation"),
    FakeReturn = jiffy:encode([{[{<<"Value">>, Val}]}]),
    meck:expect(httpabs, get, fun(_XER, URL) -> case URL of
                                  "http://666.666.666.666:666/v1/kv/salvationnotfound" -> {404, ""};
                                  "http://666.666.666.666:666/v1/kv/salvation" -> {200, FakeReturn}
                              end end),
    ?assert(consul_read_kv("", <<"salvation">>, "http://666.666.666.666:666") == {200, <<"salvation">>}),
    ?assert(consul_read_kv("", <<"salvationnotfound">>, "http://666.666.666.666:666") == {404, ""}),
    meck:unload(httpabs).

consul_get_configuration_preferences_test() ->
    try meck:new(httpabs, [passthrough]) catch _:_ -> ok end,
    Val = base64:encode(jiffy:encode({[{<<"iam">>, <<"thebestestjson">>}]})),
    FakeReturn = jiffy:encode([{[{<<"Value">>, Val}]}]),
    meck:expect(httpabs, get, fun(_XER, URL) -> case URL of
                                  "http://666.666.666.666:666/v1/kv/salvationnotfound" -> {404, ""};
                                  "http://666.666.666.666:666/v1/kv/salvation" -> {200, FakeReturn};
                                  "http://666.666.666.666:666/v1/kv/salvation:preferences" -> {200, FakeReturn};
                                  "http://666.666.666.666:666/v1/kv/salvationnotfound:preferences" -> {404, ""}
                              end end),
    ?assert(consul_get_configuration("", <<"salvation">>, "http://666.666.666.666:666") == {200, #{<<"iam">>=><<"thebestestjson">>}}),
    ?assert(consul_get_configuration("", <<"salvationnotfound">>, "http://666.666.666.666:666") == {404, ""}),
    ?assert(consul_get_preferences("", <<"salvation">>, "http://666.666.666.666:666") == {200, #{<<"iam">>=><<"thebestestjson">>}}),
    ?assert(consul_get_preferences("", <<"salvationnotfound">>, "http://666.666.666.666:666") == {404, ""}),

     meck:unload(httpabs).

consul_get_service_test() ->
    try meck:new(httpabs, [passthrough]) catch _:_ -> ok end,
    FakeConfig = jiffy:encode({[{<<"your key">>, <<"sir">>}]}),
    meck:expect(httpabs, get, fun(_XER, URL) -> case URL of
                                  "http://666.666.666.666:666/v1/catalog/service/salvationnotfound" -> {404, ""};
                                  "http://666.666.666.666:666/v1/catalog/service/salvation" -> {200, FakeConfig}
                              end end),
    ?assert(consul_get_service("", <<"salvation">>, "http://666.666.666.666:666") == #{<<"your key">> => <<"sir">>}),
    ?assertException(error, {badmatch,{404,[]}}, consul_get_service("", <<"salvationnotfound">>, "http://666.666.666.666:666")),
    meck:unload(httpabs).