diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/cache/GetDeviceSubTypeCacheLoader.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/cache/GetDeviceSubTypeCacheLoader.java index ebefb213a1a..d6296e4a734 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/cache/GetDeviceSubTypeCacheLoader.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/cache/GetDeviceSubTypeCacheLoader.java @@ -46,7 +46,7 @@ public class GetDeviceSubTypeCacheLoader extends CacheLoader getAllDeviceSubTypes(int tenantId, DeviceSubType.DeviceType deviceType) @@ -37,7 +37,8 @@ public interface DeviceSubTypeDAO { int getDeviceSubTypeCount(DeviceSubType.DeviceType deviceType) throws SubTypeMgtDAOException; - int getMaxSubTypeId(DeviceSubType.DeviceType deviceType) throws SubTypeMgtDAOException; + boolean checkDeviceSubTypeExist(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + throws SubTypeMgtDAOException; DeviceSubType getDeviceSubTypeByProvider(String subTypeName, int tenantId, DeviceSubType.DeviceType deviceType) throws SubTypeMgtDAOException; diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/impl/DeviceSubTypeDAOImpl.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/impl/DeviceSubTypeDAOImpl.java index e1348b1f734..2b18e261b7c 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/impl/DeviceSubTypeDAOImpl.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/impl/DeviceSubTypeDAOImpl.java @@ -38,15 +38,14 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO { private static final Log log = LogFactory.getLog(DeviceSubTypeDAOImpl.class); @Override - public boolean addDeviceSubType(DeviceSubType deviceSubType) - throws SubTypeMgtDAOException { + public boolean addDeviceSubType(DeviceSubType deviceSubType) throws SubTypeMgtDAOException { try { String sql = "INSERT INTO DM_DEVICE_SUB_TYPE (SUB_TYPE_ID, TENANT_ID, DEVICE_TYPE, SUB_TYPE_NAME, " + "TYPE_DEFINITION) VALUES (?, ?, ?, ?, ?)"; Connection conn = ConnectionManagerUtil.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceSubType.getSubTypeId()); + stmt.setString(1, deviceSubType.getSubTypeId()); stmt.setInt(2, deviceSubType.getTenantId()); stmt.setString(3, deviceSubType.getDeviceType().toString()); stmt.setString(4, deviceSubType.getSubTypeName()); @@ -68,7 +67,7 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO { } @Override - public boolean updateDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, + public boolean updateDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, String subTypeName, String typeDefinition) throws SubTypeMgtDAOException { try { @@ -79,7 +78,7 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, typeDefinition); stmt.setString(2, subTypeName); - stmt.setInt(3, subTypeId); + stmt.setString(3, subTypeId); stmt.setInt(4, tenantId); stmt.setString(5, deviceType.toString()); return stmt.executeUpdate() > 0; @@ -99,14 +98,14 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO { } @Override - public DeviceSubType getDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) throws SubTypeMgtDAOException { try { String sql = "SELECT * FROM DM_DEVICE_SUB_TYPE WHERE SUB_TYPE_ID = ? AND TENANT_ID = ? AND DEVICE_TYPE = ?"; Connection conn = ConnectionManagerUtil.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, subTypeId); + stmt.setString(1, subTypeId); stmt.setInt(2, tenantId); stmt.setString(3, deviceType.toString()); try (ResultSet rs = stmt.executeQuery()) { @@ -189,29 +188,30 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO { } @Override - public int getMaxSubTypeId(DeviceSubType.DeviceType deviceType) throws SubTypeMgtDAOException { + public boolean checkDeviceSubTypeExist(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + throws SubTypeMgtDAOException { try { - String sql = "SELECT COALESCE(MAX(SUB_TYPE_ID),0) as MAX_ID FROM DM_DEVICE_SUB_TYPE WHERE DEVICE_TYPE = ? "; + String sql = "SELECT * FROM DM_DEVICE_SUB_TYPE WHERE SUB_TYPE_ID = ? AND TENANT_ID = ? AND DEVICE_TYPE " + + "= ? "; Connection conn = ConnectionManagerUtil.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, deviceType.toString()); + stmt.setString(1, subTypeId); + stmt.setInt(2, tenantId); + stmt.setString(3, deviceType.toString()); try (ResultSet rs = stmt.executeQuery()) { - if (rs.next()) { - return rs.getInt("MAX_ID"); - } - return 0; + return rs.next(); } } } catch (DBConnectionException e) { - String msg = "Error occurred while obtaining DB connection to retrieve max device subtype id for " + - deviceType + " subtype"; + String msg = "Error occurred while obtaining DB connection to check device subtype exist for " + deviceType + + " subtype & subtype id: " + subTypeId; log.error(msg); throw new SubTypeMgtDAOException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while processing SQL to retrieve max device subtype id for " + deviceType - + " subtype"; + String msg = "Error occurred while processing SQL to check device subtype exist for " + deviceType + " " + + "subtype & subtype id: " + subTypeId; log.error(msg); throw new SubTypeMgtDAOException(msg, e); } diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/util/DAOUtil.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/util/DAOUtil.java index 61c1305923a..a50c75f703e 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/util/DAOUtil.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dao/util/DAOUtil.java @@ -38,7 +38,7 @@ public class DAOUtil { public String parseSubTypeToJson(Object objType) { return null; } }; deviceSubType.setTenantId(rs.getInt("TENANT_ID")); - deviceSubType.setSubTypeId(rs.getInt("SUB_TYPE_ID")); + deviceSubType.setSubTypeId(rs.getString("SUB_TYPE_ID")); deviceSubType.setSubTypeName(rs.getString("SUB_TYPE_NAME")); deviceSubType.setDeviceType(DeviceSubType.DeviceType.valueOf(rs.getString("DEVICE_TYPE"))); deviceSubType.setTypeDefinition(rs.getString("TYPE_DEFINITION")); diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubType.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubType.java index b29b802f308..8918588f6c6 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubType.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubType.java @@ -24,17 +24,17 @@ import com.fasterxml.jackson.core.JsonProcessingException; public abstract class DeviceSubType { - private int subTypeId; + private String subTypeId; private int tenantId; private DeviceType deviceType; private String subTypeName; private String typeDefinition; - public int getSubTypeId() { + public String getSubTypeId() { return subTypeId; } - public void setSubTypeId(int subTypeId) { + public void setSubTypeId(String subTypeId) { this.subTypeId = subTypeId; } diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubTypeCacheKey.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubTypeCacheKey.java index c1aa7eeab2f..206a57461d3 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubTypeCacheKey.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/dto/DeviceSubTypeCacheKey.java @@ -20,7 +20,7 @@ package io.entgra.device.mgt.subtype.mgt.dto; public class DeviceSubTypeCacheKey { int tenantId; - int subTypeId; + String subTypeId; DeviceSubType.DeviceType deviceType; public int getTenantId() { @@ -31,11 +31,11 @@ public class DeviceSubTypeCacheKey { this.tenantId = tenantId; } - public int getSubTypeId() { + public String getSubTypeId() { return subTypeId; } - public void setSubTypeId(int subTypeId) { + public void setSubTypeId(String subTypeId) { this.subTypeId = subTypeId; } diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/impl/DeviceSubTypeServiceImpl.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/impl/DeviceSubTypeServiceImpl.java index d6e6eba6ead..24c5f2750a1 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/impl/DeviceSubTypeServiceImpl.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/impl/DeviceSubTypeServiceImpl.java @@ -51,27 +51,22 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } @Override - public boolean addDeviceSubType(DeviceSubType deviceSubType) - throws SubTypeMgtPluginException { + public boolean addDeviceSubType(DeviceSubType deviceSubType) throws SubTypeMgtPluginException { String msg = ""; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); deviceSubType.setTenantId(tenantId); try { ConnectionManagerUtil.beginDBTransaction(); - - //TODO This is done as a temporary... we need to implement addDeviceSubType method either with uuid - // or auto incrementing subtype id. In order to that we have to fix hardcoded subtype id in code level.. - // ex: transport code level - - int maxSubTypeId = deviceSubTypeDAO.getMaxSubTypeId(deviceSubType.getDeviceType()); - deviceSubType.setSubTypeId(maxSubTypeId + 1); boolean result = deviceSubTypeDAO.addDeviceSubType(deviceSubType); if (result) { msg = "Device subtype added successfully,for " + deviceSubType.getDeviceType() + " subtype & subtype " + - "Id: " - + deviceSubType.getSubTypeId(); + "Id: " + deviceSubType.getSubTypeId(); + if (log.isDebugEnabled()) { + log.debug(msg); + } } else { + ConnectionManagerUtil.rollbackDBTransaction(); msg = "Device subtype failed to add,for " + deviceSubType.getDeviceType() + " subtype & subtype Id: " + deviceSubType.getSubTypeId(); throw new SubTypeMgtPluginException(msg); @@ -80,9 +75,6 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { String key = DeviceSubTypeMgtUtil.setDeviceSubTypeCacheKey(tenantId, deviceSubType.getSubTypeId(), deviceSubType.getDeviceType()); deviceSubTypeCache.put(key, deviceSubType); - if (log.isDebugEnabled()) { - log.debug(msg); - } return true; } catch (DBConnectionException e) { msg = "Error occurred while obtaining the database connection to add device subtype for " + @@ -90,6 +82,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { log.error(msg); throw new SubTypeMgtPluginException(msg, e); } catch (SubTypeMgtDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); msg = "Error occurred in the database level while adding device subtype for " + deviceSubType.getDeviceType() + " subtype & subtype Id: " + deviceSubType.getSubTypeId(); log.error(msg); @@ -100,9 +93,9 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } @Override - public boolean updateDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, - String subTypeName, - String typeDefinition) throws SubTypeMgtPluginException { + public boolean updateDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, + String subTypeName, String typeDefinition) + throws SubTypeMgtPluginException { String msg = ""; DeviceSubType deviceSubTypeOld = getDeviceSubType(subTypeId, tenantId, deviceType); @@ -117,14 +110,15 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { typeDefinition); if (result) { msg = "Device subtype updated successfully,for " + deviceType + " subtype & subtype Id: " + subTypeId; + if (log.isDebugEnabled()) { + log.debug(msg); + } } else { + ConnectionManagerUtil.rollbackDBTransaction(); msg = "Device subtype failed to update,for " + deviceType + " subtype & subtype Id: " + subTypeId; throw new SubTypeMgtPluginException(msg); } ConnectionManagerUtil.commitDBTransaction(); - if (log.isDebugEnabled()) { - log.debug(msg); - } return true; } catch (DBConnectionException e) { msg = "Error occurred while obtaining the database connection to update device subtype for " + deviceType @@ -132,6 +126,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { log.error(msg); throw new SubTypeMgtPluginException(msg, e); } catch (SubTypeMgtDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); msg = "Error occurred in the database level while updating device subtype for " + deviceType + " subtype & subtype Id: " + subTypeId; log.error(msg); @@ -144,7 +139,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } @Override - public DeviceSubType getDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) throws SubTypeMgtPluginException { try { String key = DeviceSubTypeMgtUtil.setDeviceSubTypeCacheKey(tenantId, subTypeId, deviceType); @@ -188,7 +183,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { int result = deviceSubTypeDAO.getDeviceSubTypeCount(deviceType); if (result <= 0) { String msg = "There are no any subtypes for device type: " + deviceType; - log.info(msg); + log.error(msg); } return result; } catch (DBConnectionException e) { @@ -228,5 +223,24 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService { } } - + @Override + public boolean checkDeviceSubTypeExist(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + throws SubTypeMgtPluginException { + try { + ConnectionManagerUtil.openDBConnection(); + return deviceSubTypeDAO.checkDeviceSubTypeExist(subTypeId, tenantId, deviceType); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to check device subtype exist for " + + deviceType + " subtype & subtype id: " + subTypeId; + log.error(msg); + throw new SubTypeMgtPluginException(msg, e); + } catch (SubTypeMgtDAOException e) { + String msg = "Error occurred in the database level while checking device subtype exist for " + deviceType + + " subtype & subtype id: " + subTypeId; + log.error(msg); + throw new SubTypeMgtPluginException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/spi/DeviceSubTypeService.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/spi/DeviceSubTypeService.java index 73afb32f702..24aa2fd431c 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/spi/DeviceSubTypeService.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/spi/DeviceSubTypeService.java @@ -27,10 +27,10 @@ public interface DeviceSubTypeService { boolean addDeviceSubType(DeviceSubType deviceSubType) throws SubTypeMgtPluginException; - boolean updateDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, String subTypeName, + boolean updateDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType, String subTypeName, String typeDefinition) throws SubTypeMgtPluginException; - DeviceSubType getDeviceSubType(int subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + DeviceSubType getDeviceSubType(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) throws SubTypeMgtPluginException; List getAllDeviceSubTypes(int tenantId, DeviceSubType.DeviceType deviceType) @@ -40,4 +40,7 @@ public interface DeviceSubTypeService { DeviceSubType getDeviceSubTypeByProvider(String subTypeName, int tenantId, DeviceSubType.DeviceType deviceType) throws SubTypeMgtPluginException; + + boolean checkDeviceSubTypeExist(String subTypeId, int tenantId, DeviceSubType.DeviceType deviceType) + throws SubTypeMgtPluginException; } diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/util/DeviceSubTypeMgtUtil.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/util/DeviceSubTypeMgtUtil.java index 25352667244..171d621945b 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/util/DeviceSubTypeMgtUtil.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/main/java/io/entgra/device/mgt/subtype/mgt/util/DeviceSubTypeMgtUtil.java @@ -22,14 +22,14 @@ import io.entgra.device.mgt.subtype.mgt.dto.DeviceSubType; import io.entgra.device.mgt.subtype.mgt.dto.DeviceSubTypeCacheKey; public class DeviceSubTypeMgtUtil { - public static String setDeviceSubTypeCacheKey(int tenantId, int subTypeId, DeviceSubType.DeviceType deviceType) { + public static String setDeviceSubTypeCacheKey(int tenantId, String subTypeId, DeviceSubType.DeviceType deviceType) { return tenantId + "|" + subTypeId + "|" + deviceType.toString(); } public static DeviceSubTypeCacheKey getDeviceSubTypeCacheKey(String key) { String[] keys = key.split("\\|"); int tenantId = Integer.parseInt(keys[0]); - int subTypeId = Integer.parseInt(keys[1]); + String subTypeId = keys[1]; DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.valueOf(keys[2]); DeviceSubTypeCacheKey deviceSubTypesCacheKey = new DeviceSubTypeCacheKey(); diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAONegativeTest.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAONegativeTest.java index d1ff711262a..fd0b659fda6 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAONegativeTest.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAONegativeTest.java @@ -81,7 +81,7 @@ public class DAONegativeTest extends BaseDeviceSubTypePluginTest { expectedExceptionsMessageRegExp = "Error occurred while processing SQL to insert device subtype" ) public void testAddDeviceSubTypes() throws SubTypeMgtDAOException { - int subTypeId = 1; + String subTypeId = "1"; String subTypeName = "TestSubType"; DeviceSubType deviceSubType = new DeviceSubType() { @Override @@ -121,7 +121,7 @@ public class DAONegativeTest extends BaseDeviceSubTypePluginTest { expectedExceptionsMessageRegExp = "Error occurred while processing SQL to insert device subtype" ) public void testAddDeviceSubtypes() throws SubTypeMgtDAOException { - int subTypeId = 1; + String subTypeId = "1"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String subTypeName = "TestSubType"; String typeDefinition = TestUtils.createNewDeviceSubType(subTypeId); diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAOTest.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAOTest.java index 54a164a9aa2..5592326962c 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAOTest.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/DAOTest.java @@ -49,7 +49,7 @@ public class DAOTest extends BaseDeviceSubTypePluginTest { public void testGetDeviceSubType() throws DBConnectionException, SubTypeMgtDAOException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.openDBConnection(); - DeviceSubType subTypeActual = deviceSubTypeDAO.getDeviceSubType(1, tenantId, + DeviceSubType subTypeActual = deviceSubTypeDAO.getDeviceSubType("1", tenantId, DeviceSubType.DeviceType.COM); ConnectionManagerUtil.closeDBConnection(); Assert.assertNotNull(subTypeActual, "Should not be null"); @@ -68,7 +68,7 @@ public class DAOTest extends BaseDeviceSubTypePluginTest { @Test public void testAddDeviceSubType() throws DBConnectionException, SubTypeMgtDAOException { - int subTypeId = 1; + String subTypeId = "1"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String subTypeName = "TestSubType"; DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.COM; @@ -102,7 +102,7 @@ public class DAOTest extends BaseDeviceSubTypePluginTest { @Test(dependsOnMethods = "testAddDeviceSubType") public void testUpdateDeviceSubType() throws DBConnectionException, SubTypeMgtDAOException { - int subTypeId = 1; + String subTypeId = "1"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.COM; String subTypeName = "TestSubType"; @@ -118,6 +118,17 @@ public class DAOTest extends BaseDeviceSubTypePluginTest { TestUtils.verifyUpdatedDeviceSubTypeDAO(subTypeActual); } + @Test(dependsOnMethods = "testAddDeviceSubType") + public void testGetDeviceTypeByProvider() throws DBConnectionException, SubTypeMgtDAOException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.COM; + String subTypeName = "TestSubType"; + ConnectionManagerUtil.openDBConnection(); + DeviceSubType subTypeActual = deviceSubTypeDAO.getDeviceSubTypeByProvider(subTypeName, tenantId, deviceType); + ConnectionManagerUtil.closeDBConnection(); + Assert.assertNotNull(subTypeActual, "Should not be null"); + } + @Test(dependsOnMethods = "testAddDeviceSubType") public void testGetDeviceTypeCount() throws DBConnectionException, SubTypeMgtDAOException { DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.COM; diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceNegativeTest.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceNegativeTest.java index bca0bc6c614..3562525adef 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceNegativeTest.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceNegativeTest.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package io.entgra.device.mgt.subtype.mgt; import io.entgra.device.mgt.subtype.mgt.dto.DeviceSubType; @@ -62,6 +63,7 @@ public class ServiceNegativeTest extends BaseDeviceSubTypePluginTest { expectedExceptionsMessageRegExp = "Error occurred in the database level while adding device subtype for " + "SIM subtype & subtype Id: 1") public void testAddDeviceSubTypes() throws SubTypeMgtPluginException { + String subTypeId = "1"; String subTypeName = "TestSubType"; DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.SIM; @@ -76,6 +78,7 @@ public class ServiceNegativeTest extends BaseDeviceSubTypePluginTest { return null; } }; + deviceSubType.setSubTypeId(subTypeId); deviceSubType.setSubTypeName(subTypeName); deviceSubType.setDeviceType(deviceType); deviceSubTypeService.addDeviceSubType(deviceSubType); @@ -86,7 +89,7 @@ public class ServiceNegativeTest extends BaseDeviceSubTypePluginTest { expectedExceptions = {SubTypeMgtPluginException.class}, expectedExceptionsMessageRegExp = "Cannot find device subtype for SIM subtype & subtype Id: 15") public void testUpdateDeviceSubTypes() throws SubTypeMgtPluginException { - int subTypeId = 15; + String subTypeId = "15"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.SIM; String subTypeName = "TestSubType"; diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceTest.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceTest.java index 93670db4ba8..07694bcce74 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceTest.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/ServiceTest.java @@ -47,7 +47,7 @@ public class ServiceTest extends BaseDeviceSubTypePluginTest { @Test(dependsOnMethods = "testAddDeviceSubType") public void testGetDeviceType() throws SubTypeMgtPluginException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceSubType subTypeActual = deviceSubTypeService.getDeviceSubType(1, tenantId, + DeviceSubType subTypeActual = deviceSubTypeService.getDeviceSubType("1", tenantId, DeviceSubType.DeviceType.METER); TestUtils.verifyDeviceSubType(subTypeActual); } @@ -63,7 +63,7 @@ public class ServiceTest extends BaseDeviceSubTypePluginTest { @Test public void testAddDeviceSubType() throws SubTypeMgtPluginException { - int subTypeId = 1; + String subTypeId = "1"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String subTypeName = "TestSubType"; DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.METER; @@ -80,6 +80,7 @@ public class ServiceTest extends BaseDeviceSubTypePluginTest { return null; } }; + deviceSubType.setSubTypeId(subTypeId); deviceSubType.setSubTypeName(subTypeName); deviceSubType.setDeviceType(deviceType); deviceSubType.setTenantId(tenantId); @@ -93,7 +94,7 @@ public class ServiceTest extends BaseDeviceSubTypePluginTest { @Test(dependsOnMethods = "testAddDeviceSubType") public void testUpdateDeviceSubType() throws SubTypeMgtPluginException { - int subTypeId = 1; + String subTypeId = "1"; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.METER; String subTypeName = "TestSubType"; @@ -107,6 +108,16 @@ public class ServiceTest extends BaseDeviceSubTypePluginTest { TestUtils.verifyUpdatedDeviceSubType(subTypeActual); } + @Test(dependsOnMethods = "testAddDeviceSubType") + public void testGetDeviceTypeByProvider() throws SubTypeMgtPluginException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.METER; + String subTypeName = "TestSubType"; + DeviceSubType subTypeActual = deviceSubTypeService.getDeviceSubTypeByProvider(subTypeName, tenantId, + deviceType); + TestUtils.verifyDeviceSubType(subTypeActual); + } + @Test(dependsOnMethods = "testAddDeviceSubType") public void testGetDeviceTypeCount() throws SubTypeMgtPluginException { DeviceSubType.DeviceType deviceType = DeviceSubType.DeviceType.METER; diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/TestUtils.java b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/TestUtils.java index 29016fda8ac..d8e913831c1 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/TestUtils.java +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/java/io/entgra/device/mgt/subtype/mgt/TestUtils.java @@ -32,45 +32,45 @@ import java.sql.Statement; public class TestUtils { private static final Log log = LogFactory.getLog(TestUtils.class); - public static String createNewDeviceSubType(int subtypeId) { + public static String createNewDeviceSubType(String subtypeId) { return "{\"make\": \"TestSubType\", \"model\": \"ATx-Mega SIM800\", " + "\"subTypeId\": " + subtypeId + ", \"hasSMSSupport\": true, \"hasICMPSupport\": true, " + "\"socketServerPort\": 8071}"; } - public static String createUpdateDeviceSubType(int subtypeId) { + public static String createUpdateDeviceSubType(String subtypeId) { return "{\"make\": \"TestSubType\", \"model\": \"ATx-Mega SIM900\", " + "\"subTypeId\": " + subtypeId + ", \"hasSMSSupport\": false, \"hasICMPSupport\": true, " + "\"socketServerPort\": 8071}"; } public static void verifyDeviceSubType(DeviceSubType deviceSubType) { - String typeDefExpected = TestUtils.createNewDeviceSubType(1); - Assert.assertEquals(deviceSubType.getSubTypeId(), 1); + String typeDefExpected = TestUtils.createNewDeviceSubType("1"); + Assert.assertEquals(deviceSubType.getSubTypeId(), "1"); Assert.assertEquals(deviceSubType.getDeviceType(), DeviceSubType.DeviceType.valueOf("METER")); Assert.assertEquals(deviceSubType.getSubTypeName(), "TestSubType"); Assert.assertEquals(deviceSubType.getTypeDefinition(), typeDefExpected); } public static void verifyDeviceSubTypeDAO(DeviceSubType deviceSubType) { - String typeDefExpected = TestUtils.createNewDeviceSubType(1); - Assert.assertEquals(deviceSubType.getSubTypeId(), 1); + String typeDefExpected = TestUtils.createNewDeviceSubType("1"); + Assert.assertEquals(deviceSubType.getSubTypeId(), "1"); Assert.assertEquals(deviceSubType.getDeviceType(), DeviceSubType.DeviceType.valueOf("COM")); Assert.assertEquals(deviceSubType.getSubTypeName(), "TestSubType"); Assert.assertEquals(deviceSubType.getTypeDefinition(), typeDefExpected); } public static void verifyUpdatedDeviceSubType(DeviceSubType deviceSubType) { - String typeDefExpected = TestUtils.createUpdateDeviceSubType(1); - Assert.assertEquals(deviceSubType.getSubTypeId(), 1); + String typeDefExpected = TestUtils.createUpdateDeviceSubType("1"); + Assert.assertEquals(deviceSubType.getSubTypeId(), "1"); Assert.assertEquals(deviceSubType.getDeviceType(), DeviceSubType.DeviceType.valueOf("METER")); Assert.assertEquals(deviceSubType.getSubTypeName(), "TestSubType"); Assert.assertEquals(deviceSubType.getTypeDefinition(), typeDefExpected); } public static void verifyUpdatedDeviceSubTypeDAO(DeviceSubType deviceSubType) { - String typeDefExpected = TestUtils.createUpdateDeviceSubType(1); - Assert.assertEquals(deviceSubType.getSubTypeId(), 1); + String typeDefExpected = TestUtils.createUpdateDeviceSubType("1"); + Assert.assertEquals(deviceSubType.getSubTypeId(), "1"); Assert.assertEquals(deviceSubType.getDeviceType(), DeviceSubType.DeviceType.valueOf("COM")); Assert.assertEquals(deviceSubType.getSubTypeName(), "TestSubType"); Assert.assertEquals(deviceSubType.getTypeDefinition(), typeDefExpected); diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 2f5ae6c6778..9737fd3df00 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -779,7 +779,7 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( -- DM_DEVICE_SUB_TYPE TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL, diff --git a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/h2.sql b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/h2.sql index 0ed4a1d6388..cd3d81c5664 100644 --- a/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/h2.sql +++ b/components/subtype-mgt/io.entgra.device.mgt.subtype.mgt/src/test/resources/carbon-home/dbscripts/h2.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS `DM_DEVICE_SUB_TYPE` ( `TENANT_ID` INT DEFAULT 0, - `SUB_TYPE_ID` INT NOT NULL, + `SUB_TYPE_ID` VARCHAR(45) NOT NULL, `DEVICE_TYPE` VARCHAR(25) NOT NULL, `SUB_TYPE_NAME` VARCHAR(45) NOT NULL, `TYPE_DEFINITION` TEXT NOT NULL, @@ -12,20 +12,8 @@ ); -- ----------------------------------------------------- --- Sample data for DAO test cases +-- Sample data for test cases -- ----------------------------------------------------- -INSERT INTO DM_DEVICE_TYPE(NAME, DEVICE_TYPE_META, LAST_UPDATED_TIMESTAMP, PROVIDER_TENANT_ID, SHARED_WITH_ALL_TENANTS) VALUES -('android','NULL','2020-10-14 16:05:15',-1234,1),('power-meter','NULL','2020-10-14 16:05:15',-1234,1), -('communication-module','NULL','2020-10-14 16:05:15',-1234,1),('sim','NULL','2020-10-14 16:05:15',-1234,1); -INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, LAST_UPDATED_TIMESTAMP, TENANT_ID) VALUES -('+94713192111', '192.168.118.111', 4, '413012689159555', '2022-04-04 16:55:52', '-1234'), -('NULL', '864495036647555', 3, '864495036647555', '2022-04-04 16:56:52', '-1234'), -('NULL', '000019729555', 2, '000019729555', '2022-04-04 16:56:52', '-1234'); - -INSERT INTO DM_ENROLMENT(DEVICE_ID,OWNER,OWNERSHIP,STATUS,IS_TRANSFERRED,DATE_OF_ENROLMENT,DATE_OF_LAST_UPDATE,TENANT_ID) VALUES -(1,'admin','COPE','ACTIVE',0,'2022-04-11 15:41:30','2022-04-11 15:41:30',-1234), -(2,'admin','COPE','ACTIVE',0,'2022-04-11 15:41:30','2022-04-11 15:41:30',-1234), -(3,'admin','COPE','ACTIVE',0,'2022-04-11 15:41:30','2022-04-11 15:41:30',-1234); INSERT INTO DM_DEVICE_SUB_TYPE (SUB_TYPE_ID, TENANT_ID, DEVICE_TYPE, SUB_TYPE_NAME, TYPE_DEFINITION) VALUES (3,-1234,'Meter','TestSubType','{"make": "TestSubType", "model": "ATx-Mega SIM800", "subTypeId": 3, "hasSMSSupport": true, "hasICMPSupport": true, "socketServerPort": 8071}'), diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 1542733b828..02ec2638f7a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -791,7 +791,7 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( -- DM_DEVICE_SUB_TYPE TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 292c25c4f15..458dde60692 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -759,7 +759,7 @@ CREATE TABLE DYNAMIC_TASK_PROPERTIES ( IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_SUB_TYPE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index f18484b0485..c4fb6a94f8b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -855,7 +855,7 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( -- DM_DEVICE_SUB_TYPE TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index bd83ae4e726..030d3aa9c37 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1123,7 +1123,7 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( -- DM_DEVICE_SUB_TYPE TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index f0ad9975b74..c09e26a6795 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -777,7 +777,7 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( -- DM_DEVICE_SUB_TYPE TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID INT NOT NULL, + SUB_TYPE_ID VARCHAR(45) NOT NULL, DEVICE_TYPE VARCHAR(25) NOT NULL, SUB_TYPE_NAME VARCHAR(45) NOT NULL, TYPE_DEFINITION TEXT NOT NULL,