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