From b392f93aab825888970c02981dd91d00c6a3323d Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 17 Oct 2017 13:49:45 +0530 Subject: [PATCH] Adding negative test cases for GroupProviderServiceImpl --- .../GroupManagementProviderServiceImpl.java | 18 ++- ...ManagementProviderServiceNegativeTest.java | 136 ++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 3 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 69efaa107df..2b6a7b0bb4e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -445,11 +445,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { - if (request == null) { - String msg = "Received empty request for getGroupCount"; - log.error(msg); - throw new GroupManagementException(msg); - } if (log.isDebugEnabled()) { log.debug("Get groups count, pagination request " + request.toString()); } @@ -522,7 +517,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( tenantId).getUserStoreManager(); - List currentUserRoles = getRoles(groupId); + } catch (UserStoreException e) { + String msg = "User store error in updating sharing roles."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } + List currentUserRoles = getRoles(groupId); + try { + GroupManagementDAOFactory.beginTransaction(); if (newRoles != null) { for (String role : newRoles) { @@ -546,10 +548,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.rollbackTransaction(); log.error(e); throw new GroupManagementException(e); - } catch (UserStoreException e) { - String msg = "User store error in updating sharing roles."; - log.error(msg, e); - throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { log.error(e); throw new GroupManagementException(e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java new file mode 100644 index 00000000000..2702fe4c402 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java @@ -0,0 +1,136 @@ +package org.wso2.carbon.device.mgt.core.service; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.core.TestUtils; +import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; +import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; + +import javax.sql.DataSource; +import java.util.ArrayList; +import java.util.List; + +public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManagementTest { + private GroupManagementProviderService groupManagementProviderService; + + @BeforeClass + @Override + public void init() throws Exception { + DataSource datasource = this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/no-table-data-source-config.xml")); + GroupManagementDAOFactory.init(datasource); + groupManagementProviderService = new GroupManagementProviderServiceImpl(); + } + + @Test(description = "This method tests the addDevices method under negative scenarios", + expectedExceptions = {GroupManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while adding device to group.*") + public void testAddDevicesScenario1() throws GroupManagementException, DeviceNotFoundException { + List list = TestUtils.getDeviceIdentifiersList(); + groupManagementProviderService.addDevices(1, list); + } + + @Test(description = "This method tests the addDevices method under negative circumstances", expectedExceptions = + {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred in addDevices for.*") + public void testAddDevicesScenario2() throws GroupManagementException, DeviceNotFoundException { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier("test", "test"); + List list = new ArrayList<>(); + list.add(deviceIdentifier); + groupManagementProviderService.addDevices(1, list); + } + + @Test(description = "This method tests the getGroup method of the GroupManagementProviderService under " + + "negative conditions", expectedExceptions = {GroupManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while obtaining group.*") + public void testGetGroup() throws GroupManagementException { + groupManagementProviderService.getGroup(1); + } + + @Test(description = "This method tests the getGroup method of the GroupManagementProviderService under " + + "negative conditions", expectedExceptions = {GroupManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while obtaining group with name.*") + public void testGetGroupWithName() throws GroupManagementException { + groupManagementProviderService.getGroup("1"); + } + + @Test(description = "This method tests the getGroups method of the GroupManagementProviderService under negative " + + "conditions", expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = + "Error occurred while retrieving all groups in tenant.*") + public void testGetGroups() throws GroupManagementException { + groupManagementProviderService.getGroups(); + } + + @Test(description = "This method tests the getGroups method of the GroupManagementProviderService under negative " + + "conditions", expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = + "Error occurred while retrieving all groups accessible to user.*") + public void testGetGroupsWithUserName() throws GroupManagementException { + groupManagementProviderService.getGroups("test"); + } + + @Test(description = "This method tests the getGroupCount method under negative circumstances", expectedExceptions + = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred while retrieving all " + + "groups in tenant") + public void testGetGroupCount() throws GroupManagementException { + groupManagementProviderService.getGroupCount(); + } + + @Test(description = "This method tests the getGroupCount method with username under negative circumstances", + expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred " + + "while retrieving group count of user.*") + public void testGetGroupCountWithUserName() throws GroupManagementException { + groupManagementProviderService.getGroupCount("test"); + + } + + @Test(description = "This method tests the getGroups method with pagination request under negative " + + "circumstances", expectedExceptions = {GroupManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while retrieving all groups in tenant") + public void testGetGroupsWithPaginationRequest() throws GroupManagementException { + groupManagementProviderService.getGroups(TestUtils.createPaginationRequest()); + } + + @Test(description = "This method tests the getGroups method with pagination request and username under negative " + + "circumstances", expectedExceptions = {GroupManagementException.class}, + expectedExceptionsMessageRegExp = "Error occurred while retrieving all groups accessible to user.") + public void testGetGroupsWithPaginationRequestAndUserName() throws GroupManagementException { + groupManagementProviderService.getGroups("test", TestUtils.createPaginationRequest()); + } + + @Test(description = "This method tests the get roles method under negative circumstances", + expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred " + + "while retrieving all groups in tenant.*") + public void testManageGroupSharing() throws GroupManagementException, RoleDoesNotExistException { + groupManagementProviderService.getRoles(1); + } + + @Test(description = "This method tests the getDeviceCount under negative circumstances.", expectedExceptions = + {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred while retrieving all " + + "groups in tenant.*") + public void testGetDeviceCount() throws GroupManagementException { + groupManagementProviderService.getDeviceCount(1); + } + + @Test(description = "This method tests the getDevices method under negative circumstances", expectedExceptions = + {GroupManagementException.class}) + public void testGetDevicesWithPagination() throws GroupManagementException { + groupManagementProviderService.getDevices(1, 0, 10); + } + + @Test(description = "This method tests the getGroupCount with username when the user name is given as null", + expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Received empty " + + "user name for getGroupCount.*") + public void testGetGroupCountWithUserName2() throws GroupManagementException { + groupManagementProviderService.getGroupCount(null); + } + + @Test(description = "This method tests getGroups method under negative circumstances", + expectedExceptionsMessageRegExp = "Received empty device identifier for getGroups", + expectedExceptions = {GroupManagementException.class}) + public void testGetGroupsWithDeviceIdentifier() throws GroupManagementException { + groupManagementProviderService.getGroups((DeviceIdentifier) null); + } +} 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 11cf8c467b0..6b0ff32523c 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 @@ -54,6 +54,7 @@ +