Skip to content

Commit

Permalink
KAZOO-3891: move wnm_util:to_npan/1 calls to knm_converters:to_npan/1
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Mar 3, 2016
1 parent 275f802 commit 2002503
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 106 deletions.
4 changes: 2 additions & 2 deletions applications/callflow/src/module/cf_nomorobo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ nomorobo_req(URI, Data) ->
nomorobo_uri(Call) ->
lists:foldl(fun uri_replace/2
,?URL
,[{<<"{TO}">>, wnm_util:to_npan(whapps_call:request_user(Call))}
,{<<"{FROM}">>, wnm_util:to_npan(whapps_call:caller_id_number(Call))}
,[{<<"{TO}">>, knm_converters:to_npan(whapps_call:request_user(Call))}
,{<<"{FROM}">>, knm_converters:to_npan(whapps_call:caller_id_number(Call))}
]).

-spec uri_replace({ne_binary(), ne_binary()}, ne_binary()) -> ne_binary().
Expand Down
4 changes: 2 additions & 2 deletions applications/ecallmgr/src/ecallmgr_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ build_freetdm_channel(#bridge_endpoint{invite_format = <<"npan">>
,span=Span
,channel_selection=ChannelSelection
}) ->
{'ok', <<"freetdm/", Span/binary, "/", ChannelSelection/binary, "/", (wnm_util:to_npan(Number))/binary>>};
{'ok', <<"freetdm/", Span/binary, "/", ChannelSelection/binary, "/", (knm_converters:to_npan(Number))/binary>>};
build_freetdm_channel(#bridge_endpoint{invite_format = <<"1npan">>
,number=Number
,span=Span
Expand Down Expand Up @@ -779,7 +779,7 @@ maybe_format_user(Contact, #bridge_endpoint{number='undefined'}) -> Contact;
maybe_format_user(Contact, #bridge_endpoint{invite_format = <<"e164">>, number=Number}) ->
re:replace(Contact, "^[^\@]+", knm_converters:normalize(Number), [{'return', 'binary'}]);
maybe_format_user(Contact, #bridge_endpoint{invite_format = <<"npan">>, number=Number}) ->
re:replace(Contact, "^[^\@]+", wnm_util:to_npan(Number), [{'return', 'binary'}]);
re:replace(Contact, "^[^\@]+", knm_converters:to_npan(Number), [{'return', 'binary'}]);
maybe_format_user(Contact, #bridge_endpoint{invite_format = <<"1npan">>, number=Number}) ->
re:replace(Contact, "^[^\@]+", wnm_util:to_1npan(Number), [{'return', 'binary'}]);
maybe_format_user(Contact, _) -> Contact.
Expand Down
2 changes: 1 addition & 1 deletion applications/stepswitch/src/stepswitch_formatters.erl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ invite_format_fun(JObj) ->
case wh_json:get_value(<<"Invite-Format">>, JObj) of
<<"e164">> -> fun knm_converters:normalize/1;
<<"1npan">> -> fun wnm_util:to_1npan/1;
<<"npan">> -> fun wnm_util:to_npan/1
<<"npan">> -> fun knm_converters:to_npan/1
end.

-spec maybe_match(wh_json:object(), ne_binary(), ne_binary(), ne_binary(), wh_json:object()) ->
Expand Down
4 changes: 2 additions & 2 deletions applications/trunkstore/src/ts_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ invite_format(<<"1npan">>, To) ->
];
invite_format(<<"npanxxxxxx">>, To) ->
[{<<"Invite-Format">>, <<"npan">>}
,{<<"To-DID">>, wnm_util:to_npan(To)}
,{<<"To-DID">>, knm_converters:to_npan(To)}
];
invite_format(<<"npan">>, To) ->
[{<<"Invite-Format">>, <<"npan">>}
,{<<"To-DID">>, wnm_util:to_npan(To)}
,{<<"To-DID">>, knm_converters:to_npan(To)}
];
invite_format(_, _) ->
[{<<"Invite-Format">>, <<"username">>}].
Expand Down
39 changes: 23 additions & 16 deletions core/kazoo_number_manager/src/converters/knm_converters.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,33 @@

-export([normalize/1, normalize/2, normalize/3
,is_normalized/1
,is_npan/1 ,is_1npan/1
,is_npan/1, to_npan/1
,is_1npan/1
,to_db/1
,is_reconcilable/1, is_reconcilable/2
,classify/1, available_classifiers/0
,available_converters/0
,default_converter/0
]).

-define(DEFAULT_CONVERTERS, [<<"regex">>]).
-define(DEFAULT_CONVERTER, whapps_config:get(?KNM_CONFIG_CAT, <<"converter">>, <<"regex">>)).
-define(DEFAULT_CONVERTER_B, <<"regex">>).
-define(DEFAULT_CONVERTERS, [?DEFAULT_CONVERTER_B]).

-ifdef(TEST).
-define(CONVERTER_MOD, 'knm_converter_regex').
-define(DEFAULT_CONVERTER, ?DEFAULT_CONVERTER_B).

-define(RECONCILE_REGEX, ?DEFAULT_RECONCILE_REGEX).

-else.
-define(CONVERTER_MOD, wh_util:to_atom(<<"knm_converter_", (?DEFAULT_CONVERTER)/binary>>, 'true')).
-define(DEFAULT_CONVERTER, whapps_config:get(?KNM_CONFIG_CAT, <<"converter">>, ?DEFAULT_CONVERTER_B)).

-define(RECONCILE_REGEX
,whapps_config:get_binary(
?KNM_CONFIG_CAT
,?KEY_RECONCILE_REGEX
,?DEFAULT_RECONCILE_REGEX
)
).
-define(RECONCILE_REGEX,
whapps_config:get_binary(?KNM_CONFIG_CAT, ?KEY_RECONCILE_REGEX, ?DEFAULT_RECONCILE_REGEX)).

-endif.

-define(CONVERTER_MOD, wh_util:to_atom(<<"knm_converter_", (?DEFAULT_CONVERTER)/binary>>, 'true')).

-define(DEFAULT_RECONCILE_REGEX, <<"^\\+?1?\\d{10}$|^\\+[2-9]\\d{7,}$|^011\\d*$|^00\\d*$">>).
-define(KEY_RECONCILE_REGEX, <<"reconcile_regex">>).

Expand Down Expand Up @@ -106,17 +104,17 @@
%%--------------------------------------------------------------------
-spec normalize(ne_binary()) ->
ne_binary().
normalize(<<_/binary>> = Num) ->
normalize(?NE_BINARY = Num) ->
(?CONVERTER_MOD):normalize(Num).

-spec normalize(ne_binary(), api_binary()) ->
ne_binary().
normalize(<<_/binary>> = Num, AccountId) ->
normalize(?NE_BINARY = Num, AccountId) ->
(?CONVERTER_MOD):normalize(Num, AccountId).

-spec normalize(ne_binary(), api_binary(), wh_json:object()) ->
ne_binary().
normalize(<<_/binary>> = Num, AccountId, DialPlan) ->
normalize(?NE_BINARY = Num, AccountId, DialPlan) ->
(?CONVERTER_MOD):normalize(Num, AccountId, DialPlan).

%%--------------------------------------------------------------------
Expand All @@ -135,7 +133,16 @@ is_normalized(Num) ->
%%--------------------------------------------------------------------
-spec is_npan(ne_binary()) -> boolean().
is_npan(Num) ->
(?CONVERTER_MOD):to_npan(Num) =:= Num.
to_npan(Num) =:= Num.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec to_npan(ne_binary()) -> ne_binary().
to_npan(Num) ->
(?CONVERTER_MOD):to_npan(Num).

%%--------------------------------------------------------------------
%% @public
Expand Down
91 changes: 91 additions & 0 deletions core/kazoo_number_manager/test/knm_converters_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2011-2016, 2600Hz INC
%%% @doc
%%%
%%%
%%% @end
%%% @contributors
%%% Pierre Fenoll
%%%-------------------------------------------------------------------
-module(knm_converters_test).

-define(MODULE_TESTED, knm_converters).

-ifdef(PROPER).
-include_lib("proper/include/proper.hrl").
-endif.
-include_lib("eunit/include/eunit.hrl").

%% PROPER TESTING
-ifdef(PROPER).
%%
%%% 1000000000
%% (AAABBBCCCC, 1AAABBBCCCC) -> AAABBBCCCCCC.
prop_to_npan() ->
?FORALL(Number
,range(2002000000,19999999999)
,begin
BinNum = wh_util:to_binary(Number),
NPAN = ?MODULE_TESTED:to_npan(BinNum),
case byte_size(BinNum) of
11 -> BinNum =:= <<"1", NPAN/binary>>;
_ -> NPAN =:= BinNum
end
end
).

%% %% (AAABBBCCCC, 1AAABBBCCCC) -> 1AAABBBCCCCCC.
%% prop_to_1npan() ->
%% ?FORALL(Number
%% ,range(2002000000,19999999999)
%% ,begin
%% BinNum = wh_util:to_binary(Number),
%% OneNPAN = ?MODULE_TESTED:to_1npan(BinNum),
%% case byte_size(BinNum) of
%% 11 -> OneNPAN =:= BinNum;
%% _ -> OneNPAN =:= <<"1", BinNum/binary>>
%% end
%% end
%% ).

%% (AAABBBCCCC, 1AAABBBCCCC) -> +1AAABBBCCCCCC.
prop_normalize() ->
?FORALL(Number
,range(2002000000,19999999999)
,begin
BinNum = wh_util:to_binary(Number),
E164 = ?MODULE_TESTED:normalize(BinNum),
case byte_size(BinNum) of
11 -> E164 =:= <<$+, BinNum/binary>>;
10 -> E164 =:= <<$+, $1, BinNum/binary>>;
_ -> E164 =:= BinNum
end
end
).

proper_test_() ->
{"Runs the module's PropEr tests during eunit testing",
{'timeout', 15000,
[
?_assertEqual([], proper:module(?MODULE, [{'max_shrinks', 0}]))
]}}.

-endif.

%% EUNIT TESTING
%%

normalize_test_() ->
Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
Ans = <<"+12234567890">>,
[?_assertEqual(?MODULE_TESTED:normalize(N), Ans) || N <- Ns].

to_npan_test_() ->
Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
Ans = <<"2234567890">>,
[?_assertEqual(?MODULE_TESTED:to_npan(N), Ans) || N <- Ns].

%% to_1npan_test_() ->
%% Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
%% Ans = <<"12234567890">>,
%% [?_assertEqual(?MODULE_TESTED:to_1npan(N), Ans) || N <- Ns].
83 changes: 0 additions & 83 deletions core/kazoo_number_manager/test/knm_util_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,8 @@
%%%-------------------------------------------------------------------
-module(knm_util_test).

-ifdef(PROPER).
-include_lib("proper/include/proper.hrl").
-endif.
-include_lib("eunit/include/eunit.hrl").

%% PROPER TESTING
-ifdef(PROPER).
%%
%%% 1000000000
%% (AAABBBCCCC, 1AAABBBCCCC) -> AAABBBCCCCCC.
prop_to_npan() ->
?FORALL(Number
,range(2002000000,19999999999)
,begin
BinNum = wh_util:to_binary(Number),
NPAN = knm_converter_regex:to_npan(BinNum),
case byte_size(BinNum) of
11 -> BinNum =:= <<"1", NPAN/binary>>;
_ -> NPAN =:= BinNum
end
end
).

%% (AAABBBCCCC, 1AAABBBCCCC) -> 1AAABBBCCCCCC.
prop_to_1npan() ->
?FORALL(Number
,range(2002000000,19999999999)
,begin
BinNum = wh_util:to_binary(Number),
OneNPAN = knm_converter_regex:to_1npan(BinNum),
case byte_size(BinNum) of
11 -> OneNPAN =:= BinNum;
_ -> OneNPAN =:= <<"1", BinNum/binary>>
end
end
).

%% (AAABBBCCCC, 1AAABBBCCCC) -> +1AAABBBCCCCCC.
prop_to_e164() ->
?FORALL(Number
,range(2002000000,19999999999)
,begin
BinNum = wh_util:to_binary(Number),
E164 = knm_converters:normalize(BinNum),
case byte_size(BinNum) of
11 -> E164 =:= <<$+, BinNum/binary>>;
10 -> E164 =:= <<$+, $1, BinNum/binary>>;
_ -> E164 =:= BinNum
end
end
).

proper_test_() ->
{"Runs the module's PropEr tests during eunit testing",
{'timeout', 15000,
[
?_assertEqual([], proper:module(?MODULE, [{'max_shrinks', 0}]))
]}}.

-endif.

%% EUNIT TESTING
%%

to_e164_test_() ->
Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
Ans = <<"+12234567890">>,
[?_assertEqual(Ans, knm_converters:normalize(N))
|| N <- Ns
].

to_npan_test_() ->
Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
Ans = <<"2234567890">>,
[?_assertEqual(Ans, knm_converter_regex:to_npan(N))
|| N <- Ns
].

to_1npan_test_() ->
Ns = [<<"+12234567890">>, <<"12234567890">>, <<"2234567890">>],
Ans = <<"12234567890">>,
[?_assertEqual(Ans, knm_converter_regex:to_1npan(N))
|| N <- Ns
].

pretty_print_test_() ->
[?_assertEqual(Result, knm_util:pretty_print(Format, Number))
|| {Number, Format, Result}
Expand Down

0 comments on commit 2002503

Please sign in to comment.