1: -module(mod_mam_SUITE).
    2: -compile([export_all, nowarn_export_all]).
    3: 
    4: -include_lib("eunit/include/eunit.hrl").
    5: 
    6: -import(config_parser_helper, [mod_config/2, default_mod_config/1, default_config/1, config/2]).
    7: 
    8: all() -> [
    9:           overrides_general_options,
   10:           sets_rdbms_as_default_backend,
   11:           handles_only_pm,
   12:           handles_only_muc,
   13:           disables_sync_writer_on_async_writer,
   14:           disables_sync_muc_writer_on_async_writer,
   15:           produces_valid_configurations,
   16:           handles_cassandra_config,
   17:           example_muc_only_no_pref_good_performance,
   18:           example_pm_only_good_performance
   19:          ].
   20: 
   21: %% Tests
   22: 
   23: init_per_testcase(_, Config) -> Config.
   24: 
   25: end_per_testcase(_CaseName, Config) -> Config.
   26: 
   27: overrides_general_options(_Config) ->
   28:     Deps = deps(#{backend => rdbms,
   29:                   pm => config([modules, mod_mam, pm], #{backend => cassandra}),
   30:                   muc => default_config([modules, mod_mam, muc])
   31:                  }),
   32:     ?assert(lists:keymember(mod_mam_cassandra_arch, 1, Deps)),
   33:     ?assert(lists:keymember(mod_mam_muc_rdbms_arch, 1, Deps)),
   34:     ?assertNot(lists:keymember(mod_mam_rdbms_arch, 1, Deps)).
   35: 
   36: sets_rdbms_as_default_backend(_Config) ->
   37:     Deps = deps(#{pm => default_config([modules, mod_mam, pm])}),
   38:     ?assert(lists:keymember(mod_mam_rdbms_arch, 1, Deps)).
   39: 
   40: handles_only_pm(_Config) ->
   41:     Deps = deps(#{pm => default_config([modules, mod_mam, pm])}),
   42:     ?assert(lists:keymember(mod_mam_pm, 1, Deps)),
   43:     ?assertNot(lists:keymember(mod_mam_muc, 1, Deps)).
   44: 
   45: handles_only_muc(_Config) ->
   46:     Deps = deps(#{muc => default_config([modules, mod_mam, muc])}),
   47:     ?assertNot(lists:keymember(mod_mam_pm, 1, Deps)),
   48:     ?assert(lists:keymember(mod_mam_muc, 1, Deps)).
   49: 
   50: disables_sync_writer_on_async_writer(_Config) ->
   51:     PM = default_config([modules, mod_mam, pm]),
   52:     Deps = deps(#{pm => PM}),
   53:     check_equal_opts(mod_mam_rdbms_arch, mod_config(mod_mam_rdbms_arch, #{no_writer => true}), Deps).
   54: 
   55: disables_sync_muc_writer_on_async_writer(_Config) ->
   56:     MUC = default_config([modules, mod_mam, muc]),
   57:     Deps = deps(#{muc => MUC}),
   58:     check_equal_opts(mod_mam_muc_rdbms_arch, mod_config(mod_mam_muc_rdbms_arch, #{no_writer => true}), Deps).
   59: 
   60: produces_valid_configurations(_Config) ->
   61:     AsyncOpts = default_config([modules, mod_mam, async_writer]),
   62:     PMCoreOpts = #{archive_groupchats => true,
   63:                    async_writer => AsyncOpts#{enabled => false}},
   64:     PM = config([modules, mod_mam, pm], PMCoreOpts#{user_prefs_store => rdbms}),
   65:     MUCCoreOpts = #{host => <<"host">>},
   66:     MUCArchOpts = #{db_message_format => mam_message_xml},
   67:     MUC = config([modules, mod_mam, muc],
   68:                  maps:merge(MUCCoreOpts, MUCArchOpts#{user_prefs_store => mnesia})),
   69:     Deps = deps(#{pm => PM, muc => MUC}),
   70:     Cache = default_config([modules, mod_mam, cache]),
   71: 
   72:     check_equal_opts(mod_mam_pm, mod_config(mod_mam_pm, PMCoreOpts), Deps),
   73:     check_equal_opts(mod_mam_muc, mod_config(mod_mam_muc, MUCCoreOpts), Deps),
   74:     check_equal_opts(mod_mam_rdbms_arch, default_mod_config(mod_mam_rdbms_arch), Deps),
   75:     check_equal_opts(mod_mam_muc_rdbms_arch, mod_config(mod_mam_muc_rdbms_arch,
   76:                                                         MUCArchOpts#{no_writer => true}), Deps),
   77:     check_equal_opts(mod_mam_rdbms_user, #{pm => true, muc => true}, Deps),
   78:     check_equal_opts(mod_mam_cache_user, Cache#{pm => true, muc => true}, Deps),
   79:     check_equal_opts(mod_mam_mnesia_prefs, #{muc => true}, Deps),
   80:     check_equal_opts(mod_mam_rdbms_prefs, #{pm => true}, Deps),
   81:     check_equal_opts(mod_mam_muc_rdbms_arch_async, AsyncOpts, Deps).
   82: 
   83: handles_cassandra_config(_Config) ->
   84:     PM = config([modules, mod_mam, pm], #{user_prefs_store => cassandra,
   85:                                               db_message_format => some_format}),
   86:     MUC = config([modules, mod_mam, muc], #{user_prefs_store => mnesia}),
   87:     Deps = deps(#{backend => cassandra,
   88:                   pm => config([modules, mod_mam, pm], PM),
   89:                   muc => config([modules, mod_mam, muc], MUC)}),
   90: 
   91:     check_equal_opts(mod_mam_mnesia_prefs, #{muc => true}, Deps),
   92:     check_equal_opts(mod_mam_cassandra_prefs, #{pm => true}, Deps),
   93:     check_equal_opts(mod_mam_cassandra_arch, #{db_message_format => some_format}, Deps),
   94:     check_equal_opts(mod_mam_muc_cassandra_arch, #{db_message_format => mam_message_xml}, Deps).
   95: 
   96: example_muc_only_no_pref_good_performance(_Config) ->
   97:     MUCOpts = #{host => {prefix, "muc."}},
   98:     MUC = config([modules, mod_mam, muc], MUCOpts),
   99:     Deps = deps(#{muc => MUC}),
  100:     AsyncOpts = default_config([modules, mod_mam, async_writer]),
  101:     Cache = default_config([modules, mod_mam, cache]),
  102: 
  103:     check_equal_deps(
  104:       [{mod_mam_rdbms_user, #{muc => true, pm => true}},
  105:        {mod_mam_cache_user, Cache#{muc => true}},
  106:        {mod_mam_muc_rdbms_arch, mod_config(mod_mam_muc_rdbms_arch, #{no_writer => true})},
  107:        {mod_mam_muc_rdbms_arch_async, AsyncOpts},
  108:        {mod_mam_muc, mod_config(mod_mam_muc, mod_config(mod_mam_muc, MUCOpts))}
  109:       ], Deps).
  110: 
  111: example_pm_only_good_performance(_Config) ->
  112:     PM = default_config([modules, mod_mam, pm]),
  113:     Deps = deps(#{pm => PM, user_prefs_store => mnesia}),
  114:     AsyncOpts = default_config([modules, mod_mam, async_writer]),
  115:     Cache = default_config([modules, mod_mam, cache]),
  116: 
  117:     check_equal_deps(
  118:       [{mod_mam_rdbms_user, #{pm => true}},
  119:        {mod_mam_cache_user, Cache#{pm => true}},
  120:        {mod_mam_mnesia_prefs, #{pm => true}},
  121:        {mod_mam_rdbms_arch, mod_config(mod_mam_rdbms_arch, #{no_writer => true})},
  122:        {mod_mam_rdbms_arch_async, AsyncOpts},
  123:        {mod_mam_pm, default_mod_config(mod_mam_pm)}
  124:       ], Deps).
  125: 
  126: %% Helpers
  127: 
  128: check_equal_deps(Expected, Actual) ->
  129:     ?assertEqual(lists:sort([{Mod, Opts, hard} || {Mod, Opts} <- Expected]), lists:sort(Actual)).
  130: 
  131: check_equal_opts(Mod, Opts, Deps) ->
  132:     {_, ActualOpts, _} = lists:keyfind(Mod, 1, Deps),
  133:     ?assertEqual(Opts, ActualOpts).
  134: 
  135: deps(Opts) ->
  136:     mod_mam:deps(<<"host">>, mod_config(mod_mam, Opts)).