sql script, update method and test modifications

pull/238/head
Isuri Mendis 1 year ago
parent de16f6ae5b
commit bd4ed2c01c

@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
@ -122,7 +123,13 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
Set<Integer> visited = new HashSet<>();
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice);
boolean childAdded = false;
getParentsRecursive(node, maxDepth,
// visited,
conn, parentNodes, includeDevice, childAdded);
if (!includeDevice && !childAdded) {
parentNodes.add(node);
}
return parentNodes;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " +
@ -137,13 +144,17 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
private void getParentsRecursive(DeviceNode node, int maxDepth,
// Set<Integer> visited,
Connection conn,
List<DeviceNode> parentNodes, boolean includeDevice, boolean childAdded) throws SQLException {
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 " +
@ -156,10 +167,14 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceNode parent = getDeviceFromResultSet(rs);
if (includeDevice) {
parentNodes.add(parent); // Add the parent device if includeDevice is true.
node.getParents().add(parent);
if (includeDevice && !childAdded) {
parentNodes.add(node);
childAdded = true;
}
getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice);
getParentsRecursive(parent, maxDepth - 1,
// visited,
conn, parentNodes, includeDevice, childAdded);
}
}
}
@ -202,8 +217,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
throws DeviceOrganizationMgtDAOException {
try {
String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP)" +
" VALUES (?, ?, ?)";
String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, " +
"DEVICE_ORGANIZATION_META,LAST_UPDATED_TIMESTAMP)" +
" VALUES (?, ?, ?, ?)";
Connection conn = ConnectionManagerUtil.getDBConnection();
Calendar calendar = Calendar.getInstance();
@ -213,9 +229,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
if (deviceOrganization.getParentDeviceId() != null) {
stmt.setInt(2, deviceOrganization.getParentDeviceId());
} else {
stmt.setInt(2, Types.NULL);
stmt.setNull(2, Types.INTEGER);
}
stmt.setTimestamp(3, timestamp);
stmt.setString(3,deviceOrganization.getDeviceOrganizationMeta());
stmt.setTimestamp(4, timestamp);
return stmt.executeUpdate() > 0;
}
} catch (DBConnectionException e) {
@ -275,15 +292,21 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, Integer parentDeviceId)
throws DeviceOrganizationMgtDAOException {
try {
String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?";
String sql;
Connection conn = ConnectionManagerUtil.getDBConnection();
if (parentDeviceId != null) {
// If parentDeviceId is not null, use it in the query.
sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?";
} else {
// If parentDeviceId is null, use a query that checks for null values.
sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID IS NULL";
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId);
if (parentDeviceId != null) {
stmt.setInt(2, parentDeviceId);
} else {
stmt.setInt(2, Types.NULL);
}
try (ResultSet rs = stmt.executeQuery()) {
@ -315,26 +338,47 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
throws DeviceOrganizationMgtDAOException {
DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId());
if (organization == null || deviceOrganization.getDeviceId() <= 0 ||
!(deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0)) {
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;
}
try {
String sql = "UPDATE DM_DEVICE_ORGANIZATION SET DEVICE_ID = ? , PARENT_DEVICE_ID = ? , " +
"LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? ";
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) &&
!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())){
sql += "PARENT_DEVICE_ID = ? ,";
}
if(!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())){
sql += "DEVICE_ORGANIZATION_META = ? ,";
}
sql += "LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? ";
Connection conn = ConnectionManagerUtil.getDBConnection();
Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceOrganization.getDeviceId());
if (deviceOrganization.getParentDeviceId() != null) {
stmt.setInt(2, deviceOrganization.getParentDeviceId());
} else {
stmt.setNull(2, Types.INTEGER);
int x = 0;
if((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0){
stmt.setInt(++x, deviceOrganization.getDeviceId());
}
if (!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())) {
stmt.setInt(++x, deviceOrganization.getParentDeviceId());
}
if (!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())){
stmt.setString(++x, deviceOrganization.getDeviceOrganizationMeta());
}
stmt.setTimestamp(3, timestamp);
stmt.setInt(4, deviceOrganization.getOrganizationId());
stmt.setTimestamp(++x, timestamp);
stmt.setInt(++x, deviceOrganization.getOrganizationId());
return stmt.executeUpdate() > 0;
}

