Fix group names being able to be duplicated when renaming

feature/traccar-sync
navodzoysa 2 years ago
parent 2a5630cfb7
commit 6b6e9ba640

@ -218,7 +218,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (GroupNotExistException e) {
String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'.";
String msg = "Group doesn't exist with ID '" + deviceGroup.getGroupId() + "'.";
log.warn(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
} catch (GroupAlreadyExistException e) {
String msg = "Group already exists with name '" + deviceGroup.getName() + "'.";
log.warn(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
}

@ -205,7 +205,7 @@ public class GroupManagementServiceImplTest {
}
@Test(description = "This method tests the functionality of updateGroup method under various conditions")
public void testUpdateGroup() throws GroupManagementException, GroupNotExistException {
public void testUpdateGroup() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService"))
.toReturn(groupManagementProviderService);
DeviceGroup deviceGroup = new DeviceGroup();

@ -727,7 +727,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Connection conn = GroupManagementDAOFactory.getConnection();
String sql =
"SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP "
+ "WHERE GROUP_NAME = ? AND TENANT_ID = ?";
+ "WHERE LOWER(GROUP_NAME) = LOWER(?) AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, groupName);
stmt.setInt(2, tenantId);

@ -71,7 +71,8 @@ public interface GroupManagementProviderService {
* @param groupId of the group.
* @throws GroupManagementException
*/
void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupNotExistException;
void updateGroup(DeviceGroup deviceGroup, int groupId)
throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException;
/**
* Delete existing device group.

@ -132,7 +132,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
GroupManagementDAOFactory.commitTransaction();
} else {
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
throw new GroupAlreadyExistException("Group already exists with name '" + deviceGroup.getName() + "'.");
}
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
@ -163,7 +163,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/
@Override
public void updateGroup(DeviceGroup deviceGroup, int groupId)
throws GroupManagementException, GroupNotExistException {
throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
if (deviceGroup == null) {
String msg = "Received incomplete data for updateGroup";
log.error(msg);
@ -177,6 +177,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.beginTransaction();
DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId);
if (existingGroup != null) {
boolean existingGroupName = this.groupDAO.getGroup(deviceGroup.getName(), tenantId) != null;
if (existingGroupName) {
throw new GroupAlreadyExistException("Group already exists with name '" + deviceGroup.getName() + "'.");
}
List<DeviceGroup> groupsToUpdate = new ArrayList<>();
String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR);
String parentPath = "";
@ -222,7 +226,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupNotExistException ex) {
} catch (GroupNotExistException | GroupAlreadyExistException ex) {
throw ex;
} catch (Exception e) {
String msg = "Error occurred in updating the device group with ID - '" + groupId + "'.";

@ -103,7 +103,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
}
@Test(dependsOnMethods = "createGroup")
public void updateGroup() throws GroupManagementException, GroupNotExistException {
public void updateGroup() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName(), false);
deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId());
@ -116,19 +116,19 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
// Rename again to use in different place.
@Test(dependsOnMethods = "updateGroup")
public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException {
public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED", true);
deviceGroup.setName(TestUtils.createDeviceGroup1().getName());
groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId());
}
@Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class})
public void updateGroupError() throws GroupManagementException, GroupNotExistException {
@Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class, GroupAlreadyExistException.class})
public void updateGroupError() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
groupManagementProviderService.updateGroup(null, 1);
}
@Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class})
public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException {
@Test(dependsOnMethods = "createGroup", expectedExceptions = {GroupManagementException.class, GroupNotExistException.class, GroupAlreadyExistException.class})
public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName(), false);
deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, 6);

Loading…
Cancel
Save