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

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

@ -28,7 +28,11 @@ public class DeviceOrganizationDaoUtil {
}; };
deviceOrganization.setOrganizationId(rs.getInt("ID")); deviceOrganization.setOrganizationId(rs.getInt("ID"));
deviceOrganization.setDeviceId(rs.getInt("DEVICE_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")); deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP"));
return deviceOrganization; return deviceOrganization;
} }

@ -28,7 +28,7 @@ public abstract class DeviceOrganization {
private int organizationId; private int organizationId;
private int deviceId; private int deviceId;
private int parentDeviceId; private Integer parentDeviceId;
private Date updateTime; private Date updateTime;
public int getOrganizationId() { public int getOrganizationId() {
@ -47,11 +47,11 @@ public abstract class DeviceOrganization {
this.deviceId = deviceId; this.deviceId = deviceId;
} }
public int getParentDeviceId() { public Integer getParentDeviceId() {
return parentDeviceId; return parentDeviceId;
} }
public void setParentDeviceId(int parentDeviceId) { public void setParentDeviceId(Integer parentDeviceId) {
this.parentDeviceId = 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.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.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.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.Log;
import org.apache.commons.logging.LogFactory; 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) public List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (node == null || node.getDeviceId() <= 0 || maxDepth < 0) { 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 { try {
// Open a database connection // Open a database connection
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice); return deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -70,19 +76,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override @Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (node == null || node.getDeviceId() <= 0 || maxDepth < 0) { if (node == null || node.getDeviceId() <= 0 || maxDepth <= 0) {
throw new BadRequestException("Invalid input parameters."); 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 { try {
// Open a database connection // Open a database connection
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getParentsOf(node, maxDepth, includeDevice); return deviceOrganizationDao.getParentsOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -91,76 +104,90 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
} }
// In DeviceOrganizationServiceImpl.java (implementation)
@Override @Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) public List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException {
throws DeviceOrganizationMgtPluginException { try {
if (deviceOrganization == null || deviceOrganization.getDeviceId() == 0 // Open a database connection
|| deviceOrganization.getParentDeviceId() == 0) { ConnectionManagerUtil.openDBConnection();
throw new BadRequestException("Invalid input parameters."); return deviceOrganizationDao.getAllDeviceOrganizations();
} } catch (DBConnectionException e) {
String msg = ""; String msg = "Error occurred while obtaining the database connection to retrieve all device organizations.";
// 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.";
log.error(msg); 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 @Override
// LockManager lockManager = LockManager.getInstance(); public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
// Object lock = lockManager.getLock(deviceOrganization.getDeviceId(), deviceOrganization.getParentDeviceId()); throws DeviceOrganizationMgtPluginException {
// if (deviceOrganization == null || deviceOrganization.getDeviceId() <= 0 ||
// synchronized (lock) { !(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 { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization); boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
if (result) { if (result) {
msg = "Device organization added successfully, for device ID " + deviceOrganization.getDeviceId() + msg = "Device organization added successfully. Device Organization details : " +
" and parent device ID " + deviceOrganization.getParentDeviceId(); "deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(msg); 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);
} }
ConnectionManagerUtil.commitDBTransaction(); } else {
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) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while adding device organization for device ID " + msg = "Device organization failed to add. Device Organization details : " +
deviceOrganization.getDeviceId() + " and parent device ID" "deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
+ deviceOrganization.getParentDeviceId(); throw new DeviceOrganizationMgtPluginException(msg);
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
// } 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 @Override
public boolean organizationExists(int deviceId, int parentDeviceId) throws DeviceOrganizationMgtPluginException { public boolean organizationExists(int deviceID, int parentDeviceID) throws DeviceOrganizationMgtPluginException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
boolean exists = deviceOrganizationDao.organizationExists(deviceId, parentDeviceId); return deviceOrganizationDao.organizationExists(deviceID, parentDeviceID);
return exists;
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -171,14 +198,21 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override @Override
public boolean updateDeviceOrganization(DeviceOrganization organization) public boolean updateDeviceOrganization(DeviceOrganization organization)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (organization == null || organization.getOrganizationId() <= 0) { if (organization == null || organization.getOrganizationId() <= 0 || organization.getDeviceId() <= 0
throw new BadRequestException("Invalid input parameters."); || !(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()); DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organization.getOrganizationId());
if (deviceOrganization == null) { if (deviceOrganization == null) {
String errorMsg = "Cannot find device organization for organization ID " + organization.getOrganizationId(); msg = "Cannot find device organization for organizationID = " + organization.getOrganizationId();
log.error(errorMsg); log.error(msg);
return false; return false;
} }
@ -186,26 +220,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.updateDeviceOrganization(organization); boolean result = deviceOrganizationDao.updateDeviceOrganization(organization);
if (result) { if (result) {
msg = "Device organization updated successfully,for " + organization.getOrganizationId(); msg = "Device organization updated successfully for organizationID = " + organization.getOrganizationId();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(msg); log.debug(msg);
} }
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction(); 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); throw new DeviceOrganizationMgtPluginException(msg);
} }
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return true; return true;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to update device organization for " + msg = "Error occurred while obtaining the database connection to update device organization for " +
organization.getOrganizationId(); "organizationID = " + organization.getOrganizationId();
log.error(msg); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) { } catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while updating device organization for " + msg = "Error occurred in the database level while updating device organization for " +
organization.getOrganizationId(); "organizationID = " + organization.getOrganizationId();
log.error(msg); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -214,22 +248,25 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
@Override @Override
public DeviceOrganization getDeviceOrganizationByID(int organizationId) public DeviceOrganization getDeviceOrganizationByID(int organizationID)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (organizationId <= 0) { if (organizationID <= 0) {
throw new BadRequestException("Invalid input parameters."); throw new BadRequestException("organizationID must be a positive number. " +
"Invalid input parameters for getting deviceOrganization for : "
+ "organizationID = " + organizationID);
} }
try { try {
// Open a database connection // Open a database connection
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
DeviceOrganization deviceOrganization = deviceOrganizationDao.getDeviceOrganizationByID(organizationId); return deviceOrganizationDao.getDeviceOrganizationByID(organizationID);
return deviceOrganization;
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -239,42 +276,46 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
@Override @Override
public boolean deleteDeviceOrganizationByID(int organizationId) public boolean deleteDeviceOrganizationByID(int organizationID)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (organizationId <= 0) { if (organizationID <= 0) {
throw new BadRequestException("Invalid input parameters."); 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) { if (deviceOrganization == null) {
msg = "Cannot find device organization for organization ID " + organizationId; msg = "Cannot find device organization for Deletion : organizationID = " + organizationID;
log.error(msg); log.error(msg);
return false; return false;
} }
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationId); boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationID);
if (result) { if (result) {
msg = "Device organization record deleted successfully,for " + organizationId; msg = "Device organization record deleted successfully for organizationID = " + organizationID;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(msg); log.debug(msg);
} }
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to delete,for " + organizationId; msg = "Device organization failed to delete for organizationID = " + organizationID;
throw new DeviceOrganizationMgtPluginException(msg); throw new DeviceOrganizationMgtPluginException(msg);
} }
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return true; return true;
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) { } catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -284,42 +325,45 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
@Override @Override
public boolean deleteDeviceAssociations(int deviceId) public boolean deleteDeviceAssociations(int deviceID)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (deviceId <= 0) { if (deviceID <= 0) {
throw new BadRequestException("Invalid input parameters."); 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) { if (!deviceIdExist) {
msg = "Cannot find device organization associated with device ID " + deviceId; msg = "Cannot find device organizations associated with deviceID = " + deviceID;
log.error(msg); log.error(msg);
return false; return false;
} }
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceId); boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceID);
if (result) { if (result) {
msg = "Device organization records deleted successfully,for " + deviceId; msg = "Device organization records associated with deviceID = " + deviceID + " are deleted successfully.";
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(msg); log.debug(msg);
} }
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction(); 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); throw new DeviceOrganizationMgtPluginException(msg);
} }
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return true; return true;
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) { } catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } finally {
@ -329,22 +373,25 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
} }
@Override @Override
public boolean doesDeviceIdExist(int deviceId) public boolean doesDeviceIdExist(int deviceID)
throws DeviceOrganizationMgtPluginException { throws DeviceOrganizationMgtPluginException {
if (deviceId <= 0) { if (deviceID <= 0) {
throw new BadRequestException("Invalid input parameters."); throw new BadRequestException("deviceID must be a positive number." +
"Invalid input parameters for checking deviceID existence " +
"in deviceOrganization : deviceID = " + deviceID);
} }
try { try {
// Open a database connection // Open a database connection
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
boolean deviceIdExist = deviceOrganizationDao.doesDeviceIdExist(deviceId); return deviceOrganizationDao.doesDeviceIdExist(deviceID);
return deviceIdExist;
} catch (DBConnectionException e) { } 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException 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); log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e); throw new DeviceOrganizationMgtPluginException(msg, e);
} finally { } 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.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException;
import java.sql.Date;
import java.util.List; import java.util.List;
public interface DeviceOrganizationService { public interface DeviceOrganizationService {
@ -35,6 +34,8 @@ public interface DeviceOrganizationService {
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException; throws DeviceOrganizationMgtPluginException;
List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException;
DeviceOrganization getDeviceOrganizationByID(int organizationId) DeviceOrganization getDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException; 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.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.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 io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class DAONegativeTest extends BaseDeviceOrganizationTest { public class DAONegativeTest extends BaseDeviceOrganizationTest {
@ -25,35 +19,35 @@ public class DAONegativeTest extends BaseDeviceOrganizationTest {
log.info("DAO test initialized"); log.info("DAO test initialized");
} }
@Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the addDeviceOrganization method under negative circumstances with null input") // @Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the addDeviceOrganization method under negative circumstances with null input")
public void testAddDeviceOrganizationWithNullInput() throws DeviceOrganizationMgtDAOException { // public void testAddDeviceOrganizationWithNullInput() throws DeviceOrganizationMgtDAOException {
DeviceOrganization invalidDeviceOrg = null; // DeviceOrganization invalidDeviceOrg = null;
deviceOrganizationDAO.addDeviceOrganization(invalidDeviceOrg); // deviceOrganizationDAO.addDeviceOrganization(invalidDeviceOrg);
} // }
@Test(description = "Test with invalid input parameters (bad request)") // @Test(description = "Test with invalid input parameters (bad request)")
public void testGetChildrenOfWithInvalidInput() { // public void testGetChildrenOfWithInvalidInput() {
// Create an invalid input (e.g., null node and negative maxDepth) // // Create an invalid input (e.g., null node and negative maxDepth)
DeviceNode invalidNode = null; // DeviceNode invalidNode = null;
int invalidMaxDepth = -1; // int invalidMaxDepth = -1;
boolean includeDevice = true; // boolean includeDevice = true;
//
try { // try {
deviceOrganizationDAO.getChildrenOf(invalidNode, invalidMaxDepth, includeDevice); // deviceOrganizationDAO.getChildrenOf(invalidNode, invalidMaxDepth, includeDevice);
assert false : "Expected exception for bad request was not thrown."; // assert false : "Expected exception for bad request was not thrown.";
} catch (DeviceOrganizationMgtDAOException e) { // } catch (DeviceOrganizationMgtDAOException e) {
log.info("Expected exception for bad request was thrown: " + e.getMessage()); // log.info("Expected exception for bad request was thrown: " + e.getMessage());
} // }
} // }
@Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the " + // @Test(expectedExceptions = DeviceOrganizationMgtDAOException.class, description = "This method tests the " +
"getParentsOf method under negative circumstances with invalid input") // "getParentsOf method under negative circumstances with invalid input")
public void testGetParentsOfWithInvalidInput() throws DeviceOrganizationMgtDAOException { // public void testGetParentsOfWithInvalidInput() throws DeviceOrganizationMgtDAOException {
DeviceNode invalidNode = null; // DeviceNode invalidNode = null;
int invalidMaxDepth = -1; // int invalidMaxDepth = -1;
boolean includeDevice = true; // boolean includeDevice = true;
deviceOrganizationDAO.getParentsOf(invalidNode, invalidMaxDepth, includeDevice); // deviceOrganizationDAO.getParentsOf(invalidNode, invalidMaxDepth, includeDevice);
} // }
} }

