Skip to content

Commit

Permalink
tests for scenario info rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysGonchar committed Aug 3, 2020
1 parent decd6a7 commit f656be1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
75 changes: 69 additions & 6 deletions test/amoc_api_scenarios_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-define(SCENARIOS_URL_S, "/scenarios").
-define(SCENARIOS_URL_U, "/scenarios/upload").

-define(SCENARIOS_URL_I(Module), ?SCENARIOS_URL_S ++ "/" ++
atom_to_list(Module) ++ "/info").

-define(SAMPLE_SCENARIO, sample_test).
-define(SAMPLE_SCENARIO_DECLARATION,
"-module(" ++ atom_to_list(?SAMPLE_SCENARIO) ++ ").").
Expand All @@ -17,7 +20,9 @@
get_scenarios_returns_200_and_scenarios_list_when_requested/1,
put_scenarios_returns_400_and_error_when_scenario_is_not_valid/1,
put_scenarios_returns_200_and_compile_error_when_scenario_source_not_valid/1,
put_scenarios_returns_200_when_scenario_valid/1
put_scenarios_returns_200_when_scenario_valid/1,
get_scenario_info_returns_404_when_scenario_does_not_exist/1,
get_scenario_info_returns_200_when_scenario_exists/1
]).


Expand All @@ -26,15 +31,19 @@ all() ->
get_scenarios_returns_200_and_scenarios_list_when_requested,
put_scenarios_returns_400_and_error_when_scenario_is_not_valid,
put_scenarios_returns_200_and_compile_error_when_scenario_source_not_valid,
put_scenarios_returns_200_when_scenario_valid
put_scenarios_returns_200_when_scenario_valid,
get_scenario_info_returns_404_when_scenario_does_not_exist,
get_scenario_info_returns_200_when_scenario_exists
].


init_per_testcase(_, Config) ->
amoc_api_helper:start_amoc(),
Config.

end_per_testcase(put_scenarios_returns_200_when_scenario_valid, _Config) ->
end_per_testcase(TestCase, _Config)
when TestCase =:= get_scenario_info_returns_200_when_scenario_exists;
TestCase =:= put_scenarios_returns_200_when_scenario_valid ->
amoc_api_helper:remove_module(?SAMPLE_SCENARIO),
amoc_api_helper:stop_amoc();
end_per_testcase(_, _Config) ->
Expand All @@ -47,7 +56,8 @@ get_scenarios_returns_200_and_scenarios_list_when_requested(_Config) ->
{[{Key, Scenarios}]} = Body,
?assertEqual(200, CodeHttp),
?assertEqual(<<"scenarios">>, Key),
?assert(is_list(Scenarios)).
?assert(is_list(Scenarios)),
Scenarios.

put_scenarios_returns_400_and_error_when_scenario_is_not_valid(_Config) ->
%% given
Expand All @@ -73,7 +83,7 @@ put_scenarios_returns_200_and_compile_error_when_scenario_source_not_valid(_Conf
"\n [{2,erl_parse,[\"syntax error before: \",[]]}]}]\n">>,
?assertEqual({[{<<"compile">>, Error}]}, Body).

put_scenarios_returns_200_when_scenario_valid(_Config) ->
put_scenarios_returns_200_when_scenario_valid(Config) ->
%% given
ScenarioContent = ?DUMMY_SCENARIO_MODULE(?SAMPLE_SCENARIO),
%% when
Expand All @@ -84,4 +94,57 @@ put_scenarios_returns_200_when_scenario_valid(_Config) ->
?assertEqual(200, CodeHttp),
?assertEqual({[{<<"compile">>, <<"ok">>}]}, Body),
?assert(filelib:is_regular(ScenarioFileSource)),
?assert(filelib:is_regular(ScenarioFileBeam)).
?assert(filelib:is_regular(ScenarioFileBeam)),
Scenarios = get_scenarios_returns_200_and_scenarios_list_when_requested(Config),
?assertEqual(true, lists:member(atom_to_binary(?SAMPLE_SCENARIO, utf8), Scenarios)).


get_scenario_info_returns_404_when_scenario_does_not_exist(_Config) ->
%% when
{CodeHttp, _Body} = amoc_api_helper:get(?SCENARIOS_URL_I(?SAMPLE_SCENARIO)),
%% then
?assertEqual(404, CodeHttp).

get_scenario_info_returns_200_when_scenario_exists(Config) ->
%% given scenario exists
put_scenarios_returns_200_when_scenario_valid(Config),
mock_amoc_amoc_scenario(),
%% when
{CodeHttp, Body} = amoc_api_helper:get(?SCENARIOS_URL_I(?SAMPLE_SCENARIO)),
?assertEqual(200, CodeHttp),
BodyMap = json_to_map(Body),
ExpectedInfo = #{<<"doc">> => <<"\nsome edoc\n\n">>,
<<"parameters">> =>
#{<<"interarrival">> =>
#{<<"default_value">> => <<"50">>,
<<"description">> => <<"\"a delay between creating the"
" processes for two consecutive"
" users (ms, def: 50ms)\"">>,
<<"module">> => <<"amoc_controller">>,
<<"update_fn">> =>
<<"fun amoc_controller:maybe_update_interarrival_timer/2">>,
<<"verification_fn">> =>
<<"fun amoc_controller:positive_integer/1">>},
<<"some_parameter">> =>
#{<<"default_value">> => <<"undefined">>,
<<"description">> => <<"\"some parameter\"">>,
<<"module">> => <<"sample_test">>,
<<"update_fn">> => <<"read_only">>,
<<"verification_fn">> =>
<<"fun amoc_config_attributes:none/1">>}}},
?assertEqual(ExpectedInfo,BodyMap),
meck:unload(amoc_scenario).

mock_amoc_amoc_scenario() ->
ok = meck:new(amoc_scenario, [passthrough]),
Fun = fun() ->
Modules = meck:passthrough([]),
Modules -- [amoc_config_scenario_SUITE, amoc_config_attributes_SUITE]
end,
ok = meck:expect(amoc_scenario, list_configurable_modules, Fun).

json_to_map({List}) when is_list(List) ->
maps:from_list([{K, json_to_map(V)} || {K, V} <- List]);
json_to_map(List) when is_list(List) ->
[json_to_map(Element) || Element <- List];
json_to_map(AnythingElse) -> AnythingElse.
7 changes: 7 additions & 0 deletions test/scenario_template.hrl
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
-define(DUMMY_SCENARIO_MODULE(Name), <<"
%%==============================================================================
%% @doc
%% some edoc
%% @end
%%==============================================================================
-module(",(atom_to_binary(Name,utf8))/binary,").
-behaviour(amoc_scenario).
-required_variable(#{name => some_parameter, description => \"some parameter\"}).
-export([start/1]).
-export([init/0]).
Expand Down

0 comments on commit f656be1

Please sign in to comment.