From 887d0664248f4a1f10c266a7b95306b11310c6e9 Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Thu, 3 Nov 2016 14:00:29 +0000 Subject: [PATCH] Merged 5.2.N (5.2.1) to HEAD (5.2) 131886 mward: REPO-892: ensure presence of mandatory fields git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@132312 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/rest/api/impl/PeopleImpl.java | 21 ++++++++++++++---- .../alfresco/rest/api/tests/TestPeople.java | 22 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/source/java/org/alfresco/rest/api/impl/PeopleImpl.java b/source/java/org/alfresco/rest/api/impl/PeopleImpl.java index e672028988..df4d3613c8 100644 --- a/source/java/org/alfresco/rest/api/impl/PeopleImpl.java +++ b/source/java/org/alfresco/rest/api/impl/PeopleImpl.java @@ -286,10 +286,7 @@ public class PeopleImpl implements People @Override public Person create(PersonUpdate person) { - if (person.getUserName() == null) - { - throw new InvalidArgumentException("Field 'id' is null, but is required."); - } + validateCreatePersonData(person); // TODO: check, is this transaction safe? // Unfortunately PersonService.createPerson(...) only throws an AlfrescoRuntimeException @@ -311,6 +308,22 @@ public class PeopleImpl implements People // 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) diff --git a/source/test-java/org/alfresco/rest/api/tests/TestPeople.java b/source/test-java/org/alfresco/rest/api/tests/TestPeople.java index 91db2abe8e..e4fb822352 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestPeople.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestPeople.java @@ -224,16 +224,19 @@ public class TestPeople extends EnterpriseTestApi // +ve: absolute minimum { 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(); Person p = people.create(person); assertEquals("joe.bloggs.2@" + account1.getId(), p.getId()); - assertEquals(null, p.getFirstName()); + assertEquals("Joe", p.getFirstName()); assertEquals(null, p.getLastName()); assertEquals(null, p.getDescription()); - assertEquals(null, p.getEmail()); + assertEquals("joe.bloggs.2@example.com", p.getEmail()); assertEquals(null, p.getSkypeId()); assertEquals(null, p.getGoogleId()); assertEquals(null, p.getInstantMessageId()); @@ -250,7 +253,9 @@ public class TestPeople extends EnterpriseTestApi // -ve: not enough fields! { // 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); } } @@ -292,6 +297,9 @@ public class TestPeople extends EnterpriseTestApi publicApiClient.setRequestContext(new RequestContext(account1.getId(), GUID.generate(), "password")); PersonUpdate person = new PersonUpdate.Builder(). id("myUserName01@"+account1.getId()). + firstName("Caroline"). + email("caroline.smithson@example.com"). + enabled(true). build(); people.create(person, 401); } @@ -302,6 +310,9 @@ public class TestPeople extends EnterpriseTestApi publicApiClient.setRequestContext(new RequestContext(account2.getId(), apiUser)); PersonUpdate person = new PersonUpdate.Builder(). id("myUserName02@"+account2.getId()). + firstName("Kieth"). + email("keith.smith@example.com"). + enabled(true). build(); people.create(person, 403); @@ -315,6 +326,9 @@ public class TestPeople extends EnterpriseTestApi publicApiClient.setRequestContext(new RequestContext(account1.getId(), account1Admin, "admin")); PersonUpdate person = new PersonUpdate.Builder(). id("myUserName03@"+account1.getId()). + firstName("Alison"). + email("alison.smythe@example.com"). + enabled(true). build(); people.create(person);