Updated java code gen to initialize API url at each service level.

This commit is contained in:
rpidikiti 2011-09-12 23:00:59 -07:00
parent ea01c6a7f9
commit a0c7474c27
4 changed files with 56 additions and 24 deletions

View File

@ -46,6 +46,8 @@ import $import$;
*/ */
public class $resource$ extends $extends$ { public class $resource$ extends $extends$ {
private static APIInvoker apiInvoker = null;
$methods:{ method | $methods:{ method |
/** /**
* $method.title$ * $method.title$
@ -97,11 +99,11 @@ $method.pathParameters:{ argument |
$endif$ $endif$
//make the API Call //make the API Call
$if(method.postObject)$ $if(method.postObject)$
String response = APIInvoker.invokeAPI(resourcePath, method, queryParams, postData); String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, postData);
$endif$ $endif$
$if(!method.postObject)$ $if(!method.postObject)$
String response = APIInvoker.invokeAPI(resourcePath, method, queryParams, null); String response = getApiInvoker().invokeAPI(resourcePath, method, queryParams, null);
$endif$ $endif$
$if(!method.responseVoid)$ $if(!method.responseVoid)$
@ -110,13 +112,13 @@ $if(!method.responseVoid)$
} }
$if(!method.returnValueList)$ $if(!method.returnValueList)$
//create output objects if the response has more than one object //create output objects if the response has more than one object
$method.returnValue$ responseObject = ($method.returnValue$) APIInvoker.deserialize(response, $method.returnClassName$.class); $method.returnValue$ responseObject = ($method.returnValue$) getApiInvoker().deserialize(response, $method.returnClassName$.class);
return responseObject; return responseObject;
$endif$ $endif$
$if(method.returnValueList)$ $if(method.returnValueList)$
TypeReference<ArrayList<$method.returnClassName$>> typeRef = new TypeReference<ArrayList<$method.returnClassName$>>() {}; TypeReference<ArrayList<$method.returnClassName$>> typeRef = new TypeReference<ArrayList<$method.returnClassName$>>() {};
try { try {
List<$method.returnClassName$> responseObject = (List<$method.returnClassName$>) APIInvoker.mapper.readValue(response, typeRef); List<$method.returnClassName$> responseObject = (List<$method.returnClassName$>) getApiInvoker().mapper.readValue(response, typeRef);
return responseObject; return responseObject;
} catch (IOException ioe) { } catch (IOException ioe) {
String[] args = new String[]{response, typeRef.toString()}; String[] args = new String[]{response, typeRef.toString()};
@ -128,4 +130,15 @@ $endif$
}$ }$
public static APIInvoker getApiInvoker() {
if(apiInvoker == null){
apiInvoker = APIInvoker.getApiInvoker();
}
return apiInvoker;
}
public static void setApiInvoker(APIInvoker invoker) {
apiInvoker = invoker;
}
} }

View File

@ -50,11 +50,13 @@ import com.sun.jersey.api.client.filter.LoggingFilter;
*/ */
public class APIInvoker { public class APIInvoker {
private static String apiServer = "http://api.wordnik.com/v4"; private String apiServer = "http://api.wordnik.com/v4";
private static SecurityHandler securityHandler = null; private SecurityHandler securityHandler = null;
private static boolean loggingEnabled; private boolean loggingEnabled;
private static Logger logger = null; private Logger logger = null;
private static APIInvoker apiInvoker = null;
protected static String POST = "POST"; protected static String POST = "POST";
protected static String GET = "GET"; protected static String GET = "GET";
protected static String PUT = "PUT"; protected static String PUT = "PUT";
@ -77,22 +79,33 @@ public class APIInvoker {
* for more details. {@link com.sun.jersey.api.client.filter.LoggingFilter}. Default output is sent to system.out. * for more details. {@link com.sun.jersey.api.client.filter.LoggingFilter}. Default output is sent to system.out.
* Create a logger ({@link java.util.logging.Logger} class and set using setLogger method. * Create a logger ({@link java.util.logging.Logger} class and set using setLogger method.
*/ */
public static void initialize(SecurityHandler securityHandler, String apiServer, boolean enableLogging) { public static APIInvoker initialize(SecurityHandler securityHandler, String apiServer, boolean enableLogging) {
setSecurityHandler(securityHandler); APIInvoker invoker = new APIInvoker();
invoker.setSecurityHandler(securityHandler);
if(apiServer != null && apiServer.length() > 0) { if(apiServer != null && apiServer.length() > 0) {
if(apiServer.substring(apiServer.length()-1).equals("/")){ if(apiServer.substring(apiServer.length()-1).equals("/")){
apiServer = apiServer.substring(0, apiServer.length()-1); apiServer = apiServer.substring(0, apiServer.length()-1);
} }
setApiServer(apiServer); invoker.setApiServer(apiServer);
} }
loggingEnabled = enableLogging; invoker.setLoggingEnable(enableLogging);
apiInvoker = invoker;
return invoker;
} }
/**
* Returns lst initialized API invoker
* @return
*/
public static APIInvoker getApiInvoker(){
return apiInvoker;
}
/** /**
* Set the logger instance used for Jersey logging. * Set the logger instance used for Jersey logging.
* @param aLogger * @param aLogger
*/ */
public static void setLogger(Logger aLogger) { public void setLogger(Logger aLogger) {
logger = aLogger; logger = aLogger;
} }
@ -101,11 +114,11 @@ public class APIInvoker {
* This value is set using initialize method. * This value is set using initialize method.
* @return * @return
*/ */
public static SecurityHandler setSecurityHandler() { public SecurityHandler setSecurityHandler() {
return securityHandler; return securityHandler;
} }
private static void setSecurityHandler(SecurityHandler aSecurityHandler) { private void setSecurityHandler(SecurityHandler aSecurityHandler) {
securityHandler = aSecurityHandler; securityHandler = aSecurityHandler;
} }
@ -113,11 +126,11 @@ public class APIInvoker {
* Sets the URL for the API server. It is defaulted to the server used while building the driver. * Sets the URL for the API server. It is defaulted to the server used while building the driver.
* @return * @return
*/ */
private static String getApiServer() { private String getApiServer() {
return apiServer; return apiServer;
} }
public static void setApiServer(String server) { public void setApiServer(String server) {
apiServer = server; apiServer = server;
} }
@ -135,7 +148,7 @@ public class APIInvoker {
* @return JSON response of the API call. * @return JSON response of the API call.
* @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails. * @throws com.wordnik.swagger.runtime.exception.APIException if the call to API server fails.
*/ */
public static String invokeAPI(String resourceURL, String method, Map<String, public String invokeAPI(String resourceURL, String method, Map<String,
String> queryParams, Object postData) throws APIException { String> queryParams, Object postData) throws APIException {
@ -278,7 +291,12 @@ public class APIInvoker {
return out.toString(); return out.toString();
} }
public static boolean isLoggingEnable() { public boolean isLoggingEnable() {
return loggingEnabled; return loggingEnabled;
} }
public void setLoggingEnable(boolean enabled) {
loggingEnabled = enabled;
}
} }

View File

@ -112,7 +112,8 @@ public class APITestRunner {
String apiKey = args[1]; String apiKey = args[1];
String testScriptLocation = args[2]; String testScriptLocation = args[2];
String testDataLocation = args[3]; String testDataLocation = args[3];
String testDataClass = args[4]; String testDataClass = args[4].trim();
System.out.println("class"+testDataClass+"test");
String apiPackageName = args[5]; String apiPackageName = args[5];
String libraryLocation = args[6]; String libraryLocation = args[6];
String language = args[7]; String language = args[7];
@ -123,7 +124,7 @@ public class APITestRunner {
} }
ApiKeyAuthTokenBasedSecurityHandler securityHandler = new ApiKeyAuthTokenBasedSecurityHandler(apiKey, ""); ApiKeyAuthTokenBasedSecurityHandler securityHandler = new ApiKeyAuthTokenBasedSecurityHandler(apiKey, "");
APIInvoker.initialize(securityHandler, apiServer, true); APIInvoker aAPIInvoker = APIInvoker.initialize(securityHandler, apiServer, true);
APITestRunner runner = new APITestRunner(); APITestRunner runner = new APITestRunner();
runner.initialize(testScriptLocation, testDataLocation, testDataClass); runner.initialize(testScriptLocation, testDataLocation, testDataClass);
runner.runTests(apiServer, apiPackageName, runner.getTestPackage(), language, new Integer(suiteId), apiPackageName, securityHandler, libraryLocation); runner.runTests(apiServer, apiPackageName, runner.getTestPackage(), language, new Integer(suiteId), apiPackageName, securityHandler, libraryLocation);

View File

@ -77,7 +77,7 @@ public class JavaTestCaseExecutor {
queryAndPathParameters.put("authToken", authToken); queryAndPathParameters.put("authToken", authToken);
ApiKeyAuthTokenBasedSecurityHandler securityHandler = new ApiKeyAuthTokenBasedSecurityHandler(apiKey, authToken); ApiKeyAuthTokenBasedSecurityHandler securityHandler = new ApiKeyAuthTokenBasedSecurityHandler(apiKey, authToken);
APIInvoker.initialize(securityHandler, apiServer, true); APIInvoker aAPIInvoker = APIInvoker.initialize(securityHandler, apiServer, true);
runner.executeTestCase(resource, servicePackageName, suggestedMethodName, queryAndPathParameters, postData); runner.executeTestCase(resource, servicePackageName, suggestedMethodName, queryAndPathParameters, postData);