|
|
@@ -18,6 +18,7 @@
|
|
|
-compile(export_all).
|
|
|
|
|
|
-include_lib("emqx/include/emqx.hrl").
|
|
|
+-include_lib("emqx/include/emqx_mqtt.hrl").
|
|
|
-include_lib("emqx/include/emqx_hooks.hrl").
|
|
|
-include_lib("stdlib/include/assert.hrl").
|
|
|
-include_lib("emqx/include/asserts.hrl").
|
|
|
@@ -75,6 +76,11 @@ init_per_testcase(TestCase, Config) ->
|
|
|
| Config
|
|
|
].
|
|
|
|
|
|
+end_per_testcase(_TestCase, _Config) ->
|
|
|
+ emqx_common_test_helpers:call_janitor(),
|
|
|
+ emqx_bridge_v2_testlib:delete_all_bridges_and_connectors(),
|
|
|
+ ok.
|
|
|
+
|
|
|
%%------------------------------------------------------------------------------
|
|
|
%% Helper fns
|
|
|
%%------------------------------------------------------------------------------
|
|
|
@@ -85,6 +91,7 @@ connector_config() ->
|
|
|
<<"enable">> => true,
|
|
|
<<"description">> => <<"my connector">>,
|
|
|
<<"pool_size">> => 3,
|
|
|
+ <<"proto_ver">> => <<"v5">>,
|
|
|
<<"server">> => <<"127.0.0.1:1883">>,
|
|
|
<<"resource_opts">> => #{
|
|
|
<<"health_check_interval">> => <<"15s">>,
|
|
|
@@ -105,13 +112,6 @@ source_config(Overrides0) ->
|
|
|
#{
|
|
|
<<"topic">> => <<"remote/topic">>,
|
|
|
<<"qos">> => 2
|
|
|
- },
|
|
|
- <<"local">> =>
|
|
|
- #{
|
|
|
- <<"topic">> => <<"local/topic">>,
|
|
|
- <<"qos">> => 2,
|
|
|
- <<"retain">> => false,
|
|
|
- <<"payload">> => <<"${payload}">>
|
|
|
}
|
|
|
},
|
|
|
<<"resource_opts">> => #{
|
|
|
@@ -134,6 +134,15 @@ source_config(Overrides0) ->
|
|
|
replace(Key, Value, Proplist) ->
|
|
|
lists:keyreplace(Key, 1, Proplist, {Key, Value}).
|
|
|
|
|
|
+bridge_id(Config) ->
|
|
|
+ Type = ?config(source_type, Config),
|
|
|
+ Name = ?config(source_name, Config),
|
|
|
+ emqx_bridge_resource:bridge_id(Type, Name).
|
|
|
+
|
|
|
+hookpoint(Config) ->
|
|
|
+ BridgeId = bridge_id(Config),
|
|
|
+ emqx_bridge_resource:bridge_hookpoint(BridgeId).
|
|
|
+
|
|
|
%%------------------------------------------------------------------------------
|
|
|
%% Testcases
|
|
|
%%------------------------------------------------------------------------------
|
|
|
@@ -151,6 +160,11 @@ t_create_via_http(Config) ->
|
|
|
]}},
|
|
|
emqx_bridge_v2_testlib:list_bridges_http_api_v1()
|
|
|
),
|
|
|
+ ?assertMatch(
|
|
|
+ {ok, {{_, 200, _}, _, [#{<<"enable">> := true}]}},
|
|
|
+ emqx_bridge_v2_testlib:list_connectors_http_api()
|
|
|
+ ),
|
|
|
+
|
|
|
NewSourceName = <<"my_other_source">>,
|
|
|
{ok, {{_, 201, _}, _, _}} =
|
|
|
emqx_bridge_v2_testlib:create_kind_api(
|
|
|
@@ -173,3 +187,71 @@ t_create_via_http(Config) ->
|
|
|
t_start_stop(Config) ->
|
|
|
ok = emqx_bridge_v2_testlib:t_start_stop(Config, mqtt_connector_stopped),
|
|
|
ok.
|
|
|
+
|
|
|
+t_receive_via_rule(Config) ->
|
|
|
+ SourceConfig = ?config(source_config, Config),
|
|
|
+ ?check_trace(
|
|
|
+ begin
|
|
|
+ {ok, {{_, 201, _}, _, _}} = emqx_bridge_v2_testlib:create_connector_api(Config),
|
|
|
+ {ok, {{_, 201, _}, _, _}} = emqx_bridge_v2_testlib:create_kind_api(Config),
|
|
|
+ Hookpoint = hookpoint(Config),
|
|
|
+ RepublishTopic = <<"rep/t">>,
|
|
|
+ RemoteTopic = emqx_utils_maps:deep_get(
|
|
|
+ [<<"parameters">>, <<"remote">>, <<"topic">>],
|
|
|
+ SourceConfig
|
|
|
+ ),
|
|
|
+ RuleOpts = #{
|
|
|
+ sql => <<"select * from \"", Hookpoint/binary, "\"">>,
|
|
|
+ actions => [
|
|
|
+ %% #{function => console},
|
|
|
+ #{
|
|
|
+ function => republish,
|
|
|
+ args => #{
|
|
|
+ topic => RepublishTopic,
|
|
|
+ payload => <<"${.}">>,
|
|
|
+ qos => 0,
|
|
|
+ retain => false,
|
|
|
+ user_properties => <<"${.pub_props.'User-Property'}">>
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {ok, {{_, 201, _}, _, #{<<"id">> := RuleId}}} =
|
|
|
+ emqx_bridge_v2_testlib:create_rule_api(RuleOpts),
|
|
|
+ on_exit(fun() -> emqx_rule_engine:delete_rule(RuleId) end),
|
|
|
+ {ok, Client} = emqtt:start_link([{proto_ver, v5}]),
|
|
|
+ {ok, _} = emqtt:connect(Client),
|
|
|
+ {ok, _, [?RC_GRANTED_QOS_0]} = emqtt:subscribe(Client, RepublishTopic),
|
|
|
+ ok = emqtt:publish(
|
|
|
+ Client,
|
|
|
+ RemoteTopic,
|
|
|
+ #{'User-Property' => [{<<"key">>, <<"value">>}]},
|
|
|
+ <<"mypayload">>,
|
|
|
+ _Opts = []
|
|
|
+ ),
|
|
|
+ {publish, Msg} =
|
|
|
+ ?assertReceive(
|
|
|
+ {publish, #{
|
|
|
+ topic := RepublishTopic,
|
|
|
+ retain := false,
|
|
|
+ qos := 0,
|
|
|
+ properties := #{'User-Property' := [{<<"key">>, <<"value">>}]}
|
|
|
+ }}
|
|
|
+ ),
|
|
|
+ Payload = emqx_utils_json:decode(maps:get(payload, Msg), [return_maps]),
|
|
|
+ ?assertMatch(
|
|
|
+ #{
|
|
|
+ <<"event">> := Hookpoint,
|
|
|
+ <<"payload">> := <<"mypayload">>
|
|
|
+ },
|
|
|
+ Payload
|
|
|
+ ),
|
|
|
+ emqtt:stop(Client),
|
|
|
+ ok
|
|
|
+ end,
|
|
|
+ fun(Trace) ->
|
|
|
+ ?assertEqual([], ?of_kind("action_references_nonexistent_bridges", Trace)),
|
|
|
+ ok
|
|
|
+ end
|
|
|
+ ),
|
|
|
+ ok.
|