Modifications to getChildrenOf and getParentsOf methods logic

pull/238/head
Isuri Mendis 1 year ago
parent 34da115a53
commit c8c7922e97

@ -58,27 +58,27 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try { try {
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice); boolean parentAdded = false; // Flag to track whether the parent device has been added
if (!includeDevice) { getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice, parentAdded);
childNodes.add(node); if (!includeDevice && !parentAdded) {
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
} }
return childNodes; return childNodes;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " + String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " +
"parent device ID " + "parent device ID " + node.getDeviceId();
node.getDeviceId();
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 retrieve all child devices for " + String msg = "Error occurred while processing SQL to retrieve all child devices for " +
"parent device ID" + node.getDeviceId(); "parent device ID " + node.getDeviceId();
log.error(msg); log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e); throw new DeviceOrganizationMgtDAOException(msg, e);
} }
} }
private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn, private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
List<DeviceNode> childNodes, boolean includeDevice) throws SQLException { List<DeviceNode> childNodes, boolean includeDevice, boolean parentAdded) throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) { if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
return; return;
} }
@ -98,11 +98,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
while (rs.next()) { while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs); DeviceNode child = getDeviceFromResultSet(rs);
node.getChildren().add(child); node.getChildren().add(child);
if (includeDevice) { if (includeDevice && !parentAdded) {
childNodes.add(node); // Add the parent device if includeDevice is true. childNodes.add(node); // Add the parent device only if includeDevice is true and it hasn't been added.
parentAdded = true; // Set the flag to true after adding the parent device.
} }
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice); getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice, parentAdded);
} }
} }
} }
@ -118,10 +119,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
Set<Integer> visited = new HashSet<>(); Set<Integer> visited = new HashSet<>();
try { try {
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice); if (includeDevice) {
if (!includeDevice) { parentNodes.add(node); // Add the current node to the parent nodes list when includeDevice is true
parentNodes.add(node);
} }
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice);
return parentNodes; return parentNodes;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " + String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " +
@ -136,7 +137,6 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} }
} }
private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn, private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException { List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) { if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
@ -157,15 +157,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
DeviceNode parent = getDeviceFromResultSet(rs); DeviceNode parent = getDeviceFromResultSet(rs);
if (!includeDevice && parent.getDeviceId() == node.getDeviceId()) { if (includeDevice || parent.getDeviceId() != node.getDeviceId()) {
// Skip adding the current node as a parent when includeDevice is false node.getParents().add(parent);
continue;
} }
node.getParents().add(parent); if (!parentNodes.contains(parent) && (includeDevice || parent.getDeviceId() != node.getDeviceId())) {
parentNodes.add(parent);
if (!parentNodes.contains(parent)) {
parentNodes.add(parent); // Add the parent device if it hasn't been added already.
} }
getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice); getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice);

@ -154,4 +154,7 @@ public interface DeviceOrganizationService {
boolean deleteDeviceAssociations(int deviceId) boolean deleteDeviceAssociations(int deviceId)
throws DeviceOrganizationMgtPluginException; throws DeviceOrganizationMgtPluginException;
//In case we need to remove the device organization with enrollment removal,we need to implement a callback to
//remove the device organization mapping whenever the device removal happening in enrollment level.
} }

@ -2,10 +2,15 @@ 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.DeviceOrganization;
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 io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
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 {
@ -19,35 +24,4 @@ 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")
// 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);
// }
} }

@ -32,7 +32,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
DeviceNode deviceNode = new DeviceNode(); DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(2); deviceNode.setDeviceId(2);
int maxDepth = 2; int maxDepth = 2;
boolean includeDevice = false; boolean includeDevice = true;
List<DeviceNode> childrenList = deviceOrganizationService.getChildrenOf(deviceNode, maxDepth, includeDevice); List<DeviceNode> childrenList = deviceOrganizationService.getChildrenOf(deviceNode, maxDepth, includeDevice);
Assert.assertNotNull(childrenList, "Cannot be null"); Assert.assertNotNull(childrenList, "Cannot be null");
@ -44,7 +44,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
DeviceNode deviceNode = new DeviceNode(); DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(4); deviceNode.setDeviceId(4);
int maxDepth = 2; int maxDepth = 2;
boolean includeDevice = true; boolean includeDevice = false;
List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, maxDepth, includeDevice); List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, maxDepth, includeDevice);
Assert.assertNotNull(parentList, "Cannot be null"); Assert.assertNotNull(parentList, "Cannot be null");

Loading…
Cancel
Save