From 7838eaf97b96ee7afb0931ec6046fd8854a021fc Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 19 Sep 2019 07:59:45 +0530 Subject: [PATCH] Modifying group devices API to retrieve properties --- .../service/api/GroupManagementService.java | 10 +++++++++- .../impl/GroupManagementServiceImpl.java | 4 ++-- .../impl/GroupManagementServiceImplTest.java | 12 ++++++------ .../dao/impl/group/GenericGroupDAOImpl.java | 1 - .../GroupManagementProviderService.java | 2 +- .../GroupManagementProviderServiceImpl.java | 19 ++++++++++++++++++- ...ManagementProviderServiceNegativeTest.java | 2 +- .../GroupManagementProviderServiceTest.java | 2 +- .../impl/GroupManagementServiceImpl.java | 2 +- .../impl/GroupManagementServiceImplTest.java | 6 +++--- 10 files changed, 42 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index d060f3a3df..0e5673dd29 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -681,7 +681,15 @@ public interface GroupManagementService { "pagination index/offset.", defaultValue = "5") @QueryParam("limit") - int limit); + int limit, + + @ApiParam( + name = "requireDeviceProps", + value = "Boolean flag indicating whether to include device properties \n" + + " to the device object.", + required = false) + @QueryParam("requireDeviceProps") + boolean requireDeviceProps); @Path("/id/{groupId}/devices/count") @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 49f0b4ad76..9ba2a67efe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -205,10 +205,10 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getDevicesOfGroup(int groupId, int offset, int limit) { + public Response getDevicesOfGroup(int groupId, int offset, int limit, boolean requireDeviceProps) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - List deviceList = service.getDevices(groupId, offset, limit); + List deviceList = service.getDevices(groupId, offset, limit, requireDeviceProps); int deviceCount = service.getDeviceCount(groupId); DeviceList deviceListWrapper = new DeviceList(); if (deviceList != null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java index 9a4829cb56..c176663514 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -266,16 +266,16 @@ public class GroupManagementServiceImplTest { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt()); - Mockito.doReturn(new ArrayList()).when(groupManagementProviderService).getDevices(1, 0, 10); - Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10); - Response response = groupManagementService.getDevicesOfGroup(1, 0, 10); + Mockito.doReturn(new ArrayList()).when(groupManagementProviderService).getDevices(1, 0, 10, false); + Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10, false); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10, false); + Response response = groupManagementService.getDevicesOfGroup(1, 0, 10, false); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getDevicesOfGroup request failed for a request with valid parameters"); - response = groupManagementService.getDevicesOfGroup(2, 0, 10); + response = groupManagementService.getDevicesOfGroup(2, 0, 10, false); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getDevicesOfGroup request failed for a request with valid parameters"); - response = groupManagementService.getDevicesOfGroup(3, 0, 10); + response = groupManagementService.getDevicesOfGroup(3, 0, 10, false); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "getDevicesOfGroup request succeded for a request with in-valid parameters"); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java index 7ad8b9981e..584a4f9a45 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java @@ -38,7 +38,6 @@ import java.util.List; * This class represents implementation of GroupDAO */ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { - @Override public List getGroups(GroupPaginationRequest request, int tenantId) throws GroupManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 27aa73f358..c347625aba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -164,7 +164,7 @@ public interface GroupManagementProviderService { * @return list of devices in group. * @throws GroupManagementException */ - List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException; + List getDevices(int groupId, int startIndex, int rowCount, boolean requireDeviceProps) throws GroupManagementException; /** * This method is used to retrieve the device count of a given group. 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 e800ff228a..a62bc23c2d 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 @@ -36,6 +36,8 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -56,12 +58,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid private static Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class); private GroupDAO groupDAO; + private DeviceDAO deviceDAO; /** * Set groupDAO from GroupManagementDAOFactory when class instantiate. */ public GroupManagementProviderServiceImpl() { this.groupDAO = GroupManagementDAOFactory.getGroupDAO(); + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); } /** @@ -627,7 +631,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getDevices(int groupId, int startIndex, int rowCount) + public List getDevices(int groupId, int startIndex, int rowCount, boolean requireDeviceProps) throws GroupManagementException { if (log.isDebugEnabled()) { log.debug("Group devices of group: " + groupId + " start index " + startIndex + " row count " + rowCount); @@ -638,6 +642,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid rowCount = DeviceManagerUtil.validateDeviceListPageSize(rowCount); GroupManagementDAOFactory.openConnection(); devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); + + if(requireDeviceProps) { + DeviceManagementDAOFactory.openConnection(); + for (Device device : devices) { + Device retrievedDevice = deviceDAO.getDeviceProps(device.getDeviceIdentifier(), tenantId); + if (retrievedDevice != null && !retrievedDevice.getProperties().isEmpty()) { + device.setProperties(retrievedDevice.getProperties()); + } + } + } } catch (GroupManagementDAOException | SQLException | DeviceManagementException e) { String msg = "Error occurred while getting devices in group."; log.error(msg, e); @@ -648,6 +662,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); + if(requireDeviceProps){ + DeviceManagementDAOFactory.closeConnection(); + } } return devices; } 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 index ccaf84652e..d82ce26d6b 100644 --- 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 @@ -138,7 +138,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage @Test(description = "This method tests the getDevices method under negative circumstances", expectedExceptions = {GroupManagementException.class}) public void testGetDevicesWithPagination() throws GroupManagementException { - groupManagementProviderService.getDevices(1, 0, 10); + groupManagementProviderService.getDevices(1, 0, 10, false); } @Test(description = "This method tests the getGroupCount with username when the user name is given as null", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index e1c3be07cc..5746fed8db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -225,7 +225,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void getDevices() throws GroupManagementException { - List devices = groupManagementProviderService.getDevices(1, 1, 50); + List devices = groupManagementProviderService.getDevices(1, 1, 50, false); Assert.assertNotNull(devices); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index d73533e10a..4e409ae1d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -208,7 +208,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { public Response getDevicesOfGroup(int groupId, int offset, int limit) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - List deviceList = service.getDevices(groupId, offset, limit); + List deviceList = service.getDevices(groupId, offset, limit, false); int deviceCount = service.getDeviceCount(groupId); DeviceList deviceListWrapper = new DeviceList(); if (deviceList != null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java index d3c42b794a..a763b49a98 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.v09.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -266,9 +266,9 @@ public class GroupManagementServiceImplTest { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt()); - Mockito.doReturn(new ArrayList()).when(groupManagementProviderService).getDevices(1, 0, 10); - Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10); + Mockito.doReturn(new ArrayList()).when(groupManagementProviderService).getDevices(1, 0, 10, false); + Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10, false); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10, false); Response response = groupManagementService.getDevicesOfGroup(1, 0, 10); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getDevicesOfGroup request failed for a request with valid parameters");