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: 
   72:     check_equal_opts(mod_mam, mod_config(mod_mam, 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, #{pm => true, muc => true, cache => []}, 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_riak_config(_Config) ->
   84:     PM = config([modules, mod_mam_meta, pm], #{user_prefs_store => mnesia}),
   85:     MUC = default_config([modules, mod_mam_meta, muc]),
   86:     Deps = deps(#{backend => riak,
   87:                   db_message_format => some_format,
   88:                   pm => config([modules, mod_mam_meta, pm], PM),
   89:                   muc => config([modules, mod_mam_meta, muc], MUC)}),
   90:     ?assert(lists:keymember(mod_mam, 1, Deps)),
   91:     ?assert(lists:keymember(mod_mam_muc, 1, Deps)),
   92:     check_equal_opts(mod_mam_riak_timed_arch_yz,
   93:                      #{pm => true, muc => true, db_message_format => some_format}, Deps),
   94:     check_equal_opts(mod_mam_mnesia_prefs, #{pm => true}, Deps).
   95: 
   96: handles_cassandra_config(_Config) ->
   97:     PM = config([modules, mod_mam_meta, pm], #{user_prefs_store => cassandra,
   98:                                               db_message_format => some_format}),
   99:     MUC = config([modules, mod_mam_meta, muc], #{user_prefs_store => mnesia}),
  100:     Deps = deps(#{backend => cassandra,
  101:                   pm => config([modules, mod_mam_meta, pm], PM),
  102:                   muc => config([modules, mod_mam_meta, muc], MUC)}),
  103: 
  104:     check_equal_opts(mod_mam_mnesia_prefs, #{muc => true}, Deps),
  105:     check_equal_opts(mod_mam_cassandra_prefs, #{pm => true}, Deps),
  106:     check_equal_opts(mod_mam_cassandra_arch, #{db_message_format => some_format}, Deps),
  107:     check_equal_opts(mod_mam_muc_cassandra_arch, #{db_message_format => mam_message_xml}, Deps).
  108: 
  109: example_muc_only_no_pref_good_performance(_Config) ->
  110:     MUCOpts = #{host => {prefix, "muc."}},
  111:     MUC = config([modules, mod_mam_meta, muc], MUCOpts),
  112:     Deps = deps(#{muc => MUC}),
  113:     AsyncOpts = default_config([modules, mod_mam_meta, async_writer]),
  114: 
  115:     check_equal_deps(
  116:       [{mod_mam_rdbms_user, #{muc => true, pm => true}},
  117:        {mod_mam_cache_user, #{muc => true, cache => []}},
  118:        {mod_mam_muc_rdbms_arch, mod_config(mod_mam_muc_rdbms_arch, #{no_writer => true})},
  119:        {mod_mam_muc_rdbms_arch_async, AsyncOpts},
  120:        {mod_mam_muc, mod_config(mod_mam_muc, mod_config(mod_mam_muc, MUCOpts))}
  121:       ], Deps).
  122: 
  123: example_pm_only_good_performance(_Config) ->
  124:     PM = default_config([modules, mod_mam_meta, pm]),
  125:     Deps = deps(#{pm => PM, user_prefs_store => mnesia}),
  126:     AsyncOpts = default_config([modules, mod_mam_meta, async_writer]),
  127: 
  128:     check_equal_deps(
  129:       [{mod_mam_rdbms_user, #{pm => true}},
  130:        {mod_mam_cache_user, #{pm => true, cache => []}},
  131:        {mod_mam_mnesia_prefs, #{pm => true}},
  132:        {mod_mam_rdbms_arch, mod_config(mod_mam_rdbms_arch, #{no_writer => true})},
  133:        {mod_mam_rdbms_arch_async, AsyncOpts},
  134:        {mod_mam, default_mod_config(mod_mam)}
  135:       ], Deps).
  136: 
  137: %% Helpers
  138: 
  139: check_equal_deps(Expected, Actual) ->
  140:     ?assertEqual(lists:sort([{Mod, Opts, hard} || {Mod, Opts} <- Expected]), lists:sort(Actual)).
  141: 
  142: check_equal_opts(Mod, Opts, Deps) ->
  143:     {_, ActualOpts, _} = lists:keyfind(Mod, 1, Deps),
  144:     ?assertEqual(Opts, ActualOpts).
  145: 
  146: deps(Opts) ->
  147:     mod_mam_meta:deps(<<"host">>, mod_config(mod_mam_meta, Opts)).