From 02f4b4faf3f598561f422d810bff9ea3ec00f941 Mon Sep 17 00:00:00 2001 From: isuri Date: Wed, 6 Dec 2023 15:15:20 +0530 Subject: [PATCH] get parent and children fixes --- .../api/DeviceOrganizationMgtService.java | 11 ++++++----- .../api/DeviceOrganizationMgtServiceImpl.java | 13 +++++++------ .../dao/impl/DeviceOrganizationDAOImpl.java | 8 ++++++++ .../device/organization/ServiceTest.java | 14 +++++++++++++- components/device-mgt-extensions/pom.xml | 1 + 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java index 5201d72445..7f430c9d26 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java @@ -35,6 +35,7 @@ import io.swagger.annotations.Tag; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -61,7 +62,7 @@ import javax.ws.rs.core.Response; } ), tags = { - @Tag(name = "device_organization_management, device_management", description = "Device organization " + + @Tag(name = "device_organization_management", description = "Device organization " + "management related REST-API. This can be used to manipulate device organization related details.") } ) @@ -347,14 +348,14 @@ public interface DeviceOrganizationMgtService { value = "The starting pagination index for the complete list of qualified items", required = false, defaultValue = "0") - @QueryParam("offset") + @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam( name = "limit", value = "Provide how many policy details you require from the starting pagination index/offset.", required = false, defaultValue = "5") - @QueryParam("limit") + @DefaultValue("20") @QueryParam("limit") int limit); /** @@ -415,14 +416,14 @@ public interface DeviceOrganizationMgtService { value = "The starting pagination index for the complete list of qualified items", required = false, defaultValue = "0") - @QueryParam("offset") + @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam( name = "limit", value = "Provide how many policy details you require from the starting pagination index/offset.", required = false, defaultValue = "5") - @QueryParam("limit") + @DefaultValue("20") @QueryParam("limit") int limit); /** diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java index 5be5e1b16b..0732bb0290 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java @@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -107,10 +108,10 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe @GET @Override - @Path("/leafs") + @Path("leafs") public Response getDeviceOrganizationLeafs( - @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("20") @QueryParam("limit") int limit) { RequestValidationUtil.validatePaginationParameters(offset, limit); try { DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); @@ -123,11 +124,11 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe } @GET - @Path("/roots") + @Path("roots") @Override public Response getDeviceOrganizationRoots( - @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("20") @QueryParam("limit") int limit) { RequestValidationUtil.validatePaginationParameters(offset, limit); try { DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); 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 0baa1672e7..3235f4e8f6 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 @@ -126,6 +126,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + "WHERE DO.PARENT_DEVICE_ID = ?"; + boolean hasChildren = false; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, node.getDeviceId()); @@ -133,6 +134,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { while (rs.next()) { DeviceNode child = getDeviceFromResultSet(rs); node.getChildren().add(child); + hasChildren = true; if (includeDevice && !parentAdded ) { @@ -157,6 +159,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { } } } + + // Add the parent node if it doesn't have children and includeDevice is true + if (!hasChildren && includeDevice && !parentAdded) { + childNodes.add(node); + parentAdded = true; + } } /** 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 05514ade4b..9623210e7d 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 @@ -33,7 +33,19 @@ public class ServiceTest extends BaseDeviceOrganizationTest { if (exists) { int deviceID = 17; int maxDepth = 10; - boolean includeDevice = false; + boolean includeDevice = true; + DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice); + Assert.assertNotNull(childrenList, "Cannot be null"); + } + } + + @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") + public void testGetChildrenOfWithOneParent() throws DeviceOrganizationMgtPluginException { + boolean exists = deviceOrganizationService.isDeviceIdExist(3); + if (exists) { + int deviceID = 3; + int maxDepth = 4; + boolean includeDevice = true; DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice); Assert.assertNotNull(childrenList, "Cannot be null"); } diff --git a/components/device-mgt-extensions/pom.xml b/components/device-mgt-extensions/pom.xml index a9cf69c585..4c091f360a 100644 --- a/components/device-mgt-extensions/pom.xml +++ b/components/device-mgt-extensions/pom.xml @@ -45,6 +45,7 @@ io.entgra.device.mgt.core.device.mgt.extensions.stateengine io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper io.entgra.device.mgt.core.device.mgt.extensions.device.organization + io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api