From f65aadf4d36a464b6b6c01b9fc09742a20e58b56 Mon Sep 17 00:00:00 2001 From: DenysGonchar Date: Wed, 21 Apr 2021 17:56:13 +0200 Subject: [PATCH] splitting test cases in acc_SUITE --- test/acc_SUITE.erl | 103 ++++++++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 33 deletions(-) diff --git a/test/acc_SUITE.erl b/test/acc_SUITE.erl index 5c12359356..bc235a2fd0 100644 --- a/test/acc_SUITE.erl +++ b/test/acc_SUITE.erl @@ -31,8 +31,9 @@ groups() -> init_from_element, produce_iq_meta_automatically, strip, + strip_with_params, parse_with_cdata, - tries_to_get_host_type + tries_to_get_host_type_if_it_is_not_provided ] } ]. @@ -150,11 +151,45 @@ strip(_C) -> ?assertEqual(HostType, mongoose_acc:host_type(Acc3)), ?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(Acc3)), Ref = mongoose_acc:ref(Acc3), + %% strip stanza and check that only non-permanent fields are missing + NAcc1 = mongoose_acc:strip(Acc3), + {XMLNS3, NAcc2} = mongoose_iq:xmlns(NAcc1), + ?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS3), + ?assertEqual(<<"set">>, mongoose_acc:stanza_type(NAcc2)), + ?assertEqual(Server, mongoose_acc:lserver(NAcc2)), + ?assertEqual(HostType, mongoose_acc:host_type(NAcc2)), + ?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(NAcc2)), + ?assertEqual(Ref, mongoose_acc:ref(NAcc2)), + ?assertEqual(997, mongoose_acc:get(ns, ppp, NAcc2)), + ?assertEqual([], mongoose_acc:get(ns2, NAcc2)). + +strip_with_params(_Config) -> + FromJID = jid:make(<<"jajid">>, <<"localhost">>, <<>>), + ToJID = jid:make(<<"tyjid">>, <<"localhost">>, <<>>), + Server = maps:get(lserver, ?ACC_PARAMS), + HostType = maps:get(host_type, ?ACC_PARAMS), + Acc = mongoose_acc:new(?ACC_PARAMS#{element => iq_stanza(), + from_jid => FromJID, + to_jid => ToJID}), + {XMLNS1, Acc1} = mongoose_iq:xmlns(Acc), + ?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS1), + ?assertEqual(<<"set">>, mongoose_acc:stanza_type(Acc1)), + ?assertEqual(undefined, mongoose_acc:get(ns, ppp, undefined, Acc1)), + Acc2 = mongoose_acc:set_permanent(ns, ppp, 997, Acc1), + Acc3 = mongoose_acc:set(ns2, [{a, 1}, {b, 2}], Acc2), + ?assertMatch([_, _], mongoose_acc:get(ns2, Acc3)), + ?assertEqual(Server, mongoose_acc:lserver(Acc3)), + ?assertEqual(HostType, mongoose_acc:host_type(Acc3)), + Ref = mongoose_acc:ref(Acc3), + %% strip stanza with params and check that new params are applied + %% and non-permanent fields are missing NewServer = <<"test.", Server/binary>>, NewHostType = <<"new ",HostType/binary>>, - NAcc1 = mongoose_acc:strip(#{lserver => NewServer, - host_type => NewHostType, - element => sample_stanza() }, Acc3), + NewStanza = sample_stanza(), + StripParams = #{lserver => NewServer, + host_type => NewHostType, + element => NewStanza}, + NAcc1 = mongoose_acc:strip(StripParams, Acc3), {XMLNS2, NAcc2} = mongoose_iq:xmlns(NAcc1), ?assertEqual(<<"urn:xmpp:blocking">>, XMLNS2), ?assertEqual(jid:from_binary(<<"a@localhost">>), mongoose_acc:to_jid(NAcc2)), @@ -163,19 +198,9 @@ strip(_C) -> ?assertEqual([], mongoose_acc:get(ns2, NAcc2)), ?assertEqual(sample_stanza(), mongoose_acc:element(NAcc2)), ?assertEqual(NewServer, mongoose_acc:lserver(NAcc2)), - ?assertEqual(NewHostType, mongoose_acc:host_type(NAcc2)), - NAcc3 = mongoose_acc:strip(Acc3), - {XMLNS3, NAcc4} = mongoose_iq:xmlns(NAcc3), - ?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS3), - ?assertEqual(<<"set">>, mongoose_acc:stanza_type(NAcc3)), - ?assertEqual(Server, mongoose_acc:lserver(NAcc4)), - ?assertEqual(HostType, mongoose_acc:host_type(NAcc4)), - ?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(NAcc4)), - ?assertEqual(Ref, mongoose_acc:ref(NAcc4)), - ?assertEqual(997, mongoose_acc:get(ns, ppp, NAcc4)), - ?assertEqual([], mongoose_acc:get(ns2, NAcc4)). - -tries_to_get_host_type(_Config) -> + ?assertEqual(NewHostType, mongoose_acc:host_type(NAcc2)). + +tries_to_get_host_type_if_it_is_not_provided(_Config) -> meck:new(mongoose_domain_api), meck:expect(mongoose_domain_api, get_host_type, fun @@ -183,35 +208,47 @@ tries_to_get_host_type(_Config) -> (<<"host2">>) -> {ok, <<"host 2">>}; (_) -> {error, not_found} end), + allocate_with_unknown_host_type_and_change_it_on_stripping(), + meck:reset(mongoose_domain_api), + allocate_with_known_host_type_and_change_it_on_stripping(), + meck:unload(mongoose_domain_api). + +allocate_with_known_host_type_and_change_it_on_stripping() -> AccParams=#{location => ?LOCATION}, StripParams = #{element => sample_stanza()}, %% new with <<"host 1">> host type Acc1 = mongoose_acc:new(AccParams#{lserver => <<"host1">>}), ?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc1)), ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host1">>])), - %% new with 'undefined' host type - Acc2 = mongoose_acc:new(AccParams#{lserver => <<"unknown.host1">>}), + %% host type changes from <<"host 1">> to 'undefined' + Acc2 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host1">>},Acc1), ?assertEqual(undefined, mongoose_acc:host_type(Acc2)), ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])), - %% host type changes from <<"host 1">> to 'undefined' - Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host1">>},Acc1), - ?assertEqual(undefined, mongoose_acc:host_type(Acc3)), - ?assertEqual(2, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])), %% host type changes from <<"host 1">> to <> - Acc6 = mongoose_acc:strip(StripParams#{lserver => <<"host2">>},Acc1), - ?assertEqual(<<"host 2">>, mongoose_acc:host_type(Acc6)), + Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"host2">>},Acc1), + ?assertEqual(<<"host 2">>, mongoose_acc:host_type(Acc3)), ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host2">>])), + %% check overall number of calls to mongoose_domain_api:get_host_type/1 + ?assertEqual(3, meck:num_calls(mongoose_domain_api, get_host_type, 1)). + +allocate_with_unknown_host_type_and_change_it_on_stripping() -> + AccParams=#{location => ?LOCATION}, + StripParams = #{element => sample_stanza()}, + %% new acc with 'undefined' host type + Acc1 = mongoose_acc:new(AccParams#{lserver => <<"unknown.host1">>}), + ?assertEqual(undefined, mongoose_acc:host_type(Acc1)), + ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])), %% host type changes from 'undefined' to <<"host 1">> - Acc4 = mongoose_acc:strip(StripParams#{lserver => <<"host1">>},Acc2), - ?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc4)), - ?assertEqual(2, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host1">>])), + Acc2 = mongoose_acc:strip(StripParams#{lserver => <<"host1">>},Acc1), + ?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc2)), + ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host1">>])), %% host type changes from 'undefined' to `undefined` - Acc5 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host2">>},Acc2), - ?assertEqual(undefined, mongoose_acc:host_type(Acc5)), + Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host2">>},Acc1), + ?assertEqual(undefined, mongoose_acc:host_type(Acc3)), ?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host2">>])), - %% check overall calls to mongoose_domain_api:get_host_type/1 - ?assertEqual(6, meck:num_calls(mongoose_domain_api, get_host_type, 1)), - meck:unload(mongoose_domain_api). + %% check overall number of calls to mongoose_domain_api:get_host_type/1 + ?assertEqual(3, meck:num_calls(mongoose_domain_api, get_host_type, 1)). + sample_stanza() -> {xmlel, <<"iq">>,