REPO-892: ensure presence of mandatory fields

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@131886 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2016-10-31 11:12:19 +00:00
parent 0e7a674d41
commit 9fd17791ff
2 changed files with 35 additions and 8 deletions

View File

@@ -286,10 +286,7 @@ public class PeopleImpl implements People
@Override @Override
public Person create(PersonUpdate person) public Person create(PersonUpdate person)
{ {
if (person.getUserName() == null) validateCreatePersonData(person);
{
throw new InvalidArgumentException("Field 'id' is null, but is required.");
}
// TODO: check, is this transaction safe? // TODO: check, is this transaction safe?
// Unfortunately PersonService.createPerson(...) only throws an AlfrescoRuntimeException // Unfortunately PersonService.createPerson(...) only throws an AlfrescoRuntimeException
@@ -311,6 +308,22 @@ public class PeopleImpl implements People
// return getPerson(person.getUserName()); // return getPerson(person.getUserName());
} }
private void validateCreatePersonData(PersonUpdate person)
{
checkRequiredField("id", person.getUserName());
checkRequiredField("firstName", person.getFirstName());
checkRequiredField("email", person.getEmail());
checkRequiredField("enabled", person.isEnabled());
}
private void checkRequiredField(String fieldName, Object fieldValue)
{
if (fieldValue == null)
{
throw new InvalidArgumentException("Field '"+fieldName+"' is null, but is required.");
}
}
/** /**
public Person updatePerson(String personId, final Person person) public Person updatePerson(String personId, final Person person)

View File

@@ -225,15 +225,18 @@ public class TestPeople extends EnterpriseTestApi
{ {
PersonUpdate person = new PersonUpdate.Builder(). PersonUpdate person = new PersonUpdate.Builder().
id("joe.bloggs.2@"+account1.getId()). id("joe.bloggs.2@"+account1.getId()).
firstName("Joe").
email("joe.bloggs.2@example.com").
enabled(true).
build(); build();
Person p = people.create(person); Person p = people.create(person);
assertEquals("joe.bloggs.2@" + account1.getId(), p.getId()); assertEquals("joe.bloggs.2@" + account1.getId(), p.getId());
assertEquals(null, p.getFirstName()); assertEquals("Joe", p.getFirstName());
assertEquals(null, p.getLastName()); assertEquals(null, p.getLastName());
assertEquals(null, p.getDescription()); assertEquals(null, p.getDescription());
assertEquals(null, p.getEmail()); assertEquals("joe.bloggs.2@example.com", p.getEmail());
assertEquals(null, p.getSkypeId()); assertEquals(null, p.getSkypeId());
assertEquals(null, p.getGoogleId()); assertEquals(null, p.getGoogleId());
assertEquals(null, p.getInstantMessageId()); assertEquals(null, p.getInstantMessageId());
@@ -250,7 +253,9 @@ public class TestPeople extends EnterpriseTestApi
// -ve: not enough fields! // -ve: not enough fields!
{ {
// Create a person with no fields set. // Create a person with no fields set.
PersonUpdate person = new PersonUpdate.Builder().build(); PersonUpdate person = new PersonUpdate.Builder().
id("joe.bloggs.2@"+account1.getId()).
build();
people.create(person, 400); people.create(person, 400);
} }
} }
@@ -292,6 +297,9 @@ public class TestPeople extends EnterpriseTestApi
publicApiClient.setRequestContext(new RequestContext(account1.getId(), GUID.generate(), "password")); publicApiClient.setRequestContext(new RequestContext(account1.getId(), GUID.generate(), "password"));
PersonUpdate person = new PersonUpdate.Builder(). PersonUpdate person = new PersonUpdate.Builder().
id("myUserName01@"+account1.getId()). id("myUserName01@"+account1.getId()).
firstName("Caroline").
email("caroline.smithson@example.com").
enabled(true).
build(); build();
people.create(person, 401); people.create(person, 401);
} }
@@ -302,6 +310,9 @@ public class TestPeople extends EnterpriseTestApi
publicApiClient.setRequestContext(new RequestContext(account2.getId(), apiUser)); publicApiClient.setRequestContext(new RequestContext(account2.getId(), apiUser));
PersonUpdate person = new PersonUpdate.Builder(). PersonUpdate person = new PersonUpdate.Builder().
id("myUserName02@"+account2.getId()). id("myUserName02@"+account2.getId()).
firstName("Kieth").
email("keith.smith@example.com").
enabled(true).
build(); build();
people.create(person, 403); people.create(person, 403);
@@ -315,6 +326,9 @@ public class TestPeople extends EnterpriseTestApi
publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin")); publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin"));
PersonUpdate person = new PersonUpdate.Builder(). PersonUpdate person = new PersonUpdate.Builder().
id("myUserName03@"+account1.getId()). id("myUserName03@"+account1.getId()).
firstName("Alison").
email("alison.smythe@example.com").
enabled(true).
build(); build();
people.create(person); people.create(person);