From 48a0801faf81c5f3b092257b4bfa259fe33846b7 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 30 Aug 2019 15:24:19 +0530 Subject: [PATCH] Fixing test failures --- .../GroupManagementProviderServiceImpl.java | 34 +++++++++++++------ .../src/test/resources/sql/CreateH2TestDB.sql | 8 +++++ 2 files changed, 31 insertions(+), 11 deletions(-) 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 fea3a3ed81..e800ff228a 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 @@ -213,8 +213,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), - CarbonContext.getThreadLocalCarbonContext().getTenantId())); + if(deviceGroup != null && deviceGroup.getGroupId() > 0) { + deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), + CarbonContext.getThreadLocalCarbonContext().getTenantId())); + } } catch (GroupManagementDAOException e) { String msg = "Error occurred while obtaining group '" + groupId + "'"; log.error(msg, e); @@ -250,8 +252,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), - CarbonContext.getThreadLocalCarbonContext().getTenantId())); + if(deviceGroup != null && deviceGroup.getGroupId() > 0) { + deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), + CarbonContext.getThreadLocalCarbonContext().getTenantId())); + } } catch (GroupManagementDAOException e) { String msg = "Error occurred while obtaining group with name: '" + groupName + "'"; log.error(msg, e); @@ -280,8 +284,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); deviceGroups = this.groupDAO.getGroups(tenantId); - for(DeviceGroup group : deviceGroups){ - group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + if(deviceGroups != null && !deviceGroups.isEmpty()) { + for (DeviceGroup group : deviceGroups) { + group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + } } } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; @@ -317,8 +323,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); deviceGroups = this.groupDAO.getGroups(request, tenantId); - for(DeviceGroup group : deviceGroups){ - group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + if(deviceGroups != null && !deviceGroups.isEmpty()) { + for (DeviceGroup group : deviceGroups) { + group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + } } } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; @@ -365,7 +373,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } deviceGroups = this.groupDAO.getGroups(roleList, tenantId); for (DeviceGroup deviceGroup : deviceGroups) { - deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), tenantId)); + if(deviceGroup != null && deviceGroup.getGroupId() > 0) { + deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), tenantId)); + } groups.put(deviceGroup.getGroupId(), deviceGroup); } } catch (UserStoreException | SQLException | GroupManagementDAOException e) { @@ -428,8 +438,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); - for(DeviceGroup group : allMatchingGroups){ - group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + if(allMatchingGroups != null && !allMatchingGroups.isEmpty()) { + for (DeviceGroup group : allMatchingGroups) { + group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId)); + } } } catch (GroupManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving all groups in tenant"; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index e236efe7ff..6c77071c0c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -55,6 +55,14 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES ( PRIMARY KEY (DEVICE_TYPE_NAME, DEVICE_IDENTIFICATION, PROPERTY_NAME) ); +CREATE TABLE IF NOT EXISTS GROUP_PROPERTIES ( + GROUP_ID INTEGER NOT NULL, + PROPERTY_NAME VARCHAR(100) DEFAULT 0, + PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, + TENANT_ID VARCHAR(100), + PRIMARY KEY (GROUP_ID, PROPERTY_NAME, TENANT_ID) +); + DROP TABLE IF EXISTS DM_DEVICE_GROUP_MAP; CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP ( ID INTEGER AUTO_INCREMENT NOT NULL,