r2996 - in trunk/pollen-business/src: main/java/org/chorem/pollen main/java/org/chorem/pollen/entity main/java/org/chorem/pollen/service main/resources/i18n main/xmi test/java/org/chorem/pollen test/java/org/chorem/pollen/service test/java/org/chorem/pollen/test
Author: fdesbois Date: 2010-05-14 14:06:15 +0200 (Fri, 14 May 2010) New Revision: 2996 Url: http://chorem.org/repositories/revision/pollen/2996 Log: - Use rule in tests to execute start with methodName - Add tests for createPoll : unit and integration tests - Add some methods for instantiation Added: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantHelper.java trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServicePollImplTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/ trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java Removed: trunk/pollen-business/src/test/java/org/chorem/pollen/business/ Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/PollenBusinessException.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java trunk/pollen-business/src/main/resources/i18n/pollen-business-en_GB.properties trunk/pollen-business/src/main/resources/i18n/pollen-business-fr_FR.properties trunk/pollen-business/src/main/xmi/pollen.properties trunk/pollen-business/src/main/xmi/pollen.zargo trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceFavoriteImplTest.java trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceUserImplTest.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/PollenBusinessException.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/PollenBusinessException.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/PollenBusinessException.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -51,17 +51,19 @@ FAVORITE_LIST_NAME_EXIST( n_("pollen.exception.favorite_list_name_exist")), /** - * Exception when favorite participant name ($2) and email ($3) are + * Exception when participant name ($2) and email ($3) are * already defined for the current list ($1). **/ - FAVORITE_PARTICIPANT_EXIST( - n_("pollen.exception.favorite_participant_exist")), + PARTICIPANT_EXIST(n_("pollen.exception.participant_exist")), + /** * Exception when favorite participant name ($2) with no email is * already defined for the current list ($1). **/ - FAVORITE_PARTICIPANT_EXIST_WITHOUT_EMAIL( - n_("pollen.exception.favorite_participant_exist_without_email")); + PARTICIPANT_EXIST_WITHOUT_EMAIL( + n_("pollen.exception.participant_exist_without_email")), + /** Exception when poll already exist at creation **/ + POLL_EXIST(n_("pollen.exception.poll_exist")); private String message; Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListImpl.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListImpl.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -1,5 +1,7 @@ package org.chorem.pollen.entity; +import org.chorem.pollen.PollenBusinessException; + import java.util.ArrayList; import java.util.Collection; @@ -21,7 +23,18 @@ } @Override - public void addParticipant(Participant participant) { + public void addParticipant(Participant participant) throws PollenBusinessException { + String name = participant.getName(); + String email = participant.getEmail(); + + ParticipantHelper.checkParticipantExist(this, name, email); + + FavoriteParticipant favorite = new FavoriteParticipantImpl(); + favorite.setName(participant.getName()); + favorite.setEmail(participant.getEmail()); + favorite.setWeight(participant.getWeight()); + favorite.setFavoriteList(this); + addFavoriteParticipant(favorite); } @Override Added: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantHelper.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantHelper.java (rev 0) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantHelper.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -0,0 +1,47 @@ +package org.chorem.pollen.entity; + +import org.chorem.pollen.PollenBusinessException; + +/** + * Helper classes for common Participant manipulations. + * + * @author fdesbois <fdesbois@codelutin.com> + * @version $Id$ + * @see Participant + * @see ParticipantList + */ +public class ParticipantHelper { + + /** + * Check the existence of participant from a {@code list}. The existence is + * tested on {@code name} and {@code email}. The {@code email} an be null. + * + * @param list ParticipantList which contains a list of Participant + * @param name Name of the participant to check + * @param email Email of the participant to check + * @throws PollenBusinessException if name and email correspond to an + * existing participant in the list. + */ + public static void checkParticipantExist( + ParticipantList list, String name, String email) + throws PollenBusinessException { + for (Participant participant : list.getParticipants()) { + boolean emailNull = (email == null && participant.getEmail() == null); + boolean emailCheck = emailNull || + (email != null && email.equals(participant.getEmail())); + + if (name.equals(participant.getName()) && emailCheck) { + if (emailNull) { + throw new PollenBusinessException( + PollenBusinessException.PollenExceptionType.PARTICIPANT_EXIST_WITHOUT_EMAIL, + list.getName(), name); + } else { + throw new PollenBusinessException( + PollenBusinessException.PollenExceptionType.PARTICIPANT_EXIST, + list.getName(), name, email); + } + } + } + } + +} Property changes on: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantHelper.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountImpl.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountImpl.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -1,5 +1,8 @@ package org.chorem.pollen.entity; +import org.chorem.pollen.PollenBinderHelper; +import org.chorem.pollen.PollenBusinessException; + import java.util.ArrayList; import java.util.Collection; @@ -18,14 +21,30 @@ return null; } Collection<Participant> results = new ArrayList<Participant>(); - for (PollAccount account : getChild()) { - results.add(account); - } + if (getChild() != null) { + for (PollAccount account : getChild()) { + results.add(account); + } + } return results; } @Override - public void addParticipant(Participant participant) { + public void addParticipant(Participant participant) throws PollenBusinessException { + if (!list) { + throw new UnsupportedOperationException( + "Unable to add a participant from a child account"); + } + String name = participant.getName(); + String email = participant.getEmail(); + + ParticipantHelper.checkParticipantExist(this, name, email); + + PollAccount account = new PollAccountImpl(); + account.setName(name); + account.setEmail(email); + account.setWeight(participant.getWeight()); + addChild(account); } @Override Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollImpl.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollImpl.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -70,4 +70,14 @@ setVoteCounting(voteCountingType.ordinal()); } + @Override + public Choice addNewChoice(String name, String description) { + // check exist + Choice choice = new ChoiceImpl(); + choice.setName(name); + choice.setDescription(description); + choice.setChoiceType(getChoiceType()); + addChoice(choice); + return choice; + } } Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -142,12 +142,12 @@ // The error type (message) depends on email nullity if (participant.getEmail() == null) { throw new PollenBusinessException( - PollenExceptionType.FAVORITE_PARTICIPANT_EXIST_WITHOUT_EMAIL, + PollenExceptionType.PARTICIPANT_EXIST_WITHOUT_EMAIL, list.getName(), participant.getName()); } else { throw new PollenBusinessException( - PollenExceptionType.FAVORITE_PARTICIPANT_EXIST, + PollenExceptionType.PARTICIPANT_EXIST, list.getName(), participant.getName(), participant.getEmail()); Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -76,6 +76,9 @@ creator.setUserAccount(user); } poll.setCreator(creator); + // Generate Uid to have a unique poll. Will avoid multi-submit, existing + // poll will be check by its uid. + poll.setUid(context.createPollenUrlId()); return poll; } @@ -87,23 +90,37 @@ @Override protected void executeCreatePoll(TopiaContext transaction, List<Object> errorArgs, Poll poll, Collection<ParticipantList> lists) - throws TopiaException, IllegalArgumentException { + throws TopiaException, + IllegalArgumentException, + PollenBusinessException { errorArgs.add(poll.getTitle()); errorArgs.add(poll.getUid()); PollDAO dao = PollenDAOHelper.getPollDAO(transaction); + if (dao.existByNaturalId(poll.getUid())) { + throw new PollenBusinessException( + PollenBusinessException.PollenExceptionType.POLL_EXIST); + } + // Create newPoll and copy simple properties from poll source Poll newPoll = createBasicPoll(transaction, poll); - // Create accounts depends on pollType and lists argument - List<PollAccount> accounts = createPollAccounts(transaction, - newPoll.getPollType(), lists); + // Create accounts only for non free poll + if (!newPoll.getPollType().isFree()) { + // Create accounts depends on pollType and lists argument + List<PollAccount> accounts = createPollAccounts(transaction, + newPoll.getPollType(), lists); + newPoll.setPollAccount(accounts); + } + // Create newPoll choices List<Choice> choices = createPollChoices(transaction, poll.getChoice()); + newPoll.setChoice(choices); + dao.update(newPoll); transaction.commitTransaction(); @@ -117,8 +134,10 @@ PollenDAOHelper.getPollAccountDAO(transaction); // Create newPoll and copy simple properties from poll source - Poll newPoll = dao.create(context.createPollenUrlId()); + Poll newPoll = dao.create(source.getUid()); + // Copy all properties instead of Uid (already set at creation), + // Closed (not used in this case) and Creator (managed separately) PollenBinderHelper.getSimpleTopiaBinder(Poll.class). copyExcluding(source, newPoll, Poll.UID, @@ -132,8 +151,9 @@ copy(source.getCreator(), creator, PollAccount.NAME, PollAccount.EMAIL, - PollAccount.WEIGHT, - PollAccount.ADMIN); +// PollAccount.WEIGHT, + PollAccount.ADMIN, + PollAccount.USER_ACCOUNT); accountDAO.update(creator); newPoll.setCreator(creator); @@ -178,7 +198,7 @@ createPersonAccounts(accountDAO, list); accountList.setChild(accounts); - // Add the accountList to the newPoll + // Add the accountList to the results results.add(accountList); } } @@ -306,4 +326,18 @@ throw new UnsupportedOperationException("Not supported yet."); } + @Override + protected ParticipantList executeGetNewPollList() { + PollAccount list = new PollAccountImpl(); + list.setList(true); + list.setChild(new ArrayList<PollAccount>()); + return list; + } + + @Override + protected Participant executeGetNewPollParticipant() { + PollAccount participant = new PollAccountImpl(); + return participant; + } + } Modified: trunk/pollen-business/src/main/resources/i18n/pollen-business-en_GB.properties =================================================================== --- trunk/pollen-business/src/main/resources/i18n/pollen-business-en_GB.properties 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/resources/i18n/pollen-business-en_GB.properties 2010-05-14 12:06:15 UTC (rev 2996) @@ -33,6 +33,8 @@ pollen.error.servicePoll.getAllPolls= pollen.error.servicePoll.getComments= pollen.error.servicePoll.getNewPoll= +pollen.error.servicePoll.getNewPollList= +pollen.error.servicePoll.getNewPollParticipant= pollen.error.servicePoll.getPoll= pollen.error.servicePoll.getPollForResults= pollen.error.servicePoll.getPollForUpdate= @@ -79,6 +81,9 @@ pollen.exception.favorite_participant_exist= pollen.exception.favorite_participant_exist_without_email= pollen.exception.load_configuration= +pollen.exception.participant_exist= +pollen.exception.participant_exist_without_email= +pollen.exception.poll_exist= pollen.exception.poll_not_exist=No such poll exists. Please make sure that you are using the correct link and copy it completely into your browser's address field. pollen.exception.smtp_not_available= pollen.exception.user_email_exist= Modified: trunk/pollen-business/src/main/resources/i18n/pollen-business-fr_FR.properties =================================================================== --- trunk/pollen-business/src/main/resources/i18n/pollen-business-fr_FR.properties 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/resources/i18n/pollen-business-fr_FR.properties 2010-05-14 12:06:15 UTC (rev 2996) @@ -32,6 +32,8 @@ pollen.error.servicePoll.getAllPolls= pollen.error.servicePoll.getComments= pollen.error.servicePoll.getNewPoll= +pollen.error.servicePoll.getNewPollList= +pollen.error.servicePoll.getNewPollParticipant= pollen.error.servicePoll.getPoll= pollen.error.servicePoll.getPollForResults= pollen.error.servicePoll.getPollForUpdate= @@ -75,9 +77,10 @@ pollen.error.serviceVote.getVotesByPoll= pollen.error.serviceVote.hasAlreadyVoted= pollen.exception.favorite_list_name_exist=La liste %1$s existe d\u00E9j\u00E0 pour l'utilisateur %2$s -pollen.exception.favorite_participant_exist=La liste %1$s contient d\u00E9j\u00E0 un utilisateur nomm\u00E9 %2$s avec un email %3$s -pollen.exception.favorite_participant_exist_without_email=La liste %1$s contient d\u00E9j\u00E0 un utilisateur nomm\u00E9 %2$s avec aucun email pollen.exception.load_configuration=La configuration n'a pas \u00E9t\u00E9 charg\u00E9e correctement \! Veuillez v\u00E9rifier le d\u00E9marrage de l'application. +pollen.exception.participant_exist=La liste %1$s contient d\u00E9j\u00E0 un votant nomm\u00E9 %2$s avec un email %3$s +pollen.exception.participant_exist_without_email=La liste %1$s contient d\u00E9j\u00E0 un votant nomm\u00E9 %2$s avec aucun email +pollen.exception.poll_exist= pollen.exception.poll_not_exist=Il n'y a pas de sondage \u00E0 cette adresse. Veuillez verifier que vous utilisez le lien correcte et copiez-le compl\u00E8tement dans le champ d'adresse de votre navigateur. pollen.exception.smtp_not_available=Impossible d'envoyer un email \u00E0 %1$s. Serveur smtp indisponible pour l'envoi d'email, veuillez contacter un administrateur. pollen.exception.user_email_exist=Un utilisateur est d\u00E9j\u00E0 enregistr\u00E9 avec cet email. Modified: trunk/pollen-business/src/main/xmi/pollen.properties =================================================================== --- trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/main/xmi/pollen.properties 2010-05-14 12:06:15 UTC (rev 2996) @@ -1,4 +1,4 @@ -# Pr\u00E9cise l'ent\u00EAte de l'ensemble des fichiers g\u00E9n\u00E9r\u00E9s +# Pr\u00e9cise l'ent\u00eate de l'ensemble des fichiers g\u00e9n\u00e9r\u00e9s model.tagvalue.copyright=/* *##%\n Copyright (C) 2009 Pollen\n *##%*/ #model.tagvalue.dbSchema=Pollen model.tagvalue.java.lang.String=text @@ -17,3 +17,6 @@ org.chorem.pollen.entity.FavoriteParticipant.attribute.name.tagvalue.naturalId=true org.chorem.pollen.entity.FavoriteParticipant.attribute.email.tagvalue.naturalId=true org.chorem.pollen.entity.FavoriteParticipant.attribute.email.tagvalue.notNull=false + +org.chorem.pollen.entity.Poll.attribute.uid.tagvalue.naturalId=true +org.chorem.pollen.entity.Poll.attribute.creator.tagvalue.lazy=false Modified: trunk/pollen-business/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceFavoriteImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceFavoriteImplTest.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceFavoriteImplTest.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -6,7 +6,7 @@ import org.chorem.pollen.PollenBusinessException; import org.chorem.pollen.PollenBusinessException.PollenExceptionType; import org.chorem.pollen.PollenDAOHelper; -import org.chorem.pollen.business.AbstractServiceTest; +import org.chorem.pollen.test.AbstractServiceTest; import org.chorem.pollen.entity.FavoriteList; import org.chorem.pollen.entity.FavoriteParticipant; import org.chorem.pollen.entity.FavoriteParticipantDAO; @@ -27,9 +27,13 @@ private static final Log log = LogFactory.getLog(ServiceUserImplTest.class); + @Override + protected void init() throws Exception { + } + @Test public void testCreateFavoriteParticipant() throws Exception { - start("testCreateFavoriteParticipant"); + //start("testCreateFavoriteParticipant"); UserAccount user = createUser(false); @@ -56,7 +60,7 @@ try { getServiceFavorite().createFavoriteParticipant(participant); } catch (PollenBusinessException eee) { - Assert.assertEquals(PollenExceptionType.FAVORITE_PARTICIPANT_EXIST, + Assert.assertEquals(PollenExceptionType.PARTICIPANT_EXIST, eee.getType()); } @@ -89,7 +93,7 @@ @Test public void testUpdateFavoriteParticipant() throws Exception { - start("testUpdateFavoriteParticipant"); + //start("testUpdateFavoriteParticipant"); /** PREPARE DATA **/ UserAccount user = createUser(false); @@ -105,5 +109,4 @@ getServiceFavorite().updateFavoriteParticipant(participant); } - } Added: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServicePollImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServicePollImplTest.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServicePollImplTest.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -0,0 +1,324 @@ +package org.chorem.pollen.service; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PollenDAOHelper; +import org.chorem.pollen.test.AbstractServiceTest; +import org.chorem.pollen.common.ChoiceType; +import org.chorem.pollen.common.PollType; +import org.chorem.pollen.entity.FavoriteList; +import org.chorem.pollen.entity.Participant; +import org.chorem.pollen.entity.ParticipantList; +import org.chorem.pollen.entity.Poll; +import org.chorem.pollen.entity.PollAccount; +import org.chorem.pollen.entity.PollAccountImpl; +import org.chorem.pollen.entity.PollDAO; +import org.chorem.pollen.entity.PollImpl; +import org.chorem.pollen.entity.UserAccount; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.topia.TopiaContext; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Created: 14 mai 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + * @version $Id$ + */ +public class ServicePollImplTest extends AbstractServiceTest { + + private static final Log log = LogFactory.getLog(ServicePollImplTest.class); + + private UserAccount user; + + private PollAccount creator; + + private PollAccount list1; + + private FavoriteList list2; + + private Poll poll; + + //@Before + public void init() throws Exception { + // Initialize service and default user + getServicePoll(); + user = createUser(false); + + // Poll creator + creator = new PollAccountImpl(); + creator.setName(user.getDisplayName()); + creator.setEmail(user.getEmail()); + creator.setUserAccount(user); + creator.setAdmin(true); + + // Poll + poll = new PollImpl(); + poll.setCreator(creator); + poll.setTitle("Poll"); + poll.setUid(context.createPollenUrlId()); + + // Participant Lists + // First list : new list specific for poll restriction + list1 = new PollAccountImpl(); + list1.setName("List1"); + list1.setList(true); + + Participant participant1_1 = new PollAccountImpl(); + participant1_1.setName("participant1_1"); + participant1_1.setEmail("email1_1@domain.org"); + list1.addParticipant(participant1_1); + + Participant participant1_2 = new PollAccountImpl(); + participant1_2.setName("participant1_2"); + list1.addParticipant(participant1_2); + + // Second list : existing list from user favorites + list2 = createFavoriteList("list2", user); + Participant participant2_1 = createFavoriteParticipant( + "participant2_1", "email2_1@domain.org", list2); + Participant participant2_2 = createFavoriteParticipant( + "participant2_2", "email2_2@domain.org", list2); + Participant participant2_3 = createFavoriteParticipant( + "participant2_3", null, list2); + + list2 = findFavoriteList(list2.getId(), FavoriteList.FAVORITE_PARTICIPANT); + } + + @Test + public void testInternalCreateBasicPoll() throws Exception { + //start("testInternalCreateBasicPoll"); + + /** PREPARE DATA **/ + // done in init() method + + /** EXEC METHOD **/ + + log.info("test 1 : create poll with creator from existing user"); + + TopiaContext transaction = beginTransaction(); + Poll newPoll = null; + try { + newPoll = servicePoll.createBasicPoll(transaction, poll); + Assert.assertNotNull(newPoll); + Assert.assertNotNull(newPoll.getId()); + Assert.assertNotNull(newPoll.getUid()); + Assert.assertEquals("Poll", newPoll.getTitle()); + + PollAccount newCreatorAccount = newPoll.getCreator(); + Assert.assertNotNull(newCreatorAccount); + Assert.assertNotNull(newCreatorAccount.getUid()); + Assert.assertEquals(user, newCreatorAccount.getUserAccount()); + Assert.assertTrue(newCreatorAccount.getAdmin()); + + // Will save the poll and its creator + transaction.commitTransaction(); + + } finally { + transaction.closeContext(); + } + + // Verification + transaction = beginTransaction(); + try { + PollDAO dao = PollenDAOHelper.getPollDAO(transaction); + + Poll pollFound = dao.findById(newPoll.getId()); + Assert.assertNotNull(pollFound); + Assert.assertNotNull(pollFound.getCreator()); + Assert.assertEquals(user, pollFound.getCreator().getUserAccount()); + + } finally { + transaction.closeContext(); + } + + log.info("test 2 : create poll with new creator"); + poll.getCreator().setUserAccount(null); + + transaction = beginTransaction(); + newPoll = null; + try { + newPoll = servicePoll.createBasicPoll(transaction, poll); + PollAccount newCreatorAccount = newPoll.getCreator(); + Assert.assertNull(newCreatorAccount.getUserAccount()); + } finally { + transaction.closeContext(); + } + } + + @Test + public void testInternalCreatePollAccounts() throws Exception { + //start("testInternalCreatePollAccounts"); + + /** PREPARE DATA **/ + // list initializations in init() method + + Collection<ParticipantList> lists = new ArrayList<ParticipantList>(); + // First list : new list specific for poll restriction + lists.add(list1); + // Second list : existing list from user favorites + lists.add(list2); + + /** EXEC METHOD **/ + + log.info("test 1 : createPollAccounts from one new list : RESTRICTED"); + + + TopiaContext transaction = beginTransaction(); + try { + // Only the first list of the collection will be considered : list1 + // Create only person accounts + List<PollAccount> accounts = servicePoll.createPollAccounts( + transaction, PollType.RESTRICTED, lists); + + Assert.assertEquals(2, accounts.size()); + for (PollAccount account : accounts) { + Assert.assertFalse(account.getList()); + Assert.assertNotNull(account.getId()); + Assert.assertNotNull(account.getUid()); + // No link with user + Assert.assertNull(account.getUserAccount()); + } + + } finally { + transaction.closeContext(); + } + + log.info("test 2 : createPollAccounts from two lists : GROUP." + + " One of the list is an existing favorite one"); + + transaction = beginTransaction(); + try { + // Create only list accounts + List<PollAccount> accounts = servicePoll.createPollAccounts( + transaction, PollType.GROUP, lists); + + Assert.assertEquals(2, accounts.size()); + for (PollAccount account : accounts) { + Assert.assertTrue(account.getList()); + Assert.assertNotNull(account.getId()); + Assert.assertNotNull(account.getUid()); + Assert.assertNotNull(account.getName()); + // No link with user + Assert.assertNull(account.getUserAccount()); + // Check on list1 + if (account.getName().equals(list1.getName())) { + Assert.assertEquals(2, account.getChild().size()); + // Check on list2 + } else if (account.getName().equals(list2.getName())) { + Assert.assertEquals(3, account.getChild().size()); + // Child are person accounts + for (PollAccount person : account.getChild()) { + Assert.assertFalse(person.getList()); + Assert.assertNotNull(person.getId()); + Assert.assertNotNull(person.getUid()); + // No link with user + Assert.assertNull(person.getUserAccount()); + } + } + } + + } finally { + transaction.closeContext(); + } + } + + @Test + public void integrationCreatePollFree() throws Exception { + /** PREPARE DATA **/ + user = null; + poll = servicePoll.getNewPoll(user); + poll.getCreator().setName("homer"); + poll.setPollType(PollType.FREE); + poll.setChoiceType(ChoiceType.TEXT); + poll.addNewChoice("choice1", null); + poll.addNewChoice("choice2", "desc2"); + poll.addNewChoice("choice3", "desc3"); + + /** EXEC METHOD **/ + servicePoll.createPoll(poll, null); + + /** VERIFICATION RESULT **/ + TopiaContext transaction = beginTransaction(); + try { + // No accounts + type = FREE + 3 TEXT choices + PollDAO dao = PollenDAOHelper.getPollDAO(transaction); + Assert.assertTrue(dao.existByNaturalId(poll.getUid())); + Poll pollFound = dao.findByNaturalId(poll.getUid()); + + // Check pollType and accounts + Assert.assertEquals(PollType.FREE, pollFound.getPollType()); + Assert.assertEquals(0, pollFound.getPollAccount().size()); + + // Check choices + Assert.assertEquals(ChoiceType.TEXT, pollFound.getChoiceType()); + Assert.assertEquals(3, pollFound.getChoice().size()); + + // Check creator + Assert.assertNotNull(pollFound.getCreator()); + Assert.assertNull(pollFound.getCreator().getUserAccount()); + Assert.assertNotNull(pollFound.getCreator().getName()); + + } finally { + transaction.closeContext(); + } + } + + @Test + public void integrationCreatePollRestrictedFromNewList() throws Exception { + /** PREPARE DATA **/ + poll = servicePoll.getNewPoll(user); + poll.setPollType(PollType.RESTRICTED); + poll.setChoiceType(ChoiceType.DATE); + poll.addNewChoice("date1", null); + poll.addNewChoice("date2", "desc2"); + poll.addNewChoice("date3", "desc3"); + + Collection<ParticipantList> lists = new ArrayList<ParticipantList>(); + ParticipantList list = servicePoll.getNewPollList(); + lists.add(list); + + Participant participant1 = servicePoll.getNewPollParticipant(); + participant1.setName("participant1"); + participant1.setEmail("email1"); + participant1.setWeight(1.); + list.addParticipant(participant1); + + Participant participant2 = servicePoll.getNewPollParticipant(); + participant2.setName("participant2"); + participant2.setEmail(null); + participant2.setWeight(1.); + list.addParticipant(participant2); + + /** EXEC METHOD **/ + servicePoll.createPoll(poll, lists); + + /** VERIFICATION RESULT **/ + TopiaContext transaction = beginTransaction(); + try { + // Two person accounts + type = RESTRICTED + 3 DATE choices + PollDAO dao = PollenDAOHelper.getPollDAO(transaction); + Assert.assertTrue(dao.existByNaturalId(poll.getUid())); + Poll pollFound = dao.findByNaturalId(poll.getUid()); + + // Check pollType and accounts + Assert.assertEquals(PollType.RESTRICTED, pollFound.getPollType()); + Assert.assertEquals(2, pollFound.getPollAccount().size()); + + // Check choices + Assert.assertEquals(ChoiceType.DATE, pollFound.getChoiceType()); + Assert.assertEquals(3, pollFound.getChoice().size()); + + // Check creator + Assert.assertEquals(user, pollFound.getCreator().getUserAccount()); + + } finally { + transaction.closeContext(); + } + } +} Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServicePollImplTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceUserImplTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceUserImplTest.java 2010-05-12 21:11:26 UTC (rev 2995) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/service/ServiceUserImplTest.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -7,18 +7,13 @@ import org.chorem.pollen.PollenBusinessException.PollenExceptionType; import org.chorem.pollen.PollenDAOHelper; import org.chorem.pollen.PollenException; -import org.chorem.pollen.business.AbstractServiceTest; -import org.chorem.pollen.business.TestData; -import org.chorem.pollen.entity.FavoriteList; -import org.chorem.pollen.entity.FavoriteParticipant; -import org.chorem.pollen.entity.FavoriteParticipantDAO; +import org.chorem.pollen.test.AbstractServiceTest; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.UserAccountDAO; import org.chorem.pollen.entity.UserAccountImpl; import org.junit.Assert; import org.junit.Test; import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.framework.TopiaQuery; /** * @@ -27,10 +22,14 @@ public class ServiceUserImplTest extends AbstractServiceTest { private static final Log log = LogFactory.getLog(ServiceUserImplTest.class); + + @Override + protected void init() throws Exception { + } @Test public void testCopyUserAccount() throws Exception { - start("testManageNewPassword"); + //start("testManageNewPassword"); UserAccount user = getServiceUser().getNewUser(); user.setLogin("homer"); @@ -53,7 +52,7 @@ @Test public void testCheckPassword() throws Exception { - start("testCheckPassword"); + //start("testCheckPassword"); UserAccount user = getServiceUser().getNewUser(); user.setLogin("homer"); @@ -87,7 +86,7 @@ */ @Test public void testExecuteConnect() throws Exception { - start("testExecuteConnect"); + //start("testExecuteConnect"); // The password is wouhou and login is homer UserAccount user = createUser(false); @@ -121,7 +120,7 @@ */ @Test public void testExecuteGetNewUser() throws Exception { - start("testGetNewUser"); + //start("testGetNewUser"); UserAccount user = getServiceUser().getNewUser(); Assert.assertNotNull(user); @@ -133,7 +132,7 @@ */ @Test public void testExecuteCreateUser() throws Exception { - start("testCreateUser"); + //start("testCreateUser"); UserAccount user = getServiceUser().getNewUser(); user.setLogin("hsimpson"); @@ -190,7 +189,7 @@ */ @Test public void testExecuteUpdateUser() throws Exception { - start("testUpdateUser"); + //start("testUpdateUser"); UserAccount user1 = getServiceUser().getNewUser(); user1.setLogin("hsimpson"); @@ -242,7 +241,7 @@ */ @Test public void testExecuteDeleteUser() throws Exception { - start("testUpdateUser"); + //start("testUpdateUser"); try { getServiceUser().deleteUser("test"); Copied: trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java (from rev 2992, trunk/pollen-business/src/test/java/org/chorem/pollen/business/AbstractServiceTest.java) =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -0,0 +1,246 @@ +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.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.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.junit.After; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.rules.TestName; +import org.junit.runners.model.FrameworkMethod; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.util.ApplicationConfig; + +/** + * TestManager + * + * Created: 24 févr. 2010 + * + * @author fdesbois + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +@Ignore +public abstract class AbstractServiceTest { + + private static final Log log = LogFactory.getLog(AbstractServiceTest.class); + + protected PollenContext context; + + protected ServiceUserImpl serviceUser; + + protected ServiceFavoriteImpl serviceFavorite; + + protected ServicePollImpl servicePoll; + + @Rule + public TestName rule = new TestName() { + + @Override + public void starting(FrameworkMethod method) { + super.starting(method); + try { + getTest().start(getMethodName()); + getTest().init(); + } catch (Exception eee) { + log.error(eee.getClass().getSimpleName(), eee); + } + } + + public AbstractServiceTest getTest() { + return AbstractServiceTest.this; + } + }; + + protected abstract void init() throws Exception; + + public void start(String dbname) throws IOException { + log.info("## START ## : " + dbname); + + InputStream input = AbstractServiceTest.class. + getResourceAsStream("/PollenTest.properties"); + + Properties options = new Properties(); + options.load(input); + + ApplicationConfig config = new ApplicationConfig(); + config.setOptions(options); + config.setOption( + "hibernate.connection.url", + "jdbc:h2:file:target/surefire-data/" + dbname + ); + + getContext().loadConfiguration(config); + getContext().start(getServiceUser()); + + // Set currentDate to 23/02/2010 for tests + Calendar calendar = new GregorianCalendar(2010, 1, 23, 0, 0, 0); + ((PollenContextImpl)getContext()).setCurrentDate(calendar.getTime()); + } + + @After + public void stop() throws IOException { + getContext().stop(); + context = null; + serviceUser = null; + servicePoll = null; + serviceFavorite = null; + } + + public PollenContext getContext() { + if (context == null) { + context = new PollenContextImpl(); + } + return context; + } + + public ServiceUserImpl getServiceUser() { + if (serviceUser == null) { + serviceUser = new ServiceUserImpl(); + serviceUser.setContext(getContext()); + } + return serviceUser; + } + + public ServiceFavoriteImpl getServiceFavorite() { + if (serviceFavorite == null) { + serviceFavorite = new ServiceFavoriteImpl(); + serviceFavorite.setContext(getContext()); + } + return serviceFavorite; + } + + public ServicePollImpl getServicePoll() { + if (servicePoll == null) { + servicePoll = new ServicePollImpl(); + servicePoll.setContext(getContext()); + } + return servicePoll; + } + + public TopiaContext beginTransaction() throws TopiaException { + return getContext().beginTransaction(); + } + +// public ServiceEmailImpl getServiceEmail() { +// ServiceEmailImpl instance = new ServiceEmailImpl(); +// instance.setContext(getContext()); +// return instance; +// } +// +// public ServicePollImpl getServicePoll() { +// ServicePollImpl instance = new ServicePollImpl(); +// instance.setContext(getContext()); +// return instance; +// } + + /** + * 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 + * @throws TopiaException + * @throws PollenBusinessException + */ + public UserAccount createUser(boolean admin) + throws TopiaException, PollenBusinessException { + + UserAccount user = getServiceUser().getNewUser(); + user.setLogin("homer"); + user.setEmail("homer@simpson.us"); + user.setNewPassword("wouhou"); + user.setAdmin(admin); + + getServiceUser().createUser(user); + + TopiaContext transaction = getContext().beginTransaction(); + try { + UserAccountDAO dao = + PollenDAOHelper.getUserAccountDAO(transaction); + + UserAccount findUser = dao.findByLogin(user.getLogin()); + return findUser; + } finally { + transaction.closeContext(); + } + } + + public FavoriteList createFavoriteList(String name, UserAccount user) + throws TopiaException { + + TopiaContext transaction = beginTransaction(); + try { + FavoriteListDAO dao = + PollenDAOHelper.getFavoriteListDAO(transaction); + + FavoriteList list = dao.create(name, user); + transaction.commitTransaction(); + return list; + } finally { + transaction.closeContext(); + } + } + + public FavoriteParticipant createFavoriteParticipant(String name, + String email, FavoriteList list) throws TopiaException { + TopiaContext transaction = beginTransaction(); + try { + FavoriteParticipantDAO dao = + PollenDAOHelper.getFavoriteParticipantDAO(transaction); + + FavoriteParticipant participant = dao.create(name, email, list); + transaction.commitTransaction(); + return participant; + } finally { + transaction.closeContext(); + } + } + + public FavoriteList findFavoriteList(String id, String... propertiesLoad) + throws TopiaException { + TopiaContext transaction = beginTransaction(); + try { + FavoriteListDAO dao = + PollenDAOHelper.getFavoriteListDAO(transaction); + + TopiaQuery query = dao.createQuery(). + add(TopiaEntity.ID, id). + addLoad(propertiesLoad); + + FavoriteList result = dao.findByQuery(query); + + return result; + } finally { + transaction.closeContext(); + } + } + +} Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/test/AbstractServiceTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: svn:mergeinfo + Copied: trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java (from rev 2992, trunk/pollen-business/src/test/java/org/chorem/pollen/business/TestData.java) =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java 2010-05-14 12:06:15 UTC (rev 2996) @@ -0,0 +1,40 @@ +package org.chorem.pollen.test; + +import org.nuiton.topia.TopiaContext; + +/** + * TestData + * + * Created: 24 févr. 2010 + * + * @author fdesbois + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public abstract class TestData { + + protected TopiaContext transaction; + + public void execute() throws Exception { + transaction = null; + try { + //transaction = beginTransaction(); + + test(); + + } catch (Exception eee) { + if (transaction != null) { + transaction.rollbackTransaction(); + } + throw eee; + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + } + + protected abstract void test() throws Exception; +} Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/test/TestData.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
fdesbois@users.chorem.org