java - TestNG @Dataprovider -
i have following requirements java project without testng added @test
annotation run class.
1. find classes annotated `@controller` in class path 2. find methods annotated `@requestmapping` 3. infer method properties each classes 4. load known details excel(method name, httpresponsecode, username, password) 5. send httppost or httpget or httpput
now convert above functionality 1 4 @dataprovider
, call @test
method. how can this?
sample code :
package com.hexgen.reflection.integration; import java.io.bufferedwriter; import java.io.filewriter; import java.lang.reflect.method; import java.util.arraylist; import java.util.iterator; import java.util.linkedhashmap; import java.util.list; import java.util.map; import org.apache.http.client.httpclient; import org.apache.http.impl.client.defaulthttpclient; import org.springframework.core.localvariabletableparameternamediscoverer; import org.springframework.security.access.prepost.preauthorize; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.testng.assert; import org.testng.annotations.test; import com.hexgen.reflection.request.methodinforo; import com.hexgen.reflection.request.methodparamsinforo; import com.hexgen.reflection.support.hexgencontrollerclassfinder; import com.hexgen.reflection.support.hexgenwebapitestconstants; import com.hexgen.reflection.support.loadmethoddetailsinfofromexcel; /** * * @author anthony * */ public class hexgenwebapitest { /** * class finds method not secured * , makes sure method secured works */ @suppresswarnings({ "rawtypes", "unchecked" }) @test public void hexgenwebapitest( ) { int statuscode=0; string[] requestmappingvalues=null; string[] parametersdefinedformethod=null; string strclassname=""; string strclassnametofix=""; string requestingmethod=""; string uri=""; httpclient client = new defaulthttpclient(); list<methodparamsinforo> tempparamslist = null; list<string> notsecuredmethodslist = new arraylist<string>(); map<string,string> requestingmethodmap = new linkedhashmap<string,string>(); map<string,string> methodsmap = new linkedhashmap<string, string>(); map<string,list> paramsdetailsmap = new linkedhashmap<string, list>(); map<string,string> urldetailsmap = new linkedhashmap<string, string>(); map<string,methodinforo> knowngoodmap = new linkedhashmap<string, methodinforo>(); hexgencontrollerclassfinder hexgenclassutils = new hexgencontrollerclassfinder(); httpclientrequests httprequest = new httpclientrequests(); methodparamsinforo methodparams ; loadmethoddetailsinfofromexcel methoddetails = new loadmethoddetailsinfofromexcel(); class cls; try { list controllerclassnames = hexgenclassutils.findcontrollerclasses(hexgenwebapitestconstants.base_package); iterator<class> classname = controllerclassnames.iterator(); while(classname.hasnext()) { class obj = classname.next(); cls = class.forname(obj.getname()); method[] methods = cls.getdeclaredmethods(); (method method : methods) { requestmapping requestmappingannotation = method.getannotation(requestmapping.class); // gets method maped requestmapping annotation if(requestmappingannotation !=null){ // requestmappingannotation if condition starts preauthorize preauthorizeannotation = method.getannotation(preauthorize.class); // gets method maped preauthorize annotation if(preauthorizeannotation == null){ // preauthorizeannotation if condition starts notsecuredmethodslist.add("class : "+obj.getname()+" method : "+method.getname()); } // preauthorizeannotation if condition ends requestmappingvalues = requestmappingannotation.value(); // url value requestmethod[] requestmethods = requestmappingannotation.method(); // request method type requestingmethod = requestmethods[0].name(); methodsmap.put(requestmappingvalues[0],method.getname()); //following lines request url , requesting method type urldetailsmap.put(method.getname(), requestmappingvalues[0]); requestingmethodmap.put(method.getname(), requestingmethod); class[] parametertypes = method.getparametertypes(); localvariabletableparameternamediscoverer lcl = new localvariabletableparameternamediscoverer(); parametersdefinedformethod = lcl.getparameternames(method); tempparamslist = new arraylist(); (int i=0;i<parametertypes.length;i++) { // check parameter type , put them in arraylist methodparams = new methodparamsinforo(); class parametertype=parametertypes[i]; strclassnametofix = parametertype.getname(); strclassname =strclassnametofix.replaceall(hexgenwebapitestconstants.pattern_to_remove,hexgenwebapitestconstants.path_variable_to_replace).replaceall(hexgenwebapitestconstants.pattern_to_remove_semicolon,hexgenwebapitestconstants.path_variable_to_replace); methodparams.setdatatype(strclassname); methodparams.setvariabledefined(parametersdefinedformethod[i]); if(parametertype.isarray()){ methodparams.setarray(true); } if(parametertype.isprimitive()){ methodparams.setprimitive(true); } //fixme find better way address problem if (strclassname.equals("java.math.bigdecimal")|| strclassname.equals("java.lang.string")|| strclassname.equals("boolean")) { methodparams.setprimitive(true); } tempparamslist.add(methodparams); } paramsdetailsmap.put(method.getname(),tempparamslist); //paramslist.add(tempparamslist); }//requestmappingannotation if condition ends } } /** httpresponecodes * ================= * 1. 200 response successful * * 2. 400 bad request(the request not understood server due malformed syntax. client should not repeat request without modifications.) * * 3. 403 forbidden * * 4. 405 method not allowed (the method specified in request-line not allowed resource identified request-uri. response must include allow header containing list of valid methods requested resource.) * * 5. 500 internal server error(the server encountered unexpected condition prevented fulfilling request.) * */ } catch (exception ex) { ex.printstacktrace(); } } }
i have used apache-httpclient 4.0 server communications.
you can check following, hope works , helps you. luck.
@test(dataprovider = "hexgencontrollersdata") public void hexgencontrollerstest(knowngoodinfo knowngoodinforo, object[] methodinfoobject) throws exception { int httpresponsestatus=0; string requesturl = ""; httpclient authenticationobject = null; methodproperties methodpropertiesro = null; httprequesthandler httprequesthandler = new httprequesthandler(); methodpropertiesro = (methodproperties) methodinfoobject[0]; //attempts login hexgen application authenticationobject = httprequesthandler.logintohexgen(knowngoodinforo.getuser(), knowngoodinforo.getpassword()); requesturl = hexgencontrollerstestconstants.default_url + methodpropertiesro.geturl(); //attempts send url request , gets http response code httpresponsestatus = httprequesthandler.handlehttprequest(authenticationobject, requesturl,methodpropertiesro.getrequestingmethod(),(list) methodinfoobject[1]); assert.assertequals(knowngoodinforo.gethttpresponse(), httpresponsestatus); } @dataprovider(name = "hexgencontrollersdata") public static object[][] dataproviderforsecurityinference() { string[] requestmappingvalues = null; string[] parametersdefinedformethod = null; string strclassname = ""; string strclassnametofix = ""; string requestingmethod = ""; list<methodparamsinfo> tempparamslist = null; list<string> notsecuredmethodslist = null; map<string, list> methodparametersmap = new linkedhashmap<string, list>(); map<string, methodproperties> methodpropertiesmap = new linkedhashmap<string, methodproperties>(); map<string, knowngoodinfo> knowngoodmap = null; methodparamsinfo methodparams = null; methodproperties methodpropertiesro = null; dataproviderforhexgencontrollers dataproviderforhexgencontrollers = null; try { dataproviderforhexgencontrollers = new dataproviderforhexgencontrollers(); class classinstance; list controllerclassnames = dataproviderforhexgencontrollers.findcontrollerclasses(hexgencontrollerstestconstants.base_package); iterator<class> classnames = controllerclassnames.iterator(); notsecuredmethodslist = new arraylist<string>(); while (classnames.hasnext()) { class classname = classnames.next(); classinstance = class.forname(classname.getname()); method[] methods = classinstance.getdeclaredmethods(); (method method : methods) { // gets method maped requestmapping annotation requestmapping requestmappingannotation = method.getannotation(requestmapping.class); // requestmappingannotation if condition starts if (requestmappingannotation != null) { preauthorize preauthorizeannotation = method.getannotation(preauthorize.class); // record method name if not annotated preauthorize if (preauthorizeannotation == null) { notsecuredmethodslist.add("class : "+classname.getname() + " method : " + method.getname()); } // url value requestmappingvalues = requestmappingannotation.value(); // request method type requestmethod[] requestmethodwithurl = requestmappingannotation .method(); requestingmethod = requestmethodwithurl[0].name(); // attempts request url , requesting method type methodpropertiesro = new methodproperties(); methodpropertiesro.setrequestingmethod(requestingmethod); methodpropertiesro.seturl(requestmappingvalues[0]); methodpropertiesmap.put(method.getname(),methodpropertiesro); class[] parametertypes = method.getparametertypes(); localvariabletableparameternamediscoverer localvariabledefineddiscover = new localvariabletableparameternamediscoverer(); parametersdefinedformethod = localvariabledefineddiscover .getparameternames(method); tempparamslist = new arraylist(); // check parameter type , put them in // arraylist (int = 0; < parametertypes.length; i++) { methodparams = new methodparamsinfo(); class parametertype = parametertypes[i]; strclassnametofix = parametertype.getname(); strclassname = strclassnametofix .replaceall( hexgencontrollerstestconstants.pattern_to_remove,hexgencontrollerstestconstants.pattern_to_replace) .replaceall( hexgencontrollerstestconstants.pattern_to_remove_semicolon, hexgencontrollerstestconstants.pattern_to_replace); methodparams.setdatatype(strclassname); methodparams.setvariabledefined(parametersdefinedformethod[i]); if (parametertype.isarray()) { methodparams.setarray(true); } if (parametertype.isprimitive()) { methodparams.setprimitive(true); } // fixme find better way address problem if (strclassname .equals(hexgencontrollerstestconstants.bigdecimal) || strclassname .equals(hexgencontrollerstestconstants.string) || strclassname .equals(hexgencontrollerstestconstants.boolean)) { methodparams.setprimitive(true); } tempparamslist.add(methodparams); } methodparametersmap.put(method.getname(),tempparamslist); }// requestmappingannotation if condition ends } } dataproviderforhexgencontrollers = new dataproviderforhexgencontrollers(); knowngoodmap = new linkedhashmap<string, knowngoodinfo>(); knowngoodmap = dataproviderforhexgencontrollers.getknowngoodmap(hexgencontrollerstestconstants.known_good_file_path); } catch (exception dataproviderforsecurityinferenceexception) { dataproviderforsecurityinferenceexception.printstacktrace(); } list<object[]> hexgensecurityinferencedata = new arraylist<object[]>(); (string methodname:knowngoodmap.keyset()) { hexgensecurityinferencedata.add(new object[] { knowngoodmap.get(methodname), new object[] { methodpropertiesmap.get(methodname), methodparametersmap.get(methodname) } }); } object[][] securityinferencedata = hexgensecurityinferencedata.toarray(new object[0][]); return securityinferencedata; }
and let me know results , try not forget accept answer if looking for.
Comments
Post a Comment