emqx_rule_engine_schema_tests.erl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. 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. %%--------------------------------------------------------------------
  16. -module(emqx_rule_engine_schema_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. %%===========================================================================
  19. %% Data Section
  20. %%===========================================================================
  21. %% erlfmt-ignore
  22. republish_hocon0() ->
  23. "
  24. rule_engine.rules.my_rule {
  25. description = \"some desc\"
  26. metadata = {created_at = 1693918992079}
  27. sql = \"select * from \\\"t/topic\\\" \"
  28. actions = [
  29. {function = console, args = {test = 1}}
  30. { function = republish
  31. args = {
  32. payload = \"${.}\"
  33. qos = 0
  34. retain = false
  35. topic = \"t/repu\"
  36. mqtt_properties {
  37. \"Payload-Format-Indicator\" = \"${.payload.pfi}\"
  38. \"Message-Expiry-Interval\" = \"${.payload.mei}\"
  39. \"Content-Type\" = \"${.payload.ct}\"
  40. \"Response-Topic\" = \"${.payload.rt}\"
  41. \"Correlation-Data\" = \"${.payload.cd}\"
  42. }
  43. user_properties = \"${pub_props.'User-Property'}\"
  44. }
  45. },
  46. \"bridges:kafka:kprodu\",
  47. { function = custom_fn
  48. args = {
  49. actually = not_republish
  50. }
  51. }
  52. ]
  53. }
  54. ".
  55. %%===========================================================================
  56. %% Helper functions
  57. %%===========================================================================
  58. parse(Hocon) ->
  59. {ok, Conf} = hocon:binary(Hocon),
  60. Conf.
  61. check(Conf) when is_map(Conf) ->
  62. hocon_tconf:check_plain(emqx_rule_engine_schema, Conf).
  63. -define(validation_error(Reason, Value),
  64. {emqx_rule_engine_schema, [
  65. #{
  66. kind := validation_error,
  67. reason := Reason,
  68. value := Value
  69. }
  70. ]}
  71. ).
  72. -define(ok_config(Cfg), #{
  73. <<"rule_engine">> :=
  74. #{
  75. <<"rules">> :=
  76. #{
  77. <<"my_rule">> :=
  78. Cfg
  79. }
  80. }
  81. }).
  82. %%===========================================================================
  83. %% Test cases
  84. %%===========================================================================
  85. republish_test_() ->
  86. BaseConf = parse(republish_hocon0()),
  87. [
  88. {"base config",
  89. ?_assertMatch(
  90. ?ok_config(
  91. #{
  92. <<"actions">> := [
  93. #{<<"function">> := console},
  94. #{
  95. <<"function">> := republish,
  96. <<"args">> :=
  97. #{
  98. <<"mqtt_properties">> :=
  99. #{
  100. <<"Payload-Format-Indicator">> := <<_/binary>>,
  101. <<"Message-Expiry-Interval">> := <<_/binary>>,
  102. <<"Content-Type">> := <<_/binary>>,
  103. <<"Response-Topic">> := <<_/binary>>,
  104. <<"Correlation-Data">> := <<_/binary>>
  105. }
  106. }
  107. },
  108. <<"bridges:kafka:kprodu">>,
  109. #{
  110. <<"function">> := <<"custom_fn">>,
  111. <<"args">> :=
  112. #{
  113. <<"actually">> := <<"not_republish">>
  114. }
  115. }
  116. ]
  117. }
  118. ),
  119. check(BaseConf)
  120. )}
  121. ].