From 816657a1176166d1777e002c6d670dd8b080b034 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 26 Jan 2017 22:44:44 +0530 Subject: [PATCH] Removed Thread.sleep usages. --- .../iot/integration/common/Constants.java | 31 +++++++++++++++++++ .../iot/integration/common/OAuthUtil.java | 14 ++++++--- .../iot/integration/common/RestClient.java | 4 ++- .../mobileDevice/MobileDeviceManagement.java | 5 ++- .../MobileDeviceManagementWithNoDevices.java | 24 ++++++++++++-- .../test/resources/payloads/permissions.json | 3 ++ .../src/test/resources/testng.xml | 8 ++--- 7 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 modules/integration/tests-integration/src/test/resources/payloads/permissions.json diff --git a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/Constants.java b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/Constants.java index 208d255f..6badb24d 100644 --- a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/Constants.java +++ b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/Constants.java @@ -33,6 +33,7 @@ public final class Constants { public static final String OAUTH_CLIENT_ID = "client_id"; public static final String OAUTH_CLIENT_SECRET = "client_secret"; public static final String OAUTH_ACCESS_TOKEN = "access_token"; + public static final String SCOPE = "scope"; public static final String ANDROID_DEVICE_TYPE = "android"; public static final String HTTP_METHOD_POST = "POST"; public static final String HTTP_METHOD_PUT = "PUT"; @@ -61,6 +62,36 @@ public final class Constants { + " \"applicationName\":\"app_12345\",\n" + " \"isAllowedToAllDomains\":false,\n" + " \"tags\":[\"android\", \"device_management\"],\n" + " \"isMappingAnExistingOAuthApp\":false\n" + "}").toString(); + public static final String PERMISSION_LIST = "default perm:admin-groups:count perm:admin-groups:view " + + "perm:admin-users:view perm:admin:certificates:add perm:admin:certificates:delete " + + "perm:admin:certificates:details perm:admin:certificates:verify perm:admin:certificates:view " + + "perm:admin:devices:view perm:android:blacklist-applications perm:android:change-lock-code " + + "perm:android:clear-password perm:android:configure-vpn perm:android:configure-wifi " + + "perm:android:control-camera perm:android:disenroll perm:android:encrypt-storage " + + "perm:android:enroll perm:android:enterprise-wipe perm:android:info " + + "perm:android:install-application perm:android:location perm:android:lock-devices " + + "perm:android:logcat perm:android:manage-configuration perm:android:mute perm:android:reboot " + + "perm:android:ring perm:android:send-notification perm:android:set-password-policy " + + "perm:android:set-webclip perm:android:uninstall-application perm:android:unlock-devices " + + "perm:android:update-application perm:android:upgrade-firmware perm:android:view-configuration " + + "perm:android:wipe perm:applications:install perm:applications:uninstall perm:dashboard:by-groups " + + "perm:dashboard:count-overview perm:dashboard:details perm:dashboard:device-counts " + + "perm:dashboard:feature-non-compliant perm:dashboard:filtered-count perm:dashboard:non-compliant " + + "perm:dashboard:non-compliant-count perm:dashboard:vulnerabilities perm:device-types:features " + + "perm:device-types:types perm:devices:applications perm:devices:compliance-data perm:devices:delete" + + " perm:devices:details perm:devices:effective-policy perm:devices:features perm:devices:operations " + + "perm:devices:search perm:devices:update perm:devices:view perm:get-activity perm:groups:add " + + "perm:groups:assign perm:groups:count perm:groups:device perm:groups:devices " + + "perm:groups:devices-add perm:groups:devices-count perm:groups:devices-remove perm:groups:groups " + + "perm:groups:groups-view perm:groups:remove perm:groups:roles perm:groups:share perm:groups:update " + + "perm:manage-configuration perm:notifications:mark-checked perm:notifications:view " + + "perm:policies:activate perm:policies:changes perm:policies:deactivate perm:policies:get-details " + + "perm:policies:get-policy-details perm:policies:manage perm:policies:priorities " + + "perm:policies:remove perm:policies:update perm:roles:add perm:roles:add-users " + + "perm:roles:create-combined-role perm:roles:delete perm:roles:details perm:roles:permissions " + + "perm:roles:update perm:roles:view perm:users:add perm:users:count perm:users:credentials " + + "perm:users:delete perm:users:details perm:users:is-exist perm:users:roles perm:users:search " + + "perm:users:send-invitation perm:users:update perm:users:user-details perm:view-configuration"; private APIApplicationRegistration() { throw new AssertionError(); diff --git a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/OAuthUtil.java b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/OAuthUtil.java index f7364a03..b2b7a93f 100644 --- a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/OAuthUtil.java +++ b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/OAuthUtil.java @@ -27,9 +27,15 @@ import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; */ public class OAuthUtil { - public static String getOAuthToken(String backendHTTPURL, String backendHTTPSURL) - throws Exception { - Thread.sleep(10000); + public static String getScopes(String backendHTTPURL, String backendHTTPSURL) throws Exception { + return getOAuthTokenPair(backendHTTPURL, backendHTTPSURL).get(Constants.SCOPE).toString(); + } + + public static String getOAuthToken(String backendHTTPURL, String backendHTTPSURL) throws Exception { + return getOAuthTokenPair(backendHTTPURL, backendHTTPSURL).get(Constants.OAUTH_ACCESS_TOKEN).toString(); + } + + public static JSONObject getOAuthTokenPair(String backendHTTPURL, String backendHTTPSURL) throws Exception { String AuthString = "Basic YWRtaW46YWRtaW4="; RestClient client = new RestClient(backendHTTPURL, Constants.APPLICATION_JSON, AuthString); HttpResponse oAuthData = client.post(Constants.APIApplicationRegistration.API_APP_REGISTRATION_ENDPOINT, @@ -45,6 +51,6 @@ public class OAuthUtil { Constants.APIApplicationRegistration.OAUTH_TOKEN_PAYLOAD); System.out.println(oAuthData.getData()); jsonObj = new JSONObject(oAuthData.getData()); - return jsonObj.get(Constants.OAUTH_ACCESS_TOKEN).toString(); + return jsonObj; } } diff --git a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/RestClient.java b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/RestClient.java index 620d59b6..5767f486 100644 --- a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/RestClient.java +++ b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/common/RestClient.java @@ -17,11 +17,13 @@ */ package org.wso2.iot.integration.common; +import org.wso2.carbon.automation.engine.exceptions.AutomationFrameworkException; import org.wso2.carbon.automation.test.utils.http.client.HttpRequestUtil; import org.wso2.carbon.automation.test.utils.http.client.HttpResponse; import java.io.*; import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.nio.charset.Charset; @@ -73,7 +75,7 @@ public class RestClient { this.requestHeaders.remove(headerName); } - public HttpResponse post(String endpoint, String body) throws Exception { + public HttpResponse post(String endpoint, String body) throws MalformedURLException, AutomationFrameworkException { return HttpRequestUtil.doPost(new URL(backEndUrl + endpoint), body, requestHeaders); } diff --git a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagement.java b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagement.java index caf575f6..9f9db680 100644 --- a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagement.java +++ b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagement.java @@ -41,8 +41,7 @@ public class MobileDeviceManagement extends TestBase { @Test(description = "Add an Android device.") public void addEnrollment() throws Exception { JsonObject enrollmentData = PayloadGenerator.getJsonPayload( - Constants.AndroidEnrollment.ENROLLMENT_PAYLOAD_FILE_NAME, - Constants.HTTP_METHOD_POST); + Constants.AndroidEnrollment.ENROLLMENT_PAYLOAD_FILE_NAME, Constants.HTTP_METHOD_POST); enrollmentData.addProperty(Constants.DEVICE_IDENTIFIER_KEY, Constants.DEVICE_ID); IOTResponse response = client.post(Constants.AndroidEnrollment.ENROLLMENT_ENDPOINT, enrollmentData.toString()); Assert.assertEquals(HttpStatus.SC_OK, response.getStatus()); @@ -55,7 +54,7 @@ public class MobileDeviceManagement extends TestBase { public void testCountDevices() throws Exception { IOTResponse response = client.get(Constants.MobileDeviceManagement.GET_DEVICE_COUNT_ENDPOINT); Assert.assertEquals(HttpStatus.SC_OK, response.getStatus()); - Assert.assertTrue(response.getBody().toString().equals(Constants.MobileDeviceManagement.NO_OF_DEVICES)); + Assert.assertTrue(response.getBody().equals(Constants.MobileDeviceManagement.NO_OF_DEVICES)); } diff --git a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagementWithNoDevices.java b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagementWithNoDevices.java index 33b849ee..b2e76bea 100644 --- a/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagementWithNoDevices.java +++ b/modules/integration/tests-integration/src/test/java/org/wso2/iot/integration/mobileDevice/MobileDeviceManagementWithNoDevices.java @@ -22,6 +22,7 @@ import junit.framework.Assert; import org.apache.commons.httpclient.HttpStatus; import org.junit.experimental.theories.Theories; import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import org.wso2.carbon.automation.engine.context.TestUserMode; import org.wso2.iot.integration.common.*; @@ -34,10 +35,25 @@ import java.util.concurrent.TimeUnit; public class MobileDeviceManagementWithNoDevices extends TestBase { private IOTHttpClient client; + /** + * @BeforeSuite annotation is added to run this verification before the test suite starts. + * As in IoT server, apis are published after the server startup. Due to that the generated token doesn't get + * required scope. + * This method delays test suit startup until the tokens get required scopes. + * @throws Exception + */ + @BeforeSuite + public void verifyApiPublishing() throws Exception { + super.init(TestUserMode.SUPER_TENANT_ADMIN); + + while (!checkScopes(Constants.APIApplicationRegistration.PERMISSION_LIST)) { + TimeUnit.SECONDS.sleep(5); + } + } + @BeforeClass(alwaysRun = true, groups = { Constants.MobileDeviceManagement.MOBILE_DEVICE_MANAGEMENT_GROUP}) public void initTest() throws Exception { - super.init(TestUserMode.SUPER_TENANT_ADMIN); - Thread.sleep(10000); + String accessTokenString = "Bearer " + OAuthUtil.getOAuthToken(backendHTTPSURL, backendHTTPSURL); this.client = new IOTHttpClient(backendHTTPSURL, Constants.APPLICATION_JSON, accessTokenString); } @@ -49,5 +65,9 @@ public class MobileDeviceManagementWithNoDevices extends TestBase { Assert.assertEquals(Constants.MobileDeviceManagement.NO_DEVICE, response.getBody()); } + private boolean checkScopes(String permissionsList) throws Exception { + String tokenString = OAuthUtil.getScopes(backendHTTPSURL, backendHTTPSURL); + return tokenString.contains(permissionsList); + } } \ No newline at end of file diff --git a/modules/integration/tests-integration/src/test/resources/payloads/permissions.json b/modules/integration/tests-integration/src/test/resources/payloads/permissions.json new file mode 100644 index 00000000..7b3f663a --- /dev/null +++ b/modules/integration/tests-integration/src/test/resources/payloads/permissions.json @@ -0,0 +1,3 @@ +{ + "default perm:admin-groups:count perm:admin-groups:view perm:admin-users:view perm:admin:certificates:add perm:admin:certificates:delete perm:admin:certificates:details perm:admin:certificates:verify perm:admin:certificates:view perm:admin:devices:view perm:android:blacklist-applications perm:android:change-lock-code perm:android:clear-password perm:android:configure-vpn perm:android:configure-wifi perm:android:control-camera perm:android:disenroll perm:android:encrypt-storage perm:android:enroll perm:android:enterprise-wipe perm:android:info perm:android:install-application perm:android:location perm:android:lock-devices perm:android:logcat perm:android:manage-configuration perm:android:mute perm:android:reboot perm:android:ring perm:android:send-notification perm:android:set-password-policy perm:android:set-webclip perm:android:uninstall-application perm:android:unlock-devices perm:android:update-application perm:android:upgrade-firmware perm:android:view-configuration perm:android:wipe perm:applications:install perm:applications:uninstall perm:dashboard:by-groups perm:dashboard:count-overview perm:dashboard:details perm:dashboard:device-counts perm:dashboard:feature-non-compliant perm:dashboard:filtered-count perm:dashboard:non-compliant perm:dashboard:non-compliant-count perm:dashboard:vulnerabilities perm:device-types:features perm:device-types:types perm:devices:applications perm:devices:compliance-data perm:devices:delete perm:devices:details perm:devices:effective-policy perm:devices:features perm:devices:operations perm:devices:search perm:devices:update perm:devices:view perm:get-activity perm:groups:add perm:groups:assign perm:groups:count perm:groups:device perm:groups:devices perm:groups:devices-add perm:groups:devices-count perm:groups:devices-remove perm:groups:groups perm:groups:groups-view perm:groups:remove perm:groups:roles perm:groups:share perm:groups:update perm:manage-configuration perm:notifications:mark-checked perm:notifications:view perm:policies:activate perm:policies:changes perm:policies:deactivate perm:policies:get-details perm:policies:get-policy-details perm:policies:manage perm:policies:priorities perm:policies:remove perm:policies:update perm:roles:add perm:roles:add-users perm:roles:create-combined-role perm:roles:delete perm:roles:details perm:roles:permissions perm:roles:update perm:roles:view perm:users:add perm:users:count perm:users:credentials perm:users:delete perm:users:details perm:users:is-exist perm:users:roles perm:users:search perm:users:send-invitation perm:users:update perm:users:user-details perm:view-configuration" +} \ No newline at end of file diff --git a/modules/integration/tests-integration/src/test/resources/testng.xml b/modules/integration/tests-integration/src/test/resources/testng.xml index d28ea6f3..c91658a2 100644 --- a/modules/integration/tests-integration/src/test/resources/testng.xml +++ b/modules/integration/tests-integration/src/test/resources/testng.xml @@ -18,16 +18,14 @@ - + - + - +