From b1aba20200546bd2848dcb27b5705ef63c1bc107 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 13:59:16 +0530 Subject: [PATCH 1/4] Test cases for Permission management. --- .../org.wso2.carbon.device.mgt.core/pom.xml | 12 ++ .../mgt/PermissionManagerServiceTest.java | 104 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 117 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 40f3ab5f850..a7771512a10 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -221,6 +221,18 @@ org.testng testng + + org.powermock + powermock-module-testng + 1.7.0 + test + + + org.powermock + powermock-api-mockito + 1.7.0 + test + org.wso2.carbon org.wso2.carbon.user.core diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java new file mode 100644 index 00000000000..8cd89bbb842 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.core.permission.mgt; + +import org.apache.log4j.Logger; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.testng.Assert; +import org.testng.IObjectFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.permission.mgt.Permission; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; +import org.wso2.carbon.registry.core.exceptions.RegistryException; + +import java.util.Properties; + +import static org.mockito.MockitoAnnotations.initMocks; + +@PrepareForTest(PermissionUtils.class) +public class PermissionManagerServiceTest { + + private Permission permission; + private Logger log = Logger.getLogger(PermissionManagerServiceTest.class); + private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; + private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; + private String PERMiSSION_METHOD = "ui.execute"; + private String PERMISSION_NAME = "Test Permission"; + + //For create properties to retrieve permission. + private String HTTP_METHOD = "HTTP_METHOD"; + private String URL = "URL"; + + private PermissionManagerService permissionManagerService; + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + public void init() throws RegistryException { + permissionManagerService = PermissionManagerServiceImpl.getInstance(); + initMocks(this); + this.permission = new Permission(); + permission.setName(PERMISSION_NAME); + permission.setPath(PERMISSION_PATH); + permission.setMethod(PERMiSSION_METHOD); + permission.setUrl(PERMISSION_URL); + } + + @Test (description = "Create a new permission in the permission tree.") + public void testCreatePermission() { + try { + + PowerMockito.mockStatic(PermissionUtils.class); + PowerMockito.when(PermissionUtils.putPermission(permission)).thenReturn(true); + + Assert.assertTrue(permissionManagerService.addPermission(permission)); + + } catch (PermissionManagementException e) { + log.error("Error creating permission " + e.getErrorMessage()); + } + } + + @Test (dependsOnMethods = {"testCreatePermission"}, description = "Test for retrieving the created permission " + + "from the permission tree.") + public void testGetPermission() throws PermissionManagementException { + Permission permission = permissionManagerService.getPermission(createProperties()); + + Assert.assertEquals(permission.getMethod(), PERMiSSION_METHOD); + Assert.assertEquals(permission.getName(), PERMISSION_NAME); + Assert.assertEquals(permission.getPath(), PERMISSION_PATH); + Assert.assertEquals(permission.getUrl(), PERMISSION_URL); + } + + /** + * Create a Property object which will be passed to getPermission method to retrieve a permission. + * @return : Property object which contains permission url and method. + * */ + private Properties createProperties() { + Properties properties = new Properties(); + properties.setProperty(URL, PERMISSION_URL); + properties.setProperty(HTTP_METHOD, PERMiSSION_METHOD); + return properties; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index e562ed1740b..ea17e19e71c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -41,6 +41,7 @@ + From 52b4b46e7cb176c85dbf0f853b0368f02f17f476 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 14:02:11 +0530 Subject: [PATCH 2/4] Minor code formatting. --- .../mgt/core/permission/mgt/PermissionManagerServiceTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 8cd89bbb842..20675811cf3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -69,12 +69,10 @@ public class PermissionManagerServiceTest { @Test (description = "Create a new permission in the permission tree.") public void testCreatePermission() { try { - PowerMockito.mockStatic(PermissionUtils.class); PowerMockito.when(PermissionUtils.putPermission(permission)).thenReturn(true); Assert.assertTrue(permissionManagerService.addPermission(permission)); - } catch (PermissionManagementException e) { log.error("Error creating permission " + e.getErrorMessage()); } From ebe2a7bd6b2d127024e5db852e01430750e5f505 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 14:56:43 +0530 Subject: [PATCH 3/4] Moved dependency versioning to parent pom and fixed the test failure. --- .../org.wso2.carbon.device.mgt.core/pom.xml | 4 --- .../mgt/PermissionManagerServiceTest.java | 34 +++++++++++++++---- pom.xml | 8 ++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index a7771512a10..f5193fe9786 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -224,14 +224,10 @@ org.powermock powermock-module-testng - 1.7.0 - test org.powermock powermock-api-mockito - 1.7.0 - test org.wso2.carbon diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 20675811cf3..7f6c20ba814 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -17,7 +17,8 @@ */ package org.wso2.carbon.device.mgt.core.permission.mgt; -import org.apache.log4j.Logger; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.testng.Assert; @@ -38,10 +39,10 @@ import static org.mockito.MockitoAnnotations.initMocks; public class PermissionManagerServiceTest { private Permission permission; - private Logger log = Logger.getLogger(PermissionManagerServiceTest.class); + private Log log; private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; - private String PERMiSSION_METHOD = "ui.execute"; + private String PERMISSION_METHOD = "ui.execute"; private String PERMISSION_NAME = "Test Permission"; //For create properties to retrieve permission. @@ -57,12 +58,13 @@ public class PermissionManagerServiceTest { @BeforeClass public void init() throws RegistryException { - permissionManagerService = PermissionManagerServiceImpl.getInstance(); initMocks(this); + log = LogFactory.getLog(PermissionManagerServiceTest.class); + permissionManagerService = PermissionManagerServiceImpl.getInstance(); this.permission = new Permission(); permission.setName(PERMISSION_NAME); permission.setPath(PERMISSION_PATH); - permission.setMethod(PERMiSSION_METHOD); + permission.setMethod(PERMISSION_METHOD); permission.setUrl(PERMISSION_URL); } @@ -83,12 +85,19 @@ public class PermissionManagerServiceTest { public void testGetPermission() throws PermissionManagementException { Permission permission = permissionManagerService.getPermission(createProperties()); - Assert.assertEquals(permission.getMethod(), PERMiSSION_METHOD); + Assert.assertEquals(permission.getMethod(), PERMISSION_METHOD); Assert.assertEquals(permission.getName(), PERMISSION_NAME); Assert.assertEquals(permission.getPath(), PERMISSION_PATH); Assert.assertEquals(permission.getUrl(), PERMISSION_URL); } + @Test (dependsOnMethods = {"testCreatePermission"}, + expectedExceptions = {PermissionManagementException.class}, + expectedExceptionsMessageRegExp = "Resource URI/HTTP method is empty") + public void testGetPermissionError() throws PermissionManagementException { + Permission permission = permissionManagerService.getPermission(createErrorProperty()); + } + /** * Create a Property object which will be passed to getPermission method to retrieve a permission. * @return : Property object which contains permission url and method. @@ -96,7 +105,18 @@ public class PermissionManagerServiceTest { private Properties createProperties() { Properties properties = new Properties(); properties.setProperty(URL, PERMISSION_URL); - properties.setProperty(HTTP_METHOD, PERMiSSION_METHOD); + properties.setProperty(HTTP_METHOD, PERMISSION_METHOD); + return properties; + } + + /** + * Creates property object with empty properties. + * @return : Properties object with empty set of properties. + * */ + private Properties createErrorProperty() { + Properties properties = new Properties(); + properties.setProperty(URL, ""); + properties.setProperty(HTTP_METHOD, ""); return properties; } } diff --git a/pom.xml b/pom.xml index c30878f9e14..595af103141 100644 --- a/pom.xml +++ b/pom.xml @@ -1532,6 +1532,12 @@ ${power.mock.version} test + + org.powermock + powermock-module-testng + ${power.mock.version} + test + @@ -1961,7 +1967,7 @@ 0.7.8 0.7.5.201505241946 1.0b3 - 1.6.4 + 1.7.0 From c7779b63c08ed5fa358353193e2296e8a4a76b22 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Wed, 27 Sep 2017 15:01:20 +0530 Subject: [PATCH 4/4] String constants were changed to static final. --- .../mgt/PermissionManagerServiceTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java index 7f6c20ba814..a5ca659e31c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceTest.java @@ -38,17 +38,17 @@ import static org.mockito.MockitoAnnotations.initMocks; @PrepareForTest(PermissionUtils.class) public class PermissionManagerServiceTest { - private Permission permission; - private Log log; - private String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; - private String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; - private String PERMISSION_METHOD = "ui.execute"; - private String PERMISSION_NAME = "Test Permission"; + private static final Log log = LogFactory.getLog(PermissionManagerServiceTest.class);; + private static final String PERMISSION_URL = "permission/admin/device-mgt/test/testPermission"; + private static final String PERMISSION_PATH = "permission/admin/device-mgt/test/testPermission"; + private static final String PERMISSION_METHOD = "ui.execute"; + private static final String PERMISSION_NAME = "Test Permission"; //For create properties to retrieve permission. - private String HTTP_METHOD = "HTTP_METHOD"; - private String URL = "URL"; + private static final String HTTP_METHOD = "HTTP_METHOD"; + private static final String URL = "URL"; + private Permission permission; private PermissionManagerService permissionManagerService; @ObjectFactory @@ -59,7 +59,6 @@ public class PermissionManagerServiceTest { @BeforeClass public void init() throws RegistryException { initMocks(this); - log = LogFactory.getLog(PermissionManagerServiceTest.class); permissionManagerService = PermissionManagerServiceImpl.getInstance(); this.permission = new Permission(); permission.setName(PERMISSION_NAME);