@Test public void fail_when_group_name_is_too_big() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Group name cannot be longer than 255 characters"); UserGroupValidation.validateGroupName(Strings.repeat("name", 300)); }
@Test public void fail_when_group_name_is_empty() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Group name cannot be empty"); UserGroupValidation.validateGroupName(""); }
@Test public void fail_when_group_name_is_null() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Group name cannot be empty"); UserGroupValidation.validateGroupName(null); } }
@Test public void fail_when_group_name_is_Anyone() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Anyone group cannot be used"); UserGroupValidation.validateGroupName("AnyOne"); }
@Test public void fail_when_group_name_contains_only_blank() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Group name cannot be empty"); UserGroupValidation.validateGroupName(" "); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID); GroupDto group = dbClient.groupDao().selectById(dbSession, groupId); checkFound(group, "Could not find a user group with id '%s'.", groupId); Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid()); checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid()); userSession.checkPermission(ADMINISTER, org.get()); support.checkGroupIsNotDefault(dbSession, group); boolean changed = false; String newName = request.param(PARAM_GROUP_NAME); if (newName != null) { changed = true; UserGroupValidation.validateGroupName(newName); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName); group.setName(newName); } String description = request.param(PARAM_GROUP_DESCRIPTION); if (description != null) { changed = true; group.setDescription(description); } if (changed) { dbClient.groupDao().update(dbSession, group); dbSession.commit(); } writeResponse(dbSession, request, response, org.get(), group); } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = support.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkPermission(ADMINISTER, organization); GroupDto group = new GroupDto() .setOrganizationUuid(organization.getUuid()) .setName(request.mandatoryParam(PARAM_GROUP_NAME)) .setDescription(request.param(PARAM_GROUP_DESCRIPTION)); // validations UserGroupValidation.validateGroupName(group.getName()); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName()); dbClient.groupDao().insert(dbSession, group); dbSession.commit(); writeResponse(request, response, organization, group); } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID); GroupDto group = dbClient.groupDao().selectById(dbSession, groupId); checkFound(group, "Could not find a user group with id '%s'.", groupId); Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid()); checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid()); userSession.checkPermission(ADMINISTER, org.get()); support.checkGroupIsNotDefault(dbSession, group); boolean changed = false; String newName = request.param(PARAM_GROUP_NAME); if (newName != null) { changed = true; UserGroupValidation.validateGroupName(newName); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName); group.setName(newName); } String description = request.param(PARAM_GROUP_DESCRIPTION); if (description != null) { changed = true; group.setDescription(description); } if (changed) { dbClient.groupDao().update(dbSession, group); dbSession.commit(); } writeResponse(dbSession, request, response, org.get(), group); } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = support.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkPermission(ADMINISTER, organization); GroupDto group = new GroupDto() .setOrganizationUuid(organization.getUuid()) .setName(request.mandatoryParam(PARAM_GROUP_NAME)) .setDescription(request.param(PARAM_GROUP_DESCRIPTION)); // validations UserGroupValidation.validateGroupName(group.getName()); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName()); dbClient.groupDao().insert(dbSession, group); dbSession.commit(); writeResponse(request, response, organization, group); } }