From 49bc317d20068a3c2baf0c50f4e1c902145c4891 Mon Sep 17 00:00:00 2001 From: ashvini Date: Wed, 29 May 2024 12:44:59 +0530 Subject: [PATCH 1/7] Add filter to get Web apps in Enrollment Application Install Add filter file (cherry picked from commit 7616c2255c054e21a10f4f85ced02513f94ab385) --- .../mgt/core/application/mgt/common/Filter.java | 13 +++++++++++++ .../impl/application/GenericApplicationDAOImpl.java | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java index 272e9ba8a3..88f7e90a0a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/Filter.java @@ -118,6 +118,11 @@ public class Filter { */ private boolean isNotRetired; + /** + * To check whether web applications should be returned + */ + private boolean withWebApps; + public int getLimit() { return limit; } @@ -221,4 +226,12 @@ public class Filter { public void setNotRetired(boolean notRetired) { isNotRetired = notRetired; } + + public boolean isWithWebApps() { + return withWebApps; + } + + public void setWithWebApps(boolean withWebApps) { + this.withWebApps = withWebApps; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 7c71880fa6..618149cd72 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -178,7 +178,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? "; } if (deviceTypeId != -1) { - sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; + sql += "AND (AP_APP.DEVICE_TYPE_ID = ? "; + if (filter.isWithWebApps()) { + sql += "OR AP_APP.DEVICE_TYPE_ID = 0 "; + } + sql += ") "; } if (filter.isNotRetired()) { sql += "AND AP_APP.STATUS != 'RETIRED' "; @@ -309,7 +313,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; } if (deviceTypeId != -1) { - sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; + sql += "AND (AP_APP.DEVICE_TYPE_ID = ? "; + if (filter.isWithWebApps()) { + sql += "OR AP_APP.DEVICE_TYPE_ID = 0 "; + } + sql += ") "; } if (filter.isNotRetired()) { sql += " AND AP_APP.STATUS != 'RETIRED'"; From 3c06059873d05daeefa39c7728d01f22540a7e88 Mon Sep 17 00:00:00 2001 From: Lasantha Dharmakeerthi Date: Mon, 10 Jun 2024 07:43:29 +0000 Subject: [PATCH 2/7] Fix SQL error (cherry picked from commit 1aec609bff712089d658ee56608967286658efaf) --- .../core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 58bfed7d4a..ec03136c6b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1716,7 +1716,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.LAST_UPDATED_TIMESTAMP, " + "e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, " + "e.DATE_OF_LAST_UPDATE, e.DATE_OF_ENROLMENT, e.ENROLMENT_ID FROM " + - "(SELECT e.ID, e.DEVICE_ID, e.DEVICE_TYPE, e.OWNER, " + + "(SELECT e.ID, e.DEVICE_ID, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION, e.OWNER, " + "e.OWNERSHIP, e.STATUS, e.IS_TRANSFERRED, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, e.ID AS " + "ENROLMENT_ID FROM DM_ENROLMENT e WHERE TENANT_ID = ? AND STATUS = ?) e, " + "DM_DEVICE d WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ?"; From cc602a03bd08adcfa0754d03ad8478f18204d712 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Tue, 11 Jun 2024 10:38:54 +0530 Subject: [PATCH 3/7] Add missing test cases for DevicePersistTests and GroupPersistTests (cherry picked from commit d443fef6e0314ab2a8c3799405d1ced60d096bcc) --- .../mgt/core/common/TestDataHolder.java | 18 + .../mgt/core/dao/DevicePersistTests.java | 514 ++++++++++++++++++ .../mgt/core/dao/GroupPersistTests.java | 306 ++++++++++- 3 files changed, 817 insertions(+), 21 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java index bea2094a77..322b2b449d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.MonitoringOperation; import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application; +import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceData; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.notification.mgt.Notification; @@ -32,6 +33,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; +import java.util.Calendar; public class TestDataHolder { @@ -39,11 +41,18 @@ public class TestDataHolder { public final static Integer SUPER_TENANT_ID = -1234; public final static String SUPER_TENANT_DOMAIN = "carbon.super"; public final static String initialDeviceIdentifier = "12345"; + public final static String initialDeviceName = "TEST-DEVICE"; public final static String OWNER = "admin"; public static final String OPERATION_CONFIG = "TEST-OPERATION-"; public static Device initialTestDevice; public static DeviceType initialTestDeviceType; + public static Date getTimeBefore(int minutes) { + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.MINUTE, -minutes); + return calendar.getTime(); + } + public static Device generateDummyDeviceData(String deviceType) { Device device = new Device(); device.setEnrolmentInfo(generateEnrollmentInfo(new Date().getTime(), new Date().getTime(), OWNER, EnrolmentInfo @@ -141,6 +150,15 @@ public class TestDataHolder { return device; } + public static DeviceData generateDummyDevice(DeviceIdentifier deviceIdentifier) { + DeviceData deviceData = new DeviceData(); + deviceData.setDeviceIdentifier(deviceIdentifier); + deviceData.setDeviceOwner(OWNER); + deviceData.setDeviceOwnership(EnrolmentInfo.OwnerShip.BYOD.toString()); + deviceData.setLastModifiedDate(getTimeBefore(1)); + return deviceData; + } + public static DeviceType generateDeviceTypeData(String devTypeName) { DeviceType deviceType = new DeviceType(); deviceType.setName(devTypeName); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DevicePersistTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DevicePersistTests.java index e76a4f7850..a8bc1b6924 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DevicePersistTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/DevicePersistTests.java @@ -18,7 +18,13 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceData; +import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoCluster; +import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoCoordinate; +import io.entgra.device.mgt.core.device.mgt.common.geo.service.GeoQuery; +import org.apache.commons.collections.map.SingletonMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -34,6 +40,7 @@ import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; import java.sql.*; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -89,6 +96,7 @@ public class DevicePersistTests extends BaseDeviceManagementTest { public void testAddDeviceTest() { int tenantId = TestDataHolder.SUPER_TENANT_ID; Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); + device.setName(TestDataHolder.initialDeviceName); try { DeviceManagementDAOFactory.beginTransaction(); @@ -275,6 +283,7 @@ public class DevicePersistTests extends BaseDeviceManagementTest { pr.setStatusList(Collections.singletonList(Status.ACTIVE.name())); List results = deviceDAO.getDevicesByStatus(pr, TestDataHolder.SUPER_TENANT_ID); Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + "enrolment", e); @@ -283,4 +292,509 @@ public class DevicePersistTests extends BaseDeviceManagementTest { } } + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesByTenantId() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevices(TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDeviceId() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDeviceIdentifierWithTenantId() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDeviceData() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + DeviceData deviceData = TestDataHolder.generateDummyDevice(deviceIdentifier); + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(deviceData, TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByOwner() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDateSince() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(deviceIdentifier, TestDataHolder.getTimeBefore(1), TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDateSinceWithDeviceId() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(device.getDeviceIdentifier(), TestDataHolder.getTimeBefore(1), TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByEnrollmentStatus() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + Device results = deviceDAO.getDevice(deviceIdentifier, Status.ACTIVE, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(device.getDeviceIdentifier(), results.getDeviceIdentifier(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDeviceIdentifier() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + SingletonMap results = deviceDAO.getDevice(deviceIdentifier); + Assert.assertNotNull(results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceByDeviceType() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevices(device.getType(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getAllocatedDevices() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getAllocatedDevices(device.getType(), TestDataHolder.SUPER_TENANT_ID, 1, 0); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesOfUser() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesOfUserWithDeviceType() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, device.getType(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesOfUserWithDeviceStatus() throws DeviceManagementDAOException, TransactionManagementException { + List status = new ArrayList<>() ; + status.add(Status.ACTIVE.name()); + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevicesOfUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID, status); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getCountOfDevicesInGroup() throws DeviceManagementDAOException, TransactionManagementException { + PaginationRequest pr = new PaginationRequest(0, 10); + pr.setGroupId(1); + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getCountOfDevicesInGroup(pr, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(0, results, "No device count returned in group"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device count" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountWithOwner() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCount(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountWithType() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCount(device.getType(), Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void setEnrolmentStatusInBulk() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + List devices = new ArrayList<>() ; + devices.add(device.getDeviceIdentifier()); + try { + DeviceManagementDAOFactory.beginTransaction(); + boolean results = deviceDAO.setEnrolmentStatusInBulk(device.getType(), Status.ACTIVE.name(),TestDataHolder.SUPER_TENANT_ID, devices ); + Assert.assertTrue(results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCount() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCount(TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountWithPagination() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + PaginationRequest pr = new PaginationRequest(0, 10); + pr.setDeviceName(device.getName()); + pr.setDeviceType(device.getType()); + pr.setOwner(TestDataHolder.OWNER); + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCount(pr, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByType() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByType(device.getType(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByUser() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByUser(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByName() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByName(TestDataHolder.initialDeviceName, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByOwnership() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByOwnership(EnrolmentInfo.OwnerShip.BYOD.name(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByStatus() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByStatus(Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceCountByStatusWithType() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getDeviceCountByStatus(device.getType(), Status.ACTIVE.name(), TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getActiveEnrolment() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + EnrolmentInfo results = deviceDAO.getActiveEnrolment(deviceIdentifier, TestDataHolder.SUPER_TENANT_ID); + Assert.assertEquals(Status.ACTIVE, results.getStatus(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDeviceEnrolledTenants() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDeviceEnrolledTenants(); + Assert.assertEquals(1, results.size(), "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void findGeoClusters() throws DeviceManagementDAOException, TransactionManagementException { + GeoQuery geoQuery = new GeoQuery(new GeoCoordinate(123, 123), new GeoCoordinate(123, 123), 12345); + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.findGeoClusters(geoQuery, TestDataHolder.SUPER_TENANT_ID); + Assert.assertNotNull(results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getAppNotInstalledDevices() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + PaginationRequest pr = new PaginationRequest(0, 10); + pr.setDeviceType(device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getAppNotInstalledDevices(pr, TestDataHolder.SUPER_TENANT_ID, "com.google.calc", "1.0.0"); + Assert.assertNotNull(results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getCountOfAppNotInstalledDevices() throws DeviceManagementDAOException, TransactionManagementException { + Device device = TestDataHolder.initialTestDevice; + PaginationRequest pr = new PaginationRequest(0, 10); + pr.setDeviceType(device.getType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getCountOfAppNotInstalledDevices(pr, TestDataHolder.SUPER_TENANT_ID, "com.google.calc", "1.0.0"); + Assert.assertEquals(1, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getDevicesByEncryptionStatus() throws DeviceManagementDAOException, TransactionManagementException { + PaginationRequest pr = new PaginationRequest(0, 10); + try { + DeviceManagementDAOFactory.beginTransaction(); + List results = deviceDAO.getDevicesByEncryptionStatus(pr, TestDataHolder.SUPER_TENANT_ID, false); + Assert.assertNotNull(results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "testAddDeviceTest") + public void getCountOfDevicesByEncryptionStatus() throws DeviceManagementDAOException, TransactionManagementException { + try { + DeviceManagementDAOFactory.beginTransaction(); + int results = deviceDAO.getCountOfDevicesByEncryptionStatus(TestDataHolder.SUPER_TENANT_ID, true); + Assert.assertEquals(0, results, "No device returned"); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the device" + e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupPersistTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupPersistTests.java index 0ea871994c..e484bc4923 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupPersistTests.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupPersistTests.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.core.dao; +import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; @@ -54,19 +56,18 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); - GroupManagementDAOFactory.closeConnection(); log.debug("Group added to database. ID: " + groupId); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } DeviceGroup group = getGroupById(groupId); if (!isMock()) { @@ -83,22 +84,21 @@ public class GroupPersistTests extends BaseDeviceManagementTest { request.setGroupName(null); request.setOwner(null); List groups = groupDAO.getGroups(request, TestDataHolder.SUPER_TENANT_ID); - GroupManagementDAOFactory.closeConnection(); if (!isMock()) { Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotNull(groups.get(0), "Group is null"); log.debug("No of Groups found: " + groups.size()); } } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while find group by name."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -114,21 +114,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest { } GroupManagementDAOFactory.commitTransaction(); List roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID); - GroupManagementDAOFactory.closeConnection(); if (!isMock()) { Assert.assertEquals(roles, addedRoles, "Added roles are not equal to returned roles."); } log.debug("Group shared with roles."); } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while find group by name."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -141,22 +140,21 @@ public class GroupPersistTests extends BaseDeviceManagementTest { roles.remove(0); } List deviceGroups = groupDAO.getGroups(roles.toArray(new String[roles.size()]), TestDataHolder.SUPER_TENANT_ID); - GroupManagementDAOFactory.closeConnection(); if (!isMock()) { Assert.assertEquals(deviceGroups.size(), 1, "Unexpected number of device groups found with role."); Assert.assertEquals(deviceGroups.get(0).getGroupId(), groupId, "Unexpected groupId found with role."); } log.debug("Group found for given roles."); } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while getting groups shared with roles."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -271,19 +269,18 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.updateGroup(group, groupId, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); - GroupManagementDAOFactory.closeConnection(); log.debug("Group updated"); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while updating group details."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } if (!isMock()) { group = getGroupById(groupId); @@ -301,20 +298,20 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupDAO.deleteGroup(group.getGroupId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); - GroupManagementDAOFactory.closeConnection(); log.debug("Group deleted"); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while updating group details."; log.error(msg, e); Assert.fail(msg, e); } catch (TransactionManagementException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while initiating transaction."; log.error(msg, e); Assert.fail(msg, e); } + finally { + GroupManagementDAOFactory.closeConnection(); + } group = getGroupById(groupId); if (!isMock()) { Assert.assertNull(group, "Group is not deleted"); @@ -325,23 +322,290 @@ public class GroupPersistTests extends BaseDeviceManagementTest { try { GroupManagementDAOFactory.openConnection(); DeviceGroup deviceGroup = groupDAO.getGroup(groupId, TestDataHolder.SUPER_TENANT_ID); - GroupManagementDAOFactory.closeConnection(); if (deviceGroup == null && isMock()) { deviceGroup = new DeviceGroup(); deviceGroup.setGroupId(groupId); } return deviceGroup; } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while retrieving group details."; log.error(msg, e); Assert.fail(msg, e); } catch (SQLException e) { - GroupManagementDAOFactory.closeConnection(); String msg = "Error occurred while opening a connection to the data source."; log.error(msg, e); Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } return null; } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getAllDevicesOfGroupWithStatus() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + List deviceStatus = new ArrayList<>(); + deviceStatus.add(EnrolmentInfo.Status.ACTIVE.name()); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getAllDevicesOfGroup(deviceGroup.getName(), deviceStatus, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting devices of group '" + groupId + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getAllDevicesOfGroup() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getAllDevicesOfGroup(deviceGroup.getName(), TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting devices of group '" + groupId + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getGroupUnassignedDevices() { + DeviceGroup deviceGroup = getGroupById(groupId); + Device device = TestDataHolder.initialTestDevice; + Assert.assertNotNull(deviceGroup, "Group is null"); + PaginationRequest pr = new PaginationRequest(0,10); + pr.setDeviceType(device.getType()); + List groupNames = new ArrayList<>(); + groupNames.add(deviceGroup.getName()); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getGroupUnassignedDevices(pr, groupNames); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting devices of group '" + groupId + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getOwnGroupsCount() { + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getOwnGroupsCount(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID, "/"); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting own group count for '" + TestDataHolder.OWNER + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getOwnGroups() { + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getOwnGroups(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting own groups for '" + TestDataHolder.OWNER + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getOwnGroupIds() { + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getOwnGroupIds(TestDataHolder.OWNER, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting own group Ids for '" + TestDataHolder.OWNER + "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getDeviceCount() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getDeviceCount(deviceGroup.getGroupId(), TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting device count for '" +groupId+ "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void isDeviceMappedToGroup() { + DeviceGroup deviceGroup = getGroupById(groupId); + Device device = TestDataHolder.initialTestDevice; + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.isDeviceMappedToGroup(deviceGroup.getGroupId(), Integer.parseInt(device.getDeviceIdentifier()), TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while checking device map to group for '" +groupId+ "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getGroupCount() { + DeviceGroup deviceGroup = getGroupById(groupId); + GroupPaginationRequest pr = new GroupPaginationRequest(0,10); + pr.setGroupName(deviceGroup.getName()); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getGroupCount(pr, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting group count for '" +TestDataHolder.SUPER_TENANT_ID+ "'."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getGroupCountWithStatus() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getGroupCount(TestDataHolder.SUPER_TENANT_ID, EnrolmentInfo.Status.ACTIVE.name()); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting group count for" + TestDataHolder.SUPER_TENANT_ID; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getRootGroups() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getRootGroups(TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting group count for " + TestDataHolder.SUPER_TENANT_ID; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"addDeviceToGroupTest"}) + public void getAllGroupProperties() { + DeviceGroup deviceGroup = getGroupById(groupId); + Assert.assertNotNull(deviceGroup, "Group is null"); + try { + GroupManagementDAOFactory.beginTransaction(); + groupDAO.getAllGroupProperties(groupId, TestDataHolder.SUPER_TENANT_ID); + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while getting groups for " + TestDataHolder.SUPER_TENANT_ID; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } } From 922c775cfa645f4ca34b70c5857aa0ebca28ee4a Mon Sep 17 00:00:00 2001 From: Gimhan-minion Date: Thu, 13 Jun 2024 00:51:15 +0530 Subject: [PATCH 4/7] Modify permission of operation status update (cherry picked from commit 31c25de95663c7d25345515ff0e70e5ca3121179) --- .../jaxrs/service/api/DeviceManagementService.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java index f1e464a0d9..a028654cdc 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/DeviceManagementService.java @@ -165,6 +165,13 @@ import java.util.Map; roles = {"Internal/devicemgt-user"}, permissions = {"/device-mgt/devices/change-status"} ), + @Scope( + name = "Update status of a given operation", + description = "Updates the status of a given operation of a given device", + key = "dm:devices:ops:status:update", + roles = {"Internal/devicemgt-user"}, + permissions = {"/device-mgt/devices/operations/status-update"} + ), @Scope( name = "Enroll Device", description = "Register a device", @@ -2714,12 +2721,12 @@ public interface DeviceManagementService { @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", - value = "Update status of a given opeation", + value = "Update status of a given operation", notes = "Updates the status of a given operation of a given device in Entgra IoT Server.", tags = "Device Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:ops:view") + @ExtensionProperty(name = Constants.SCOPE, value = "dm:devices:ops:status:update") }) } ) From 43a6c289c55f771058e6b2a261d368b3c2419cfc Mon Sep 17 00:00:00 2001 From: pasindu Date: Fri, 14 Jun 2024 09:31:17 +0530 Subject: [PATCH 5/7] Change scope key not found log to debug level log (cherry picked from commit eadb2efb8636395b0a35f29589a163e187d46a27) --- .../extension/rest/api/PublisherRESTAPIServicesImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java index 8db730cc7b..e322ae0307 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java @@ -120,7 +120,9 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { throw new BadRequestException(msg); } else if (HttpStatus.SC_NOT_FOUND == response.code()) { String msg = "Shared scope key not found : " + key; - log.info(msg); + if (log.isDebugEnabled()) { + log.debug(msg); + } return false; } else { String msg = "Response : " + response.code() + response.body(); From 0de768cee03ef45065baab487b40c1001aef0a18 Mon Sep 17 00:00:00 2001 From: "amalka.subasinghe" Date: Mon, 24 Jun 2024 12:04:41 +0530 Subject: [PATCH 6/7] scopes and related permissions added when scope is not attached to an api (cherry picked from commit b7848fd0c315819f9082f60a4a833501305b2688) --- .../pom.xml | 4 +++- .../webapp/publisher/APIPublisherServiceImpl.java | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/pom.xml index 10bfbf4145..a668025519 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/pom.xml @@ -205,7 +205,9 @@ org.wso2.carbon.utils;version="4.6", org.wso2.carbon.utils.multitenancy;version="4.6", org.apache.commons.lang, - org.json + org.json, + io.entgra.device.mgt.core.device.mgt.common.permission.mgt, + io.entgra.device.mgt.core.device.mgt.core.permission.mgt jsr311-api;scope=compile|runtime;inline=false diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index e9a58e556c..cab16f8482 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -46,6 +46,7 @@ import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig; import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermission; import io.entgra.device.mgt.core.device.mgt.core.config.permission.DefaultPermissions; import io.entgra.device.mgt.core.device.mgt.core.config.permission.ScopeMapping; +import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -68,6 +69,8 @@ import org.wso2.carbon.user.core.tenant.TenantSearchResult; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; +import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionUtils; +import io.entgra.device.mgt.core.device.mgt.common.permission.mgt.PermissionManagementException; import java.io.BufferedReader; import java.io.File; @@ -610,9 +613,17 @@ public class APIPublisherServiceImpl implements APIPublisherService { if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getName())) { publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope); + // todo: permission changed in update path, is not handled yet. } else { - // todo: come to this level means, that scope is removed from API, but haven't removed from the scope-role-permission-mappings list - log.warn(scope.getName() + " not available as shared scope"); + // This scope doesn't have an api attached. + log.warn(scope.getName() + " not available as shared, add as new scope"); + publisherRESTAPIServices.addNewSharedScope(apiApplicationKey, accessTokenInfo, scope); + // add permission if not exist + try { + PermissionUtils.putPermission(permission); + } catch(PermissionManagementException e) { + log.error("Error when adding permission ", e); + } } } for (String role : rolePermissions.keySet()) { From faa9cea80a22067d23252c28b67cd8dfc2a1c3ca Mon Sep 17 00:00:00 2001 From: Sasini Sandamali Date: Mon, 24 Jun 2024 14:21:34 +0000 Subject: [PATCH 7/7] Add necessary improvements and configs for grafana version 10.3.3 (#396) Co-authored-by: Sasini_Sandamali Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/396 Co-authored-by: Sasini Sandamali Co-committed-by: Sasini Sandamali (cherry picked from commit 01d76c6dbd251e090ea70d4e5f32f38e6ec54b40) --- .../proxy/api/GrafanaAPIProxyService.java | 50 +++++++++++++++++++ .../api/impl/GrafanaAPIProxyServiceImpl.java | 37 +++++++++++++- .../impl/util/GrafanaRequestHandlerUtil.java | 20 +++++--- .../core/config/GrafanaConfiguration.java | 11 ++++ .../config/xml/bean/ValidationConfig.java | 45 +++++++++++++++++ .../main/resources/conf/grafana-config.xml | 6 +++ .../grafana-config.xml.j2 | 6 +++ 7 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/xml/bean/ValidationConfig.java diff --git a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/GrafanaAPIProxyService.java b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/GrafanaAPIProxyService.java index 18fb6201af..5bdf335d30 100644 --- a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/GrafanaAPIProxyService.java +++ b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/GrafanaAPIProxyService.java @@ -107,6 +107,23 @@ public interface GrafanaAPIProxyService { ) Response frontendMetrics(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo); + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/user/auth-tokens/rotate") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Rotate authentication tokens", + tags = "Analytics", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "grafana:api:view") + }) + } + ) + Response rotateAuthToken(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo); + @GET @Produces(MediaType.APPLICATION_JSON) @Path("/dashboards/uid/{uid}") @@ -123,6 +140,22 @@ public interface GrafanaAPIProxyService { ) Response getDashboard(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/folders/{uid}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Grafana dashboard folder information", + tags = "Analytics", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "grafana:api:view") + }) + } + ) + Response getFolders(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException; + @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -140,6 +173,23 @@ public interface GrafanaAPIProxyService { ) Response getAnnotations(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException; + @GET + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/prometheus/grafana/api/v1/rules") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Accessing Grafana Prometheus rule information", + tags = "Analytics", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "grafana:api:view") + }) + } + ) + Response prometheusRuleInfo(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) throws ClassNotFoundException; + @GET @Produces(MediaType.APPLICATION_JSON) @Path("/alerts/states-for-dashboard") diff --git a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/GrafanaAPIProxyServiceImpl.java b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/GrafanaAPIProxyServiceImpl.java index 8ed6fe1ca1..92eed32c74 100644 --- a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/GrafanaAPIProxyServiceImpl.java +++ b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/GrafanaAPIProxyServiceImpl.java @@ -26,6 +26,8 @@ import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.impl.util.Grafa import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.impl.util.GrafanaRequestHandlerUtil; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.common.exception.GrafanaManagementException; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.bean.GrafanaPanelIdentifier; +import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfiguration; +import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfigurationManager; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.exception.MaliciousQueryAttempt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,9 +58,13 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService { @Override public Response queryDatasource(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo) { try { + GrafanaConfiguration configuration = GrafanaConfigurationManager.getInstance().getGrafanaConfiguration(); GrafanaPanelIdentifier panelIdentifier = GrafanaRequestHandlerUtil.getPanelIdentifier(headers); - GrafanaMgtAPIUtils.getGrafanaQueryService().buildSafeQuery(body, panelIdentifier.getDashboardId(), - panelIdentifier.getPanelId(), requestUriInfo.getRequestUri()); + boolean queryValidationConfig = configuration.getValidationConfig().getDSQueryValidation(); + if (queryValidationConfig) { + GrafanaMgtAPIUtils.getGrafanaQueryService().buildSafeQuery(body, panelIdentifier.getDashboardId(), + panelIdentifier.getPanelId(), requestUriInfo.getRequestUri()); + } return GrafanaRequestHandlerUtil.proxyPassPostRequest(body, requestUriInfo, panelIdentifier.getOrgId()); } catch (MaliciousQueryAttempt e) { return Response.status(Response.Status.BAD_REQUEST).entity( @@ -83,6 +89,15 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService { return proxyPassPostRequest(body, headers, requestUriInfo); } + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/user/auth-tokens/rotate") + @Override + public Response rotateAuthToken(JsonObject body, @Context HttpHeaders headers, @Context UriInfo requestUriInfo) { + return proxyPassPostRequest(body, headers, requestUriInfo); + } + @GET @Produces(MediaType.APPLICATION_JSON) @Path("/dashboards/uid/{uid}") @@ -91,6 +106,14 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService { return proxyPassGetRequest(headers, requestUriInfo); } + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/folders/{uid}") + @Override + public Response getFolders(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) { + return proxyPassGetRequest(headers, requestUriInfo); + } + @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -99,6 +122,16 @@ public class GrafanaAPIProxyServiceImpl implements GrafanaAPIProxyService { public Response getAnnotations(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) { return proxyPassGetRequest(headers, requestUriInfo); } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/prometheus/grafana/api/v1/rules") + @Override + public Response prometheusRuleInfo(@Context HttpHeaders headers, @Context UriInfo requestUriInfo) { + return proxyPassGetRequest(headers, requestUriInfo); + } + @GET @Produces(MediaType.APPLICATION_JSON) @Path("/alerts/states-for-dashboard") diff --git a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/util/GrafanaRequestHandlerUtil.java b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/util/GrafanaRequestHandlerUtil.java index d81a6d4d26..e94b3ccf53 100644 --- a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/util/GrafanaRequestHandlerUtil.java +++ b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/api/impl/util/GrafanaRequestHandlerUtil.java @@ -22,6 +22,8 @@ import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.bean.ErrorRespo import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.api.exception.RefererNotValid; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.common.exception.GrafanaManagementException; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.bean.GrafanaPanelIdentifier; +import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfiguration; +import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.GrafanaConfigurationManager; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.exception.GrafanaEnvVariablesNotDefined; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.util.GrafanaConstants; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.util.GrafanaUtil; @@ -120,19 +122,23 @@ public class GrafanaRequestHandlerUtil { return path; } - public static GrafanaPanelIdentifier getPanelIdentifier(HttpHeaders headers) throws RefererNotValid { + public static GrafanaPanelIdentifier getPanelIdentifier(HttpHeaders headers) throws RefererNotValid, GrafanaManagementException { String referer = headers.getHeaderString(GrafanaConstants.REFERER_HEADER); - if(referer == null) { + if (referer == null) { String errMsg = "Request does not contain Referer header"; log.error(errMsg); throw new RefererNotValid(errMsg); } + GrafanaConfiguration configuration = GrafanaConfigurationManager.getInstance().getGrafanaConfiguration(); + boolean dashboardIntegrationConfig = configuration.getValidationConfig().getDashboardIntegration(); GrafanaPanelIdentifier panelIdentifier = GrafanaUtil.getPanelIdentifierFromReferer(referer); - if(panelIdentifier.getDashboardId() == null || - panelIdentifier.getPanelId() == null || panelIdentifier.getOrgId() == null) { - String errMsg = "Referer must contain dashboardId, panelId and orgId"; - log.error(errMsg); - throw new RefererNotValid(errMsg); + if (!dashboardIntegrationConfig) { + if (panelIdentifier.getDashboardId() == null || + panelIdentifier.getPanelId() == null || panelIdentifier.getOrgId() == null) { + String errMsg = "Referer must contain dashboardId, panelId, and orgId"; + log.error(errMsg); + throw new RefererNotValid(errMsg); + } } return panelIdentifier; } diff --git a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/GrafanaConfiguration.java b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/GrafanaConfiguration.java index bf2cbce90f..137dc86025 100644 --- a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/GrafanaConfiguration.java +++ b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/GrafanaConfiguration.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.CacheConfiguration; +import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.ValidationConfig; import io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean.User; import javax.xml.bind.annotation.XmlElement; @@ -30,6 +31,7 @@ import java.util.List; public class GrafanaConfiguration { private User adminUser; + private ValidationConfig validationConfig; private List caches; @XmlElement(name = "AdminUser") @@ -37,6 +39,15 @@ public class GrafanaConfiguration { return adminUser; } + @XmlElement(name = "ValidationConfig") + public ValidationConfig getValidationConfig() { + return validationConfig; + } + + public void setValidationConfig(ValidationConfig validationConfig) { + this.validationConfig = validationConfig; + } + public void setAdminUser(User user) { this.adminUser = user; } diff --git a/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/xml/bean/ValidationConfig.java b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/xml/bean/ValidationConfig.java new file mode 100644 index 0000000000..b15955bbe2 --- /dev/null +++ b/components/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core/src/main/java/io/entgra/device/mgt/core/analytics/mgt/grafana/proxy/core/config/xml/bean/ValidationConfig.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.core.config.xml.bean; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "ValidationConfig") +public class ValidationConfig { + private boolean dsQueryValidation; + private boolean dashboardIntegration; + + @XmlElement(name = "DSQueryValidation") + public boolean getDSQueryValidation() { + return dsQueryValidation; + } + + public void setDSQueryValidation(boolean dsQueryValidation) { + this.dsQueryValidation = dsQueryValidation; + } + + @XmlElement(name = "DashboardIntegration") + public boolean getDashboardIntegration() { + return dashboardIntegration; + } + + public void setDashboardIntegration(boolean dashboardIntegration) { + this.dashboardIntegration = dashboardIntegration; + } +} diff --git a/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf/grafana-config.xml b/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf/grafana-config.xml index f6ae22a601..e7b5a7bf3e 100644 --- a/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf/grafana-config.xml +++ b/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf/grafana-config.xml @@ -32,4 +32,10 @@ admin admin + + + true + + false + diff --git a/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf_templates.templates.repository.conf/grafana-config.xml.j2 b/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf_templates.templates.repository.conf/grafana-config.xml.j2 index 623a6cda06..dbf9d38a6e 100644 --- a/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf_templates.templates.repository.conf/grafana-config.xml.j2 +++ b/features/analytics-mgt/grafana-mgt/io.entgra.device.mgt.core.analytics.mgt.grafana.proxy.server.feature/src/main/resources/conf_templates.templates.repository.conf/grafana-config.xml.j2 @@ -32,4 +32,10 @@ admin admin + + + true + + false +