diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index 3235f4e8f6..a90e1447fe 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -323,8 +323,13 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List deviceOrganizations = new ArrayList<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID IS NULL " + + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION " + + "WHERE (PARENT_DEVICE_ID IS NULL AND " + + "DEVICE_ID NOT IN " + + "(SELECT DEVICE_ID FROM DM_DEVICE_ORGANIZATION " + + "WHERE PARENT_DEVICE_ID IS NOT NULL)) " + "LIMIT ? OFFSET ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, request.getLimit()); stmt.setInt(2, request.getOffSet()); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 9623210e7d..3987b9b227 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -148,7 +148,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { // Define specific combinations of deviceID and parentDeviceID int[][] combinations = { - {20, 19}, {19, 18}, {18, 17}, {20, 5}, {20, 17}, {19, 16}, {17, 16}, {16, 17} + {20, 19}, {19, 18}, {18, 17}, {20, 5}, {20, 17}, {19, 16}, {17, 16}, {16, 17}, {2, 1} // Add more combinations as needed }; @@ -175,6 +175,17 @@ public class ServiceTest extends BaseDeviceOrganizationTest { iterationCount++; } + DeviceOrganization nullParent = new DeviceOrganization(); + nullParent.setDeviceId(1); + nullParent.setParentDeviceId(null); + boolean isNullParentAdded = deviceOrganizationService.addDeviceOrganization(nullParent); + + DeviceOrganization secondNullParent = new DeviceOrganization(); + secondNullParent.setDeviceId(2); + secondNullParent.setParentDeviceId(null); + boolean isSecondNullParentAdded = deviceOrganizationService.addDeviceOrganization(secondNullParent); + + // Optionally, you can assert that the correct number of organizations were inserted Assert.assertEquals(organizationCount, combinations.length, "Inserted organizations count mismatch"); } @@ -270,6 +281,23 @@ public class ServiceTest extends BaseDeviceOrganizationTest { Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); } + @Test(dependsOnMethods = "testAddMultipleDeviceOrganizations") + public void testGetMultipleRootOrganizations() throws DeviceOrganizationMgtPluginException { + int offset = 0; + int limit = 10; + PaginationRequest request = new PaginationRequest(offset, limit); + List organizations = deviceOrganizationService.getDeviceOrganizationRoots(request); + 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"); + } + @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") public void testGetLeafOrganizationsWithNullParents() throws DeviceOrganizationMgtPluginException { int offset = 0;