Author: fdesbois Date: 2010-05-27 13:35:27 +0200 (Thu, 27 May 2010) New Revision: 3018 Url: http://chorem.org/repositories/revision/pollen/3018 Log: - Repair mistake in query test - Implement canVote method + JUnit test for GROUP case Added: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java trunk/pollen-business/src/main/xmi/pollen.properties trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java trunk/pollen-business/src/test/resources/log4j.properties Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-27 09:27:44 UTC (rev 3017) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-05-27 11:35:27 UTC (rev 3018) @@ -1,16 +1,21 @@ package org.chorem.pollen.service; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.chorem.pollen.EntityQueryProperty; import org.chorem.pollen.PollenBusinessException; import org.chorem.pollen.PollenContext; import org.chorem.pollen.PollenDAOHelper; import org.chorem.pollen.PollenException; +import org.chorem.pollen.PollenUtils; import org.chorem.pollen.bean.Filter; +import org.chorem.pollen.common.PollType; import org.chorem.pollen.entity.Choice; import org.chorem.pollen.entity.Poll; import org.chorem.pollen.entity.PollAccount; import org.chorem.pollen.entity.PollAccountDAO; import org.chorem.pollen.entity.PollAccountImpl; +import org.chorem.pollen.entity.PollDAO; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.Vote; import org.chorem.pollen.entity.VoteDAO; @@ -18,6 +23,7 @@ import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaEntity; import java.util.List; @@ -29,6 +35,8 @@ */ public class ServiceVoteImpl extends ServiceVoteAbstract { + private static final Log log = LogFactory.getLog(ServiceVoteImpl.class); + private PollenContext context; public void setContext(PollenContext context) { @@ -52,9 +60,98 @@ } @Override - protected boolean executeCanVote(TopiaContext transaction, Poll poll, + protected boolean executeCanVote(TopiaContext transaction, + Poll poll, PollAccount participant) throws Exception { - return false; + + if (log.isDebugEnabled()) { + log.debug("Poll started : " + poll.isStarted()); + log.debug("Poll finished : " + poll.isFinished()); + log.debug("Poll closed : " + poll.getClosed()); + } + boolean result = true; + if (!poll.isRunning()) { + if (log.isInfoEnabled()) { + log.info("Poll is no longer running... Participant can't vote"); + } + result = false; + } else if (poll.getPollType().isRestrictedOrGroup()) { + if (log.isInfoEnabled()) { + log.info("Poll is restricted... Participant need to be identified"); + } + + PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); + + EntityQueryProperty participantProperty = + PollenUtils.getEntityQueryProperty("A"); + EntityQueryProperty pollProperty = + PollenUtils.getEntityQueryProperty("P"); + EntityQueryProperty groupProperty = + PollenUtils.getEntityQueryProperty("G"); + + TopiaQuery query = dao.createQuery(participantProperty.name()). + addFrom(Poll.class, pollProperty.name()). + addEquals(pollProperty.name(), poll); + + // By default the parentCollection is pollAccount in poll + String parentCollectionProperty = + pollProperty.nameProperty(Poll.POLL_ACCOUNT); + + if (poll.getPollType().isGroup()) { + // Add the group into the query + query.addFrom(PollAccount.class, groupProperty.name()). + // It's not really necessary, but better to be sure + addEquals(groupProperty.nameProperty(PollAccount.LIST), + Boolean.TRUE). + // Add link between the group and the poll + addInElements(groupProperty.name(), parentCollectionProperty); + + // In case of group, the parentCollection is the child + // pollAccount from the group + parentCollectionProperty = + groupProperty.nameProperty(PollAccount.CHILD); + } + + // Add link between the participant to find and his parent + query.addInElements(participantProperty.name(), parentCollectionProperty). + addEquals(participantProperty.name(), participant). + addWhere(pollProperty.nameProperty(Poll.CREATOR), + TopiaQuery.Op.NEQ, + participant + ); + +// TopiaQuery query = dao.createQuery("participant"). +// addFrom(Poll.class, "poll"). +// addEquals("poll", poll); +// +// // By default the parentCollection is pollAccount in poll +// String parentCollectionProperty = +// TopiaQuery.getProperty("poll", Poll.POLL_ACCOUNT); +// +// if (poll.getPollType().isGroup()) { +// query.addFrom(PollAccount.class, "group"). +// // It's not really necessary, but better to be sure +// addEquals("group." + PollAccount.LIST, Boolean.TRUE). +// addInElements("group", parentCollectionProperty); +// +// // In case of group, the parentCollection is the child +// // pollAccount from the list +// parentCollectionProperty = +// TopiaQuery.getProperty("group", PollAccount.CHILD); +// } +// +// // Add link between the Account to find and his parent +// query.addInElements("participant", parentCollectionProperty). +// addEquals("participant." + TopiaEntity.TOPIA_ID, +// participant.getTopiaId()); + + if (log.isDebugEnabled()) { + log.debug("Query : " + query); + } + + result = dao.existByQuery(query); + } + return result; } @Override Modified: trunk/pollen-business/src/main/xmi/pollen.properties =================================================================== --- trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-27 09:27:44 UTC (rev 3017) +++ trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-27 11:35:27 UTC (rev 3018) @@ -1,5 +1,7 @@ # Pr\u00e9cise l'ent\u00eate de l'ensemble des fichiers g\u00e9n\u00e9r\u00e9s model.tagvalue.copyright=/* *##%\n Copyright (C) 2009 Pollen\n *##%*/ +# toString are particularly difficult to use because of lazy initialization +model.tagvalue.notGenerateToString=true #model.tagvalue.dbSchema=Pollen model.tagvalue.java.lang.String=text model.tagvalue.exceptionClass=org.chorem.pollen.PollenException Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java 2010-05-27 09:27:44 UTC (rev 3017) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java 2010-05-27 11:35:27 UTC (rev 3018) @@ -84,7 +84,7 @@ Assert.assertEquals(result.fullQuery(), "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + FavoriteList.class.getName() + " L" + - " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + ") ORDER BY P." + FavoriteParticipant.NAME); log.info("test2 : orderBy not set (default to topiaCreateDate desc)"); @@ -98,7 +98,7 @@ Assert.assertEquals(result.fullQuery(), "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + FavoriteList.class.getName() + " L" + - " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + ") ORDER BY P." + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); log.info("test3 : orderBy name and email desc"); @@ -113,7 +113,7 @@ Assert.assertEquals(result.fullQuery(), "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + FavoriteList.class.getName() + " L" + - " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + " WHERE P IN elements(L." + FavoriteList.FAVORITE_PARTICIPANT + ") ORDER BY P." + FavoriteParticipant.NAME + ", P." + FavoriteParticipant.EMAIL + " desc"); Added: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java 2010-05-27 11:35:27 UTC (rev 3018) @@ -0,0 +1,80 @@ +package org.chorem.pollen.service; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PollenBusinessException; +import org.chorem.pollen.PollenDAOHelper; +import org.chorem.pollen.entity.Poll; +import org.chorem.pollen.entity.PollAccount; +import org.chorem.pollen.entity.PollAccountDAO; +import org.chorem.pollen.entity.UserAccount; +import org.chorem.pollen.test.AbstractServiceTest; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; + +import java.util.Iterator; + +/** + * Created: 27 mai 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + * @version $Id$ + */ +public class ServiceVoteImplTest extends AbstractServiceTest { + + private static final Log log = LogFactory.getLog(ServiceVoteImplTest.class); + + @Override + protected void init() throws Exception { + getServiceVote(); + } + + @Test + public void testCanVoteForGroupPoll() + throws PollenBusinessException, TopiaException { + + /** PREPARE DATA **/ + UserAccount user = createUser(false); + + Poll pollCreated = createGroupPoll("Poll", user, 3, "Group1", "Group2"); + + PollAccount group1 = pollCreated.getPollAccount().iterator().next(); + PollAccount existingParticipant = group1.getChild().iterator().next(); + + // Reload the poll to really be unconnected (no loading of PollAccount) + TopiaContext transaction = beginTransaction(); + Poll poll = null; + try { + poll = PollenDAOHelper.getPollDAO(transaction). + findByTopiaId(pollCreated.getTopiaId()); + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + /** EXEC METHOD **/ + log.info("test 1 : participant found and can vote"); + boolean result = serviceVote.canVote(poll, existingParticipant); + Assert.assertTrue(result); + + log.info("test 2 : poll creator can't vote"); + result = serviceVote.canVote(poll, poll.getCreator()); + Assert.assertFalse(result); + + log.info("test 3 : existingParticipant can't vote, poll is closed"); + poll.setClosed(true); + + transaction = beginTransaction(); + try { + PollenDAOHelper.getPollDAO(transaction).update(poll); + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + result = serviceVote.canVote(poll, existingParticipant); + Assert.assertFalse(result); + } +} Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceVoteImplTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-27 09:27:44 UTC (rev 3017) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-27 11:35:27 UTC (rev 3018) @@ -1,25 +1,26 @@ package org.chorem.pollen.test; -import java.io.IOException; -import java.io.InputStream; -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenBusinessException; import org.chorem.pollen.PollenContext; import org.chorem.pollen.PollenContextImpl; import org.chorem.pollen.PollenDAOHelper; +import org.chorem.pollen.common.PollType; import org.chorem.pollen.entity.FavoriteList; import org.chorem.pollen.entity.FavoriteListDAO; import org.chorem.pollen.entity.FavoriteParticipant; import org.chorem.pollen.entity.FavoriteParticipantDAO; +import org.chorem.pollen.entity.Poll; +import org.chorem.pollen.entity.PollAccount; +import org.chorem.pollen.entity.PollAccountDAO; +import org.chorem.pollen.entity.PollDAO; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.UserAccountDAO; import org.chorem.pollen.service.ServiceFavoriteImpl; import org.chorem.pollen.service.ServicePollImpl; import org.chorem.pollen.service.ServiceUserImpl; +import org.chorem.pollen.service.ServiceVoteImpl; import org.junit.After; import org.junit.Ignore; import org.junit.Rule; @@ -31,16 +32,23 @@ import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.util.ApplicationConfig; +import java.io.IOException; +import java.io.InputStream; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Properties; + /** * TestManager - * + * <p/> * Created: 24 févr. 2010 * * @author fdesbois * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ + * <p/> + * Mise a jour: $Date$ + * par : $Author$ */ @Ignore public abstract class AbstractServiceTest { @@ -55,6 +63,8 @@ protected ServicePollImpl servicePoll; + protected ServiceVoteImpl serviceVote; + @Rule public TestName rule = new TestName() { @@ -97,7 +107,7 @@ // Set currentDate to 23/02/2010 for tests Calendar calendar = new GregorianCalendar(2010, 1, 23, 0, 0, 0); - ((PollenContextImpl)getContext()).setCurrentDate(calendar.getTime()); + ((PollenContextImpl) getContext()).setCurrentDate(calendar.getTime()); } @After @@ -140,6 +150,14 @@ return servicePoll; } + public ServiceVoteImpl getServiceVote() { + if (serviceVote == null) { + serviceVote = new ServiceVoteImpl(); + serviceVote.setContext(getContext()); + } + return serviceVote; + } + public TopiaContext beginTransaction() throws TopiaException { return getContext().beginTransaction(); } @@ -157,13 +175,9 @@ // } /** - * Create a user :<br /> - * <ul> - * <li>login : homer</li> - * <li>email : homer@simpson.us</li> - * <li>password : wouhou</li> - * </ul> - * You can decide if this user is an admin using {@code admin} argument. + * Create a user :<br /> <ul> <li>login : homer</li> <li>email : + * homer@simpson.us</li> <li>password : wouhou</li> </ul> You can decide if + * this user is an admin using {@code admin} argument. * * @param admin flag to create the user as an admin * @return the new UserAccount created @@ -184,7 +198,7 @@ TopiaContext transaction = getContext().beginTransaction(); try { UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(transaction); + PollenDAOHelper.getUserAccountDAO(transaction); UserAccount findUser = dao.findByLogin(user.getLogin()); return findUser; @@ -210,7 +224,7 @@ } public FavoriteParticipant createFavoriteParticipant(String name, - String email, FavoriteList list) throws TopiaException { + String email, FavoriteList list) throws TopiaException { TopiaContext transaction = beginTransaction(); try { FavoriteParticipantDAO dao = @@ -243,4 +257,67 @@ } } + protected Poll createPoll(TopiaContext transaction, + String title, UserAccount user, PollType type) + throws TopiaException { + + PollAccountDAO accountDAO = + PollenDAOHelper.getPollAccountDAO(transaction); + + PollAccount creator = accountDAO.create(context.createPollenUrlId()); + creator.setName(user.getDisplayName()); + creator.setEmail(user.getEmail()); + creator.setUserAccount(user); + creator.setAdmin(true); + + PollDAO pollDAO = PollenDAOHelper.getPollDAO(transaction); + + Poll poll = pollDAO.create(context.createPollenUrlId()); + poll.setCreator(creator); + poll.setTitle(title); + poll.setPollType(type); + + return poll; + } + + public Poll createGroupPoll(String title, + UserAccount user, + int nbParticipantsByGroup, + String... groupNames) + throws TopiaException { + + TopiaContext transaction = beginTransaction(); + try { + + Poll poll = createPoll(transaction, title, user, PollType.GROUP); + + PollAccountDAO accountDAO = + PollenDAOHelper.getPollAccountDAO(transaction); + + for (String groupName : groupNames) { + PollAccount group = accountDAO.create(context.createPollenUrlId()); + group.setName(groupName); + group.setList(true); + + for (int i = 1; i <= nbParticipantsByGroup; i++) { + PollAccount participant = + accountDAO.create(context.createPollenUrlId()); + participant.setName(groupName + "_participant" + i); + participant.setEmail(groupName + "_email" + i + "@domain.org"); + participant.setWeight(1.); + group.addChild(participant); + } + + poll.addPollAccount(group); + } + + transaction.commitTransaction(); + + return poll; + + } finally { + transaction.closeContext(); + } + } + } Modified: trunk/pollen-business/src/test/resources/log4j.properties =================================================================== --- trunk/pollen-business/src/test/resources/log4j.properties 2010-05-27 09:27:44 UTC (rev 3017) +++ trunk/pollen-business/src/test/resources/log4j.properties 2010-05-27 11:35:27 UTC (rev 3018) @@ -8,7 +8,7 @@ log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d [%p] %c{2} %m%n -log4j.logger.org.chorem.pollen=info +log4j.logger.org.chorem.pollen=debug log4j.logger.org.chorem.pollen.business.PollenContextImpl=warn log4j.logger.org.chorem.pollen.business.services.SendMail=warn