This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit 2ac5fb9653ee6e40a552724e6f9e768bb080e37f Author: Yannick Martel <martel@©odelutin.com> Date: Tue Jan 6 17:22:05 2015 +0100 add a Question search service --- .../ifremer/coselmar/beans/QuestionSearchBean.java | 40 +++ .../persistence/entity/QuestionTopiaDao.java | 93 ++++++- .../services/CoselmarRestApplicationListener.java | 4 +- .../services/v1/InitialisationService.java | 13 + .../coselmar/services/v1/QuestionsWebService.java | 108 ++++++-- .../services/FakeCoselmarApplicationContext.java | 8 + .../coselmar/services/QuestionsWebServiceTest.java | 299 +++++++++++++++++++++ 7 files changed, 544 insertions(+), 21 deletions(-) diff --git a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/beans/QuestionSearchBean.java b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/beans/QuestionSearchBean.java new file mode 100644 index 0000000..7e29a07 --- /dev/null +++ b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/beans/QuestionSearchBean.java @@ -0,0 +1,40 @@ +package fr.ifremer.coselmar.beans; + +import java.io.Serializable; +import java.util.List; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class QuestionSearchBean implements Serializable { + + private static final long serialVersionUID = -3318003570685481073L; + + protected List<String> keywords; + protected String status; + protected String privacy; + + public List<String> getKeywords() { + return keywords; + } + + public void setKeywords(List<String> keywords) { + this.keywords = keywords; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getPrivacy() { + return privacy; + } + + public void setPrivacy(String privacy) { + this.privacy = privacy; + } +} diff --git a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/QuestionTopiaDao.java b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/QuestionTopiaDao.java index 54151cf..d1d1e71 100644 --- a/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/QuestionTopiaDao.java +++ b/coselmar-persistence/src/main/java/fr/ifremer/coselmar/persistence/entity/QuestionTopiaDao.java @@ -28,11 +28,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import fr.ifremer.coselmar.beans.QuestionSearchBean; import fr.ifremer.coselmar.persistence.DaoUtils; +import org.apache.commons.lang3.StringUtils; public class QuestionTopiaDao extends AbstractQuestionTopiaDao<Question> { - public List<Question> findForExpert(CoselmarUser expert) { + public List<Question> findForExpert(CoselmarUser expert, QuestionSearchBean searchBean) { StringBuilder hqlBuilder = new StringBuilder("SELECT Q FROM " + Question.class.getName() + " Q " + " INNER JOIN Q." + Question.PROPERTY_PARTICIPANTS + " CUG "); @@ -41,7 +43,7 @@ public class QuestionTopiaDao extends AbstractQuestionTopiaDao<Question> { String publicCondition = DaoUtils.getQueryForAttributeEquals("Q", Question.PROPERTY_PRIVACY, args, Privacy.PUBLIC, ""); - hqlBuilder.append(" WHERE (" + publicCondition + " ) "); + hqlBuilder.append(" WHERE ( (" + publicCondition + " ) "); String privateCondition = DaoUtils.getQueryForAttributeEquals("Q", Question.PROPERTY_PRIVACY, args, Privacy.PRIVATE, ""); @@ -49,7 +51,49 @@ public class QuestionTopiaDao extends AbstractQuestionTopiaDao<Question> { String userCondition = DaoUtils.andAttributeContains("CUG", CoselmarUserGroup.PROPERTY_MEMBERS, args, expert); - hqlBuilder.append(userCondition + ")"); + hqlBuilder.append(userCondition + ") )"); + + if (searchBean != null) { + String finerHql = refineSearch(searchBean, "Q", args); + hqlBuilder.append(" AND (" + finerHql + ")" ); + } + + + List<Question> questions = forHql(hqlBuilder.toString(), args).findAll(); + + return questions; + } + + public List<Question> findForClient(CoselmarUser client, QuestionSearchBean searchBean) { + + StringBuilder hqlBuilder = new StringBuilder("SELECT Q FROM " + Question.class.getName() + " Q "); + + Map<String, Object> args = new HashMap<>(); + + String clientCondition = DaoUtils.andAttributeContains("Q", Question.PROPERTY_PRIVACY, args, Privacy.PUBLIC); + + hqlBuilder.append(" WHERE 1=1 AND (" + clientCondition + " ) "); + + if (searchBean != null) { + String finerHql = refineSearch(searchBean, "Q", args); + hqlBuilder.append(" AND (" + finerHql + ")" ); + } + + List<Question> questions = forHql(hqlBuilder.toString(), args).findAll(); + + return questions; + } + + public List<Question> findWithSearchBean(QuestionSearchBean searchBean) { + + StringBuilder hqlBuilder = new StringBuilder("SELECT Q FROM " + Question.class.getName() + " Q "); + + Map<String, Object> args = new HashMap<>(); + + if (searchBean != null) { + String finerHql = refineSearch(searchBean, "Q", args); + hqlBuilder.append(" WHERE (" + finerHql + ")" ); + } List<Question> questions = forHql(hqlBuilder.toString(), args).findAll(); @@ -79,4 +123,47 @@ public class QuestionTopiaDao extends AbstractQuestionTopiaDao<Question> { return values; } + public String refineSearch(QuestionSearchBean searchBean, String alias, Map args) { + + StringBuilder finerHqlBuilder = new StringBuilder(" 1=1 "); + + String privacy = searchBean.getPrivacy(); + + if (StringUtils.isNotBlank(privacy)) { + String privacyHql = DaoUtils.andAttributeEquals(alias, Question.PROPERTY_PRIVACY, args, Privacy.valueOf(privacy.toUpperCase())); + finerHqlBuilder.append(privacyHql); + } + + String status = searchBean.getStatus(); + + if (StringUtils.isNotBlank(status)) { + String statusHql = DaoUtils.andAttributeEquals(alias, Question.PROPERTY_STATUS, args, Status.valueOf(status.toUpperCase())); + finerHqlBuilder.append(statusHql); + } + + List<String> keywords = searchBean.getKeywords(); + if (keywords != null && !keywords.isEmpty()) { + // should be like : """ ( (1=1) + // AND ( 1=0 OR title like keyword1 OR summary like keyword1 OR theme contains keyword1 ) + // AND ( 1=0 OR title like keyword2 OR summary like keyword2 OR theme contains keyword2 ) + // ) """ + StringBuilder keywordsBuilder = new StringBuilder(" ( ( 1 = 1 ) "); + + for (String keyword : keywords) { + keywordsBuilder.append(" AND ( 1=0 "); + keywordsBuilder.append(DaoUtils.orAttributeLike(alias, Question.PROPERTY_TITLE, args, keyword)); + keywordsBuilder.append(DaoUtils.orAttributeLike(alias, Question.PROPERTY_SUBMISSION_DATE, args, keyword)); + keywordsBuilder.append(DaoUtils.orAttributeContains(alias, Question.PROPERTY_THEME, args, keyword)); + keywordsBuilder.append(" ) "); + } + + keywordsBuilder.append(" ) "); + String keywordHql = keywordsBuilder.toString(); + finerHqlBuilder.append(" AND " + keywordHql); + } + + String finerHql = finerHqlBuilder.toString(); + return finerHql; + } + } //QuestionTopiaDao diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarRestApplicationListener.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarRestApplicationListener.java index 8af741c..5715dd0 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarRestApplicationListener.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/CoselmarRestApplicationListener.java @@ -30,6 +30,7 @@ import java.util.Set; import com.google.common.collect.Sets; import fr.ifremer.coselmar.beans.DocumentBean; import fr.ifremer.coselmar.beans.QuestionBean; +import fr.ifremer.coselmar.beans.QuestionSearchBean; import fr.ifremer.coselmar.beans.UserBean; import fr.ifremer.coselmar.beans.UserSearchBean; import fr.ifremer.coselmar.converter.DateConverter; @@ -50,7 +51,8 @@ public class CoselmarRestApplicationListener implements WebMotionServerListener DocumentBean.class, UserBean.class, QuestionBean.class, - UserSearchBean.class + UserSearchBean.class, + QuestionSearchBean.class ); @Override diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/InitialisationService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/InitialisationService.java index 1cd58d1..7ab6844 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/InitialisationService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/InitialisationService.java @@ -70,6 +70,19 @@ public class InitialisationService extends CoselmarSimpleServiceSupport { lambdaUser.setPassword(lambdaPassword); lambdaUser.setSalt(lambdaSalt); + // Set a default supervisor + CoselmarUser defaultSupervisor = getCoselmarUserDao().create(); + defaultSupervisor.setFirstname("Default"); + defaultSupervisor.setName("Supervisor"); + defaultSupervisor.setMail("default.supervisor@temporary.coselmar"); + defaultSupervisor.setRole(CoselmarUserRole.SUPERVISOR); + defaultSupervisor.setActive(true); + + String defaultSupSalt = servicesContext.generateSalt(); + String defaultSupPassword = servicesContext.encodePassword(defaultSupSalt, "manager1234"); + defaultSupervisor.setPassword(defaultSupPassword); + defaultSupervisor.setSalt(defaultSupSalt); + commit(); } diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java index 2434279..57ad349 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java @@ -37,6 +37,7 @@ import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import fr.ifremer.coselmar.beans.DocumentBean; import fr.ifremer.coselmar.beans.QuestionBean; +import fr.ifremer.coselmar.beans.QuestionSearchBean; import fr.ifremer.coselmar.beans.UserBean; import fr.ifremer.coselmar.beans.UserWebToken; import fr.ifremer.coselmar.converter.BeanEntityConverter; @@ -218,7 +219,7 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { } } - public List<QuestionBean> getQuestions() throws InvalidCredentialException, UnauthorizedException { + public List<QuestionBean> getQuestions(QuestionSearchBean searchBean) throws InvalidCredentialException, UnauthorizedException { // Check authentication String authorization = getContext().getHeader("Authorization"); @@ -232,24 +233,11 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { List<Question> questionList; - if (StringUtils.equalsIgnoreCase(CoselmarUserRole.ADMIN.name(), currentUserRole) - || StringUtils.equalsIgnoreCase(CoselmarUserRole.SUPERVISOR.name(), currentUserRole)) { - questionList = getQuestionDao().findAll(); - - } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.MEMBER.name(), currentUserRole)) { - questionList = getQuestionDao().forPrivacyEquals(Privacy.PUBLIC).findAll(); - - } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.EXPERT.name(), currentUserRole)) { - questionList = getQuestionDao().findForExpert(currentUser); + if (searchBean != null) { + questionList = getAllFilteredQuestions(currentUser, searchBean); - } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.CLIENT.name(), currentUserRole)) { - questionList = getQuestionDao().forClientsContains(currentUser).findAll(); } else { - String message = "Not allowed to access this page"; - if (log.isWarnEnabled()) { - log.warn("Unknown user type try to access questions list."); - } - throw new UnauthorizedException(message); + questionList = getAllQuestions(currentUser); } List<QuestionBean> result = new ArrayList<>(questionList.size()); @@ -824,4 +812,90 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { } + /** + * Methods that retrieve all questions depending of current user role permission : + * <ul> + * <li>{@code ADMIN} and {@code SUPERVISOR} can access to all questions ;</li> + * <li>{@code MEMBER} can access to all public questions ;</li> + * <li>{@code EXPERT} can access to all private questions where he is participant or client and all public questions ;</li> + * <li>{@code CLIENT} can access to all questions where he is client.</li> + * </ul> + * + * @param currentUser : current user that make request + * + * @return list of all question user is authorized to access. + * @throws UnauthorizedException + */ + protected List<Question> getAllQuestions(CoselmarUser currentUser) throws UnauthorizedException { + String currentUserRole = currentUser.getRole().name(); + + List<Question> questionList; + if (StringUtils.equalsIgnoreCase(CoselmarUserRole.ADMIN.name(), currentUserRole) + || StringUtils.equalsIgnoreCase(CoselmarUserRole.SUPERVISOR.name(), currentUserRole)) { + questionList = getQuestionDao().findAll(); + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.MEMBER.name(), currentUserRole)) { + questionList = getQuestionDao().forPrivacyEquals(Privacy.PUBLIC).findAll(); + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.EXPERT.name(), currentUserRole)) { + questionList = getQuestionDao().findForExpert(currentUser, null); + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.CLIENT.name(), currentUserRole)) { + questionList = getQuestionDao().forClientsContains(currentUser).findAll(); + } else { + String message = "Not allowed to access this page"; + if (log.isWarnEnabled()) { + log.warn("Unknown user type try to access questions list."); + } + throw new UnauthorizedException(message); + } + return questionList; + } + + /** + * Methods that retrieve all questions matching search filter (based on + * title, summary, themes, status, privacy), also depending of current user role permission : + * <ul> + * <li>{@code ADMIN} and {@code SUPERVISOR} can access to all questions ;</li> + * <li>{@code MEMBER} can only access to all public questions (so, if filter is on private, it has no result) ;</li> + * <li>{@code EXPERT} can access to all private questions where he is participant or client and all public questions ;</li> + * <li>{@code CLIENT} can access to all questions where he is client.</li> + * </ul> + * + * @param currentUser : current user that make request + * @param searchBean : bean containing search param to filter result + * + * @return list of all question user is authorized to access. + * @throws UnauthorizedException + */ + protected List<Question> getAllFilteredQuestions(CoselmarUser currentUser, QuestionSearchBean searchBean) throws UnauthorizedException { + String currentUserRole = currentUser.getRole().name(); + + List<Question> questionList; + if (StringUtils.equalsIgnoreCase(CoselmarUserRole.ADMIN.name(), currentUserRole) + || StringUtils.equalsIgnoreCase(CoselmarUserRole.SUPERVISOR.name(), currentUserRole)) { + questionList = getQuestionDao().findWithSearchBean(searchBean); + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.MEMBER.name(), currentUserRole)) { + if (StringUtils.equals(searchBean.getPrivacy(), Privacy.PRIVATE.name())) { + questionList = new ArrayList<>(0); + } else { + questionList = getQuestionDao().findWithSearchBean(searchBean); + } + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.EXPERT.name(), currentUserRole)) { + questionList = getQuestionDao().findForExpert(currentUser, searchBean); + + } else if (StringUtils.equalsIgnoreCase(CoselmarUserRole.CLIENT.name(), currentUserRole)) { + questionList = getQuestionDao().findForClient(currentUser, searchBean); + } else { + String message = "Not allowed to access this page"; + if (log.isWarnEnabled()) { + log.warn("Unknown user type try to access questions list."); + } + throw new UnauthorizedException(message); + } + return questionList; + } + } diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java index bdfacbc..c7c3129 100644 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/FakeCoselmarApplicationContext.java @@ -39,6 +39,7 @@ import fr.ifremer.coselmar.persistence.CoselmarTopiaApplicationContext; import fr.ifremer.coselmar.persistence.CoselmarTopiaPersistenceContext; import fr.ifremer.coselmar.services.config.CoselmarServicesConfig; import fr.ifremer.coselmar.services.config.CoselmarServicesConfigOption; +import fr.ifremer.coselmar.services.v1.InitialisationService; import org.apache.commons.logging.Log; import org.junit.rules.TestWatcher; import org.junit.runner.Description; @@ -120,6 +121,13 @@ public class FakeCoselmarApplicationContext extends TestWatcher implements Cosel Map<String, String> topiaProperties = configuration.getTopiaProperties(); applicationContext = new CoselmarTopiaApplicationContext(topiaProperties); + {//Init some users + CoselmarTopiaPersistenceContext persistenceContext = newPersistenceContext(); + CoselmarServicesContext serviceContext = newServiceContext(persistenceContext, Locale.FRANCE); + serviceContext.newService(InitialisationService.class).createDefaultUsers(); + persistenceContext.close(); + } + } @Override diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/QuestionsWebServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/QuestionsWebServiceTest.java new file mode 100644 index 0000000..f315b76 --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/QuestionsWebServiceTest.java @@ -0,0 +1,299 @@ +package fr.ifremer.coselmar.services; + +/* + * #%L + * Coselmar :: Rest Services + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2014 Ifremer, Code Lutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import fr.ifremer.coselmar.beans.QuestionBean; +import fr.ifremer.coselmar.converter.JsonHelper; +import org.apache.http.HttpResponse; +import org.apache.http.client.fluent.Request; +import org.apache.http.client.fluent.Response; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class QuestionsWebServiceTest extends AbstractCoselmarWebServiceTest { + + protected FakeCoselmarServicesContext serviceContext; + + protected FakeCoselmarServicesContext getServiceContext() { + + if (serviceContext == null) { + serviceContext = application.newServiceContext(application.newPersistenceContext(), Locale.FRANCE); + } + + return serviceContext; + } + + @Test + public void testSearchQuestions() throws Exception { + + //First : login ! + Request loginRequest = createRequest("/v1/users/login") + .addParameter("mail", "default.supervisor@temporary.coselmar") + .addParameter("password", "manager1234") + .Post(); + + Response loginResponse = loginRequest.execute(); + String loginContent = loginResponse.returnContent().asString(); + Assert.assertNotNull(loginContent); + showTestResult(loginContent); + + Gson gson = new Gson(); + Map<String, String> loginMap = gson.fromJson(loginContent, Map.class); + + String supervisorToken = loginMap.get("jwt"); + + // Create first document + String documentOneTitle = "Ceci est le titre"; + Request addQuestionsRequest = createRequest("/v1/questions") + .addParameter("question", + "{title: '" + documentOneTitle + "', summary: 'ceci est le resume'," + + " submissionDate: '" + (new Date()).getTime() + "'," + + " themes: ['test', 'questionOne'], type: 'question'," + + " privacy : 'PUBLIC' }") + .Post() + .addHeader("Authorization", "Bearer " + supervisorToken); + + HttpResponse addQuestionResponse = addQuestionsRequest.execute().returnResponse(); + Assert.assertEquals(200, addQuestionResponse.getStatusLine().getStatusCode()); + + // Create second document + String documentTwoTitle = "There s someone missing. The question s Who?"; + addQuestionsRequest = createRequest("/v1/questions") + .addParameter("question", + "{title: '" + documentTwoTitle + "'," + + " summary: 'Something old, Something new, Something borrowed, Something blue.'," + + " submissionDate: '" + (new Date()).getTime() + "'," + + " themes: ['big bang two', 'Pandorica', 'River', 'Universe'], type: 'question'," + + " privacy : 'PUBLIC' }") + .Post() + .addHeader("Authorization", "Bearer " + supervisorToken); + + addQuestionResponse = addQuestionsRequest.execute().returnResponse(); + Assert.assertEquals(200, addQuestionResponse.getStatusLine().getStatusCode()); + + // And a third document ! + String documentThreeTitle = "Private things"; + addQuestionsRequest = createRequest("/v1/questions") + .addParameter("question", + "{title: '" + documentThreeTitle + "'," + + " summary: 'This question is private, zero access for others'," + + " submissionDate: '" + (new Date()).getTime() + "'," + + " themes: ['Universe', 'test'], type: 'question'," + + " privacy : 'PRIVATE' }") + .Post() + .addHeader("Authorization", "Bearer " + supervisorToken); + + addQuestionResponse = addQuestionsRequest.execute().returnResponse(); + Assert.assertEquals(200, addQuestionResponse.getStatusLine().getStatusCode()); + + // First search : as supervisor, no search bean : retrieve the three documents + Request searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + Response searchResponse = searchRequest.execute(); + String searchResultContent = searchResponse.returnContent().asString(); + JsonHelper jsonHelper = new JsonHelper(true); + List<QuestionBean> result = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(3, result.size()); + Assert.assertNotNull(result.get(0)); + Assert.assertNotNull(result.get(1)); + Assert.assertNotNull(result.get(2)); + + + // Second search : as supervisor, searchBean only with public privacy : retrieve two documents (one and two) + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public'}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + List<QuestionBean> publicPrivacyResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(2, publicPrivacyResult.size()); + List<String> expectedTitles = Arrays.asList(documentOneTitle, documentTwoTitle); + Assert.assertTrue(expectedTitles.contains(publicPrivacyResult.get(0).getTitle())); + Assert.assertTrue(expectedTitles.contains(publicPrivacyResult.get(1).getTitle())); + + + // Third search : as supervisor, searchBean with public privacy and test keywords : retrieve documentOne + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public', keywords : ['test']}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + List<QuestionBean> publicTestResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(1, publicTestResult.size()); + Assert.assertEquals(documentOneTitle, publicTestResult.get(0).getTitle()); + + + // Fourth search : as supervisor, searchBean with test and pandorica keywords : retrieve nothing + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public', keywords : ['test', 'pandorica']}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + List<QuestionBean> shouldNoResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertTrue(shouldNoResult.isEmpty()); + + + // Fifth search : as supervisor, searchBean with test keyword : retrieve documentOne and documentThree + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{keywords : ['test']}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + List<QuestionBean> testKeywordResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(2, testKeywordResult.size()); + expectedTitles = Arrays.asList(documentOneTitle, documentThreeTitle); + Assert.assertTrue(expectedTitles.contains(testKeywordResult.get(0).getTitle())); + Assert.assertTrue(expectedTitles.contains(testKeywordResult.get(1).getTitle())); + + + // Sixth search : as supervisor, searchBean with test and universe keywords : retrieve documentThree + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{keywords : ['test', 'Universe']}") + .Get() + .addHeader("Authorization", "Bearer " + supervisorToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + List<QuestionBean> testUniverseKeywordsResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(1, testUniverseKeywordsResult.size()); + Assert.assertEquals(documentThreeTitle, testUniverseKeywordsResult.get(0).getTitle()); + + + + //Have login for an expert ! + loginRequest = createRequest("/v1/users/login") + .addParameter("mail", "lambda.expert@temporary.coselmar") + .addParameter("password", "manager1234") + .Post(); + + loginResponse = loginRequest.execute(); + loginContent = loginResponse.returnContent().asString(); + Assert.assertNotNull(loginContent); + showTestResult(loginContent); + + loginMap = gson.fromJson(loginContent, Map.class); + + String expertToken = loginMap.get("jwt"); + + // Seventh search : as expert, no search bean : retrieve the two public documents + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{}") + .Get() + .addHeader("Authorization", "Bearer " + expertToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + result = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(2, result.size()); + expectedTitles = Arrays.asList(documentOneTitle, documentTwoTitle); + Assert.assertTrue(expectedTitles.contains(result.get(0).getTitle())); + + + // Eighth search : as expert, searchBean only with public privacy : retrieve two documents (one and two) (same as seventh) + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public'}") + .Get() + .addHeader("Authorization", "Bearer " + expertToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + publicPrivacyResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(2, publicPrivacyResult.size()); + expectedTitles = Arrays.asList(documentOneTitle, documentTwoTitle); + Assert.assertTrue(expectedTitles.contains(publicPrivacyResult.get(0).getTitle())); + Assert.assertTrue(expectedTitles.contains(publicPrivacyResult.get(1).getTitle())); + + + // Ninth search : as expert, searchBean with public privacy and test keywords : retrieve documentOne + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public', keywords : ['test']}") + .Get() + .addHeader("Authorization", "Bearer " + expertToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + publicTestResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertEquals(1, publicTestResult.size()); + Assert.assertEquals(documentOneTitle, publicTestResult.get(0).getTitle()); + + + // Tenth search : as supervisor, searchBean with test and pandorica keywords : retrieve nothing + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{privacy : 'public', keywords : ['test', 'pandorica']}") + .Get() + .addHeader("Authorization", "Bearer " + expertToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + shouldNoResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertTrue(shouldNoResult.isEmpty()); + + + // Eleventh search : as expert, searchBean with pandorica keyword : retrieve nothing (documentThree is private) + searchRequest = createRequest("/v1/questions") + .addParameter("searchBean", "{keywords : ['pandorica']}") + .Get() + .addHeader("Authorization", "Bearer " + expertToken); + + searchResponse = searchRequest.execute(); + searchResultContent = searchResponse.returnContent().asString(); + shouldNoResult = jsonHelper.fromJson(searchResultContent, new TypeToken<ArrayList<QuestionBean>>(){}.getType()); + + Assert.assertTrue(shouldNoResult.isEmpty()); + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.