Device Organization unique key add, get all operation and test optimization

pull/238/head
Isuri Mendis 1 year ago
parent cfb47a1ebb
commit 63ae04e858

@ -48,6 +48,13 @@ public interface DeviceOrganizationDAO {
*/
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException;
/**
* get All Device Organization records
* @return
* @throws DeviceOrganizationMgtDAOException
*/
List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtDAOException;
/**
* add a new reocrd to device organization table
*

@ -19,10 +19,8 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.BadRequestDaoException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import org.apache.commons.logging.Log;
@ -33,6 +31,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
@ -40,6 +39,7 @@ import java.util.List;
import java.util.Set;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganization;
public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
@ -49,10 +49,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
public List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException {
List<DeviceNode> childNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
// Input validation
if (node == null || maxDepth < 0) {
throw new BadRequestDaoException("Invalid input parameters.");
}
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice);
@ -107,9 +104,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
@Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException {
if (node == null || maxDepth <= 0) {
throw new BadRequestDaoException("Invalid input parameters.");
}
List<DeviceNode> parentNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
try {
@ -169,12 +164,35 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
@Override
public List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtDAOException {
List<DeviceOrganization> deviceOrganizations = new ArrayList<>();
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceOrganization deviceOrganization = loadDeviceOrganization(rs);
deviceOrganizations.add(deviceOrganization);
}
}
}
return deviceOrganizations;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieving all device organizations details.";
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to retrieving all device organizations.";
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtDAOException {
if (deviceOrganization == null || deviceOrganization.getDeviceId() <= 0 || deviceOrganization.getParentDeviceId() <= 0) {
throw new BadRequestDaoException("Invalid input parameters.");
}
try {
String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP)" +
@ -185,11 +203,14 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceOrganization.getDeviceId());
stmt.setInt(2, deviceOrganization.getParentDeviceId());
if (deviceOrganization.getParentDeviceId() != null) {
stmt.setInt(2, deviceOrganization.getParentDeviceId());
} else {
stmt.setNull(2, Types.INTEGER);
}
stmt.setTimestamp(3, timestamp);
return stmt.executeUpdate() > 0;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to insert device organization for " +
deviceOrganization.getDeviceId();
@ -238,11 +259,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
public boolean updateDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtDAOException {
DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId());
if (deviceOrganization == null) {
return false;
}
if (deviceOrganization.getDeviceId() == 0 || deviceOrganization.getParentDeviceId() == 0) {
if (organization == null || deviceOrganization.getDeviceId() <= 0 ||
!(deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0)) {
return false;
}
try {
@ -254,7 +273,11 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceOrganization.getDeviceId());
stmt.setInt(2, deviceOrganization.getParentDeviceId());
if (deviceOrganization.getParentDeviceId() != null) {
stmt.setInt(2, deviceOrganization.getParentDeviceId());
} else {
stmt.setNull(2, Types.INTEGER);
}
stmt.setTimestamp(3, timestamp);
stmt.setInt(4, deviceOrganization.getOrganizationId());
return stmt.executeUpdate() > 0;
@ -284,21 +307,21 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
stmt.setInt(1, organizationId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return DeviceOrganizationDaoUtil.loadDeviceOrganization(rs);
return loadDeviceOrganization(rs);
}
log.info("No Device Organization found");
log.info("No Device Organization found for retrieval for organizationID = " + organizationId);
return null;
}
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to get device organization details for " +
organizationId;
"organizationID = " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to get device organization details for " +
organizationId;
"organizationID = " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}

@ -28,7 +28,11 @@ public class DeviceOrganizationDaoUtil {
};
deviceOrganization.setOrganizationId(rs.getInt("ID"));
deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID"));
deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID"));
if (rs.getInt("PARENT_DEVICE_ID") != 0) {
deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID"));
} else {
deviceOrganization.setParentDeviceId(null);
}
deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP"));
return deviceOrganization;
}

@ -28,7 +28,7 @@ public abstract class DeviceOrganization {
private int organizationId;
private int deviceId;
private int parentDeviceId;
private Integer parentDeviceId;
private Date updateTime;
public int getOrganizationId() {
@ -47,11 +47,11 @@ public abstract class DeviceOrganization {
this.deviceId = deviceId;
}
public int getParentDeviceId() {
public Integer getParentDeviceId() {
return parentDeviceId;
}
public void setParentDeviceId(int parentDeviceId) {
public void setParentDeviceId(Integer parentDeviceId) {
this.parentDeviceId = parentDeviceId;
}

@ -27,7 +27,6 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.excep
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.util.LockManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -47,18 +46,25 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
public List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException {
if (node == null || node.getDeviceId() <= 0 || maxDepth < 0) {
throw new BadRequestException("Invalid input parameters.");
String msg = "Invalid input parameters for retrieving child devices : " +
"deviceID = " + (node != null ? node.getDeviceId() : null) + ", maxDepth = " + maxDepth +
", includeDevice = " + includeDevice;
throw new BadRequestException(msg);
}
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
String msg = "Error occurred while obtaining the database connection to retrieve child devices : " +
"deviceID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " +
includeDevice;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving child devices";
String msg = "Error occurred in the database level while retrieving child devices : " +
"deviceID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " +
includeDevice;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -70,19 +76,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException {
if (node == null || node.getDeviceId() <= 0 || maxDepth < 0) {
throw new BadRequestException("Invalid input parameters.");
if (node == null || node.getDeviceId() <= 0 || maxDepth <= 0) {
String msg = "Invalid input parameters for retrieving parent devices. Params : " +
"deviceID = " + (node != null ? node.getDeviceId() : null) + ", maxDepth = " + maxDepth +
", includeDevice = " + includeDevice;
throw new BadRequestException(msg);
}
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getParentsOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve parent devices";
String msg = "Error occurred while obtaining the database connection to retrieve parent devices for : " +
"device ID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " +
includeDevice;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving parent devices";
String msg = "Error occurred in the database level while retrieveing parent devices for : " +
"device ID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " +
includeDevice;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -91,76 +104,90 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
}
// In DeviceOrganizationServiceImpl.java (implementation)
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtPluginException {
if (deviceOrganization == null || deviceOrganization.getDeviceId() == 0
|| deviceOrganization.getParentDeviceId() == 0) {
throw new BadRequestException("Invalid input parameters.");
}
String msg = "";
// Check if an organization with the same deviceId and parentDeviceId already exists
if (organizationExists(deviceOrganization.getDeviceId(), deviceOrganization.getParentDeviceId())) {
msg = "Device organization with the same deviceId and parentDeviceId already exists.";
public List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException {
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getAllDeviceOrganizations();
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve all device organizations.";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving all device organizations.";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
// Close the database connection
ConnectionManagerUtil.closeDBConnection();
}
}
// Use LockManager to get a lock object based on deviceId and parentDeviceId
// LockManager lockManager = LockManager.getInstance();
// Object lock = lockManager.getLock(deviceOrganization.getDeviceId(), deviceOrganization.getParentDeviceId());
//
// synchronized (lock) {
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtPluginException {
if (deviceOrganization == null || deviceOrganization.getDeviceId() <= 0 ||
!(deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0)) {
throw new BadRequestException("Invalid input parameters for adding deviceOrganizations : " +
"deviceOrganization = " + deviceOrganization +
", deviceID = " + "deviceID should be a positive number"
+ "parentDeviceID = " + "parentDeviceID should be a positive number or null");
}
String msg;
int deviceID = deviceOrganization.getDeviceId();
int parentDeviceID = deviceOrganization.getParentDeviceId();
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
if (result) {
msg = "Device organization added successfully, for device ID " + deviceOrganization.getDeviceId() +
" and parent device ID " + deviceOrganization.getParentDeviceId();
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to add, for device ID " + deviceOrganization.getDeviceId() +
" and parent device ID " + deviceOrganization.getParentDeviceId();
throw new DeviceOrganizationMgtPluginException(msg);
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
if (result) {
msg = "Device organization added successfully. Device Organization details : " +
"deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
if (log.isDebugEnabled()) {
log.debug(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to add device organization for device ID "
+ deviceOrganization.getDeviceId() + " and parent device ID"
+ deviceOrganization.getParentDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while adding device organization for device ID " +
deviceOrganization.getDeviceId() + " and parent device ID"
+ deviceOrganization.getParentDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
msg = "Device organization failed to add. Device Organization details : " +
"deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
throw new DeviceOrganizationMgtPluginException(msg);
}
// }
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to add device organization. " +
"Device Organization details : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while adding device organization. "
+ "Device Organization details : device ID = " + deviceID + ", parent device ID = " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
// Helper method to check if an organization with the same deviceId and parentDeviceId already exists
// Helper method to check if an organization with the same deviceID and parentDeviceID already exists
@Override
public boolean organizationExists(int deviceId, int parentDeviceId) throws DeviceOrganizationMgtPluginException {
public boolean organizationExists(int deviceID, int parentDeviceID) throws DeviceOrganizationMgtPluginException {
try {
ConnectionManagerUtil.openDBConnection();
boolean exists = deviceOrganizationDao.organizationExists(deviceId, parentDeviceId);
return exists;
return deviceOrganizationDao.organizationExists(deviceID, parentDeviceID);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to check organization existence.";
String msg = "Error occurred while obtaining the database connection to check organization existence. " +
"Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while checking organization existence.";
String msg = "Error occurred in the database level while checking organization existence. " +
"Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -171,14 +198,21 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override
public boolean updateDeviceOrganization(DeviceOrganization organization)
throws DeviceOrganizationMgtPluginException {
if (organization == null || organization.getOrganizationId() <= 0) {
throw new BadRequestException("Invalid input parameters.");
if (organization == null || organization.getOrganizationId() <= 0 || organization.getDeviceId() <= 0
|| !(organization.getParentDeviceId() == null || organization.getParentDeviceId() > 0)) {
throw new BadRequestException("Invalid input parameters for deviceOrganization update. : "
+ "deviceOrganization = " + organization
+ ", deviceID = " + (organization != null ? organization.getDeviceId() :
"deviceID should be a positive number")
+ ", parentDeviceID = parentDeviceID should be a positive number or null"
+ ", organizationID = " + (organization != null ? organization.getOrganizationId() : null)
);
}
String msg = "";
String msg;
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organization.getOrganizationId());
if (deviceOrganization == null) {
String errorMsg = "Cannot find device organization for organization ID " + organization.getOrganizationId();
log.error(errorMsg);
msg = "Cannot find device organization for organizationID = " + organization.getOrganizationId();
log.error(msg);
return false;
}
@ -186,26 +220,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.updateDeviceOrganization(organization);
if (result) {
msg = "Device organization updated successfully,for " + organization.getOrganizationId();
msg = "Device organization updated successfully for organizationID = " + organization.getOrganizationId();
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to update,for " + organization.getOrganizationId();
msg = "Device organization failed to update for organizationID = " + organization.getOrganizationId();
throw new DeviceOrganizationMgtPluginException(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to update device organization for " +
organization.getOrganizationId();
"organizationID = " + organization.getOrganizationId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while updating device organization for " +
organization.getOrganizationId();
"organizationID = " + organization.getOrganizationId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -214,22 +248,25 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
@Override
public DeviceOrganization getDeviceOrganizationByID(int organizationId)
public DeviceOrganization getDeviceOrganizationByID(int organizationID)
throws DeviceOrganizationMgtPluginException {
if (organizationId <= 0) {
throw new BadRequestException("Invalid input parameters.");
if (organizationID <= 0) {
throw new BadRequestException("organizationID must be a positive number. " +
"Invalid input parameters for getting deviceOrganization for : "
+ "organizationID = " + organizationID);
}
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
DeviceOrganization deviceOrganization = deviceOrganizationDao.getDeviceOrganizationByID(organizationId);
return deviceOrganization;
return deviceOrganizationDao.getDeviceOrganizationByID(organizationID);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
String msg = "Error occurred while obtaining the database connection to retrieve deviceOrganization for : "
+ "organizationID = " + organizationID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving child devices";
String msg = "Error occurred in the database level while retrieving deviceOrganization for : "
+ "organizationID = " + organizationID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -239,42 +276,46 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
@Override
public boolean deleteDeviceOrganizationByID(int organizationId)
public boolean deleteDeviceOrganizationByID(int organizationID)
throws DeviceOrganizationMgtPluginException {
if (organizationId <= 0) {
throw new BadRequestException("Invalid input parameters.");
if (organizationID <= 0) {
throw new BadRequestException("organizationID must be a positive number." +
"Invalid input parameters for deviceOrganization Deletion : " +
"organizationID = " + organizationID);
}
String msg = "";
String msg;
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationId);
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationID);
if (deviceOrganization == null) {
msg = "Cannot find device organization for organization ID " + organizationId;
msg = "Cannot find device organization for Deletion : organizationID = " + organizationID;
log.error(msg);
return false;
}
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationId);
boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationID);
if (result) {
msg = "Device organization record deleted successfully,for " + organizationId;
msg = "Device organization record deleted successfully for organizationID = " + organizationID;
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to delete,for " + organizationId;
msg = "Device organization failed to delete for organizationID = " + organizationID;
throw new DeviceOrganizationMgtPluginException(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to delete device organization for " + organizationId;
msg = "Error occurred while obtaining the database connection to delete device organization for " +
"organizationID = " + organizationID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while deleting device organization for " + organizationId;
msg = "Error occurred in the database level while deleting device organization for " +
"organizationID = " + organizationID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -284,42 +325,45 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
@Override
public boolean deleteDeviceAssociations(int deviceId)
public boolean deleteDeviceAssociations(int deviceID)
throws DeviceOrganizationMgtPluginException {
if (deviceId <= 0) {
throw new BadRequestException("Invalid input parameters.");
if (deviceID <= 0) {
throw new BadRequestException("deviceID must be a positive number." +
"Invalid input parameters for deviceID = " + deviceID);
}
String msg = "";
String msg;
boolean deviceIdExist = doesDeviceIdExist(deviceId);
boolean deviceIdExist = doesDeviceIdExist(deviceID);
if (!deviceIdExist) {
msg = "Cannot find device organization associated with device ID " + deviceId;
msg = "Cannot find device organizations associated with deviceID = " + deviceID;
log.error(msg);
return false;
}
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceId);
boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceID);
if (result) {
msg = "Device organization records deleted successfully,for " + deviceId;
msg = "Device organization records associated with deviceID = " + deviceID + " are deleted successfully.";
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to delete,for " + deviceId;
msg = "Device organization records associated with deviceID = " + deviceID + " have failed to delete.";
throw new DeviceOrganizationMgtPluginException(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to delete device organization for " + deviceId;
msg = "Error occurred while obtaining the database connection to delete device organizations associated with " +
"deviceID = " + deviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while deleting device organization for " + deviceId;
msg = "Error occurred in the database level while deleting device organizations associated with " +
"deviceID = " + deviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -329,22 +373,25 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
@Override
public boolean doesDeviceIdExist(int deviceId)
public boolean doesDeviceIdExist(int deviceID)
throws DeviceOrganizationMgtPluginException {
if (deviceId <= 0) {
throw new BadRequestException("Invalid input parameters.");
if (deviceID <= 0) {
throw new BadRequestException("deviceID must be a positive number." +
"Invalid input parameters for checking deviceID existence " +
"in deviceOrganization : deviceID = " + deviceID);
}
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
boolean deviceIdExist = deviceOrganizationDao.doesDeviceIdExist(deviceId);
return deviceIdExist;
return deviceOrganizationDao.doesDeviceIdExist(deviceID);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to check deviceID exists";
String msg = "Error occurred while obtaining the database connection to check deviceID existence " +
"in deviceOrganization : deviceID = " + deviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while checking the existence of deviceID";
String msg = "Error occurred in the database level while checking the existence " +
"in deviceOrganization : deviceID = " + deviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {

@ -21,7 +21,6 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.D
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException;
import java.sql.Date;
import java.util.List;
public interface DeviceOrganizationService {
@ -35,6 +34,8 @@ public interface DeviceOrganizationService {
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException;
List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException;
DeviceOrganization getDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException;

@ -2,16 +2,10 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class DAONegativeTest extends BaseDeviceOrganizationTest {
@ -25,35 +19,35 @@ public class DAONegativeTest extends BaseDeviceOrganizationTest {
log.info("DAO test initialized");
}
@Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the addDeviceOrganization method under negative circumstances with null input")
public void testAddDeviceOrganizationWithNullInput() throws DeviceOrganizationMgtDAOException {
DeviceOrganization invalidDeviceOrg = null;
deviceOrganizationDAO.addDeviceOrganization(invalidDeviceOrg);
}
@Test(description = "Test with invalid input parameters (bad request)")
public void testGetChildrenOfWithInvalidInput() {
// Create an invalid input (e.g., null node and negative maxDepth)
DeviceNode invalidNode = null;
int invalidMaxDepth = -1;
boolean includeDevice = true;
try {
deviceOrganizationDAO.getChildrenOf(invalidNode, invalidMaxDepth, includeDevice);
assert false : "Expected exception for bad request was not thrown.";
} catch (DeviceOrganizationMgtDAOException e) {
log.info("Expected exception for bad request was thrown: " + e.getMessage());
}
}
@Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the " +
"getParentsOf method under negative circumstances with invalid input")
public void testGetParentsOfWithInvalidInput() throws DeviceOrganizationMgtDAOException {
DeviceNode invalidNode = null;
int invalidMaxDepth = -1;
boolean includeDevice = true;
deviceOrganizationDAO.getParentsOf(invalidNode, invalidMaxDepth, includeDevice);
}
// @Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the addDeviceOrganization method under negative circumstances with null input")
// public void testAddDeviceOrganizationWithNullInput() throws DeviceOrganizationMgtDAOException {
// DeviceOrganization invalidDeviceOrg = null;
// deviceOrganizationDAO.addDeviceOrganization(invalidDeviceOrg);
// }
// @Test(description = "Test with invalid input parameters (bad request)")
// public void testGetChildrenOfWithInvalidInput() {
// // Create an invalid input (e.g., null node and negative maxDepth)
// DeviceNode invalidNode = null;
// int invalidMaxDepth = -1;
// boolean includeDevice = true;
//
// try {
// deviceOrganizationDAO.getChildrenOf(invalidNode, invalidMaxDepth, includeDevice);
// assert false : "Expected exception for bad request was not thrown.";
// } catch (DeviceOrganizationMgtDAOException e) {
// log.info("Expected exception for bad request was thrown: " + e.getMessage());
// }
// }
// @Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the " +
// "getParentsOf method under negative circumstances with invalid input")
// public void testGetParentsOfWithInvalidInput() throws DeviceOrganizationMgtDAOException {
// DeviceNode invalidNode = null;
// int invalidMaxDepth = -1;
// boolean includeDevice = true;
// deviceOrganizationDAO.getParentsOf(invalidNode, invalidMaxDepth, includeDevice);
// }
}

@ -29,8 +29,8 @@ public class DAOTest extends BaseDeviceOrganizationTest {
log.info("DAO test initialized");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetChildrenOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetChildrenOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection();
DeviceNode node = new DeviceNode();
node.setDeviceId(2);
@ -42,27 +42,27 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetParentsOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection();
DeviceNode node = new DeviceNode();
node.setDeviceId(4);
int maxDepth = 4;
boolean includeDevice = true;
List<DeviceNode> childrenList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice);
boolean includeDevice = false;
List<DeviceNode> parentList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice);
ConnectionManagerUtil.closeDBConnection();
log.info(childrenList.size());
Assert.assertNotNull(childrenList, "Cannot be null");
log.info(parentList.size());
Assert.assertNotNull(parentList, "Cannot be null");
}
@Test
public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
public void testAddDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setDeviceId(2);
deviceOrganization.setParentDeviceId(null);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization);
@ -70,20 +70,22 @@ public class DAOTest extends BaseDeviceOrganizationTest {
ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization1 = new DeviceOrganization() {
};
deviceOrganization1.setDeviceId(3);
deviceOrganization1.setParentDeviceId(2);
deviceOrganization1.setDeviceId(4);
deviceOrganization1.setParentDeviceId(1);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction();
boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization2 = new DeviceOrganization() {
};
deviceOrganization1.setDeviceId(4);
deviceOrganization1.setParentDeviceId(2);
deviceOrganization1.setDeviceId(3);
deviceOrganization1.setParentDeviceId(1);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction();
boolean result2 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
@ -91,12 +93,12 @@ public class DAOTest extends BaseDeviceOrganizationTest {
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testUpdateDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setDeviceId(2);
deviceOrganization.setParentDeviceId(1);
deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationDAO.updateDeviceOrganization(deviceOrganization);
@ -106,11 +108,10 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
if (deviceOrganization != null) {
log.info("Device Organization device ID : " + deviceOrganization.getDeviceId() +
@ -118,18 +119,17 @@ public class DAOTest extends BaseDeviceOrganizationTest {
}
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testDoesDeviceIdExistDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean doesDeviceIdExist = deviceOrganizationDAO.doesDeviceIdExist(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(doesDeviceIdExist, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testDeleteDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction();
@ -137,12 +137,27 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void deleteDeviceOrganizationsByDeviceId() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void deleteDeviceOrganizationsByDeviceIdDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetAllOrganizations() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
List<DeviceOrganization> organizations = deviceOrganizationDAO.getAllDeviceOrganizations();
for (DeviceOrganization organization : organizations) {
log.info("organizationID = " + organization.getOrganizationId());
log.info("deviceID = " + organization.getDeviceId());
log.info("parentDeviceID = " + organization.getParentDeviceId());
log.info("updateTime = " + organization.getUpdateTime());
log.info("----------------------------------------------");
}
ConnectionManagerUtil.closeDBConnection();
}
}

@ -13,8 +13,6 @@ import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.Date;
public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
private static final Log log = LogFactory.getLog(ServiceNegativeTest.class);
@ -54,8 +52,8 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice);
}
@Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode",
expectedExceptions = {DeviceOrganizationMgtPluginException.class})
@Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode"
, expectedExceptions = {DeviceOrganizationMgtPluginException.class})
public void testGetParentsOfWithInvalidDeviceNode() throws DeviceOrganizationMgtPluginException {
DeviceNode invalidNode = new DeviceNode(); // Provide an invalid DeviceNode
int maxDepth = 2;
@ -63,10 +61,19 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice);
}
@Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode"
, expectedExceptions = {DeviceOrganizationMgtPluginException.class}
)
public void testGetParentsOfWithNullDeviceNode() throws DeviceOrganizationMgtPluginException {
DeviceNode invalidNode = null; // Provide an invalid DeviceNode
int maxDepth = 2;
boolean includeDevice = true;
deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice);
}
@Test(description = "This method tests Add Device Organization method under negative circumstances with null data",
expectedExceptions = {DeviceOrganizationMgtPluginException.class},
expectedExceptionsMessageRegExp = ".*Invalid input parameters.*")
expectedExceptions = {DeviceOrganizationMgtPluginException.class})
public void testAddDeviceOrganizationWithInvalidInput() throws DeviceOrganizationMgtPluginException {
DeviceOrganization invalidOrganization = new DeviceOrganization() {
};
@ -87,9 +94,8 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
// Create a valid organization
DeviceOrganization validOrganization = new DeviceOrganization() {
};
validOrganization.setDeviceId(4);
validOrganization.setParentDeviceId(3);
validOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
validOrganization.setDeviceId(1);
validOrganization.setParentDeviceId(0);
try {
// Add the organization once

@ -26,7 +26,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
log.info("Service test initialized");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 4, dependsOnMethods = "testAddDeviceOrganization")
public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode();
@ -38,7 +38,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 5, dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode();
@ -50,12 +50,9 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(parentList, "Cannot be null");
}
@Test(
// expectedExceptions = {DeviceOrganizationMgtPluginException.class}
)
@Test(priority = 1)
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
@ -69,79 +66,59 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganizationTwo.setDeviceId(4);
deviceOrganizationTwo.setParentDeviceId(2);
try {
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo);
Assert.assertNotNull(result, "Cannot be null");
Assert.assertNotNull(result1, "Cannot be null");
Assert.assertNotNull(result2, "Cannot be null");
} catch (DeviceOrganizationMgtPluginException e){
// Clean up: Delete the added organization if it was successfully added to avoid conflicts in future tests
deviceOrganizationService.deleteDeviceAssociations(deviceOrganization.getDeviceId());
}
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo);
Assert.assertTrue(result);
Assert.assertTrue(result1);
Assert.assertTrue(result2);
}
// @Test(description = "This method tests Concurrent Access to Add Device Organization",
// expectedExceptions = {DeviceOrganizationMgtPluginException.class})
// public void testConcurrentAddDeviceOrganization() throws InterruptedException {
// ExecutorService executor = Executors.newFixedThreadPool(4);
// final DeviceOrganization validOrganization = new DeviceOrganization(){};
// validOrganization.setDeviceId(3);
// validOrganization.setParentDeviceId(2);
// validOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
//
// for (int i = 0; i < 4; i++) {
// executor.execute(() -> {
// try {
// deviceOrganizationService.addDeviceOrganization(validOrganization);
// } catch (DeviceOrganizationMgtPluginException e) {
// // Handle the exception
// }
// });
// }
//
// executor.shutdown();
// executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
// }
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 6, dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 2, dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(5);
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(1);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 3, dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(1);
Assert.assertNotNull(deviceIdExist, "Cannot be null");
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(4);
Assert.assertTrue(deviceIdExist);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(priority = 7, dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceOrganizationByID(5);
Assert.assertNotNull(result, "Cannot be null");
boolean rs = deviceOrganizationService.deleteDeviceOrganizationByID(1);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void deleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceAssociations(1);
Assert.assertNotNull(result, "Cannot be null");
@Test(priority = 8, dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceAssociations(4);
Assert.assertTrue(rs);
}
@Test(priority = 9, dependsOnMethods = "testAddDeviceOrganization")
public void testGetAllOrganizations() throws DeviceOrganizationMgtPluginException {
List<DeviceOrganization> organizations = deviceOrganizationService.getAllDeviceOrganizations();
for (DeviceOrganization organization : organizations) {
log.info("organizationID = "+organization.getOrganizationId());
log.info("deviceID = "+organization.getDeviceId());
log.info("parentDeviceID = "+organization.getParentDeviceId());
log.info("updateTime = "+organization.getUpdateTime());
log.info("----------------------------------------------");
}
Assert.assertNotNull(organizations, "List of organizations cannot be null");
Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty");
}
}

@ -814,6 +814,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -27,7 +27,8 @@
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- -----------------------------------------------------

Loading…
Cancel
Save