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 d060f3a3df9..0e5673dd299 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/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 33cf2ec35be..91770d28ea7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -652,10 +652,19 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { List devices; DeviceList deviceList = new DeviceList(); try { + if(map.getProperties().isEmpty()){ + if (log.isDebugEnabled()) { + log.debug("No search criteria defined when querying devices."); + } + return Response.status(Response.Status.BAD_REQUEST).entity("No search criteria defined.").build(); + } DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); devices = dms.getDevicesBasedOnProperties(map.getProperties()); if(devices == null || devices.isEmpty()){ - return Response.status(Response.Status.OK).entity("No device found matching query criteria.").build(); + if (log.isDebugEnabled()) { + log.debug("No Devices Found for criteria : " + map); + } + return Response.status(Response.Status.NOT_FOUND).entity("No device found matching query criteria.").build(); } } catch (DeviceManagementException e) { String msg = "Error occurred while searching for devices that matches the provided device properties"; 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 49f0b4ad767..9ba2a67efe5 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 9a4829cb568..c1766635143 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 7ad8b9981ee..584a4f9a455 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 27aa73f3584..c347625abae 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 e800ff228a2..a62bc23c2d8 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 ccaf84652e7..d82ce26d6b2 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 e1c3be07cc0..5746fed8db6 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 d73533e10a7..4e409ae1d7c 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 d3c42b794a9..a763b49a989 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");