Skip to content

Commit

Permalink
Merge pull request #212 from dimuthud/master
Browse files Browse the repository at this point in the history
Adding tenant mode for test cases and fixing the tenant api resource path -AT- to @
  • Loading branch information
rswijesena committed Jun 5, 2015
2 parents 3460889 + ef043e4 commit b893c0f
Show file tree
Hide file tree
Showing 31 changed files with 1,132 additions and 539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import org.apache.axiom.om.OMElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wso2.am.integration.test.utils.APIManagerIntegrationTestException;
import org.wso2.am.integration.test.utils.bean.APIMURLBean;
import org.wso2.am.integration.test.utils.clients.APIPublisherRestClient;
import org.wso2.am.integration.test.utils.clients.APIStoreRestClient;
import org.wso2.am.integration.test.utils.generic.APIMTestCaseUtils;
import org.wso2.am.integration.test.utils.generic.ServiceDeploymentUtil;
import org.wso2.carbon.automation.engine.context.AutomationContext;
Expand Down Expand Up @@ -281,12 +286,68 @@ private boolean isProxyWSDlExist(String serviceUrl, long synchronizingDelay)
}
}

public static void cleanup(){

}

/**
* clean up deployed artifacts and other services
* Cleaning up the API manager by removing all APIs and applications other than default application
*
* @throws APIManagerIntegrationTestException
* @param userName - username of the api created tenant
* @param passWord - password of the api created tenant
* @param storeUrl - store url
* @param publisherUrl - publisher url
* @throws APIManagerIntegrationTestException - occurred when calling the apis
* @throws org.json.JSONException - occurred when reading the json
*/
protected void cleanup() throws APIManagerIntegrationTestException {
public static void cleanUp(String userName, String passWord, String storeUrl,
String publisherUrl) throws APIManagerIntegrationTestException,
JSONException {

APIStoreRestClient apiStore = new APIStoreRestClient(storeUrl);
apiStore.login(userName, passWord);
APIPublisherRestClient publisherRestClient = new APIPublisherRestClient(publisherUrl);
publisherRestClient.login(userName, passWord);
String subscriptionData = apiStore.getAllSubscriptions().getData();
JSONObject jsonSubscription = new JSONObject(subscriptionData);

if(jsonSubscription.getString("error").equals("false")) {
JSONObject jsonSubscriptionsObject = jsonSubscription.getJSONObject("subscriptions");
JSONArray jsonApplicationsArray = jsonSubscriptionsObject.getJSONArray("applications");

//Remove API Subscriptions
for (int i = 0; i < jsonApplicationsArray.length(); i++) {
JSONObject appObject = jsonApplicationsArray.getJSONObject(i);
int id = appObject.getInt("id");
JSONArray subscribedAPIJSONArray = appObject.getJSONArray("subscriptions");
for (int j = 0; j < subscribedAPIJSONArray.length(); j++) {
JSONObject subscribedAPI = subscribedAPIJSONArray.getJSONObject(j);
apiStore.removeAPISubscription(subscribedAPI.getString("name"), subscribedAPI.getString("version"),
subscribedAPI.getString("provider"), String.valueOf(id));
}
}
}

String apiData = apiStore.getAPI().getData();
JSONObject jsonAPIData = new JSONObject(apiData);
JSONArray jsonAPIArray = jsonAPIData.getJSONArray("apis");

//delete all APIs
for (int i = 0; i < jsonAPIArray.length(); i++) {
JSONObject api = jsonAPIArray.getJSONObject(i);
publisherRestClient.deleteAPI(api.getString("name"), api.getString("version"), userName);
}

//delete all application other than default application
String applicationData = apiStore.getAllApplications().getData();
JSONObject jsonApplicationData = new JSONObject(applicationData);
JSONArray applicationArray = jsonApplicationData.getJSONArray("applications");
for (int i = 0; i < applicationArray.length(); i++) {
JSONObject jsonApplication = applicationArray.getJSONObject(i);
if (!jsonApplication.getString("name").equals("DefaultApplication")) {
apiStore.removeApplication(jsonApplication.getString("name"));
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,39 @@ public class APIMURLBean {

public APIMURLBean(ContextUrls contextUrls) {

String tempUrl = contextUrls.getBackEndUrl();
webAppURLHttp = contextUrls.getWebAppURL() + "/";

if (tempUrl.endsWith("/services/")) {
String tempUrl = contextUrls.getWebAppURL();
if (tempUrl.contains("/t/")) {
tempUrl = tempUrl.split("/t/")[0];
}
webAppURLHttp = tempUrl + "/";

tempUrl = contextUrls.getBackEndUrl();
if (tempUrl.contains("/t/")) {
tempUrl = tempUrl.split("/t/")[0];
}
if (tempUrl.contains("/services/")) {
tempUrl = tempUrl.replace("/services/", "");
}

webAppURLHttps = tempUrl + "/";

tempUrl = contextUrls.getServiceUrl();

if (tempUrl.endsWith("/services")) {
if (tempUrl.contains("/t/")) {
tempUrl = tempUrl.split("/t/")[0];
}
if (tempUrl.contains("/services")) {
tempUrl = tempUrl.replace("/services", "");
}

webAppURLNhttp = tempUrl + "/";

tempUrl = contextUrls.getSecureServiceUrl();

if (tempUrl.endsWith("/services")) {
if (tempUrl.contains("/t/")) {
tempUrl = tempUrl.split("/t/")[0];
}
if (tempUrl.contains("/services")) {
tempUrl = tempUrl.replace("/services", "");
}

webAppURLNhttps = tempUrl + "/";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,23 @@ public void setEnvironment() throws Exception {
String session = login.login("admin", "admin", "localhost");
// Upload the synapse
String file = "artifacts" + File.separator + "AM" + File.separator + "synapseconfigs" +
File.separator + "property" + File.separator +
"CONTENT_TYPE_TEST.xml";
File.separator + "property" + File.separator +
"CONTENT_TYPE_TEST.xml";
OMElement synapseConfig = apimTestCaseUtils.loadResource(file);
apimTestCaseUtils.updateSynapseConfiguration(synapseConfig, gatewayContext.getContextUrls().getBackEndUrl(),
session);
Thread.sleep(5000);

}

@SetEnvironment(executionEnvironments = { ExecutionEnvironment.ALL })
@SetEnvironment(executionEnvironments = {ExecutionEnvironment.ALL})
@org.testng.annotations.Test(groups = "wso2.am",
description = "Test for reading the multipart/form-data Content-Type header in the request")
public void testTRANSPORT_HEADERSPropertTest() throws Exception {

wireServer.start();

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8280/ContentTypeAPI");
HttpPost httppost = new HttpPost(gatewayUrls.getWebAppURLNhttp() + "ContentTypeAPI");

String relativeFilePath = "/artifacts/AM/synapseconfigs/property/CONTENT_TYPE_TEST.xml";
relativeFilePath = relativeFilePath.replaceAll("[\\\\/]", File.separator);
Expand All @@ -88,13 +87,13 @@ public void testTRANSPORT_HEADERSPropertTest() throws Exception {
String wireResponse = wireServer.getCapturedMessage();

Assert.assertTrue(wireResponse.contains("Content-Type: multipart/form-data"),
"Content-Type header have multipart/form-data value properly");
"Content-Type header have multipart/form-data value properly");

// response should be something like multipart/form-data; boundary=9u5f_0bx6sx1lHerRtXmOkKAprCjG0ESSS
// If it contains something like bellow, that is incorrect
Assert.assertFalse(wireResponse.contains(
"Content-Type: multipart/form-data; charset=UTF-8; boundary=MIMEBoundary_"),
"Content-Type header contains invalid charset and boundary values");
"Content-Type: multipart/form-data; charset=UTF-8; boundary=MIMEBoundary_"),
"Content-Type header contains invalid charset and boundary values");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public void setEnvironment() throws Exception {

serverConfigurationManager = new ServerConfigurationManager(
gatewayContext);
serverConfigurationManager.applyConfigurationWithoutRestart(new File(getAMResourceLocation()
+ File.separator + "configFiles/json_to_xml/" + "axis2.xml"));
serverConfigurationManager.applyConfiguration(new File(getAMResourceLocation()
+ File.separator + "configFiles/json_to_xml/" + "synapse.properties"));
serverConfigurationManager.applyConfigurationWithoutRestart(
new File(getAMResourceLocation() + File.separator + "configFiles/json_to_xml/" + "axis2.xml"));

serverConfigurationManager.applyConfiguration(
new File(getAMResourceLocation() + File.separator + "configFiles/json_to_xml/" + "synapse.properties"));

gatewaySessionCookie = createSession(gatewayContext);

Expand All @@ -80,8 +81,8 @@ public void jsonToXmlTestCase() throws Exception {

try {
response = HttpRequestUtil.doPost(new URL(
gatewayUrls.getWebAppURLNhttp()+"Weather/1.0.0"), payload,
requestHeaders);
gatewayUrls.getWebAppURLNhttp() + "Weather/1.0.0"), payload,
requestHeaders);
} catch (Exception e) {
Assert.assertFalse(
e.getLocalizedMessage().contains("Connection error"),
Expand All @@ -90,12 +91,14 @@ public void jsonToXmlTestCase() throws Exception {

assert response != null;
Assert.assertEquals(response.getResponseCode(), 404,
"Response code mismatched while Json to XML test case");
"Response code mismatched while Json to XML test case");
}

@AfterClass(alwaysRun = true)
public void destroy() throws Exception {
super.cleanup();
super.cleanUp(gatewayContext.getContextTenant().getTenantAdmin().getUserName(),
gatewayContext.getContextTenant().getContextUser().getPassword(),
storeUrls.getWebAppURLHttp(), publisherUrls.getWebAppURLHttp());
serverConfigurationManager.restoreToLastConfiguration();
}

Expand Down
Loading

0 comments on commit b893c0f

Please sign in to comment.