Permalink
Browse files

new set_dynvars type: jsonpath

  • Loading branch information...
1 parent fbb576e commit 68916b5045f4b87fa86a5cf57b6e03f6c866bd61 @nniclausse nniclausse committed Sep 18, 2010
Showing with 33 additions and 4 deletions.
  1. +19 −1 src/test/ts_test_search.erl
  2. +5 −1 src/tsung/ts_client.erl
  3. +5 −1 src/tsung_controller/ts_config.erl
  4. +4 −1 tsung-1.0.dtd
@@ -50,6 +50,19 @@ parse_dyn_var_jsonpath3_test() ->
JSONPath = "titi[?name=bar].val",
?assertEqual([{'myvar',42}], ts_search:parse_dynvar([{jsonpath,'myvar', JSONPath} ],list_to_binary(Data))).
+parse_dyn_var_jsonpath4_test() ->
+ myset_env(),
+ Data="\r\n\r\n{\"titi\": [{\"val\": 123, \"name\": \"foo\"}, {\"val\": 42, \"name\": \"bar\"}]}",
+ JSONPath = "titi[?name=void].val",
+ ?assertEqual([{'myvar',undefined}], ts_search:parse_dynvar([{jsonpath,'myvar', JSONPath} ],list_to_binary(Data))).
+
+parse_dyn_var_jsonpath5_test() ->
+ myset_env(),
+ Data="\r\n\r\n{\"titi\": [{\"val\": 123, \"status\": \"foo\"}, {\"val\": 42, \"status\": \"OK\"}, {\"val\": 48, \"status\": \"OK\"}]}",
+ JSONPath = "titi[?status=OK].val",
+ ?assertEqual([{'myvar',[42,48]}], ts_search:parse_dynvar([{jsonpath,'myvar', JSONPath} ],list_to_binary(Data))).
+
+
parse_dyn_var_jsonpath_int_test() ->
myset_env(),
Data="\r\n\r\n{\"titi\": [{\"val\": 123, \"name\": \"foo\"}, {\"val\": 42, \"name\": \"bar\"}]}",
@@ -62,7 +75,6 @@ parse_dyn_var_jsonpath_xmpp_test() ->
JSONPath = "nodes",
?assertMatch([{'nodes',[<<"suno-12">>,<<"suno-13">>]}], ts_search:parse_dynvar([{jsonpath,'nodes', JSONPath} ],list_to_binary(Data))).
-
parse_dyn_var_xpath_test() ->
myset_env(),
Data="\r\n\r\n<html><body>"++?FORMDATA++"</body></html>",
@@ -316,6 +328,12 @@ dynvars_random2_test() ->
[String,String2] = ts_client:set_dynvars(random,{string,20},[toto,titi],[]),
?assertMatch({20,20},{length(String),length(String2)}).
+dynvars_jsonpath_test() ->
+ myset_env(),
+ Data="\r\n\r\n{\"titi\": [{\"val\": 123, \"name\": \"foo\"}, {\"val\": 42, \"name\": \"bar\"}]}",
+ JSONPath = "titi[?name=bar].val",
+ Dynvars=ts_dynvars:new(data,Data),
+ ?assertEqual(42,ts_client:set_dynvars(jsonpath,{JSONPath,data},[toto],#dyndata{dynvars=Dynvars})).
%%TODO: out of order..
View
@@ -444,7 +444,11 @@ set_dynvars(file,{random,FileId,Delimiter},_Vars,_DynData) ->
ts_utils:split(Line,Delimiter);
set_dynvars(file,{iter,FileId,Delimiter},_Vars,_DynData) ->
{ok,Line} = ts_file_server:get_next_line(FileId),
- ts_utils:split(Line,Delimiter).
+ ts_utils:split(Line,Delimiter);
+set_dynvars(jsonpath,{JSONPath, From},_Vars,DynData) ->
+ {ok, Val} = ts_dynvars:lookup(From,DynData#dyndata.dynvars),
+ JSON=mochijson2:decode(Val),
+ ts_utils:jsonpath(JSONPath, JSON).
%% @spec ctrl_struct(CtrlData::term(),State::#state_rcv{},Count::integer) ->
%% {next_state, NextStateName::atom(), NextState::#state_rcv{}} |
@@ -778,7 +778,11 @@ parse(Element = #xmlElement{name=setdynvars, attributes=Attrs},
"random_number" ->
Start = getAttr(integer,Attrs,start,1),
End = getAttr(integer,Attrs,'end',10),
- {setdynvars,random,{number,Start,End},Vars}
+ {setdynvars,random,{number,Start,End},Vars};
+ "jsonpath" ->
+ From = getAttr(atom, Attrs,from),
+ JSONPath = getAttr(Attrs,jsonpath),
+ {setdynvars,jsonpath,{JSONPath, From},Vars}
end,
?LOGF("Add setdynvars in session ~p as id ~p",[CurS#session.id,Id+1],?INFO),
ets:insert(Tab, {{CurS#session.id, Id+1}, Action}),
View
@@ -278,7 +278,8 @@ repeat | if | change_type | foreach)*>
<!ELEMENT setdynvars (var*) >
<!ATTLIST setdynvars
- sourcetype (random_string | urandom_string | random_number | file | erlang | eval) #REQUIRED
+ sourcetype (random_string | urandom_string | random_number |
+ file | erlang | eval| jsonpath) #REQUIRED
callback CDATA #IMPLIED
code CDATA #IMPLIED
fileid CDATA #IMPLIED
@@ -287,6 +288,8 @@ repeat | if | change_type | foreach)*>
length CDATA #IMPLIED
start CDATA #IMPLIED
end CDATA #IMPLIED
+ from CDATA #IMPLIED
+ jsonpath CDATA #IMPLIED
>
<!ELEMENT var (#PCDATA) >
<!ATTLIST var

0 comments on commit 68916b5

Please sign in to comment.