From 3f7f68c9c3853d9ee98855d03be70634351c530b Mon Sep 17 00:00:00 2001 From: sinthuja Date: Wed, 11 Oct 2017 22:34:03 +0530 Subject: [PATCH] Adding more fixes to the testcases. --- .../carbon/apimgt/handlers/TestUtils.java | 25 ++++++++++++------- .../apimgt/handlers/mock/MockClient.java | 11 ++++++-- .../handlers/mock/MockHttpResponse.java | 4 +++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/TestUtils.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/TestUtils.java index 2c3842a3e3f..5b22e1c5d92 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/TestUtils.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/TestUtils.java @@ -22,14 +22,21 @@ import org.testng.Assert; import java.io.File; import java.net.URL; +/** + * Utils class which provides utility methods for other testcases. + */ public class TestUtils { - static final String IOT_CORE_HOST = "iot.core.wso2.com"; static final String IOT_CORE_HTTPS_PORT = "9443"; static final String IOT_KEYMANAGER_HOST = "iot.keymanager.wso2.com"; static final String IOT_KEYMANAGER_PORT = "9443"; static final String CONTENT_TYPE = "application/json"; + private static final String IOT_HOST_PROPERTY = "iot.core.host"; + private static final String IOT_PORT_PROPERTY = "iot.core.https.port"; + private static final String IOT_KEY_MANAGER_HOST_PROPERTY = "iot.keymanager.host"; + private static final String IOT_KEY_MANAGER_PORT_PROPERTY = "iot.keymanager.https.port"; + static String getAbsolutePathOfConfig(String configFilePath) { ClassLoader classLoader = TestUtils.class.getClassLoader(); URL invalidConfig = classLoader.getResource(configFilePath); @@ -39,16 +46,16 @@ public class TestUtils { } static void setSystemProperties() { - System.setProperty("iot.core.host", IOT_CORE_HOST); - System.setProperty("iot.core.https.port", IOT_CORE_HTTPS_PORT); - System.setProperty("iot.keymanager.host", IOT_KEYMANAGER_HOST); - System.setProperty("iot.keymanager.https.port", IOT_KEYMANAGER_PORT); + System.setProperty(IOT_HOST_PROPERTY, IOT_CORE_HOST); + System.setProperty(IOT_PORT_PROPERTY, IOT_CORE_HTTPS_PORT); + System.setProperty(IOT_KEY_MANAGER_HOST_PROPERTY, IOT_KEYMANAGER_HOST); + System.setProperty(IOT_KEY_MANAGER_PORT_PROPERTY, IOT_KEYMANAGER_PORT); } static void resetSystemProperties() { - System.clearProperty("iot.core.host"); - System.clearProperty("iot.core.https.port"); - System.clearProperty("iot.keymanager.host"); - System.clearProperty("iot.keymanager.https.port"); + System.clearProperty(IOT_HOST_PROPERTY); + System.clearProperty(IOT_PORT_PROPERTY); + System.clearProperty(IOT_KEY_MANAGER_HOST_PROPERTY); + System.clearProperty(IOT_KEY_MANAGER_PORT_PROPERTY); } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockClient.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockClient.java index 3c5a8123ed1..6c369690260 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockClient.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockClient.java @@ -29,6 +29,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +/** + * Mock implementation for CloseableHttpClient to be used in test cases. + */ public class MockClient extends CloseableHttpClient { private List responses = new ArrayList<>(); private int responseCount = 0; @@ -36,8 +39,12 @@ public class MockClient extends CloseableHttpClient { @Override protected CloseableHttpResponse doExecute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws IOException { - this.responseCount++; - return this.responses.get(this.responseCount - 1); + if (this.responseCount < this.responses.size()) { + this.responseCount++; + return this.responses.get(this.responseCount - 1); + } else { + return new MockHttpResponse(); + } } @Override diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockHttpResponse.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockHttpResponse.java index 3eb070c18e5..adfd7652c50 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockHttpResponse.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/mock/MockHttpResponse.java @@ -28,6 +28,10 @@ import org.apache.http.params.HttpParams; import java.io.IOException; import java.util.Locale; +/** + * Mock http response to be used in the test cases. + * + */ public class MockHttpResponse implements CloseableHttpResponse { private HttpEntity httpEntity; private StatusLine statusLine;