@ -29,8 +29,8 @@ public class DAOTest extends BaseDeviceOrganizationTest {
log.info("DAO test initialized"); log.info("DAO test initialized");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetChildrenOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testGetChildrenOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
DeviceNode node = new DeviceNode(); DeviceNode node = new DeviceNode();
node.setDeviceId(2); node.setDeviceId(2);
@ -42,27 +42,27 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null"); Assert.assertNotNull(childrenList, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testGetParentsOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
DeviceNode node = new DeviceNode(); DeviceNode node = new DeviceNode();
node.setDeviceId(4); node.setDeviceId(4);
int maxDepth = 4; int maxDepth = 4;
boolean includeDevice = true; boolean includeDevice = false;
List<DeviceNode> childrenList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice); List<DeviceNode> parentList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice);
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
log.info(childrenList.size()); log.info(parentList.size());
Assert.assertNotNull(childrenList, "Cannot be null"); Assert.assertNotNull(parentList, "Cannot be null");
} }
@Test @Test
public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testAddDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
DeviceOrganization deviceOrganization = new DeviceOrganization() { DeviceOrganization deviceOrganization = new DeviceOrganization() {
}; };
deviceOrganization.setDeviceId(4); deviceOrganization.setDeviceId(2);
deviceOrganization.setParentDeviceId(3); deviceOrganization.setParentDeviceId(null);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization); boolean result = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization);
@ -70,20 +70,22 @@ public class DAOTest extends BaseDeviceOrganizationTest {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization1 = new DeviceOrganization() { DeviceOrganization deviceOrganization1 = new DeviceOrganization() {
}; };
deviceOrganization1.setDeviceId(3); deviceOrganization1.setDeviceId(4);
deviceOrganization1.setParentDeviceId(2); deviceOrganization1.setParentDeviceId(1);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis())); deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1); boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization2 = new DeviceOrganization() { DeviceOrganization deviceOrganization2 = new DeviceOrganization() {
}; };
deviceOrganization1.setDeviceId(4); deviceOrganization1.setDeviceId(3);
deviceOrganization1.setParentDeviceId(2); deviceOrganization1.setParentDeviceId(1);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis())); deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result2 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1); boolean result2 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null"); Assert.assertNotNull(result, "Cannot be null");
@ -91,12 +93,12 @@ public class DAOTest extends BaseDeviceOrganizationTest {
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testUpdateDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testUpdateDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = new DeviceOrganization() { DeviceOrganization deviceOrganization = new DeviceOrganization() {
}; };
deviceOrganization.setDeviceId(4); deviceOrganization.setDeviceId(2);
deviceOrganization.setParentDeviceId(1); deviceOrganization.setParentDeviceId(1);
deviceOrganization.setOrganizationId(1); deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationDAO.updateDeviceOrganization(deviceOrganization); boolean result = deviceOrganizationDAO.updateDeviceOrganization(deviceOrganization);
@ -106,11 +108,10 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(result, "Cannot be null"); Assert.assertNotNull(result, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testGetDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1); DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
if (deviceOrganization != null) { if (deviceOrganization != null) {
log.info("Device Organization device ID : " + deviceOrganization.getDeviceId() + log.info("Device Organization device ID : " + deviceOrganization.getDeviceId() +
@ -118,18 +119,17 @@ public class DAOTest extends BaseDeviceOrganizationTest {
} }
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testDoesDeviceIdExist() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testDoesDeviceIdExistDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean doesDeviceIdExist = deviceOrganizationDAO.doesDeviceIdExist(1); boolean doesDeviceIdExist = deviceOrganizationDAO.doesDeviceIdExist(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(doesDeviceIdExist, "Cannot be null"); Assert.assertNotNull(doesDeviceIdExist, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testDeleteDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void testDeleteDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1); boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
@ -137,12 +137,27 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(result, "Cannot be null"); Assert.assertNotNull(result, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void deleteDeviceOrganizationsByDeviceId() throws DBConnectionException, DeviceOrganizationMgtDAOException { public void deleteDeviceOrganizationsByDeviceIdDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1); boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null"); 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.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Date;
public class ServiceNegativeTest extends BaseDeviceOrganizationTest { public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
private static final Log log = LogFactory.getLog(ServiceNegativeTest.class); private static final Log log = LogFactory.getLog(ServiceNegativeTest.class);
@ -54,8 +52,8 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice); deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice);
} }
@Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode", @Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode"
expectedExceptions = {DeviceOrganizationMgtPluginException.class}) , expectedExceptions = {DeviceOrganizationMgtPluginException.class})
public void testGetParentsOfWithInvalidDeviceNode() throws DeviceOrganizationMgtPluginException { public void testGetParentsOfWithInvalidDeviceNode() throws DeviceOrganizationMgtPluginException {
DeviceNode invalidNode = new DeviceNode(); // Provide an invalid DeviceNode DeviceNode invalidNode = new DeviceNode(); // Provide an invalid DeviceNode
int maxDepth = 2; int maxDepth = 2;
@ -63,10 +61,19 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
deviceOrganizationService.getParentsOf(invalidNode, maxDepth, includeDevice); 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", @Test(description = "This method tests Add Device Organization method under negative circumstances with null data",
expectedExceptions = {DeviceOrganizationMgtPluginException.class}, expectedExceptions = {DeviceOrganizationMgtPluginException.class})
expectedExceptionsMessageRegExp = ".*Invalid input parameters.*")
public void testAddDeviceOrganizationWithInvalidInput() throws DeviceOrganizationMgtPluginException { public void testAddDeviceOrganizationWithInvalidInput() throws DeviceOrganizationMgtPluginException {
DeviceOrganization invalidOrganization = new DeviceOrganization() { DeviceOrganization invalidOrganization = new DeviceOrganization() {
}; };
@ -87,9 +94,8 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
// Create a valid organization // Create a valid organization
DeviceOrganization validOrganization = new DeviceOrganization() { DeviceOrganization validOrganization = new DeviceOrganization() {
}; };
validOrganization.setDeviceId(4); validOrganization.setDeviceId(1);
validOrganization.setParentDeviceId(3); validOrganization.setParentDeviceId(0);
validOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
try { try {
// Add the organization once // Add the organization once

@ -26,7 +26,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
log.info("Service test initialized"); log.info("Service test initialized");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(priority = 4, dependsOnMethods = "testAddDeviceOrganization")
public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException { public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode(); DeviceNode deviceNode = new DeviceNode();
@ -38,7 +38,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null"); Assert.assertNotNull(childrenList, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(priority = 5, dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DeviceOrganizationMgtPluginException { public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode(); DeviceNode deviceNode = new DeviceNode();
@ -50,12 +50,9 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(parentList, "Cannot be null"); Assert.assertNotNull(parentList, "Cannot be null");
} }
@Test( @Test(priority = 1)
// expectedExceptions = {DeviceOrganizationMgtPluginException.class}
)
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException { public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization() { DeviceOrganization deviceOrganization = new DeviceOrganization() {
}; };
deviceOrganization.setDeviceId(4); deviceOrganization.setDeviceId(4);
@ -69,79 +66,59 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganizationTwo.setDeviceId(4); deviceOrganizationTwo.setDeviceId(4);
deviceOrganizationTwo.setParentDeviceId(2); deviceOrganizationTwo.setParentDeviceId(2);
try { boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization); boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne); boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo);
boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo); Assert.assertTrue(result);
Assert.assertNotNull(result, "Cannot be null"); Assert.assertTrue(result1);
Assert.assertNotNull(result1, "Cannot be null"); Assert.assertTrue(result2);
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());
}
} }
// @Test(description = "This method tests Concurrent Access to Add Device Organization", @Test(priority = 6, dependsOnMethods = "testAddDeviceOrganization")
// 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")
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException { public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization() { DeviceOrganization deviceOrganization = new DeviceOrganization() {
}; };
deviceOrganization.setDeviceId(4); deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3); deviceOrganization.setParentDeviceId(3);
deviceOrganization.setOrganizationId(1); deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization); boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
Assert.assertNotNull(result, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(priority = 2, dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException { 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 { public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(4);
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(1); Assert.assertTrue(deviceIdExist);
Assert.assertNotNull(deviceIdExist, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(priority = 7, dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException { public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceOrganizationByID(5); boolean rs = deviceOrganizationService.deleteDeviceOrganizationByID(1);
Assert.assertNotNull(result, "Cannot be null");
} }
@Test(dependsOnMethods = "testAddDeviceOrganization") @Test(priority = 8, dependsOnMethods = "testAddDeviceOrganization")
public void deleteDeviceAssociations() throws DeviceOrganizationMgtPluginException { public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceAssociations(1); boolean rs = deviceOrganizationService.deleteDeviceAssociations(4);
Assert.assertNotNull(result, "Cannot be null"); 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) CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (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 fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) 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-- -- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -27,7 +27,8 @@
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (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 fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) 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