Modifying group devices API to retrieve properties

3.x.x
Ace 5 years ago
parent fc03c404ad
commit 7838eaf97b

@ -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

@ -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<Device> deviceList = service.getDevices(groupId, offset, limit);
List<Device> deviceList = service.getDevices(groupId, offset, limit, requireDeviceProps);
int deviceCount = service.getDeviceCount(groupId);
DeviceList deviceListWrapper = new DeviceList();
if (deviceList != null) {

@ -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<Device>()).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<Device>()).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");
}

@ -38,7 +38,6 @@ import java.util.List;
* This class represents implementation of GroupDAO
*/
public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, int tenantId)
throws GroupManagementDAOException {

@ -164,7 +164,7 @@ public interface GroupManagementProviderService {
* @return list of devices in group.
* @throws GroupManagementException
*/
List<Device> getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException;
List<Device> getDevices(int groupId, int startIndex, int rowCount, boolean requireDeviceProps) throws GroupManagementException;
/**
* This method is used to retrieve the device count of a given group.

@ -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<Device> getDevices(int groupId, int startIndex, int rowCount)
public List<Device> 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;
}

@ -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",

@ -225,7 +225,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"))
public void getDevices() throws GroupManagementException {
List<Device> devices = groupManagementProviderService.getDevices(1, 1, 50);
List<Device> devices = groupManagementProviderService.getDevices(1, 1, 50, false);
Assert.assertNotNull(devices);
}

@ -208,7 +208,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
public Response getDevicesOfGroup(int groupId, int offset, int limit) {
try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
List<Device> deviceList = service.getDevices(groupId, offset, limit);
List<Device> deviceList = service.getDevices(groupId, offset, limit, false);
int deviceCount = service.getDeviceCount(groupId);
DeviceList deviceListWrapper = new DeviceList();
if (deviceList != null) {

@ -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<Device>()).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<Device>()).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");

Loading…
Cancel
Save