Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_testservice.sas
Go to the documentation of this file.
1/**
2 @file mp_testservice.sas
3 @brief Will execute a test against a SASjs web service on SAS 9 or Viya
4 @details Prepares the input files and retrieves the resulting datasets from
5 the response JSON.
6
7 %mp_testjob(
8 duration=60*5
9 )
10
11 Note - the _webout fileref should NOT be assigned prior to running this macro.
12
13 @param [in] program The _PROGRAM endpoint to test
14 @param [in] inputfiles=(0) A list of space seperated fileref:filename pairs as
15 follows:
16 inputfiles=inref:filename inref2:filename2
17 @param [in] inputparams=(0) A dataset containing name/value pairs in the
18 following format:
19 |name:$32|value:$1000|
20 |---|---|
21 |stpmacname|some value|
22 |mustbevalidname|can be anything, oops, %abort!!|
23
24 @param [in] debug= (log) Provide the _debug value
25 @param [in] mdebug= (0) Set to 1 to provide macro debugging
26 @param [in] viyaresult= (WEBOUT_JSON) The Viya result type to return. For
27 more info, see mv_getjobresult.sas
28 @param [in] viyacontext= (SAS Job Execution compute context) The Viya compute
29 context on which to run the service
30 @param [out] outlib= (0) Output libref to contain the final tables. Set to
31 0 if the service output is not in JSON format.
32 @param [out] outref= (0) Output fileref to create, to contain the full _webout
33 response.
34
35 <h4> SAS Macros </h4>
36 @li mf_getplatform.sas
37 @li mf_getuniquefileref.sas
38 @li mf_getuniquename.sas
39 @li mp_abort.sas
40 @li mp_binarycopy.sas
41 @li mv_getjobresult.sas
42 @li mv_jobflow.sas
43
44 @version 9.4
45 @author Allan Bowe
46
47**/
48
49%macro mp_testservice(program,
50 inputfiles=0,
51 inputparams=0,
52 debug=log,
53 mdebug=0,
54 outlib=0,
55 outref=0,
56 viyaresult=WEBOUT_JSON,
57 viyacontext=SAS Job Execution compute context
58)/*/STORE SOURCE*/;
59%local dbg;
60%if &mdebug=1 %then %do;
61 %put &sysmacroname entry vars:;
62 %put _local_;
63%end;
64%else %let dbg=*;
65
66/* sanitise inputparams */
67%local pcnt;
68%let pcnt=0;
69%if &inputparams ne 0 %then %do;
70 data _null_;
71 set &inputparams;
72 if not nvalid(name,'v7') then putlog (_all_)(=);
73 else if name in (
74 'program','inputfiles','inputparams','debug','outlib','outref'
75 ) then putlog (_all_)(=);
76 else do;
77 x+1;
78 call symputx(name,quote(cats(value)),'l');
79 call symputx('pval'!!left(x),name,'l');
80 call symputx('pcnt',x,'l');
81 end;
82 run;
83 %mp_abort(iftrue= (%mf_nobs(&inputparams) ne &pcnt)
84 ,mac=&sysmacroname
85 ,msg=%str(Invalid values in &inputparams)
86 )
87%end;
88
89
90%local fref1 webref;
91%let fref1=%mf_getuniquefileref();
92%let webref=%mf_getuniquefileref();
93
94%local platform;
95%let platform=%mf_getplatform();
96%if &platform=SASMETA %then %do;
97
98 /* parse the input files */
99 %local webcount i var;
100 %if %quote(&inputfiles) ne 0 %then %do;
101 %let webcount=%sysfunc(countw(&inputfiles));
102 %put &=webcount;
103 %do i=1 %to &webcount;
104 %let var=%scan(&inputfiles,&i,%str( ));
105 %local webfref&i webname&i;
106 %let webref&i=%scan(&var,1,%str(:));
107 %let webname&i=%scan(&var,2,%str(:));
108 %put webref&i=&&webref&i;
109 %put webname&i=&&webname&i;
110 %end;
111 %end;
112 %else %let webcount=0;
113
114 proc stp program="&program";
115 inputparam _program="&program"
116 %do i=1 %to &webcount;
117 %if &webcount=1 %then %do;
118 _webin_fileref="&&webref&i"
119 _webin_name="&&webname&i"
120 %end;
121 %else %do;
122 _webin_fileref&i="&&webref&i"
123 _webin_name&i="&&webname&i"
124 %end;
125 %end;
126 _webin_file_count="&webcount"
127 _debug="&debug"
128 %do i=1 %to &pcnt;
129 /* resolve name only, proc stp fetches value */
130 &&pval&i=&&&&&&pval&i
131 %end;
132 ;
133 %do i=1 %to &webcount;
134 inputfile &&webref&i;
135 %end;
136 outputfile _webout=&webref;
137 run;
138
139 data _null_;
140 infile &webref;
141 file &fref1;
142 input;
143 length line $10000;
144 if index(_infile_,'>>weboutBEGIN<<') then do;
145 line=tranwrd(_infile_,'>>weboutBEGIN<<','');
146 put line;
147 end;
148 else if index(_infile_,'>>weboutEND<<') then do;
149 line=tranwrd(_infile_,'>>weboutEND<<','');
150 put line;
151 stop;
152 end;
153 else put _infile_;
154 run;
155 data _null_;
156 infile &fref1;
157 input;
158 put _infile_;
159 run;
160 %if &outlib ne 0 %then %do;
161 libname &outlib json (&fref1);
162 %end;
163 %if &outref ne 0 %then %do;
164 filename &outref temp;
165 %mp_binarycopy(inref=&webref,outref=&outref)
166 %end;
167
168%end;
169%else %if &platform=SASVIYA %then %do;
170
171 /* prepare inputparams */
172 %local ds1;
173 %let ds1=%mf_getuniquename();
174 %if "&inputparams" ne "0" %then %do;
175 proc transpose data=&inputparams out=&ds1;
176 id name;
177 var value;
178 run;
179 %end;
180 %else %do;
181 data &ds1;run;
182 %end;
183
184 /* parse the input files - convert to sasjs params */
185 %local webcount i var sasjs_tables;
186 %if %quote(&inputfiles) ne 0 %then %do;
187 %let webcount=%sysfunc(countw(&inputfiles));
188 %put &=webcount;
189 %do i=1 %to &webcount;
190 %let var=%scan(&inputfiles,&i,%str( ));
191 %local webfref&i webname&i sasjs&i.data;
192 %let webref&i=%scan(&var,1,%str(:));
193 %let webname&i=%scan(&var,2,%str(:));
194 %put webref&i=&&webref&i;
195 %put webname&i=&&webname&i;
196
197 %let sasjs_tables=&sasjs_tables &&webname&i;
198 data _null_;
199 infile &&webref&i lrecl=32767;
200 input;
201 if _n_=1 then call symputx("sasjs&i.data",_infile_);
202 else call symputx(
203 "sasjs&i.data",cats(symget("sasjs&i.data"),'0D0A'x,_infile_)
204 );
205 putlog "&sysmacroname infile: " _infile_;
206 run;
207 data &ds1;
208 set &ds1;
209 length sasjs&i.data $32767 sasjs_tables $1000;
210 sasjs&i.data=symget("sasjs&i.data");
211 sasjs_tables=symget("sasjs_tables");
212 run;
213 %end;
214 %end;
215 %else %let webcount=0;
216
217 data &ds1;
218 retain _program "&program";
219 retain _contextname "&viyacontext";
220 set &ds1;
221 putlog "&sysmacroname inputparams:";
222 putlog (_all_)(=);
223 run;
224
225 %mv_jobflow(inds=&ds1
226 ,maxconcurrency=1
227 ,outds=work.results
228 ,outref=&fref1
229 ,mdebug=&mdebug
230 )
231 /* show the log */
232 data _null_;
233 infile &fref1;
234 input;
235 putlog _infile_;
236 run;
237 /* get the uri to fetch results */
238 data _null_;
239 set work.results;
240 call symputx('uri',uri);
241 putlog "&sysmacroname: fetching results for " uri;
242 run;
243 /* fetch results from webout.json */
244 %mv_getjobresult(uri=&uri,
245 result=&viyaresult,
246 outref=&outref,
247 outlib=&outlib,
248 mdebug=&mdebug
249 )
250
251%end;
252%else %do;
253 %put %str(ERR)OR: Unrecognised platform: &platform;
254%end;
255
256%if &mdebug=0 %then %do;
257 filename &webref clear;
258%end;
259%else %do;
260 %put &sysmacroname exit vars:;
261 %put _local_;
262%end;
263
264%mend mp_testservice;