service and dao layer modifications for deviceOrganization core

backup
Isuri Mendis 1 year ago
parent bd4ed2c01c
commit 05d1e9dd53

@ -56,12 +56,14 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
public List<DeviceNode> getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtDAOException {
List<DeviceNode> childNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
// Set<Integer> visited = new HashSet<>();
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
boolean parentAdded = false; // Flag to track whether the parent device has been added
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice, parentAdded);
getChildrenRecursive(node, maxDepth,
// visited,
conn, childNodes, includeDevice, parentAdded);
if (!includeDevice && !parentAdded) {
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
}
@ -79,14 +81,18 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
private void getChildrenRecursive(DeviceNode node, int maxDepth,
// Set<Integer> visited,
Connection conn,
List<DeviceNode> childNodes, boolean includeDevice, boolean parentAdded)
throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
if (maxDepth <= 0
// || visited.contains(node.getDeviceId())
) {
return;
}
visited.add(node.getDeviceId());
// visited.add(node.getDeviceId());
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE D " +
@ -106,7 +112,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
parentAdded = true; // Set the flag to true after adding the parent device.
}
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice, parentAdded);
getChildrenRecursive(child, maxDepth - 1,
// visited,
conn, childNodes, includeDevice, parentAdded);
}
}
}
@ -231,7 +239,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} else {
stmt.setNull(2, Types.INTEGER);
}
if (deviceOrganization.getDeviceOrganizationMeta() != null) {
stmt.setString(3,deviceOrganization.getDeviceOrganizationMeta());
} else {
stmt.setString(3,"");
}
stmt.setTimestamp(4, timestamp);
return stmt.executeUpdate() > 0;
}
@ -336,24 +349,20 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
@Override
public boolean updateDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtDAOException {
String msg;
DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId());
if (organization == null) {
return false;
}
if (!((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0) &&
!((deviceOrganization.getParentDeviceId() > 0 || deviceOrganization.getParentDeviceId() == null)
&& !Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())) &&
(Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta()))) {
log.error("No data to update in device organization. All the provided details already exists.");
return false;
msg = "Device Organization does not exist for organization ID = " + deviceOrganization.getOrganizationId();
throw new DeviceOrganizationMgtDAOException(msg);
}
try {
String sql = "UPDATE DM_DEVICE_ORGANIZATION SET ";
if((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0){
sql += "DEVICE_ID = ? , ";
}
if((deviceOrganization.getParentDeviceId() > 0 || deviceOrganization.getParentDeviceId() == null) &&
if((deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0) &&
!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())){
sql += "PARENT_DEVICE_ID = ? ,";
}
@ -383,12 +392,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to update device organization for " +
msg = "Error occurred while obtaining DB connection to update device organization for " +
deviceOrganization.getOrganizationId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to update device organization for " +
msg = "Error occurred while processing SQL to update device organization for " +
deviceOrganization.getOrganizationId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
@ -401,8 +410,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
@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 " +
"FROM DM_DEVICE_ORGANIZATION do WHERE do.ID = ? ";
String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION do WHERE do.ID = ? ";
Connection conn = ConnectionManagerUtil.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {

@ -32,6 +32,7 @@ public class DeviceOrganizationDaoUtil {
} else {
deviceOrganization.setParentDeviceId(null);
}
deviceOrganization.setDeviceOrganizationMeta(rs.getString("DEVICE_ORGANIZATION_META"));
deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP"));
return deviceOrganization;
}

@ -31,6 +31,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
import java.util.Objects;
public class DeviceOrganizationServiceImpl implements DeviceOrganizationService {
@ -248,21 +249,15 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
* {@inheritDoc}
*/
@Override
public boolean updateDeviceOrganization(DeviceOrganization organization)
public boolean updateDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtPluginException {
String msg;
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organization.getOrganizationId());
if (deviceOrganization == null) {
msg = "Cannot find device organization for organizationID = " + organization.getOrganizationId();
DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId());
if (organization == null) {
msg = "Cannot find device organization for organizationID = " + deviceOrganization.getOrganizationId();
log.error(msg);
return false;
}
if (organization.getDeviceId() == deviceOrganization.getDeviceId() &&
organization.getParentDeviceId().equals(deviceOrganization.getParentDeviceId()) &&
organization.getDeviceOrganizationMeta().equals(deviceOrganization.getDeviceOrganizationMeta())){
log.error("No data to update in device organization. All the provided details already exists.");
return false;
throw new DeviceOrganizationMgtPluginException(msg);
}
try {

@ -94,19 +94,6 @@ public class DAOTest extends BaseDeviceOrganizationTest {
}
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testUpdateDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = new DeviceOrganization();
deviceOrganization.setDeviceId(2);
deviceOrganization.setParentDeviceId(1);
deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationDAO.updateDeviceOrganization(deviceOrganization);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
}
@Test(dependsOnMethods = "testAddDeviceOrganizationDAO")
public void testGetDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();

@ -13,7 +13,6 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Random;
public class ServiceTest extends BaseDeviceOrganizationTest {
@ -33,7 +32,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
if (exists) {
DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(17);
int maxDepth = 2;
int maxDepth = 10;
boolean includeDevice = true;
List<DeviceNode> childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceNode, maxDepth, includeDevice);
@ -55,6 +54,21 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
}
}
@Test()
public void testAddDeviceOrganizationWithoutMetaData() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(4);
deviceOrganizationOne.setParentDeviceId(null);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
Assert.assertTrue(result1);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, null);
Assert.assertNotNull(organization1);
}
@Test()
public void testAddDeviceOrganizationWithNullParent() throws DeviceOrganizationMgtPluginException {
@ -64,11 +78,13 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
Assert.assertTrue(result1);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, null);
Assert.assertNotNull(organization1);
}
@Test()
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
@ -78,45 +94,40 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, 4);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, 4);
Assert.assertNotNull(organization1);
Assert.assertTrue(result1 || organization != null);
}
@Test()
public void testAddMultipleRandomDeviceOrganizations() throws DeviceOrganizationMgtPluginException {
DeviceOrganizationService deviceOrganizationService = new DeviceOrganizationServiceImpl();
public void testAddDeviceOrganizationForDelete() throws DeviceOrganizationMgtPluginException {
int[] deviceIds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(2);
deviceOrganizationOne.setParentDeviceId(null);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
// Initialize counters for tracking the number of organizations and iterations
int organizationCount = 0;
int iterations = 0;
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(2, null);
while (organizationCount < 100 && iterations < 1000) {
// Randomly select two different device IDs from the array
int parentDeviceId = deviceIds[new Random().nextInt(deviceIds.length)];
int childDeviceId = deviceIds[new Random().nextInt(deviceIds.length)];
Assert.assertTrue(result1 || organization != null);
// Check if the selected device IDs are different
if (parentDeviceId != childDeviceId) {
DeviceOrganization organization = new DeviceOrganization();
organization.setDeviceId(childDeviceId);
organization.setParentDeviceId(parentDeviceId);
}
boolean result = deviceOrganizationService.addDeviceOrganization(organization);
@Test()
public void testAddDeviceOrganizationForDeleteAssociations() throws DeviceOrganizationMgtPluginException {
// Optionally, add assertions to check the results if needed
if (result) {
organizationCount++;
}
}
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(1);
deviceOrganizationOne.setParentDeviceId(null);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
iterations++;
}
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(1, null);
Assert.assertTrue(result1 || organization != null);
Assert.assertEquals(organizationCount, 100, "Inserted 100 organizations");
}
@Test()
@ -127,7 +138,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
// Define specific combinations of deviceID and parentDeviceID
int[][] combinations = {
{20, 19}, {19, 18}, {18, 17},{20,5},{20,17}
{20, 19}, {19, 18}, {18, 17}, {20, 5}, {20, 17}, {19, 16}, {17, 16}, {16, 17}
// Add more combinations as needed
};
@ -158,37 +169,64 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertEquals(organizationCount, combinations.length, "Inserted organizations count mismatch");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
@Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent")
public void testUpdateDeviceOrganizationWithSameData() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization();
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setDeviceId(3);
deviceOrganization.setParentDeviceId(null);
deviceOrganization.setOrganizationId(1);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByID(1);
if (organization != null) {
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
// DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
// Assert.assertNotNull(organization);
Assert.assertTrue(result);
}
}
@Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent")
public void testUpdateDeviceOrganizationWithDifferentData() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization();
deviceOrganization.setDeviceId(3);
deviceOrganization.setParentDeviceId(4);
deviceOrganization.setOrganizationId(1);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByID(1);
if (organization != null) {
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
Assert.assertTrue(result);
}
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(1);
int organizationID = 1;
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(organizationID);
if (deviceOrganization != null) {
log.info("In Device Organization with organizationID = " + organizationID
+ ", deviceID = " + deviceOrganization.getDeviceId()
+ ", ParentDeviceID = " + deviceOrganization.getParentDeviceId()
+ ", Meta Data = " + deviceOrganization.getDeviceOrganizationMeta()
);
}
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
boolean deviceIdExist = deviceOrganizationService.isDeviceIdExist(4);
Assert.assertTrue(deviceIdExist);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganizationForDelete")
public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceOrganizationByID(1);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganizationForDeleteAssociations")
public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceAssociations(4);
boolean rs = deviceOrganizationService.deleteDeviceAssociations(1);
Assert.assertTrue(rs);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@ -201,19 +239,19 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
log.info("updateTime = " + organization.getUpdateTime());
log.info("----------------------------------------------");
}
// Assert.assertNotNull(organizations, "List of organizations cannot be null");
// Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty");
Assert.assertNotNull(organizations, "List of organizations cannot be null");
Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddMultipleDeviceOrganizations")
public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException {
int deviceID = 20;
int parentDeviceID = 19;
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID);
// Assert.assertNotNull(organization, "Organization should not be null");
// Assert.assertEquals(organization.getDeviceId(), deviceID, "Device ID should match");
// Assert.assertEquals(organization.getParentDeviceId().intValue(), parentDeviceID, "Parent Device ID should match");
Assert.assertNotNull(organization, "Organization should not be null");
Assert.assertEquals(organization.getDeviceId(), deviceID, "Device ID should match");
Assert.assertEquals(organization.getParentDeviceId().intValue(), parentDeviceID, "Parent Device ID should match");
}
}

Loading…
Cancel
Save