/**
  @file
  @brief testing startupservice service

  <h4> SAS Macros </h4>
  @li mf_getuniquefileref.sas
  @li mp_assertdsobs.sas
  @li mpe_getpath2settings.sas
  @li mx_append2pgm.sas
  @li mx_testservice.sas

**/

%let _program=&appLoc/services/public/startupservice;

%mx_testservice(&_program,
  mdebug=&sasjs_mdebug,
  outlib=webout,
  viyacontext=&defaultcontext
)

data work.globvars;
  set webout.globvars;
  putlog (_all_)(=);
run;

data work.xlmaps;
  set webout.xlmaps;
  putlog (_all_)(=);
run;

%mp_assertdsobs(work.globvars,
  desc=Fromsas table returned,
  test=HASOBS,
  outds=work.test_results
)

%mp_assertdsobs(work.xlmaps,
  desc=xlmaps table returned,
  test=HASOBS,
  outds=work.test_results
)

/* test mpe_security table */
%let testlib=%upcase(%mf_getuniquefileref());

/* remove the admin group */
filename append temp;
data _null_;
  file append;
  put '%let dc_admin_group=xxx;';
  put " libname &testlib '%sysfunc(pathname(&dc_libref))';";
run;
%let settingspath=%mpe_getpath2settings();
%mx_append2pgm(&settingspath, inref=append)

/* insert single approved table in mpe_security */
data work.append;
  set &DC_LIBREF..mpe_security;
  LIBREF="&testlib";
  DSN='MPE_X_TEST';
  ACCESS_LEVEL='EDIT';
  SAS_GROUP="&dc_admin_group";
  tx_from=0;
  tx_to='31DEC9999:23:59:59'dt;
  output;
  stop;
run;
proc append base=&dc_libref..mpe_security data=work.append;
run;
/* add to MPE_TABLES also */
data work.append2 ;
  set  &dc_libref..MPE_TABLES;
  where libref="&dc_libref" and %sysfunc(datetime()) < tx_to;
  libref="&testlib";
  tx_from=0;
  tx_to='31DEC9999:23:59:59'dt;
run;
proc sort data=work.append2 nodupkey; by libref dsn;run;
proc append base=&dc_libref..mpe_tables data=work.append2;run;


%mx_testservice(&_program,
  mdebug=&sasjs_mdebug,
  outlib=webout2,
  viyacontext=&defaultcontext
)

data work.sasdatasets2;
  set webout2.sasdatasets;
  putlog (_all_)(=);
  if libref="&testlib";
run;
%mp_assertdsobs(work.sasdatasets2,
  desc=Only 1 table shown,
  test=EQUALS 1
)

/* now try all tables */
data work.append3;
  set &DC_LIBREF..mpe_security;
  where libref="&testlib" and SAS_GROUP="&dc_admin_group";
  DSN='*ALL*';
run;
proc append base=&dc_libref..mpe_security data=work.append3;
run;
%mx_testservice(&_program,
  mdebug=&sasjs_mdebug,
  outlib=webout3,
  viyacontext=&defaultcontext
)


data work.sasdatasets3;
  set webout3.sasdatasets;
  putlog (_all_)(=);
  if libref="&testlib";
run;
%mp_assertdsobs(work.sasdatasets3,
  desc=Only 1 table shown,
  test=ATLEAST 10
)

/* revert the admin group back */
filename appback temp;
data _null_;
  file appback;
  put '%let dc_admin_group=' "&dc_admin_group;";
run;
%mx_append2pgm(&settingspath, inref=appback)