Covering more database specific method calls.

revert-70aa11f8
sinthuja 7 years ago
parent 4d2d7b9ef5
commit 0252a5f2eb

@ -105,16 +105,6 @@ public interface DeviceDAO {
*/ */
boolean updateDevice(Device device, int tenantId) throws DeviceManagementDAOException; boolean updateDevice(Device device, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to remove a device.
*
* @param deviceId id of the device that should be removed.
* @param tenantId tenant id.
* @return returns the id of removed device.
* @throws DeviceManagementDAOException
*/
int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve a device of a given device-identifier and tenant-id. * This method is used to retrieve a device of a given device-identifier and tenant-id.
* *
@ -212,16 +202,6 @@ public interface DeviceDAO {
*/ */
List<Device> getDevices(PaginationRequest request, int tenantId) throws DeviceManagementDAOException; List<Device> getDevices(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the devices of a given tenant and type as a paginated result.
*
* @param request PaginationRequest object holding the data for pagination and search.
* @param tenantId tenant id.
* @return returns paginated list of devices of provided type.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByType(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve all the devices of a given tenant and device type. * This method is used to retrieve all the devices of a given tenant and device type.
* *
@ -249,7 +229,7 @@ public interface DeviceDAO {
* @param username user name. * @param username user name.
* @param type device type. * @param type device type.
* @param tenantId tenant id. * @param tenantId tenant id.
* @return * @return List of devices.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<Device> getDevicesOfUser(String username, String type, int tenantId) throws DeviceManagementDAOException; List<Device> getDevicesOfUser(String username, String type, int tenantId) throws DeviceManagementDAOException;
@ -371,16 +351,6 @@ public interface DeviceDAO {
EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser, EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser,
int tenantId) throws DeviceManagementDAOException; int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve current enrollment of a given device.
*
* @param deviceId device id.
* @param tenantId tenant id.
* @return returns EnrolmentInfo object.
* @throws DeviceManagementDAOException
*/
EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException;
/** /**
* This method is used to retrieve current active enrollment of a given device and tenant id. * This method is used to retrieve current active enrollment of a given device and tenant id.
* *
@ -423,29 +393,6 @@ public interface DeviceDAO {
List<Device> getDevicesByStatus(PaginationRequest request, int tenantId) List<Device> getDevicesByStatus(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException; throws DeviceManagementDAOException;
/**
* This method is used to retrieve the enrollment id of a given device and status.
*
* @param deviceId device id.
* @param status enrollment status.
* @param tenantId tenant id.
* @return returns the id of current enrollment.
* @throws DeviceManagementDAOException
*/
int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve the enrollment info of a given list of devices and status.
*
* @param deviceIds A list of device identifiers.
* @param status enrollment status.
* @param tenantId tenant id.
* @return returns a list of enrolment info objects.
* @throws DeviceManagementDAOException
*/
List<EnrolmentInfo> getEnrolmentsByStatus(List<DeviceIdentifier> deviceIds, Status status,
int tenantId) throws DeviceManagementDAOException;
List<Integer> getDeviceEnrolledTenants() throws DeviceManagementDAOException; List<Integer> getDeviceEnrolledTenants() throws DeviceManagementDAOException;
} }

@ -104,11 +104,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
} }
@Override
public int removeDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException {
return 0;
}
@Override @Override
public Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException { public Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
@ -914,37 +909,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
} }
@Override
public EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " +
"FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " +
"AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " +
"AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType());
stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = DeviceManagementDAOUtil.loadMatchingEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override @Override
public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
@ -977,81 +941,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
} }
} }
public int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "SELECT e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) dtm " +
"WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?;";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType());
stmt.setInt(3, tenantId);
stmt.setString(4, status.toString());
stmt.setInt(5, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt("ENROLMENT_ID");
} else {
return -1; // if no results found
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"id of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
public List<EnrolmentInfo> getEnrolmentsByStatus(List<DeviceIdentifier> deviceIds, Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<EnrolmentInfo> enrolments = new ArrayList<>();
try {
conn = this.getConnection();
StringBuilder sql = new StringBuilder();
sql.append("SELECT e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, " +
"e.STATUS FROM DM_ENROLMENT e WHERE e.DEVICE_ID IN (SELECT d.ID FROM DM_DEVICE d " +
"WHERE d.DEVICE_IDENTIFICATION IN (");
// adding arguments to the sql query
Iterator iterator = deviceIds.iterator();
while (iterator.hasNext()) {
iterator.next();
sql.append(" ?");
if (iterator.hasNext()) {
sql.append(",");
}
}
sql.append(") AND d.TENANT_ID = ?) AND e.STATUS = ? AND e.TENANT_ID = ?");
stmt = conn.prepareStatement(sql.toString());
int index = 1;
for (DeviceIdentifier id : deviceIds) {
stmt.setString(index++, id.getId());
}
stmt.setInt(index++, tenantId);
stmt.setString(index++, status.toString());
stmt.setInt(index, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs));
}
return enrolments;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"ids of devices", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn; Connection conn;

@ -153,41 +153,6 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; return devices;
} }
@Override
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"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, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
}
@Override @Override
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId) public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {

@ -159,42 +159,6 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; return devices;
} }
@Override
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, "
+ "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, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, "
+ "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? "
+ "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY ENROLMENT_ID"
+ " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
}
@Override @Override
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId) public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
@ -401,36 +365,6 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; return devices;
} }
public int getEnrolmentByStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "SELECT e.ID ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) dtm " +
"WHERE e.DEVICE_ID = dtm.ID AND e.STATUS = ? AND e.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType());
stmt.setInt(3, tenantId);
stmt.setString(4, status.toString());
stmt.setInt(5, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt("ENROLMENT_ID");
} else {
return -1; // if no results found
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"id of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }

@ -140,41 +140,6 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; return devices;
} }
@Override
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"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, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getRowCount());
stmt.setInt(5, request.getStartIndex());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
}
@Override @Override
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId) public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {

@ -156,42 +156,6 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
return devices; return devices;
} }
@Override
public List<Device> getDevicesByType(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"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, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " +
"AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? ORDER BY ENROLMENT_ID" +
" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getDeviceType());
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + request.getDeviceType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
}
@Override @Override
public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId) public List<Device> getDevicesOfUser(PaginationRequest request, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {

@ -739,13 +739,18 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
public void testGetDeviesOfUserPaginated() throws DeviceManagementException { public void testGetDeviesOfUserPaginated() throws DeviceManagementException, NoSuchFieldException,
if (!isMock()) { IllegalAccessException {
PaginationRequest request = new PaginationRequest(0, 100); PaginationRequest request = new PaginationRequest(0, 100);
request.setOwner("admin"); request.setOwner("admin");
PaginationResult result = deviceMgtService.getDevicesOfUser(request, true); MockDataSource dataSource = setDatasourceForGetDevice();
Assert.assertTrue(result.getRecordsTotal() > 0); if (dataSource != null){
setMockDeviceCount(dataSource.getConnection(0));
} }
PaginationResult result = deviceMgtService.getDevicesOfUser(request, true);
cleanupMockDatasource(dataSource);
Assert.assertTrue(result.getRecordsTotal() > 0);
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}, expectedExceptions = @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}, expectedExceptions =
@ -756,13 +761,18 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})
public void testGetDeviesByOwnership() throws DeviceManagementException { public void testGetDeviesByOwnership() throws DeviceManagementException, NoSuchFieldException,
if (!isMock()) { IllegalAccessException {
PaginationRequest request = new PaginationRequest(0, 100); PaginationRequest request = new PaginationRequest(0, 100);
request.setOwnership(EnrolmentInfo.OwnerShip.BYOD.toString()); request.setOwnership(EnrolmentInfo.OwnerShip.BYOD.toString());
PaginationResult result = deviceMgtService.getDevicesByOwnership(request); MockDataSource dataSource = setDatasourceForGetDevice();
Assert.assertTrue(result.getRecordsTotal() > 0); if (dataSource != null){
setMockDeviceCount(dataSource.getConnection(0));
} }
PaginationResult result = deviceMgtService.getDevicesByOwnership(request);
cleanupMockDatasource(dataSource);
Assert.assertTrue(result.getRecordsTotal() > 0);
} }
@Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"}) @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})

Loading…
Cancel
Save