From 1e93dcce2bc91c87e8af38bda08294bcf0aac07a Mon Sep 17 00:00:00 2001 From: isuri Date: Thu, 21 Sep 2023 15:50:59 +0530 Subject: [PATCH] Device Organization CRUD operations --- .../dao/DeviceOrganizationDAO.java | 9 + .../dao/impl/DeviceOrganizationDAOImpl.java | 118 +- .../dao/util/DeviceOrganizationDaoUtil.java | 21 +- .../organization/dto/DeviceOrganization.java | 16 +- .../impl/DeviceOrganizationServiceImpl.java | 111 +- ...DeviceOrganizationMgtServiceComponent.java | 3 +- .../spi/DeviceOrganizationService.java | 10 + .../device/organization/DAONegativeTest.java | 52 + .../device/organization/DAOTest.java | 96 ++ .../device/organization/DataSourceConfig.java | 78 ++ .../organization/ServiceNegativeTest.java | 46 + .../device/organization/ServiceTest.java | 90 ++ .../device/organization/TestUtils.java | 53 + .../mock/BaseDeviceOrganizationTest.java | 157 +++ .../organization/mock/MockConnection.java | 329 +++++ .../organization/mock/MockDataSource.java | 123 ++ .../mock/MockDatabaseMetaData.java | 937 ++++++++++++++ .../organization/mock/MockResultSet.java | 1099 +++++++++++++++++ .../organization/mock/MockStatement.java | 545 ++++++++ .../carbon-home/dbscripts/dm-db-h2.sql | 14 +- .../resources/carbon-home/dbscripts/h2.sql | 18 +- .../datasource/data-source-config-mock.xml | 33 - .../data-source-config-no-table.xml | 33 - .../config/datasource/data-source-config.xml | 33 - .../src/test/resources/sql/h2.sql | 11 - .../src/test/resources/testng.xml | 6 +- 26 files changed, 3878 insertions(+), 163 deletions(-) create mode 100644 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/DAONegativeTest.java create mode 100644 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/DAOTest.java create mode 100644 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/DataSourceConfig.java create mode 100644 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/ServiceNegativeTest.java create mode 100644 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 create mode 100644 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/TestUtils.java create mode 100644 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/mock/BaseDeviceOrganizationTest.java create mode 100644 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/mock/MockConnection.java create mode 100644 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/mock/MockDataSource.java create mode 100644 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/mock/MockDatabaseMetaData.java create mode 100644 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/mock/MockResultSet.java create mode 100644 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/mock/MockStatement.java delete mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-mock.xml delete mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-no-table.xml delete mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config.xml delete mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql 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/DeviceOrganizationDAO.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/DeviceOrganizationDAO.java index 6fd54216d0..aeb2a22763 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/DeviceOrganizationDAO.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/DeviceOrganizationDAO.java @@ -18,8 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao; import io.entgra.device.mgt.core.device.mgt.common.Device; +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 java.sql.Date; import java.util.List; public interface DeviceOrganizationDAO { @@ -27,4 +29,11 @@ public interface DeviceOrganizationDAO { List getChildDevices(int parentId) throws DeviceOrganizationMgtDAOException; List getParentDevices(Integer deviceID) throws DeviceOrganizationMgtDAOException; + + boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException; + + boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, int organizationId) + throws DeviceOrganizationMgtDAOException; + + DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException; } 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 3950020071..de4a370b3e 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 @@ -20,19 +20,17 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao. import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.List; -import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.generateParameterPlaceholders; import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet; public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { @@ -44,11 +42,11 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List childDevices = null; try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT d.ID, d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE " + + String sql = "SELECT d.ID, d.DESCRIPTION, d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " + "FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.PARENT_DEVICE_ID = ? AND d.DEVICE_TYPE_ID = t.ID"; + "WHERE d.ID = ? AND d.DEVICE_TYPE_ID = t.ID"; - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, parentId); try (ResultSet rs = stmt.executeQuery()) { childDevices = new ArrayList<>(); @@ -77,12 +75,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List parentDevices = null; try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT d.ID, d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE " + + String sql = "SELECT d.ID, d.DESCRIPTION,d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " + "FROM DM_DEVICE d, DM_DEVICE_TYPE t " + - "WHERE d.ID IN (SELECT PARENT_DEVICE_ID FROM DM_DEVICE WHERE ID = ?) " + + "WHERE d.ID IN (SELECT ID FROM DM_DEVICE WHERE ID = ?) " + "AND d.DEVICE_TYPE_ID = t.ID"; - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, deviceID); try (ResultSet rs = stmt.executeQuery()) { parentDevices = new ArrayList<>(); @@ -106,18 +104,92 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { } } + @Override + public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException { + try { + String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP, STATUS)" + + " VALUES (?, ?, ?, ?)"; + + Connection conn = ConnectionManagerUtil.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceOrganization.getDeviceId()); + stmt.setInt(2, deviceOrganization.getParentDeviceId()); + stmt.setDate(3, deviceOrganization.getUpdateTime()); + stmt.setString(4, deviceOrganization.getStatus().toString()); + return stmt.executeUpdate() > 0; + } + + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to insert device organization for " + + deviceOrganization.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to insert device organization for " + + deviceOrganization.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + @Override + public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, int organizationId) + throws DeviceOrganizationMgtDAOException { + try { + String sql = "UPDATE DM_DEVICE_ORGANIZATION SET DEVICE_ID = ? , PARENT_DEVICE_ID = ? , " + + "LAST_UPDATED_TIMESTAMP = ? , STATUS = ? WHERE ID = ? "; + + Connection conn = ConnectionManagerUtil.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceID); + stmt.setInt(2, parentDeviceID); + stmt.setDate(3, timestamp); + stmt.setString(4, status); + stmt.setInt(5, organizationId); + return stmt.executeUpdate() > 0; + } - // Simulated database storage + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to update device organization for " + + organizationId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to update device organization for " + + organizationId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + @Override + public DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException { + try { + String sql = "SELECT do.ID,do.DEVICE_ID, do.PARENT_DEVICE_ID, do.LAST_UPDATED_TIMESTAMP, do.STATUS " + + "FROM DM_DEVICE_ORGANIZATION do WHERE do.ID = ? "; + + Connection conn = ConnectionManagerUtil.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, organizationId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return DeviceOrganizationDaoUtil.loadDeviceOrganization(rs); + } + return null; + } + } + + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to get device organization details for " + + organizationId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to get device organization details for " + + organizationId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } -// public void addDevice(Device device) { -// devices.put(device.getId(), device); -// } -// -// public void addChildDevice(int parentId, int childId) { -// deviceHierarchy.computeIfAbsent(parentId, k -> new ArrayList<>()).add(childId); -// } -// -// public Device getDeviceById(int deviceId) { -// return devices.get(deviceId); -// } } 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/util/DeviceOrganizationDaoUtil.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/util/DeviceOrganizationDaoUtil.java index fe932a8b40..fbd2f0cd90 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/util/DeviceOrganizationDaoUtil.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/util/DeviceOrganizationDaoUtil.java @@ -2,8 +2,10 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao. import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import java.sql.ResultSet; import java.sql.SQLException; @@ -14,16 +16,15 @@ public class DeviceOrganizationDaoUtil { private static final Log log = LogFactory.getLog(DeviceOrganizationDaoUtil.class); - // Helper method to generate parameter placeholders for the SQL query - public static String generateParameterPlaceholders(int count) { - StringBuilder placeholders = new StringBuilder(); - for (int i = 0; i < count; i++) { - placeholders.append("?"); - if (i < count - 1) { - placeholders.append(", "); - } - } - return placeholders.toString(); + public static DeviceOrganization loadDeviceOrganization(ResultSet rs) throws SQLException { + DeviceOrganization deviceOrganization = new DeviceOrganization() { + }; + deviceOrganization.setOrganizationId(rs.getInt("ID")); + deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID")); + deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID")); + deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP")); + deviceOrganization.setStatus(DeviceOrganization.DeviceOrganizationStatus.valueOf(rs.getString("STATUS"))); + return deviceOrganization; } // Helper method to create a Device object from a ResultSet 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/dto/DeviceOrganization.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/dto/DeviceOrganization.java index 9e41771802..9ba3199b46 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/dto/DeviceOrganization.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/dto/DeviceOrganization.java @@ -18,7 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; -import java.util.Date; + +import java.sql.Date; public abstract class DeviceOrganization { @@ -26,6 +27,7 @@ public abstract class DeviceOrganization { private int deviceId; private int parentDeviceId; private Date updateTime; + private DeviceOrganizationStatus status; public int getOrganizationId() { return organizationId; @@ -58,4 +60,16 @@ public abstract class DeviceOrganization { public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } + + public DeviceOrganizationStatus getStatus() { + return status; + } + + public void setStatus(DeviceOrganizationStatus status) { + this.status = status; + } + + public enum DeviceOrganizationStatus { + ACT, INA + } } 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/impl/DeviceOrganizationServiceImpl.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/impl/DeviceOrganizationServiceImpl.java index b087f2dfaa..8c50fe8b19 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/impl/DeviceOrganizationServiceImpl.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/impl/DeviceOrganizationServiceImpl.java @@ -22,6 +22,8 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.D import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; @@ -29,6 +31,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.D import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.sql.Date; import java.util.ArrayList; import java.util.List; @@ -43,7 +46,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } @Override - public List getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException { + public List getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) + throws DeviceOrganizationMgtPluginException { try { // Open a database connection ConnectionManagerUtil.openDBConnection(); @@ -69,7 +73,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } @Override - public List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException { + public List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) + throws DeviceOrganizationMgtPluginException { try { // Open a database connection ConnectionManagerUtil.openDBConnection(); @@ -135,4 +140,106 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } } } + + @Override + public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) + throws DeviceOrganizationMgtPluginException { + String msg = ""; + + try { + ConnectionManagerUtil.beginDBTransaction(); + boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization); + if (result) { + msg = "Device organization added successfully,for " + deviceOrganization.getDeviceId(); + if (log.isDebugEnabled()) { + log.debug(msg); + } + } else { + ConnectionManagerUtil.rollbackDBTransaction(); + msg = "Device organization failed to add,for " + deviceOrganization.getDeviceId(); + throw new DeviceOrganizationMgtPluginException(msg); + } + ConnectionManagerUtil.commitDBTransaction(); + return true; + } catch (DBConnectionException e) { + msg = "Error occurred while obtaining the database connection to add device organization for " + + deviceOrganization.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + msg = "Error occurred in the database level while adding device organization for " + + deviceOrganization.getDeviceId(); + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, + int organizationId) throws DeviceOrganizationMgtPluginException { + String msg = ""; + DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationId); + if (deviceOrganization == null) { + String errorMsg = "Cannot find device organization for organization ID" + organizationId; + log.error(errorMsg); + throw new NullPointerException(errorMsg); +// throw new BadRequestException(errorMsg); + } + + try { + ConnectionManagerUtil.beginDBTransaction(); + boolean result = deviceOrganizationDao.updateDeviceOrganization(deviceID, parentDeviceID, timestamp, status, + organizationId); + if (result) { + msg = "Device organization updated successfully,for " + deviceID; + if (log.isDebugEnabled()) { + log.debug(msg); + } + } else { + ConnectionManagerUtil.rollbackDBTransaction(); + msg = "Device organization failed to update,for " + deviceID; + throw new DeviceOrganizationMgtPluginException(msg); + } + ConnectionManagerUtil.commitDBTransaction(); + return true; + } catch (DBConnectionException e) { + msg = "Error occurred while obtaining the database connection to update device organization for " + deviceID; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + msg = "Error occurred in the database level while updating device organization for " + deviceID; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + + } + } + + @Override + public DeviceOrganization getDeviceOrganizationByID(int organizationId) + throws DeviceOrganizationMgtPluginException { + try { + // Open a database connection + ConnectionManagerUtil.openDBConnection(); + DeviceOrganization deviceOrganization = deviceOrganizationDao.getDeviceOrganizationByID(organizationId); + return deviceOrganization; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to retrieve child devices"; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + String msg = "Error occurred in the database level while retrieving child devices"; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + // Close the database connection + ConnectionManagerUtil.closeDBConnection(); + } + } + } 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/internal/DeviceOrganizationMgtServiceComponent.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/internal/DeviceOrganizationMgtServiceComponent.java index 1bab2b6344..a86f629ae1 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/internal/DeviceOrganizationMgtServiceComponent.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/internal/DeviceOrganizationMgtServiceComponent.java @@ -21,7 +21,6 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.inte import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager; import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig; import io.entgra.device.mgt.core.device.mgt.core.config.datasource.DataSourceConfig; -import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl.DeviceOrganizationServiceImpl; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; @@ -83,7 +82,7 @@ public class DeviceOrganizationMgtServiceComponent { protected void deactivate(ComponentContext componentContext) { if (log.isDebugEnabled()) { - log.debug("De-activating Device SubType Management Service Component"); + log.debug("De-activating Device Organization Management Service Component"); } } 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/spi/DeviceOrganizationService.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/spi/DeviceOrganizationService.java index 2949f454e1..d515fa5110 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/spi/DeviceOrganizationService.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/spi/DeviceOrganizationService.java @@ -18,8 +18,10 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; +import java.sql.Date; import java.util.List; public interface DeviceOrganizationService { @@ -27,4 +29,12 @@ public interface DeviceOrganizationService { List getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException; List getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException; + + boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtPluginException; + + boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, + int organizationId) throws DeviceOrganizationMgtPluginException; + + DeviceOrganization getDeviceOrganizationByID(int organizationId) + throws DeviceOrganizationMgtPluginException; } 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/DAONegativeTest.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/DAONegativeTest.java new file mode 100644 index 0000000000..433be9ef94 --- /dev/null +++ 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/DAONegativeTest.java @@ -0,0 +1,52 @@ +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.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.DBConnectionException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class DAONegativeTest extends BaseDeviceOrganizationTest { + + private static final Log log = LogFactory.getLog(DAONegativeTest.class); + + private DeviceOrganizationDAO deviceOrganizationDAO; + + @BeforeClass + public void init() { + deviceOrganizationDAO = DeviceOrganizationDAOFactory.getDeviceOrganizationDAO(); + log.info("DAO test initialized"); + } + + @Test(description = "This method tests the add device organization method under negative circumstances with null data", + expectedExceptions = {NullPointerException.class} + ) + public void testAddDeviceOrganization() throws DeviceOrganizationMgtDAOException { + DeviceOrganization deviceOrganization = new DeviceOrganization() { + }; + try { + ConnectionManagerUtil.beginDBTransaction(); + deviceOrganizationDAO.addDeviceOrganization(deviceOrganization); + ConnectionManagerUtil.commitDBTransaction(); + } catch (DeviceOrganizationMgtDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred while processing SQL to insert device organization"; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to insert device organization"; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + +} 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/DAOTest.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/DAOTest.java new file mode 100644 index 0000000000..472cc67505 --- /dev/null +++ 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/DAOTest.java @@ -0,0 +1,96 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization; + +import io.entgra.device.mgt.core.device.mgt.common.Device; +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.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.DBConnectionException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.sql.Date; +import java.util.List; + +public class DAOTest extends BaseDeviceOrganizationTest { + + private static final Log log = LogFactory.getLog(DAOTest.class); + + private DeviceOrganizationDAO deviceOrganizationDAO; + + @BeforeClass + public void init() { + deviceOrganizationDAO = DeviceOrganizationDAOFactory.getDeviceOrganizationDAO(); + log.info("DAO test initialized"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetChildrenOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.openDBConnection(); + List childrenList = deviceOrganizationDAO.getChildDevices(3); + ConnectionManagerUtil.closeDBConnection(); + Assert.assertNotNull(childrenList, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.openDBConnection(); + List parentList = deviceOrganizationDAO.getParentDevices(4); + ConnectionManagerUtil.closeDBConnection(); + Assert.assertNotNull(parentList, "Cannot be null"); + } + + @Test + public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { + + DeviceOrganization.DeviceOrganizationStatus status = DeviceOrganization.DeviceOrganizationStatus.ACT; + DeviceOrganization deviceOrganization = new DeviceOrganization() { + }; + deviceOrganization.setDeviceId(4); + deviceOrganization.setParentDeviceId(3); + deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); + deviceOrganization.setStatus(status); + ConnectionManagerUtil.beginDBTransaction(); + boolean result = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); + + Assert.assertNotNull(result, "Cannot be null"); + + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testUpdateDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.beginDBTransaction(); + boolean result = deviceOrganizationDAO.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "ACTIVE", 1); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); + + Assert.assertNotNull(result, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testUpdateDeviceOrganizationInactivate() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.beginDBTransaction(); + boolean result = deviceOrganizationDAO.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "INACTIVE", 1); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); + + Assert.assertNotNull(result, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.beginDBTransaction(); + DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); + + Assert.assertNotNull(deviceOrganization, "Cannot be null"); + } +} 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/DataSourceConfig.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/DataSourceConfig.java new file mode 100644 index 0000000000..240633038a --- /dev/null +++ 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/DataSourceConfig.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DataSourceConfig") +public class DataSourceConfig { + + private String url; + private String driverClassName; + private String user; + private String password; + + @Override + public String toString() { + return "DataSourceConfig[" + + " Url ='" + url + '\'' + + ", DriverClassName ='" + driverClassName + '\'' + + ", UserName ='" + user + '\'' + + ", Password ='" + password + '\'' + + "]"; + } + + @XmlElement(name = "Url", nillable = false) + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + @XmlElement(name = "DriverClassName", nillable = false) + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + @XmlElement(name = "User", nillable = false) + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + @XmlElement(name = "Password", nillable = false) + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} 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/ServiceNegativeTest.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/ServiceNegativeTest.java new file mode 100644 index 0000000000..24d6e541fa --- /dev/null +++ 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/ServiceNegativeTest.java @@ -0,0 +1,46 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization; + +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl.DeviceOrganizationServiceImpl; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.sql.Date; + +public class ServiceNegativeTest extends BaseDeviceOrganizationTest { + + private static final Log log = LogFactory.getLog(ServiceNegativeTest.class); + + private DeviceOrganizationService deviceOrganizationService; + + @BeforeClass + public void init() { + deviceOrganizationService = new DeviceOrganizationServiceImpl(); + log.info("Service test initialized"); + } + + @Test(description = "This method tests Add Device Organization method under negative circumstances with null data", + expectedExceptions = {NullPointerException.class}) + + public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException { + + DeviceOrganization deviceOrganization = new DeviceOrganization(){}; + boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization); + } + + @Test(description = "This method tests Update Device Organization method under negative circumstances with " + + "invalid data", + expectedExceptions = {NullPointerException.class}) + + public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException { + + boolean result = deviceOrganizationService.updateDeviceOrganization(2,3, + new Date(System.currentTimeMillis()), "INACTIVE", 5); + } +} 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 new file mode 100644 index 0000000000..2aa223b8aa --- /dev/null +++ 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 @@ -0,0 +1,90 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization; + +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl.DeviceOrganizationServiceImpl; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.sql.Date; +import java.util.List; + +public class ServiceTest extends BaseDeviceOrganizationTest { + + private static final Log log = LogFactory.getLog(ServiceTest.class); + + private DeviceOrganizationService deviceOrganizationService; + + @BeforeClass + public void init() { + deviceOrganizationService = new DeviceOrganizationServiceImpl(); + log.info("Service test initialized"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException { + + DeviceNode deviceNode = new DeviceNode(); + deviceNode.setDeviceId(3); + List childrenList = deviceOrganizationService.getChildrenOf(deviceNode, 2, true); + + Assert.assertNotNull(childrenList, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetParentsOf() throws DeviceOrganizationMgtPluginException { + + DeviceNode deviceNode = new DeviceNode(); + deviceNode.setDeviceId(4); + List parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, true); + + Assert.assertNotNull(parentList, "Cannot be null"); + } + + @Test + public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException { + + DeviceOrganization.DeviceOrganizationStatus status = DeviceOrganization.DeviceOrganizationStatus.ACT; + + DeviceOrganization deviceOrganization = new DeviceOrganization() { + }; + deviceOrganization.setDeviceId(4); + deviceOrganization.setParentDeviceId(3); + deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis())); + deviceOrganization.setStatus(status); + boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization); + + Assert.assertNotNull(result, "Cannot be null"); + + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException { + + boolean result = deviceOrganizationService.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "ACTIVE", 1); + + Assert.assertNotNull(result, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testUpdateDeviceOrganizationInactivate() throws DeviceOrganizationMgtPluginException { + + boolean result = deviceOrganizationService.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "INACTIVE", 1); + + Assert.assertNotNull(result, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException { + + DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(1); + + Assert.assertNotNull(deviceOrganization, "Cannot be null"); + } +} 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/TestUtils.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/TestUtils.java new file mode 100644 index 0000000000..9dcac705be --- /dev/null +++ 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/TestUtils.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization; + +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class TestUtils { + + private static final Log log = LogFactory.getLog(TestUtils.class); + + public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + ConnectionManagerUtil.closeDBConnection(); + } + } +} 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/mock/BaseDeviceOrganizationTest.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/mock/BaseDeviceOrganizationTest.java new file mode 100644 index 0000000000..4703ebeb6b --- /dev/null +++ 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/mock/BaseDeviceOrganizationTest.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.DataSourceConfig; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.TestUtils; +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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.tomcat.jdbc.pool.PoolProperties; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.w3c.dom.Document; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; + +import javax.sql.DataSource; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.lang.reflect.Field; +import java.sql.Connection; +import java.sql.Statement; + +public abstract class BaseDeviceOrganizationTest { + + private static final Log log = LogFactory.getLog(BaseDeviceOrganizationTest.class); + + private static final String datasourceLocation = "src/test/resources/carbon-home/repository/conf/" + + "datasources/data-source-config.xml"; + + private static boolean mock; + + @BeforeSuite + @Parameters({"isMock"}) + public void setup(@Optional("false") boolean isMock) throws Exception { + log.info("Setting up test suite"); + this.initDataSource(); + this.initSQLScript(); + this.initializeCarbonContext(); + this.initServices(); + mock = isMock; + log.info("Setting up test suite done!"); + } + + protected void initDataSource() throws Exception { + DataSource dataSource = this.getDataSource(this.readDataSourceConfig()); + Class clazz1 = ConnectionManagerUtil.class; + Field f1 = clazz1.getDeclaredField("dataSource"); + f1.setAccessible(true); + f1.set(clazz1, dataSource); + + Class clazz2 = DeviceOrganizationDAOFactory.class; + Field f2 = clazz2.getDeclaredField("databaseEngine"); + f2.setAccessible(true); + f2.set(clazz2, DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2); + } + + private void initServices() { + + } + + @BeforeClass + public abstract void init() throws Exception; + + protected DataSource getDataSource(DataSourceConfig config) { + if (!isMock()) { + PoolProperties properties = new PoolProperties(); + properties.setUrl(config.getUrl()); + properties.setDriverClassName(config.getDriverClassName()); + properties.setUsername(config.getUser()); + properties.setPassword(config.getPassword()); + return new org.apache.tomcat.jdbc.pool.DataSource(properties); + } else { + return new MockDataSource(config.getUrl()); + } + } + + private void initializeCarbonContext() { + if (System.getProperty("carbon.home") == null) { + File file = new File("src/test/resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("../resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("../../resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + file = new File("../../../resources/carbon-home"); + if (file.exists()) { + System.setProperty("carbon.home", file.getAbsolutePath()); + } + } + + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants + .SUPER_TENANT_DOMAIN_NAME); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); + } + + protected DataSourceConfig readDataSourceConfig() throws DeviceManagementException { + try { + File file = new File(BaseDeviceOrganizationTest.datasourceLocation); + Document doc = DeviceManagerUtil.convertToDocument(file); + JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class); + Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); + return (DataSourceConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new DeviceManagementException("Error occurred while reading data source configuration", e); + } + } + + private void initSQLScript() throws Exception { + Connection conn = null; + Statement stmt = null; + try { + ConnectionManagerUtil.beginDBTransaction(); + conn = ConnectionManagerUtil.getDBConnection(); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/carbon-home/dbscripts/dm-db-h2.sql'"); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/carbon-home/dbscripts/h2.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } + } + + protected boolean isMock() { + return mock; + } + +} 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/mock/MockConnection.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/mock/MockConnection.java new file mode 100644 index 0000000000..d0e34b7538 --- /dev/null +++ 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/mock/MockConnection.java @@ -0,0 +1,329 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +/** + * This is mock class which provides mock database connection. + */ + +public class MockConnection implements Connection { + + private final String url; + private final List statements = new ArrayList<>(); + private int statementCounter = 0; + + public MockConnection(String url) { + this.url = url; + } + + @Override + public Statement createStatement() throws SQLException { + return getStatement(); + } + + private MockStatement getStatement() { + if (!statements.isEmpty()) { + MockStatement statement = this.statements.get(this.statementCounter); + statementCounter++; + return statement; + } + return new MockStatement(); + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + return getStatement(); + } + + @Override + public CallableStatement prepareCall(String sql) throws SQLException { + return null; + } + + @Override + public String nativeSQL(String sql) throws SQLException { + return null; + } + + @Override + public boolean getAutoCommit() throws SQLException { + return false; + } + + @Override + public void setAutoCommit(boolean autoCommit) throws SQLException { + + } + + @Override + public void commit() throws SQLException { + + } + + @Override + public void rollback() throws SQLException { + + } + + @Override + public void close() throws SQLException { + + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + return new MockDatabaseMetaData(this.url); + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public void setReadOnly(boolean readOnly) throws SQLException { + + } + + @Override + public String getCatalog() throws SQLException { + return null; + } + + @Override + public void setCatalog(String catalog) throws SQLException { + + } + + @Override + public int getTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public void setTransactionIsolation(int level) throws SQLException { + + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public void clearWarnings() throws SQLException { + + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + return null; + } + + @Override + public Map> getTypeMap() throws SQLException { + return null; + } + + @Override + public void setTypeMap(Map> map) throws SQLException { + + } + + @Override + public int getHoldability() throws SQLException { + return 0; + } + + @Override + public void setHoldability(int holdability) throws SQLException { + + } + + @Override + public Savepoint setSavepoint() throws SQLException { + return null; + } + + @Override + public Savepoint setSavepoint(String name) throws SQLException { + return null; + } + + @Override + public void rollback(Savepoint savepoint) throws SQLException { + + } + + @Override + public void releaseSavepoint(Savepoint savepoint) throws SQLException { + + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { + return new MockStatement(); + } + + @Override + public Clob createClob() throws SQLException { + return null; + } + + @Override + public Blob createBlob() throws SQLException { + return null; + } + + @Override + public NClob createNClob() throws SQLException { + return null; + } + + @Override + public SQLXML createSQLXML() throws SQLException { + return null; + } + + @Override + public boolean isValid(int timeout) throws SQLException { + return false; + } + + @Override + public void setClientInfo(String name, String value) throws SQLClientInfoException { + + } + + @Override + public String getClientInfo(String name) throws SQLException { + return null; + } + + @Override + public Properties getClientInfo() throws SQLException { + return null; + } + + @Override + public void setClientInfo(Properties properties) throws SQLClientInfoException { + + } + + @Override + public Array createArrayOf(String typeName, Object[] elements) throws SQLException { + return null; + } + + @Override + public Struct createStruct(String typeName, Object[] attributes) throws SQLException { + return null; + } + + @Override + public String getSchema() throws SQLException { + return null; + } + + @Override + public void setSchema(String schema) throws SQLException { + + } + + @Override + public void abort(Executor executor) throws SQLException { + + } + + @Override + public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { + + } + + @Override + public int getNetworkTimeout() throws SQLException { + return 0; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + public void addMockStatement(MockStatement mockStatement) { + this.statements.add(mockStatement); + } + +} 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/mock/MockDataSource.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/mock/MockDataSource.java new file mode 100644 index 0000000000..8b266cd85f --- /dev/null +++ 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/mock/MockDataSource.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import javax.sql.DataSource; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +/** + * This is the mock data source implementation that will be used in the test cases. + */ +public class MockDataSource implements DataSource { + private final List connections = new ArrayList<>(); + private final String url; + private boolean throwException = false; + private int connectionCounter = 0; + + public MockDataSource(String url) { + this.url = url; + } + + @Override + public Connection getConnection() throws SQLException { + if (throwException) { + throw new SQLException("Cannot created test connection."); + } else { + if (!connections.isEmpty()) { + if (this.connectionCounter < this.connections.size()) { + Connection connection = this.connections.get(this.connectionCounter); + this.connectionCounter++; + return connection; + } else { + return new MockConnection(url); + } + } + return new MockConnection(url); + } + } + + public void setConnection(Connection connection) { + this.connections.add(connection); + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return null; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public PrintWriter getLogWriter() throws SQLException { + return null; + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + + } + + @Override + public int getLoginTimeout() throws SQLException { + return 0; + } + + @Override + public void setLoginTimeout(int seconds) throws SQLException { + + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } + + public void setThrowException(boolean throwException) { + this.throwException = throwException; + } + + public void reset() { + this.throwException = false; + this.connections.clear(); + this.connectionCounter = 0; + } + + public String getUrl() { + return this.url; + } + + public MockConnection getConnection(int id) { + return (MockConnection) this.connections.get(id); + } + +} 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/mock/MockDatabaseMetaData.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/mock/MockDatabaseMetaData.java new file mode 100644 index 0000000000..fb74650902 --- /dev/null +++ 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/mock/MockDatabaseMetaData.java @@ -0,0 +1,937 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants; + +import java.sql.*; + +public class MockDatabaseMetaData implements DatabaseMetaData { + private final String url; + + public MockDatabaseMetaData(String url) { + this.url = url; + } + + @Override + public boolean allProceduresAreCallable() throws SQLException { + return false; + } + + @Override + public boolean allTablesAreSelectable() throws SQLException { + return false; + } + + @Override + public String getURL() throws SQLException { + return null; + } + + @Override + public String getUserName() throws SQLException { + return null; + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedHigh() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedLow() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedAtStart() throws SQLException { + return false; + } + + @Override + public boolean nullsAreSortedAtEnd() throws SQLException { + return false; + } + + @Override + public String getDatabaseProductName() throws SQLException { + if (this.url.contains("mysql")) { + return DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL; + } else if (this.url.contains("h2")) { + return DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2; + } else if (this.url.contains("oracle")) { + return DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE; + } else if (this.url.contains("postgresql")) { + return DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL; + } else if (this.url.contains("sqlserver")) { + return DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL; + } else { + return null; + } + } + + @Override + public String getDatabaseProductVersion() throws SQLException { + return null; + } + + @Override + public String getDriverName() throws SQLException { + return null; + } + + @Override + public String getDriverVersion() throws SQLException { + return null; + } + + @Override + public int getDriverMajorVersion() { + return 0; + } + + @Override + public int getDriverMinorVersion() { + return 0; + } + + @Override + public boolean usesLocalFiles() throws SQLException { + return false; + } + + @Override + public boolean usesLocalFilePerTable() throws SQLException { + return false; + } + + @Override + public boolean supportsMixedCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesUpperCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesLowerCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesMixedCaseIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesUpperCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesLowerCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public boolean storesMixedCaseQuotedIdentifiers() throws SQLException { + return false; + } + + @Override + public String getIdentifierQuoteString() throws SQLException { + return null; + } + + @Override + public String getSQLKeywords() throws SQLException { + return null; + } + + @Override + public String getNumericFunctions() throws SQLException { + return null; + } + + @Override + public String getStringFunctions() throws SQLException { + return null; + } + + @Override + public String getSystemFunctions() throws SQLException { + return null; + } + + @Override + public String getTimeDateFunctions() throws SQLException { + return null; + } + + @Override + public String getSearchStringEscape() throws SQLException { + return null; + } + + @Override + public String getExtraNameCharacters() throws SQLException { + return null; + } + + @Override + public boolean supportsAlterTableWithAddColumn() throws SQLException { + return false; + } + + @Override + public boolean supportsAlterTableWithDropColumn() throws SQLException { + return false; + } + + @Override + public boolean supportsColumnAliasing() throws SQLException { + return false; + } + + @Override + public boolean nullPlusNonNullIsNull() throws SQLException { + return false; + } + + @Override + public boolean supportsConvert() throws SQLException { + return false; + } + + @Override + public boolean supportsConvert(int fromType, int toType) throws SQLException { + return false; + } + + @Override + public boolean supportsTableCorrelationNames() throws SQLException { + return false; + } + + @Override + public boolean supportsDifferentTableCorrelationNames() throws SQLException { + return false; + } + + @Override + public boolean supportsExpressionsInOrderBy() throws SQLException { + return false; + } + + @Override + public boolean supportsOrderByUnrelated() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupBy() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupByUnrelated() throws SQLException { + return false; + } + + @Override + public boolean supportsGroupByBeyondSelect() throws SQLException { + return false; + } + + @Override + public boolean supportsLikeEscapeClause() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleResultSets() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsNonNullableColumns() throws SQLException { + return false; + } + + @Override + public boolean supportsMinimumSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsCoreSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsExtendedSQLGrammar() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92EntryLevelSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92IntermediateSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsANSI92FullSQL() throws SQLException { + return false; + } + + @Override + public boolean supportsIntegrityEnhancementFacility() throws SQLException { + return false; + } + + @Override + public boolean supportsOuterJoins() throws SQLException { + return false; + } + + @Override + public boolean supportsFullOuterJoins() throws SQLException { + return false; + } + + @Override + public boolean supportsLimitedOuterJoins() throws SQLException { + return false; + } + + @Override + public String getSchemaTerm() throws SQLException { + return null; + } + + @Override + public String getProcedureTerm() throws SQLException { + return null; + } + + @Override + public String getCatalogTerm() throws SQLException { + return null; + } + + @Override + public boolean isCatalogAtStart() throws SQLException { + return false; + } + + @Override + public String getCatalogSeparator() throws SQLException { + return null; + } + + @Override + public boolean supportsSchemasInDataManipulation() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInProcedureCalls() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInTableDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInIndexDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInDataManipulation() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInProcedureCalls() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInTableDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInIndexDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException { + return false; + } + + @Override + public boolean supportsPositionedDelete() throws SQLException { + return false; + } + + @Override + public boolean supportsPositionedUpdate() throws SQLException { + return false; + } + + @Override + public boolean supportsSelectForUpdate() throws SQLException { + return false; + } + + @Override + public boolean supportsStoredProcedures() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInComparisons() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInExists() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInIns() throws SQLException { + return false; + } + + @Override + public boolean supportsSubqueriesInQuantifieds() throws SQLException { + return false; + } + + @Override + public boolean supportsCorrelatedSubqueries() throws SQLException { + return false; + } + + @Override + public boolean supportsUnion() throws SQLException { + return false; + } + + @Override + public boolean supportsUnionAll() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenCursorsAcrossCommit() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenCursorsAcrossRollback() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenStatementsAcrossCommit() throws SQLException { + return false; + } + + @Override + public boolean supportsOpenStatementsAcrossRollback() throws SQLException { + return false; + } + + @Override + public int getMaxBinaryLiteralLength() throws SQLException { + return 0; + } + + @Override + public int getMaxCharLiteralLength() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInGroupBy() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInIndex() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInOrderBy() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInSelect() throws SQLException { + return 0; + } + + @Override + public int getMaxColumnsInTable() throws SQLException { + return 0; + } + + @Override + public int getMaxConnections() throws SQLException { + return 0; + } + + @Override + public int getMaxCursorNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxIndexLength() throws SQLException { + return 0; + } + + @Override + public int getMaxSchemaNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxProcedureNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxCatalogNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxRowSize() throws SQLException { + return 0; + } + + @Override + public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { + return false; + } + + @Override + public int getMaxStatementLength() throws SQLException { + return 0; + } + + @Override + public int getMaxStatements() throws SQLException { + return 0; + } + + @Override + public int getMaxTableNameLength() throws SQLException { + return 0; + } + + @Override + public int getMaxTablesInSelect() throws SQLException { + return 0; + } + + @Override + public int getMaxUserNameLength() throws SQLException { + return 0; + } + + @Override + public int getDefaultTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public boolean supportsTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsTransactionIsolationLevel(int level) throws SQLException { + return false; + } + + @Override + public boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException { + return false; + } + + @Override + public boolean supportsDataManipulationTransactionsOnly() throws SQLException { + return false; + } + + @Override + public boolean dataDefinitionCausesTransactionCommit() throws SQLException { + return false; + } + + @Override + public boolean dataDefinitionIgnoredInTransactions() throws SQLException { + return false; + } + + @Override + public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, + String columnNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) + throws SQLException { + return null; + } + + @Override + public ResultSet getSchemas() throws SQLException { + return null; + } + + @Override + public ResultSet getCatalogs() throws SQLException { + return null; + } + + @Override + public ResultSet getTableTypes() throws SQLException { + return null; + } + + @Override + public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, + String columnNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) + throws SQLException { + return null; + } + + @Override + public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) + throws SQLException { + return null; + } + + @Override + public ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException { + return null; + } + + @Override + public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { + return null; + } + + @Override + public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { + return null; + } + + @Override + public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException { + return null; + } + + @Override + public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, + String foreignCatalog, String foreignSchema, String foreignTable) + throws SQLException { + return null; + } + + @Override + public ResultSet getTypeInfo() throws SQLException { + return null; + } + + @Override + public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) + throws SQLException { + return null; + } + + @Override + public boolean supportsResultSetType(int type) throws SQLException { + return false; + } + + @Override + public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException { + return false; + } + + @Override + public boolean ownUpdatesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean ownDeletesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean ownInsertsAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean othersUpdatesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean othersDeletesAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean othersInsertsAreVisible(int type) throws SQLException { + return false; + } + + @Override + public boolean updatesAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean deletesAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean insertsAreDetected(int type) throws SQLException { + return false; + } + + @Override + public boolean supportsBatchUpdates() throws SQLException { + return false; + } + + @Override + public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) + throws SQLException { + return null; + } + + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public boolean supportsSavepoints() throws SQLException { + return false; + } + + @Override + public boolean supportsNamedParameters() throws SQLException { + return false; + } + + @Override + public boolean supportsMultipleOpenResults() throws SQLException { + return false; + } + + @Override + public boolean supportsGetGeneratedKeys() throws SQLException { + return false; + } + + @Override + public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, + String attributeNamePattern) throws SQLException { + return null; + } + + @Override + public boolean supportsResultSetHoldability(int holdability) throws SQLException { + return false; + } + + @Override + public int getResultSetHoldability() throws SQLException { + return 0; + } + + @Override + public int getDatabaseMajorVersion() throws SQLException { + return 0; + } + + @Override + public int getDatabaseMinorVersion() throws SQLException { + return 0; + } + + @Override + public int getJDBCMajorVersion() throws SQLException { + return 0; + } + + @Override + public int getJDBCMinorVersion() throws SQLException { + return 0; + } + + @Override + public int getSQLStateType() throws SQLException { + return 0; + } + + @Override + public boolean locatorsUpdateCopy() throws SQLException { + return false; + } + + @Override + public boolean supportsStatementPooling() throws SQLException { + return false; + } + + @Override + public RowIdLifetime getRowIdLifetime() throws SQLException { + return null; + } + + @Override + public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException { + return null; + } + + @Override + public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException { + return false; + } + + @Override + public boolean autoCommitFailureClosesAllResultSets() throws SQLException { + return false; + } + + @Override + public ResultSet getClientInfoProperties() throws SQLException { + return null; + } + + @Override + public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, + String columnNamePattern) throws SQLException { + return null; + } + + @Override + public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, + String columnNamePattern) throws SQLException { + return null; + } + + @Override + public boolean generatedKeyAlwaysReturned() throws SQLException { + return false; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } +} 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/mock/MockResultSet.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/mock/MockResultSet.java new file mode 100644 index 0000000000..cfed4f7149 --- /dev/null +++ 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/mock/MockResultSet.java @@ -0,0 +1,1099 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +public class MockResultSet implements ResultSet { + private final List stringList = new ArrayList<>(); + private final List integerList = new ArrayList<>(); + private final List doubleList = new ArrayList<>(); + private final List booleanList = new ArrayList<>(); + private final List timestamps = new ArrayList<>(); + + private final AtomicInteger stringCounter = new AtomicInteger(0); + private final AtomicInteger integerCounter = new AtomicInteger(0); + private final AtomicInteger doubleCounter = new AtomicInteger(0); + private final AtomicInteger booleanCounter = new AtomicInteger(0); + private final AtomicInteger timestampCounter = new AtomicInteger(0); + + private boolean iterated = false; + private boolean hasData = false; + + @Override + public boolean next() throws SQLException { + if (!this.iterated && this.hasData) { + this.iterated = true; + return true; + } else { + return false; + } + } + + @Override + public void close() throws SQLException { + + } + + @Override + public boolean wasNull() throws SQLException { + return false; + } + + @Override + public String getString(int columnIndex) throws SQLException { + Object item = getItem(this.stringList, this.stringCounter); + if (item != null) { + return (String) item; + } else { + return ""; + } + } + + private Object getItem(List list, AtomicInteger counter) { + if (!list.isEmpty()) { + return list.get(counter.getAndIncrement()); + } + return null; + } + + @Override + public boolean getBoolean(int columnIndex) throws SQLException { + Object item = getItem(this.booleanList, this.booleanCounter); + if (item != null) { + return (Boolean) item; + } else { + return false; + } + } + + @Override + public byte getByte(int columnIndex) throws SQLException { + return 0; + } + + @Override + public short getShort(int columnIndex) throws SQLException { + return 0; + } + + @Override + public int getInt(int columnIndex) throws SQLException { + Object item = getItem(this.integerList, this.integerCounter); + if (item != null) { + return (Integer) item; + } else { + return 0; + } + } + + @Override + public long getLong(int columnIndex) throws SQLException { + return 0; + } + + @Override + public float getFloat(int columnIndex) throws SQLException { + return 0; + } + + @Override + public double getDouble(int columnIndex) throws SQLException { + Object item = getItem(this.doubleList, this.doubleCounter); + if (item != null) { + return (Double) item; + } else { + return 0.0; + } + } + + @Override + public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { + return null; + } + + @Override + public byte[] getBytes(int columnIndex) throws SQLException { + return new byte[0]; + } + + @Override + public Date getDate(int columnIndex) throws SQLException { + return null; + } + + @Override + public Time getTime(int columnIndex) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(int columnIndex) throws SQLException { + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } + } + + @Override + public InputStream getAsciiStream(int columnIndex) throws SQLException { + return null; + } + + @Override + public InputStream getUnicodeStream(int columnIndex) throws SQLException { + return null; + } + + @Override + public InputStream getBinaryStream(int columnIndex) throws SQLException { + return null; + } + + @Override + public String getString(String columnLabel) throws SQLException { + Object item = getItem(this.stringList, this.stringCounter); + if (item != null) { + return (String) item; + } else { + return ""; + } + } + + @Override + public boolean getBoolean(String columnLabel) throws SQLException { + Object item = getItem(this.booleanList, this.booleanCounter); + if (item != null) { + return (Boolean) item; + } else { + return false; + } + } + + @Override + public byte getByte(String columnLabel) throws SQLException { + return 0; + } + + @Override + public short getShort(String columnLabel) throws SQLException { + return 0; + } + + @Override + public int getInt(String columnLabel) throws SQLException { + Object item = getItem(this.integerList, this.integerCounter); + if (item != null) { + return (Integer) item; + } else { + return 0; + } + } + + @Override + public long getLong(String columnLabel) throws SQLException { + return 0; + } + + @Override + public float getFloat(String columnLabel) throws SQLException { + return 0; + } + + @Override + public double getDouble(String columnLabel) throws SQLException { + Object item = getItem(this.doubleList, this.doubleCounter); + if (item != null) { + return (Double) item; + } else { + return 0.0; + } + } + + @Override + public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { + return null; + } + + @Override + public byte[] getBytes(String columnLabel) throws SQLException { + return new byte[0]; + } + + @Override + public Date getDate(String columnLabel) throws SQLException { + return null; + } + + @Override + public Time getTime(String columnLabel) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(String columnLabel) throws SQLException { + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } + } + + @Override + public InputStream getAsciiStream(String columnLabel) throws SQLException { + return null; + } + + @Override + public InputStream getUnicodeStream(String columnLabel) throws SQLException { + return null; + } + + @Override + public InputStream getBinaryStream(String columnLabel) throws SQLException { + return null; + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public void clearWarnings() throws SQLException { + + } + + @Override + public String getCursorName() throws SQLException { + return null; + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public Object getObject(int columnIndex) throws SQLException { + return null; + } + + @Override + public Object getObject(String columnLabel) throws SQLException { + return null; + } + + @Override + public int findColumn(String columnLabel) throws SQLException { + return 0; + } + + @Override + public Reader getCharacterStream(int columnIndex) throws SQLException { + return null; + } + + @Override + public Reader getCharacterStream(String columnLabel) throws SQLException { + return null; + } + + @Override + public BigDecimal getBigDecimal(int columnIndex) throws SQLException { + return null; + } + + @Override + public BigDecimal getBigDecimal(String columnLabel) throws SQLException { + return null; + } + + @Override + public boolean isBeforeFirst() throws SQLException { + return false; + } + + @Override + public boolean isAfterLast() throws SQLException { + return false; + } + + @Override + public boolean isFirst() throws SQLException { + return false; + } + + @Override + public boolean isLast() throws SQLException { + return false; + } + + @Override + public void beforeFirst() throws SQLException { + + } + + @Override + public void afterLast() throws SQLException { + + } + + @Override + public boolean first() throws SQLException { + return false; + } + + @Override + public boolean last() throws SQLException { + return false; + } + + @Override + public int getRow() throws SQLException { + return 0; + } + + @Override + public boolean absolute(int row) throws SQLException { + return false; + } + + @Override + public boolean relative(int rows) throws SQLException { + return false; + } + + @Override + public boolean previous() throws SQLException { + return false; + } + + @Override + public int getFetchDirection() throws SQLException { + return 0; + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + + } + + @Override + public int getFetchSize() throws SQLException { + return 0; + } + + @Override + public void setFetchSize(int rows) throws SQLException { + + } + + @Override + public int getType() throws SQLException { + return 0; + } + + @Override + public int getConcurrency() throws SQLException { + return 0; + } + + @Override + public boolean rowUpdated() throws SQLException { + return false; + } + + @Override + public boolean rowInserted() throws SQLException { + return false; + } + + @Override + public boolean rowDeleted() throws SQLException { + return false; + } + + @Override + public void updateNull(int columnIndex) throws SQLException { + + } + + @Override + public void updateBoolean(int columnIndex, boolean x) throws SQLException { + + } + + @Override + public void updateByte(int columnIndex, byte x) throws SQLException { + + } + + @Override + public void updateShort(int columnIndex, short x) throws SQLException { + + } + + @Override + public void updateInt(int columnIndex, int x) throws SQLException { + + } + + @Override + public void updateLong(int columnIndex, long x) throws SQLException { + + } + + @Override + public void updateFloat(int columnIndex, float x) throws SQLException { + + } + + @Override + public void updateDouble(int columnIndex, double x) throws SQLException { + + } + + @Override + public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { + + } + + @Override + public void updateString(int columnIndex, String x) throws SQLException { + + } + + @Override + public void updateBytes(int columnIndex, byte[] x) throws SQLException { + + } + + @Override + public void updateDate(int columnIndex, Date x) throws SQLException { + + } + + @Override + public void updateTime(int columnIndex, Time x) throws SQLException { + + } + + @Override + public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { + + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { + + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException { + + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException { + + } + + @Override + public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException { + + } + + @Override + public void updateObject(int columnIndex, Object x) throws SQLException { + + } + + @Override + public void updateNull(String columnLabel) throws SQLException { + + } + + @Override + public void updateBoolean(String columnLabel, boolean x) throws SQLException { + + } + + @Override + public void updateByte(String columnLabel, byte x) throws SQLException { + + } + + @Override + public void updateShort(String columnLabel, short x) throws SQLException { + + } + + @Override + public void updateInt(String columnLabel, int x) throws SQLException { + + } + + @Override + public void updateLong(String columnLabel, long x) throws SQLException { + + } + + @Override + public void updateFloat(String columnLabel, float x) throws SQLException { + + } + + @Override + public void updateDouble(String columnLabel, double x) throws SQLException { + + } + + @Override + public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException { + + } + + @Override + public void updateString(String columnLabel, String x) throws SQLException { + + } + + @Override + public void updateBytes(String columnLabel, byte[] x) throws SQLException { + + } + + @Override + public void updateDate(String columnLabel, Date x) throws SQLException { + + } + + @Override + public void updateTime(String columnLabel, Time x) throws SQLException { + + } + + @Override + public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException { + + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { + + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { + + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { + + } + + @Override + public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException { + + } + + @Override + public void updateObject(String columnLabel, Object x) throws SQLException { + + } + + @Override + public void insertRow() throws SQLException { + + } + + @Override + public void updateRow() throws SQLException { + + } + + @Override + public void deleteRow() throws SQLException { + + } + + @Override + public void refreshRow() throws SQLException { + + } + + @Override + public void cancelRowUpdates() throws SQLException { + + } + + @Override + public void moveToInsertRow() throws SQLException { + + } + + @Override + public void moveToCurrentRow() throws SQLException { + + } + + @Override + public Statement getStatement() throws SQLException { + return null; + } + + @Override + public Object getObject(int columnIndex, Map> map) throws SQLException { + return null; + } + + @Override + public Ref getRef(int columnIndex) throws SQLException { + return null; + } + + @Override + public Blob getBlob(int columnIndex) throws SQLException { + return null; + } + + @Override + public Clob getClob(int columnIndex) throws SQLException { + return null; + } + + @Override + public Array getArray(int columnIndex) throws SQLException { + return null; + } + + @Override + public Object getObject(String columnLabel, Map> map) throws SQLException { + return null; + } + + @Override + public Ref getRef(String columnLabel) throws SQLException { + return null; + } + + @Override + public Blob getBlob(String columnLabel) throws SQLException { + return null; + } + + @Override + public Clob getClob(String columnLabel) throws SQLException { + return null; + } + + @Override + public Array getArray(String columnLabel) throws SQLException { + return null; + } + + @Override + public Date getDate(int columnIndex, Calendar cal) throws SQLException { + return null; + } + + @Override + public Date getDate(String columnLabel, Calendar cal) throws SQLException { + return null; + } + + @Override + public Time getTime(int columnIndex, Calendar cal) throws SQLException { + return null; + } + + @Override + public Time getTime(String columnLabel, Calendar cal) throws SQLException { + return null; + } + + @Override + public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } + } + + @Override + public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { + Object item = getItem(this.timestamps, this.timestampCounter); + if (item != null) { + return (Timestamp) item; + } else { + return new Timestamp(System.currentTimeMillis()); + } + } + + @Override + public URL getURL(int columnIndex) throws SQLException { + return null; + } + + @Override + public URL getURL(String columnLabel) throws SQLException { + return null; + } + + @Override + public void updateRef(int columnIndex, Ref x) throws SQLException { + + } + + @Override + public void updateRef(String columnLabel, Ref x) throws SQLException { + + } + + @Override + public void updateBlob(int columnIndex, Blob x) throws SQLException { + + } + + @Override + public void updateBlob(String columnLabel, Blob x) throws SQLException { + + } + + @Override + public void updateClob(int columnIndex, Clob x) throws SQLException { + + } + + @Override + public void updateClob(String columnLabel, Clob x) throws SQLException { + + } + + @Override + public void updateArray(int columnIndex, Array x) throws SQLException { + + } + + @Override + public void updateArray(String columnLabel, Array x) throws SQLException { + + } + + @Override + public RowId getRowId(int columnIndex) throws SQLException { + return null; + } + + @Override + public RowId getRowId(String columnLabel) throws SQLException { + return null; + } + + @Override + public void updateRowId(int columnIndex, RowId x) throws SQLException { + + } + + @Override + public void updateRowId(String columnLabel, RowId x) throws SQLException { + + } + + @Override + public int getHoldability() throws SQLException { + return 0; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public void updateNString(int columnIndex, String nString) throws SQLException { + + } + + @Override + public void updateNString(String columnLabel, String nString) throws SQLException { + + } + + @Override + public void updateNClob(int columnIndex, NClob nClob) throws SQLException { + + } + + @Override + public void updateNClob(String columnLabel, NClob nClob) throws SQLException { + + } + + @Override + public NClob getNClob(int columnIndex) throws SQLException { + return null; + } + + @Override + public NClob getNClob(String columnLabel) throws SQLException { + return null; + } + + @Override + public SQLXML getSQLXML(int columnIndex) throws SQLException { + return null; + } + + @Override + public SQLXML getSQLXML(String columnLabel) throws SQLException { + return null; + } + + @Override + public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { + + } + + @Override + public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { + + } + + @Override + public String getNString(int columnIndex) throws SQLException { + return null; + } + + @Override + public String getNString(String columnLabel) throws SQLException { + return null; + } + + @Override + public Reader getNCharacterStream(int columnIndex) throws SQLException { + return null; + } + + @Override + public Reader getNCharacterStream(String columnLabel) throws SQLException { + return null; + } + + @Override + public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + + } + + @Override + public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { + + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { + + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { + + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { + + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { + + } + + @Override + public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { + + } + + @Override + public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { + + } + + @Override + public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { + + } + + @Override + public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { + + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { + + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { + + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { + + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { + + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { + + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { + + } + + @Override + public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { + + } + + @Override + public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { + + } + + @Override + public void updateClob(int columnIndex, Reader reader) throws SQLException { + + } + + @Override + public void updateClob(String columnLabel, Reader reader) throws SQLException { + + } + + @Override + public void updateNClob(int columnIndex, Reader reader) throws SQLException { + + } + + @Override + public void updateNClob(String columnLabel, Reader reader) throws SQLException { + + } + + @Override + public T getObject(int columnIndex, Class type) throws SQLException { + return null; + } + + @Override + public T getObject(String columnLabel, Class type) throws SQLException { + return null; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + public void addString(String string) { + this.stringList.add(string); + this.hasData = true; + } + + public void addInteger(Integer integer) { + this.integerList.add(integer); + this.hasData = true; + } + + public void addBoolean(Boolean bool) { + this.booleanList.add(bool); + this.hasData = true; + } + + public void addDouble(Double doubleVal) { + this.doubleList.add(doubleVal); + this.hasData = true; + } + + public void addTimestamp(Timestamp timestamp) { + this.timestamps.add(timestamp); + this.hasData = 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/mock/MockStatement.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/mock/MockStatement.java new file mode 100644 index 0000000000..8988a0c800 --- /dev/null +++ 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/mock/MockStatement.java @@ -0,0 +1,545 @@ +/* + * Copyright (c) 2018 - 2023, 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.device.mgt.extensions.device.organization.mock; + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +/** + * This is the mock statement for the test cases. + */ +public class MockStatement implements PreparedStatement { + private final List resultSets = new ArrayList<>(); + private int resultSetCounter; + + @Override + public ResultSet executeQuery(String sql) throws SQLException { + return this.getMockResultSet(); + } + + private ResultSet getMockResultSet() { + if (!this.resultSets.isEmpty()) { + ResultSet resultSet = this.resultSets.get(this.resultSetCounter); + this.resultSetCounter++; + return resultSet; + } else { + return new MockResultSet(); + } + } + + @Override + public int executeUpdate(String sql) throws SQLException { + return 0; + } + + @Override + public void close() throws SQLException { + + } + + @Override + public int getMaxFieldSize() throws SQLException { + return 0; + } + + @Override + public void setMaxFieldSize(int max) throws SQLException { + + } + + @Override + public int getMaxRows() throws SQLException { + return 0; + } + + @Override + public void setMaxRows(int max) throws SQLException { + + } + + @Override + public void setEscapeProcessing(boolean enable) throws SQLException { + + } + + @Override + public int getQueryTimeout() throws SQLException { + return 0; + } + + @Override + public void setQueryTimeout(int seconds) throws SQLException { + + } + + @Override + public void cancel() throws SQLException { + + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public void clearWarnings() throws SQLException { + + } + + @Override + public void setCursorName(String name) throws SQLException { + + } + + @Override + public boolean execute(String sql) throws SQLException { + return false; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return getMockResultSet(); + } + + @Override + public int getUpdateCount() throws SQLException { + return 0; + } + + @Override + public boolean getMoreResults() throws SQLException { + return false; + } + + @Override + public int getFetchDirection() throws SQLException { + return 0; + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + + } + + @Override + public int getFetchSize() throws SQLException { + return 0; + } + + @Override + public void setFetchSize(int rows) throws SQLException { + + } + + @Override + public int getResultSetConcurrency() throws SQLException { + return 0; + } + + @Override + public int getResultSetType() throws SQLException { + return 0; + } + + @Override + public void addBatch(String sql) throws SQLException { + + } + + @Override + public void clearBatch() throws SQLException { + + } + + @Override + public int[] executeBatch() throws SQLException { + return new int[0]; + } + + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public boolean getMoreResults(int current) throws SQLException { + return false; + } + + @Override + public ResultSet getGeneratedKeys() throws SQLException { + return getMockResultSet(); + } + + @Override + public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, String[] columnNames) throws SQLException { + return 0; + } + + @Override + public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + return false; + } + + @Override + public boolean execute(String sql, int[] columnIndexes) throws SQLException { + return false; + } + + @Override + public boolean execute(String sql, String[] columnNames) throws SQLException { + return false; + } + + @Override + public int getResultSetHoldability() throws SQLException { + return 0; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public boolean isPoolable() throws SQLException { + return false; + } + + @Override + public void setPoolable(boolean poolable) throws SQLException { + + } + + @Override + public void closeOnCompletion() throws SQLException { + + } + + @Override + public boolean isCloseOnCompletion() throws SQLException { + return false; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public ResultSet executeQuery() throws SQLException { + return getMockResultSet(); + } + + @Override + public int executeUpdate() throws SQLException { + return 0; + } + + @Override + public void setNull(int parameterIndex, int sqlType) throws SQLException { + + } + + @Override + public void setBoolean(int parameterIndex, boolean x) throws SQLException { + + } + + @Override + public void setByte(int parameterIndex, byte x) throws SQLException { + + } + + @Override + public void setShort(int parameterIndex, short x) throws SQLException { + + } + + @Override + public void setInt(int parameterIndex, int x) throws SQLException { + + } + + @Override + public void setLong(int parameterIndex, long x) throws SQLException { + + } + + @Override + public void setFloat(int parameterIndex, float x) throws SQLException { + + } + + @Override + public void setDouble(int parameterIndex, double x) throws SQLException { + + } + + @Override + public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { + + } + + @Override + public void setString(int parameterIndex, String x) throws SQLException { + + } + + @Override + public void setBytes(int parameterIndex, byte[] x) throws SQLException { + + } + + @Override + public void setDate(int parameterIndex, Date x) throws SQLException { + + } + + @Override + public void setTime(int parameterIndex, Time x) throws SQLException { + + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { + + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { + + } + + @Override + public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { + + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { + + } + + @Override + public void clearParameters() throws SQLException { + + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { + + } + + @Override + public void setObject(int parameterIndex, Object x) throws SQLException { + + } + + @Override + public boolean execute() throws SQLException { + return false; + } + + @Override + public void addBatch() throws SQLException { + + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { + + } + + @Override + public void setRef(int parameterIndex, Ref x) throws SQLException { + + } + + @Override + public void setBlob(int parameterIndex, Blob x) throws SQLException { + + } + + @Override + public void setClob(int parameterIndex, Clob x) throws SQLException { + + } + + @Override + public void setArray(int parameterIndex, Array x) throws SQLException { + + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { + + } + + @Override + public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { + + } + + @Override + public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { + + } + + @Override + public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { + + } + + @Override + public void setURL(int parameterIndex, URL x) throws SQLException { + + } + + @Override + public ParameterMetaData getParameterMetaData() throws SQLException { + return null; + } + + @Override + public void setRowId(int parameterIndex, RowId x) throws SQLException { + + } + + @Override + public void setNString(int parameterIndex, String value) throws SQLException { + + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException { + + } + + @Override + public void setNClob(int parameterIndex, NClob value) throws SQLException { + + } + + @Override + public void setClob(int parameterIndex, Reader reader, long length) throws SQLException { + + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException { + + } + + @Override + public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException { + + } + + @Override + public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException { + + } + + @Override + public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException { + + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException { + + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException { + + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException { + + } + + @Override + public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { + + } + + @Override + public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { + + } + + @Override + public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException { + + } + + @Override + public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException { + + } + + @Override + public void setClob(int parameterIndex, Reader reader) throws SQLException { + + } + + @Override + public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException { + + } + + @Override + public void setNClob(int parameterIndex, Reader reader) throws SQLException { + + } + + public void addResultSet(MockResultSet resultSet) { + this.resultSets.add(resultSet); + } +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 14782e4fd0..f73e3c84ed 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -802,19 +802,19 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TYPE_DEFINITION TEXT NOT NULL, PRIMARY KEY (SUB_TYPE_ID,DEVICE_TYPE) ); - -- END OF DM_DEVICE_SUB_TYPE TABLE-- ---DM_DEVICE_ORGANIZATION TABLE-- +-- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INTEGER auto_increment NOT NULL, - DEVICE_ID INT(11) DEFAULT NULL, - PARENT_DEVICE_ID INT(11) DEFAULT NULL, + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INT(11) DEFAULT NULL, + PARENT_DEVICE_ID INT(11) DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + STATUS VARCHAR(50) DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION - ); +); -- END OF DM_DEVICE_ORGANIZATION TABLE-- \ No newline at end of file diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql index f1bc3b8a1b..c04edf125c 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql @@ -23,9 +23,10 @@ DEVICE_ID INT(11) DEFAULT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + STATUS VARCHAR(50) DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -33,9 +34,14 @@ -- ----------------------------------------------------- -- Sample data for test cases -- ----------------------------------------------------- -INSERT INTO DM_DEVICE_TYPE () -INSERT INTO DM_DEVICE () -INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP) VALUES -(), -(); +INSERT INTO DM_DEVICE_TYPE (ID,NAME,DEVICE_TYPE_META,LAST_UPDATED_TIMESTAMP,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS) +VALUES (1,'METER','meter',CURRENT_TIMESTAMP(),1,true); +INSERT INTO DM_DEVICE (ID,DESCRIPTION,NAME,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,LAST_UPDATED_TIMESTAMP,TENANT_ID) VALUES +(1,'test device 1','Meter 01',1,'0001',CURRENT_TIMESTAMP(),1), +(2,'test device 2','Meter 02',1,'0002',CURRENT_TIMESTAMP(),1), +(3,'test device 3','Meter 03',1,'0003',CURRENT_TIMESTAMP(),1), +(4,'test device 4','Meter 04',1,'0004',CURRENT_TIMESTAMP(),1), +-- INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP) VALUES +-- (2,1,CURRENT_TIMESTAMP()), +-- (3,2,CURRENT_TIMESTAMP()); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-mock.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-mock.xml deleted file mode 100644 index f6031ebc08..0000000000 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-mock.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true - io.entgra.device.mgt.core.device.mgt.core.mock.MockJDBCDriver - wso2carbon - wso2carbon - - - - - - - - - diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-no-table.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-no-table.xml deleted file mode 100644 index b28626efe7..0000000000 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config-no-table.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - jdbc:h2:mem:nodb-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true - org.h2.Driver - wso2carbon - wso2carbon - - - - - - - - - diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config.xml deleted file mode 100644 index 27b6dfeaeb..0000000000 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/config/datasource/data-source-config.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true - org.h2.Driver - wso2carbon - wso2carbon - - - - - - - - - diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql deleted file mode 100644 index 4cd078f2c8..0000000000 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/sql/h2.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INTEGER auto_increment NOT NULL, - DEVICE_ID INT(11) DEFAULT NULL, - PARENT_DEVICE_ID INT(11) DEFAULT NULL, - LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION - CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION - ); \ No newline at end of file diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/testng.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/testng.xml index 3726f2af17..561fc24753 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/testng.xml +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/testng.xml @@ -23,13 +23,15 @@ - + + - + +