Merge branch 'master' into 'master'

Fixing issues in the /query-devices API

See merge request entgra/carbon-device-mgt!243
3.x.x
Charitha Goonetilleke 5 years ago
commit 5c2af197d3

@ -681,7 +681,15 @@ public interface GroupManagementService {
"pagination index/offset.", "pagination index/offset.",
defaultValue = "5") defaultValue = "5")
@QueryParam("limit") @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") @Path("/id/{groupId}/devices/count")
@GET @GET

@ -652,10 +652,19 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
List<Device> devices; List<Device> devices;
DeviceList deviceList = new DeviceList(); DeviceList deviceList = new DeviceList();
try { 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(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
devices = dms.getDevicesBasedOnProperties(map.getProperties()); devices = dms.getDevicesBasedOnProperties(map.getProperties());
if(devices == null || devices.isEmpty()){ 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) { } catch (DeviceManagementException e) {
String msg = "Error occurred while searching for devices that matches the provided device properties"; String msg = "Error occurred while searching for devices that matches the provided device properties";

@ -205,10 +205,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response getDevicesOfGroup(int groupId, int offset, int limit) { public Response getDevicesOfGroup(int groupId, int offset, int limit, boolean requireDeviceProps) {
try { try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); 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); int deviceCount = service.getDeviceCount(groupId);
DeviceList deviceListWrapper = new DeviceList(); DeviceList deviceListWrapper = new DeviceList();
if (deviceList != null) { if (deviceList != null) {

@ -266,16 +266,16 @@ public class GroupManagementServiceImplTest {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService"))
.toReturn(groupManagementProviderService); .toReturn(groupManagementProviderService);
Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt()); Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt());
Mockito.doReturn(new ArrayList<Device>()).when(groupManagementProviderService).getDevices(1, 0, 10); Mockito.doReturn(new ArrayList<Device>()).when(groupManagementProviderService).getDevices(1, 0, 10, false);
Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10); Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10, false);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10, false);
Response response = groupManagementService.getDevicesOfGroup(1, 0, 10); Response response = groupManagementService.getDevicesOfGroup(1, 0, 10, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getDevicesOfGroup request failed for a request with valid parameters"); "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(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getDevicesOfGroup request failed for a request with valid parameters"); "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(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"getDevicesOfGroup request succeded for a request with in-valid parameters"); "getDevicesOfGroup request succeded for a request with in-valid parameters");
} }

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

@ -164,7 +164,7 @@ public interface GroupManagementProviderService {
* @return list of devices in group. * @return list of devices in group.
* @throws GroupManagementException * @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. * 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.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; 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.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.GroupDAO;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; 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 static Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class);
private GroupDAO groupDAO; private GroupDAO groupDAO;
private DeviceDAO deviceDAO;
/** /**
* Set groupDAO from GroupManagementDAOFactory when class instantiate. * Set groupDAO from GroupManagementDAOFactory when class instantiate.
*/ */
public GroupManagementProviderServiceImpl() { public GroupManagementProviderServiceImpl() {
this.groupDAO = GroupManagementDAOFactory.getGroupDAO(); this.groupDAO = GroupManagementDAOFactory.getGroupDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
} }
/** /**
@ -627,7 +631,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @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 { throws GroupManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Group devices of group: " + groupId + " start index " + startIndex + " row count " + rowCount); 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); rowCount = DeviceManagerUtil.validateDeviceListPageSize(rowCount);
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); 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) { } catch (GroupManagementDAOException | SQLException | DeviceManagementException e) {
String msg = "Error occurred while getting devices in group."; String msg = "Error occurred while getting devices in group.";
log.error(msg, e); log.error(msg, e);
@ -648,6 +662,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
throw new GroupManagementException(msg, e); throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
if(requireDeviceProps){
DeviceManagementDAOFactory.closeConnection();
}
} }
return devices; return devices;
} }

@ -138,7 +138,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage
@Test(description = "This method tests the getDevices method under negative circumstances", expectedExceptions = @Test(description = "This method tests the getDevices method under negative circumstances", expectedExceptions =
{GroupManagementException.class}) {GroupManagementException.class})
public void testGetDevicesWithPagination() throws GroupManagementException { 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", @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")) @Test(dependsOnMethods = ("createGroup"))
public void getDevices() throws GroupManagementException { 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); Assert.assertNotNull(devices);
} }

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

@ -266,9 +266,9 @@ public class GroupManagementServiceImplTest {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService"))
.toReturn(groupManagementProviderService); .toReturn(groupManagementProviderService);
Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt()); Mockito.doReturn(1).when(groupManagementProviderService).getDeviceCount(Mockito.anyInt());
Mockito.doReturn(new ArrayList<Device>()).when(groupManagementProviderService).getDevices(1, 0, 10); Mockito.doReturn(new ArrayList<Device>()).when(groupManagementProviderService).getDevices(1, 0, 10, false);
Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10); Mockito.doReturn(null).when(groupManagementProviderService).getDevices(2, 0, 10, false);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getDevices(3, 0, 10, false);
Response response = groupManagementService.getDevicesOfGroup(1, 0, 10); Response response = groupManagementService.getDevicesOfGroup(1, 0, 10);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getDevicesOfGroup request failed for a request with valid parameters"); "getDevicesOfGroup request failed for a request with valid parameters");

Loading…
Cancel
Save