Add test cases to verify DAO functionality

revert-70aa11f8
charitha 8 years ago
parent 54211bc3cc
commit ca5d96df67

@ -608,10 +608,10 @@ public class GroupDAOImpl implements GroupDAO {
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (";
int index = 0;
while (index++ < rolesCount) {
sql += (rolesCount - 1 != index) ? "?," : "?";
while (index++ < rolesCount - 1) {
sql += "?,";
}
sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID";
sql += "?)) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID";
stmt = conn.prepareStatement(sql);
index = 0;

@ -31,6 +31,7 @@ import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GroupPersistTests extends BaseDeviceManagementTest {
@ -47,7 +48,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
}
@Test
public void testAddGroupTest() {
public void addGroupTest() {
DeviceGroup deviceGroup = TestDataHolder.generateDummyGroupData();
try {
GroupManagementDAOFactory.beginTransaction();
@ -72,7 +73,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
log.debug("Group name: " + group.getName());
}
@Test(dependsOnMethods = {"testAddGroupTest"})
@Test(dependsOnMethods = {"addGroupTest"})
public void getGroupTest() {
try {
GroupManagementDAOFactory.openConnection();
@ -96,7 +97,82 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
}
}
@Test(dependsOnMethods = {"testAddGroupTest"})
@Test(dependsOnMethods = {"addGroupTest"})
public void shareGroupTest() {
try {
GroupManagementDAOFactory.beginTransaction();
List<String> addedRoles = new ArrayList<>();
for (int i = 0; i < 3; i++) {
String role = "role-" + i;
groupDAO.addRole(groupId, role, TestDataHolder.SUPER_TENANT_ID);
addedRoles.add(role);
}
GroupManagementDAOFactory.commitTransaction();
List<String> roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(roles, addedRoles, "Added roles are not equal to returned roles.");
log.debug("Group shared with roles.");
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while find group by name.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"shareGroupTest"})
public void getShareGroupTest() {
try {
GroupManagementDAOFactory.openConnection();
List<String> roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID);
roles.remove(0);
List<DeviceGroup> deviceGroups = groupDAO.getGroups(roles.toArray(new String[roles.size()]), TestDataHolder.SUPER_TENANT_ID);
Assert.assertEquals(deviceGroups.size(), 1, "Unexpected number of device groups found with role.");
Assert.assertEquals(deviceGroups.get(0).getGroupId(), groupId, "Unexpected groupId found with role.");
log.debug("Group found for given roles.");
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while getting groups shared with roles.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"getShareGroupTest"})
public void unshareGroupTest() {
try {
GroupManagementDAOFactory.beginTransaction();
List<String> rolesToRemove = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID);
for (String role : rolesToRemove) {
groupDAO.removeRole(groupId, role, TestDataHolder.SUPER_TENANT_ID);
}
GroupManagementDAOFactory.commitTransaction();
List<String> roles = groupDAO.getRoles(groupId, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotEquals(roles, rolesToRemove, "Roles not removed.");
log.debug("Group unshared with given roles.");
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while find group by name.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addGroupTest"})
public void addDeviceToGroupTest() {
Device initialTestDevice = TestDataHolder.initialTestDevice;
DeviceGroup deviceGroup = getGroupById(groupId);
@ -162,7 +238,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
}
}
@Test(dependsOnMethods = {"removeDeviceFromGroupTest"})
@Test(dependsOnMethods = {"removeDeviceFromGroupTest", "unshareGroupTest"})
public void updateGroupTest() {
String name = "Test Updated";
String desc = "Desc updated";

Loading…
Cancel
Save