From c1875e30832aaabec64e11da09298371ab41a7ca Mon Sep 17 00:00:00 2001 From: Alexandru Epure Date: Tue, 9 Aug 2016 14:16:15 +0000 Subject: [PATCH] Merged 5.2.N (5.2.1) to HEAD (5.2) 129190 mmuller: Merged RETURN-OF-THE-API (5.2.0) to 5.2.N (5.2.1) 129017 adavis: REPO-243 People Live Search - Added additional tests - Removed check in Person constructor for no id as we want to be able to create a Person from json which might not have the id. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@129364 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/rest/api/impl/QueriesImpl.java | 7 +- .../rest/api/tests/QueriesPeopleApiTest.java | 149 +++++++++++------- .../rest/api/tests/client/data/Person.java | 4 - 3 files changed, 97 insertions(+), 63 deletions(-) diff --git a/source/java/org/alfresco/rest/api/impl/QueriesImpl.java b/source/java/org/alfresco/rest/api/impl/QueriesImpl.java index 683abfc767..91ca43db59 100644 --- a/source/java/org/alfresco/rest/api/impl/QueriesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/QueriesImpl.java @@ -385,7 +385,8 @@ public class QueriesImpl implements Queries, InitializingBean } public CollectionWithPagingInfo find(Parameters parameters, - String termName, int minTermLength, String queryTemplateName, Sort sort, Map sortParamsToQNames, SortColumn... defaultSort) + String termName, int minTermLength, String queryTemplateName, + Sort sort, Map sortParamsToQNames, SortColumn... defaultSort) { SearchParameters sp = new SearchParameters(); sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO); @@ -541,6 +542,10 @@ public class QueriesImpl implements Queries, InitializingBean for (SortColumn sortCol : sortCols) { QName sortPropQName = sortParamsToQNames.get(sortCol.column); + if (sortPropQName == null) + { + throw new InvalidArgumentException("Invalid sort field: "+sortCol.column); + } sortPropQNames.add(sortPropQName); } diff --git a/source/test-java/org/alfresco/rest/api/tests/QueriesPeopleApiTest.java b/source/test-java/org/alfresco/rest/api/tests/QueriesPeopleApiTest.java index 6693043a8d..c3a2c53f40 100644 --- a/source/test-java/org/alfresco/rest/api/tests/QueriesPeopleApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/QueriesPeopleApiTest.java @@ -56,14 +56,15 @@ import com.sun.star.lang.IllegalArgumentException; */ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest { - private static final String URL_QUERIES_LSN = "queries/live-search-people"; + private static final String URL_QUERIES_LSP = "queries/live-search-people"; private static String TEST_TERM_PREFIX = Long.toString(System.currentTimeMillis()/1000); // TODO Yuck: Would like to use @BeforeClass and @AfterClass. But creating and // deleting users is hard from from static methods. For the moment do it // in the first and last tests, but we have to get the TEST count right! - private static int TEST_COUNT = 6; + // If we don't, a test fails or the users get left behind (not too bad). + private static int TEST_COUNT = 12; private static int testCounter = 0; // Test usernames @@ -103,6 +104,7 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest @Before @Override + @SuppressWarnings("deprecation") public void setup() throws Exception { super.setup(); @@ -239,9 +241,9 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest createParamIdNotNull(Queries.PARAM_ORDERBY, orderBy); createParamIdNotNull(Queries.PARAM_FIELDS, fields); - response = getAll(URL_QUERIES_LSN, paging, params, expectedStatus); + response = getAll(URL_QUERIES_LSP, paging, params, expectedStatus); - if (expectedPeople != null) + if (expectedPeople != null && expectedStatus == 200) { people = Person.parsePeople(response.getJsonResponse()).getList(); @@ -254,17 +256,13 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest } String exp = expected.toString().replaceAll(TEST_TERM_PREFIX, ""); String act = actual.toString().replaceAll(TEST_TERM_PREFIX, ""); - if (!exp.equals(act)) - { - - } assertEquals(exp, act); } } private void createParamIdNotNull(String param, String value) { - if (value != null) + if (value != null && params != null) { params.put(param, value); } @@ -282,11 +280,28 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest return list.toArray(new String[list.size()]); } + @Test + @SuppressWarnings("deprecation") + public void testUnauthenticated() throws Exception + { + setRequestContext(null); + expectedStatus = 401; + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } + @Test public void testOnlyTestUsersAndDefaultOrder() throws Exception { // Checks only test users are found as a result of using TEST_TERM_PREFIX. - // Also checks the default sort order. + + // Also checks the default sort order (firstname lastname): + // 5 A + // 6 C + // 1 A A + // 2 A B + // 3 B A + // 4 C checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); } @@ -316,13 +331,21 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); } + + @Test + public void testNoParams() throws Exception + { + params = null; + expectedStatus = 400; + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } @Test public void testNoTerm() throws Exception { term = null; expectedStatus = 400; - expectedPeople = null; checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); } @@ -332,26 +355,60 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest { term = "X"; expectedStatus = 400; - expectedPeople = null; + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } + + @Test + public void testOrderbySameAsDefault() throws Exception + { + orderBy = "firstName asc, lastName"; // same as default (asc is default order) + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } + + @Test + public void testOrderbyDescAndAsc() throws Exception + { + // 4 C + // 3 B A + // 1 A A + // 2 A B + // 5 A + // 6 C + orderBy = "firstName desc, lastName"; + expectedPeople = expectedPeople(USER4, USER3, USER1, USER2, USER5, USER6); checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); } -// TODO commented out tests: - -// Returns 500 rather than 400 -// @Test -// public void testUnknownOrderByField() throws Exception -// { -// orderBy = "rubbish"; -// expectedStatus = 400; -// -// checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); -// } + @Test + public void testBadOrderByField() throws Exception + { + orderBy = "rubbish"; + expectedStatus = 400; + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } -// Rubbish is taken to be "asc" - is this a bug? + @Test + public void testFieldsFirstLast() throws Exception + { + fields = PARAM_FIRSTNAME+","+PARAM_LASTNAME; + term = LAST_A; + expectedPeople = new String[] + { + "Person ["+ "lastName=LastA, ]", // USER5 + "Person ["+"firstName=FirstA, lastName=LastA, ]", // USER1 + "Person ["+"firstName=FirstB, lastName=LastA, ]", // USER3 + }; + + checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); + } + +// // TODO Rubbish is taken to be "asc" so returns 200 rather than 400 - is this a framework bug? // @Test -// public void testOrderByDirection() throws Exception +// public void testBadOrderByDirection() throws Exception // { // orderBy = "firstName rubbish, lastName asc"; // expectedStatus = 400; @@ -359,45 +416,21 @@ public class QueriesPeopleApiTest extends AbstractSingleNetworkSiteTest // checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); // } -// Server response is 500 from the first call. +// // TODO Having a space in the list discards everything after the space - found in manual testing - is this a framework bug? // @Test -// public void testOrderby() throws Exception -// { -// // {USER_1, FIRST_A, LAST_A}, -// // {USER_2, FIRST_A, LAST_B}, -// // {USER_3, FIRST_B, LAST_A}, -// // {USER_4, FIRST_C, }, -// // {USER_5, null, LAST_A}, -// // {USER_6, null, LAST_C}, -// -// orderBy = "firstName asc, lastName"; // same as default (asc is default order) -// -// checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); -// -// orderBy = "firstName desc, lastName"; // with desc and asc -// -// expectedPeople = new String[] {"TODO"}; -// expectedPeople = expectedPeople(USER4, USER3, USER1, USER2, USER5, USER6); -// } - -// This test causes the following exception because we did not request the id field! -// testFields(org.alfresco.rest.api.tests.QueriesPeopleApiTest) Time elapsed: 6.21 sec <<< ERROR! -// java.lang.IllegalArgumentException: null -// at org.alfresco.rest.api.tests.client.data.Person.(Person.java:73) -// at org.alfresco.rest.api.tests.client.data.Person.parsePerson(Person.java:278) -// at org.alfresco.rest.api.tests.client.data.Person.parsePeople(Person.java:503) -// at org.alfresco.rest.api.tests.QueriesPeopleApiTest.checkApiCall(QueriesPeopleApiTest.java:246) -// at org.alfresco.rest.api.tests.QueriesPeopleApiTest.testFields(QueriesPeopleApiTest.java:353) -// @Test -// public void testFields() throws Exception +// public void testFieldsWithSpace() throws Exception // { // fields = PARAM_FIRSTNAME+", "+PARAM_LASTNAME; // term = LAST_A; // expectedPeople = new String[] // { -// "lastName=LastA,", // USER5 -// "firstName=FirstA, lastName=LastA,", // USER1 -// "firstName=FirstB, lastName=LastA,", // USER3 +// "Person ["+ "lastName=LastA, ]", // USER5 +// "Person ["+"firstName=FirstA, lastName=LastA, ]", // USER1 +// "Person ["+"firstName=FirstB, lastName=LastA, ]", // USER3 +// // But is the following: +//// "Person ["+ "]", +//// "Person ["+"firstName=FirstA, ]", +//// "Person ["+"firstName=FirstB, ]", // }; // // checkApiCall(term, orderBy, fields, paging, expectedStatus, expectedPeople); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/data/Person.java b/source/test-java/org/alfresco/rest/api/tests/client/data/Person.java index a2fad04a82..81e5ae772e 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/data/Person.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/data/Person.java @@ -68,10 +68,6 @@ public class Person implements Serializable, Comparable, ExpectedCompari String mob, String instantmsg, String google, String description) { super(); - if(id == null) - { - throw new IllegalArgumentException(); - } this.id = id; this.username = username; this.enabled = enabled;