@ -30,6 +30,7 @@ public class DeviceOrganization {
private int organizationId;
private int deviceId;
private Integer parentDeviceId;
private String deviceOrganizationMeta;
private Date updateTime;
public int getOrganizationId() {
@ -56,6 +57,14 @@ public class DeviceOrganization {
this.parentDeviceId = parentDeviceId;
}
public String getDeviceOrganizationMeta() {
return deviceOrganizationMeta;
}
public void setDeviceOrganizationMeta(String deviceOrganizationMeta) {
this.deviceOrganizationMeta = deviceOrganizationMeta;
}
public Date getUpdateTime() {
return updateTime;
}

@ -151,6 +151,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
Integer parentDeviceID = deviceOrganization.getParentDeviceId();
boolean exists = isDeviceOrganizationExist(deviceID,parentDeviceID);
if (exists){
log.error("Device Organization already exists");
return false;
}
try {
@ -249,16 +250,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override
public boolean updateDeviceOrganization(DeviceOrganization organization)
throws DeviceOrganizationMgtPluginException {
if (organization == null || organization.getOrganizationId() <= 0 || organization.getDeviceId() <= 0
|| !(organization.getParentDeviceId() == null || organization.getParentDeviceId() > 0)) {
throw new BadRequestException("Invalid input parameters for deviceOrganization update. : "
+ "deviceOrganization = " + organization
+ ", deviceID = " + (organization != null ? organization.getDeviceId() :
"deviceID should be a positive number")
+ ", parentDeviceID = parentDeviceID should be a positive number or null"
+ ", organizationID = " + (organization != null ? organization.getOrganizationId() : null)
);
}
String msg;
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organization.getOrganizationId());
if (deviceOrganization == null) {
@ -266,6 +258,12 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
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;
}
try {
ConnectionManagerUtil.beginDBTransaction();

@ -79,6 +79,32 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
deviceOrganizationService.addDeviceOrganization(invalidOrganization);
}
@Test(description = "This method tests Add Device Organization method under negative circumstances with invalid" +
"parent ID",
expectedExceptions = {DeviceOrganizationMgtPluginException.class})
public void testAddDeviceOrganizationWithInvalidParentID() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(3);
deviceOrganizationOne.setParentDeviceId(30);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
}
@Test(description = "This method tests Add Device Organization method under negative circumstances with invalid" +
"child ID",
expectedExceptions = {DeviceOrganizationMgtPluginException.class})
public void testAddDeviceOrganizationWithInvalidChildID() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(30);
deviceOrganizationOne.setParentDeviceId(3);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
}
@Test(description = "This method tests isDeviceOrganizationExist method under negative circumstances with an organization that doesn't exist")
public void testOrganizationDoesNotExist() throws DeviceOrganizationMgtPluginException {
int nonExistentDeviceId = 9999; // An ID that doesn't exist

@ -27,7 +27,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
log.info("Service test initialized");
}
@Test(priority = 4, dependsOnMethods = "testAddMultipleDeviceOrganizations")
@Test(dependsOnMethods = "testAddMultipleDeviceOrganizations")
public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException {
boolean exists = deviceOrganizationService.isDeviceIdExist(17);
if (exists){
@ -41,49 +41,50 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
}
}
@Test(priority = 5, dependsOnMethods = "testAddMultipleDeviceOrganizations")
@Test(dependsOnMethods = "testAddMultipleDeviceOrganizations")
public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
boolean exists = deviceOrganizationService.isChildDeviceIdExist(20);
if (exists) {
DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(20);
int maxDepth = 2;
boolean includeDevice = true;
int maxDepth = 3;
boolean includeDevice = false;
List<DeviceNode> parentList = deviceOrganizationService.getParentsOfDeviceNode(deviceNode, maxDepth, includeDevice);
Assert.assertNotNull(parentList, "Cannot be null");
}
}
@Test(priority = 1)
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
@Test()
public void testAddDeviceOrganizationWithNullParent() throws DeviceOrganizationMgtPluginException {
// DeviceOrganization deviceOrganization = new DeviceOrganization();
// deviceOrganization.setDeviceId(4);
// deviceOrganization.setParentDeviceId(3);
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(3);
deviceOrganizationOne.setParentDeviceId(null);
// DeviceOrganization deviceOrganizationTwo = new DeviceOrganization();
// deviceOrganizationTwo.setDeviceId(4);
// deviceOrganizationTwo.setParentDeviceId(2);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
// deviceOrganizationService.deleteDeviceAssociations(4);
// deviceOrganizationService.deleteDeviceAssociations(3);
// boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
// DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, null);
// boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo);
// DeviceOrganization organization2 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 2);
// Assert.assertNotNull(organization);
Assert.assertNotNull(organization1);
// Assert.assertNotNull(organization2);
}
@Test()
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganizationOne = new DeviceOrganization();
deviceOrganizationOne.setDeviceId(3);
deviceOrganizationOne.setParentDeviceId(4);
deviceOrganizationOne.setDeviceOrganizationMeta("Physical Relationship");
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, 4);
@Test(priority = 11)
Assert.assertNotNull(organization1);
}
@Test()
public void testAddMultipleRandomDeviceOrganizations() throws DeviceOrganizationMgtPluginException {
DeviceOrganizationService deviceOrganizationService = new DeviceOrganizationServiceImpl();
@ -118,7 +119,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertEquals(organizationCount, 100, "Inserted 100 organizations");
}
@Test(priority = 11)
@Test()
public void testAddMultipleDeviceOrganizations() throws DeviceOrganizationMgtPluginException {
DeviceOrganizationService deviceOrganizationService = new DeviceOrganizationServiceImpl();
@ -157,41 +158,40 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertEquals(organizationCount, combinations.length, "Inserted organizations count mismatch");
}
@Test(priority = 6, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization();
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
// DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
// Assert.assertNotNull(organization);
}
@Test(priority = 2, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(1);
}
@Test(priority = 3, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
boolean deviceIdExist = deviceOrganizationService.isDeviceIdExist(4);
}
@Test(priority = 7, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceOrganizationByID(1);
}
@Test(priority = 8, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceAssociations(4);
}
@Test(priority = 9, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetAllOrganizations() throws DeviceOrganizationMgtPluginException {
List<DeviceOrganization> organizations = deviceOrganizationService.getAllDeviceOrganizations();
for (DeviceOrganization organization : organizations) {
@ -205,7 +205,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
// Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty");
}
@Test(priority = 10, dependsOnMethods = "testAddDeviceOrganization")
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException {
int deviceID = 20;
int parentDeviceID = 19;

@ -856,13 +856,16 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION
(
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
DEVICE_ORGANIZATION_META TEXT 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_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -23,10 +23,13 @@
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
DEVICE_ORGANIZATION_META TEXT 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,
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

Loading…
Cancel
Save