From 2665243aa3972453a292aaea08d9bcd7bc912566 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Sun, 5 Apr 2020 16:12:07 +0000 Subject: [PATCH] Change methods to get device info if necessary This change was done in order to fix a issue while reading IOS operations which uses agent(eg: location). Now it's possible to get devices with there properties --- .../pom.xml | 18 ++++ .../mgt/core/scep/SCEPManager.java | 32 ++++++- .../mgt/core/scep/SCEPManagerImpl.java | 64 ++++++++++---- .../carbon/device/mgt/core/dao/DeviceDAO.java | 26 +++++- .../core/dao/impl/AbstractDeviceDAOImpl.java | 88 ++++++++++++++----- .../DeviceManagementProviderService.java | 34 ++++++- .../DeviceManagementProviderServiceImpl.java | 53 +++++++---- .../DeviceManagementProviderServiceTest.java | 25 +++++- 8 files changed, 271 insertions(+), 69 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml index 0ef334c5dc..23c91b7c60 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml @@ -16,6 +16,23 @@ ~ specific language governing permissions and limitations ~ under the License. --> + @@ -58,6 +75,7 @@ org.osgi.framework, org.osgi.service.component, org.apache.commons.logging, + org.apache.commons.collections.map, javax.security.auth.x500, javax.xml.*, javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManager.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManager.java index 511f228054..9c7b866ef4 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManager.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManager.java @@ -15,10 +15,40 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.certificate.mgt.core.scep; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.user.api.UserStoreException; public interface SCEPManager { - TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException; + /** + * This method is used to retrieve a device of a given identifier wrapped by a + * {@link TenantedDeviceWrapper} with it's tenant info + * + * @param deviceIdentifier device identifier + * @return {@link TenantedDeviceWrapper} with a device's info and tenant info + * @throws SCEPException will be thrown in case device is null or if a + * {@link DeviceManagementException} or a {@link UserStoreException} is thrown + */ + TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) + throws SCEPException; } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManagerImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManagerImpl.java index 2917176d54..0e0b284a2c 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManagerImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/scep/SCEPManagerImpl.java @@ -15,8 +15,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.certificate.mgt.core.scep; +import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.certificate.mgt.core.internal.CertificateManagementDataHolder; @@ -29,8 +48,6 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import java.util.HashMap; - public class SCEPManagerImpl implements SCEPManager { private static final Log log = LogFactory.getLog(SCEPManagerImpl.class); DeviceManagementProviderService dms; @@ -40,21 +57,26 @@ public class SCEPManagerImpl implements SCEPManager { } @Override - public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) throws SCEPException { - TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper(); - try { - HashMap deviceHashMap = dms.getTenantedDevice(deviceIdentifier); - Object[] keySet = deviceHashMap.keySet().toArray(); + public TenantedDeviceWrapper getValidatedDevice(DeviceIdentifier deviceIdentifier) + throws SCEPException { + SingletonMap deviceMap; - if (keySet.length == 0) { - throw new SCEPException("Lookup device not found for the device identifier"); + try { + deviceMap = dms.getTenantedDevice(deviceIdentifier, false); + if (deviceMap == null) { + String msg = "Lookup device not found for the device identifier " + deviceIdentifier.getId() + + " of device type " + deviceIdentifier.getType(); + log.error(msg); + throw new SCEPException(msg); } - Integer tenantId = (Integer) keySet[0]; - tenantedDeviceWrapper.setDevice(deviceHashMap.get(tenantId)); - tenantedDeviceWrapper.setTenantId(tenantId); - + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting device " + deviceIdentifier; + log.error(msg); + throw new SCEPException(msg, e); + } + try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); @@ -66,16 +88,22 @@ public class SCEPManagerImpl implements SCEPManager { log.error(msg); throw new SCEPException(msg); } - + TenantedDeviceWrapper tenantedDeviceWrapper = new TenantedDeviceWrapper(); + int tenantId = (int) deviceMap.getKey(); String tenantDomain = realmService.getTenantManager().getDomain(tenantId); + + tenantedDeviceWrapper.setTenantId(tenantId); tenantedDeviceWrapper.setTenantDomain(tenantDomain); + tenantedDeviceWrapper.setDevice((Device) deviceMap.getValue()); + + return tenantedDeviceWrapper; + } catch (UserStoreException e) { - throw new SCEPException("Error occurred while getting the tenant domain.", e); - } catch (DeviceManagementException e) { - throw new SCEPException("Error occurred while getting device '" + deviceIdentifier + "'.", e); + String msg = "Error occurred while getting the tenant domain."; + log.error(msg); + throw new SCEPException(msg, e); } finally { PrivilegedCarbonContext.endTenantFlow(); } - return tenantedDeviceWrapper; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 3cfa66baf1..316762012b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -32,9 +32,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.dao; +import org.apache.commons.collections.map.SingletonMap; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -50,7 +68,6 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import java.sql.SQLException; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -258,12 +275,13 @@ public interface DeviceDAO { throws DeviceManagementDAOException; /** + * This method is used to retrieve a device of a given identifier with it's tenant id * * @param deviceIdentifier device id. - * @return HashMap - * @throws DeviceManagementDAOException + * @return {@link SingletonMap} with device and corresponding tenant id + * @throws DeviceManagementDAOException will be thrown in case of a {@link SQLException} */ - HashMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; + SingletonMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; /** * This method is used to retrieve a device of a given tenant id. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 1a8e168730..7abfd7babf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -33,9 +33,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.dao.impl; +import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -45,7 +63,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.configuration.mgt.DevicePropertyInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceData; -import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -64,7 +81,6 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringJoiner; @@ -647,34 +663,58 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override - public HashMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - Device device; - HashMap deviceHashMap = new HashMap<>(); + public SingletonMap getDevice(DeviceIdentifier deviceIdentifier) + throws DeviceManagementDAOException { try { - conn = this.getConnection(); - String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, e.TENANT_ID, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.DESCRIPTION, d.NAME, " + - "t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + - "t.NAME = ? AND t.ID = d.DEVICE_TYPE_ID AND d.DEVICE_IDENTIFICATION = ? ) d1 WHERE d1.ID = e.DEVICE_ID ORDER BY e.DATE_OF_LAST_UPDATE DESC"; - stmt = conn.prepareStatement(sql); + Connection conn = this.getConnection(); + String sql = "SELECT d1.ID AS DEVICE_ID, " + + "d1.DESCRIPTION, " + + "d1.NAME AS DEVICE_NAME, " + + "d1.DEVICE_TYPE, " + + "e.TENANT_ID, " + + "d1.DEVICE_IDENTIFICATION, " + + "e.OWNER, " + + "e.OWNERSHIP, " + + "e.STATUS, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, " + + "e.ID AS ENROLMENT_ID " + + "FROM DM_ENROLMENT e, " + + "(SELECT d.ID, " + + "d.DESCRIPTION, " + + "d.NAME, " + + "t.NAME AS DEVICE_TYPE, " + + "d.DEVICE_IDENTIFICATION " + + "FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t " + + "WHERE t.NAME = ? " + + "AND t.ID = d.DEVICE_TYPE_ID " + + "AND d.DEVICE_IDENTIFICATION = ?) d1 " + + "WHERE d1.ID = e.DEVICE_ID " + + "ORDER BY e.DATE_OF_LAST_UPDATE DESC"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, deviceIdentifier.getType()); stmt.setString(2, deviceIdentifier.getId()); - rs = stmt.executeQuery(); - if (rs.next()) { - device = DeviceManagementDAOUtil.loadDevice(rs); - deviceHashMap.put(rs.getInt("TENANT_ID"), device); + + try (ResultSet rs = stmt.executeQuery()) { + SingletonMap deviceMap = null; + if (rs.next()) { + deviceMap = new SingletonMap( + rs.getInt("TENANT_ID"), + DeviceManagementDAOUtil.loadDevice(rs) + ); + } + return deviceMap; + } } + } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while listing devices for type " + - "'" + deviceIdentifier.getType() + "'", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + String msg = "Error occurred while listing devices (with tenant id) for type " + + deviceIdentifier.getType(); + log.error(msg); + throw new DeviceManagementDAOException(msg, e); } - return deviceHashMap; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 209ae86bba..86f6c74f75 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -32,9 +32,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.service; +import org.apache.commons.collections.map.SingletonMap; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.FeatureManager; @@ -67,13 +85,14 @@ import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecu import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.common.ui.policy.mgt.PolicyConfigurationManager; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; +import java.sql.SQLException; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -564,7 +583,18 @@ public interface DeviceManagementProviderService { */ int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException; - HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + /** + * This method is used to retrieve a device of a given identifier with it's tenant id + * + * @param deviceIdentifier device identifier + * @param requireDeviceInfo boolean indicating whether the device info and properties + * is also required + * @return {@link SingletonMap} with a device and it's corresponding tenant id + * @throws DeviceManagementException will be thrown in case of a {@link SQLException} + * or {@link DeviceManagementDAOException} + */ + SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo) + throws DeviceManagementException; void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, ConfigurationManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index ba5e26af51..8293f3c14c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -32,8 +32,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.service; +import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -1174,43 +1193,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + public SingletonMap getTenantedDevice(DeviceIdentifier deviceIdentifier, boolean requireDeviceInfo) + throws DeviceManagementException { if (deviceIdentifier == null) { String msg = "Received null deviceIdentifier for getTenantedDevice"; log.error(msg); throw new DeviceManagementException(msg); } if (log.isDebugEnabled()) { - log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" + - deviceIdentifier.getType() + "'"); + log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type " + + deviceIdentifier.getType()); } - HashMap deviceHashMap; + + SingletonMap deviceMap; try { DeviceManagementDAOFactory.openConnection(); - deviceHashMap = deviceDAO.getDevice(deviceIdentifier); - if (deviceHashMap == null) { + deviceMap = deviceDAO.getDevice(deviceIdentifier); + if (deviceMap == null) { if (log.isDebugEnabled()) { - log.debug("No device is found upon the type '" + deviceIdentifier.getType() + "' and id '" + - deviceIdentifier.getId() + "'"); + log.debug("Unable to find device for type " + deviceIdentifier.getType() + + " and id " + deviceIdentifier.getId()); } - return new HashMap<>(); } + } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while obtaining the device for id '" + deviceIdentifier.getId() + "'"; + String msg = "Error occurred while obtaining the device for id " + deviceIdentifier.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in getTenantedDevice device: " + deviceIdentifier.getId(); + String msg = "Error occurred while opening a connection for the data source"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } - return deviceHashMap; + + if (requireDeviceInfo && deviceMap != null) { + getAllDeviceInfo((Device) deviceMap.getValue()); + } + return deviceMap; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 5e7d70680c..1fcce383aa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -33,9 +33,27 @@ * specific language governing permissions and limitations * under the License. */ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.service; +import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mockito.Mockito; @@ -82,7 +100,6 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Properties; @@ -732,11 +749,11 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) public void testGetTenantedDevice() throws DeviceManagementException { - HashMap deviceMap = deviceMgtService.getTenantedDevice(new + SingletonMap deviceMap = deviceMgtService.getTenantedDevice(new DeviceIdentifier - (DEVICE_ID, DEVICE_TYPE)); + (DEVICE_ID, DEVICE_TYPE), false); if (!isMock()) { - Assert.assertTrue(!deviceMap.isEmpty()); + Assert.assertFalse(deviceMap.isEmpty()); } }