Author: tchemit Date: 2012-07-31 18:17:34 +0200 (Tue, 31 Jul 2012) New Revision: 3576 Url: http://chorem.org/repositories/revision/pollen/3576 Log: refs #715: Import voting users list doesn't work (review and fix csv import + remove 3 classes :) Removed: trunk/pollen-services/src/main/java/org/chorem/pollen/services/FavoriteListImport.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportCSV.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportLDAP.java Modified: trunk/pollen-services/pom.xml trunk/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/FavoriteListImportException.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java trunk/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties trunk/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties trunk/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java Modified: trunk/pollen-services/pom.xml =================================================================== --- trunk/pollen-services/pom.xml 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/pom.xml 2012-07-31 16:17:34 UTC (rev 3576) @@ -84,10 +84,6 @@ <artifactId>nuiton-utils</artifactId> </dependency> <dependency> - <groupId>org.nuiton</groupId> - <artifactId>nuiton-csv</artifactId> - </dependency> - <dependency> <groupId>org.nuiton.i18n</groupId> <artifactId>nuiton-i18n</artifactId> </dependency> Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/FavoriteListImport.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/FavoriteListImport.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/FavoriteListImport.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -1,57 +0,0 @@ -/* - * #%L - * Pollen :: Services - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.chorem.pollen.services; - -import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.services.exceptions.FavoriteListImportException; -import org.chorem.pollen.services.impl.FavoriteListImportCSV; -import org.chorem.pollen.services.impl.FavoriteListImportLDAP; - -import java.util.List; - -/** - * Used to import a favorite List or a List of {@link PollAccount} from a given - * source url (file, external service, ...) - * <p/> - * Created: 16/04/12 - * - * @author fdesbois <desbois@codelutin.com> - * @see FavoriteListImportLDAP - * @see FavoriteListImportCSV - * @since 1.3 - */ -public interface FavoriteListImport { - - /** - * Execute the import from given {@code url}. This will returned the - * successful list of {@link PollAccount} instanciated during import. No - * database save is done here, only the extraction from an input url to - * a list of entities to manage. - * - * @param url Url of the file or service that contains data to import - * @return the List of PollAccount imported - * @throws FavoriteListImportException for any import error - */ - public List<PollAccount> execute(String url) - throws FavoriteListImportException; -} Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/FavoriteListImportException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/FavoriteListImportException.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/FavoriteListImportException.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -22,8 +22,6 @@ */ package org.chorem.pollen.services.exceptions; -import org.chorem.pollen.services.FavoriteListImport; - import java.util.Locale; import static org.nuiton.i18n.I18n._; @@ -31,7 +29,7 @@ import static org.nuiton.i18n.I18n.n_; /** - * Exception during {@link FavoriteListImport#execute(String)} error. There is + * Exception during favorite list import. There is * always a cause from librairies used for import. * * @author fdesbois <desbois@codelutin.com> Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportCSV.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportCSV.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportCSV.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -1,184 +0,0 @@ -/* - * #%L - * Pollen :: Services - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.chorem.pollen.services.impl; - -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.PollenTechnicalException; -import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.services.FavoriteListImport; -import org.chorem.pollen.services.PollenServicePredicates; -import org.chorem.pollen.services.PollenServiceSupport; -import org.chorem.pollen.services.exceptions.FavoriteListImportException; -import org.nuiton.util.StringUtil; -import org.nuiton.util.csv.Import; -import org.nuiton.util.csv.ImportModel; -import org.nuiton.util.csv.ImportRuntimeException; -import org.nuiton.util.csv.ImportableColumn; -import org.nuiton.util.csv.ModelBuilder; -import org.nuiton.util.csv.ValueParser; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.Reader; -import java.text.ParseException; -import java.util.List; - -import static org.nuiton.i18n.I18n._; - -/** - * Implementation of {@link FavoriteListImport} as a service for CSV import. - * Nuiton-CSV is used to manage the {@link Import} using a specific {@link - * ImportModel} for pollen. Empty rows (no votingId or no email) are ignored. - * <p/> - * Created: 16/04/12 - * - * @author fdesbois <desbois@codelutin.com> - * @since 1.3 - */ -public class FavoriteListImportCSV extends PollenServiceSupport implements FavoriteListImport { - - private static final Log log = LogFactory.getLog(FavoriteListImportCSV.class); - - private static final ValueParser<String> EMAIL_PARSER = new ValueParser<String>() { - - @Override - public String parse(String value) throws ParseException { - if (value != null && !StringUtil.isEmail(value)) { - throw new ParseException(_("pollen.error.email.invalid"), 0); - } - return value; - } - }; - - protected class FavoriteListImportModel implements ImportModel<PollAccount> { - - @Override - public char getSeparator() { - return ','; - } - - @Override - public void pushCsvHeaderNames(List<String> headerNames) { - } - - @Override - public PollAccount newEmptyInstance() { - return newInstance(getDAO(PollAccount.class)); - } - - @Override - public Iterable<ImportableColumn<PollAccount, Object>> getColumnsForImport() { - ModelBuilder modelBuilder = new ModelBuilder(); - modelBuilder.newMandatoryColumn(PollAccount.PROPERTY_VOTING_ID, - PollAccount.PROPERTY_VOTING_ID); - modelBuilder.newMandatoryColumn(PollAccount.PROPERTY_EMAIL, - PollAccount.PROPERTY_EMAIL, - EMAIL_PARSER); - return modelBuilder.getColumnsForImport(); - } - } - - @Override - public List<PollAccount> execute(String url) throws FavoriteListImportException { - - List<PollAccount> results; - Reader reader = null; - try { - reader = new FileReader(new File(url)); - - Import<PollAccount> csvImport = Import.newImport(new FavoriteListImportModel(), reader); - - // Filter on empty account (without email or votingId) - results = Lists.newArrayList( - Iterables.filter(csvImport, - PollenServicePredicates.POLL_ACCOUNT_NOT_EMPTY) - ); - - if (log.isInfoEnabled()) { - log.info(results.size() + " comptes importés."); - } - - } catch (FileNotFoundException ex) { - throw new PollenTechnicalException(ex); - - } catch (ImportRuntimeException ex) { - if (log.isDebugEnabled()) { - log.debug("Error during CSV import", ex); - } - throw new FavoriteListImportException("CSV", ex); - - } finally { - IOUtils.closeQuietly(reader); - } - return results; - } - -// @Override -// public List<PollAccount> execute(String url) throws FavoriteListImportException { -// -// List<PollAccount> results; -// Reader reader = null; -// try { -// reader = new FileReader(new File(url)); -// -// // Définition de la stratégie de mapping -// ColumnPositionMappingStrategy<PollAccountImpl> strat = -// new ColumnPositionMappingStrategy<PollAccountImpl>(); -// -// String[] columns = new String[] { -// PollAccount.PROPERTY_VOTING_ID, -// PollAccount.PROPERTY_EMAIL -// }; -// -// strat.setType(PollAccountImpl.class); -// strat.setColumnMapping(columns); -// -// // Parsing du fichier CSV -// CsvToBean<PollAccountImpl> csv = new CsvToBean<PollAccountImpl>(); -// List<PollAccount> importedData = Lists.<PollAccount>newArrayList(csv.parse(strat, reader)); -// -// // Suppression des comptes null -// results = Lists.newArrayList( -// Iterables.filter(importedData, -// PollenServicePredicates.POLL_ACCOUNT_NOT_EMPTY) -// ); -// -// if (log.isInfoEnabled()) { -// log.info(results.size() + " comptes importés."); -// } -// -// } catch (Exception ex) { -// log.warn("Error during CSV import", ex); -// throw new FavoriteListImportException(ex); -// -// } finally { -// IOUtils.closeQuietly(reader); -// } -// return results; -// } -} Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportLDAP.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportLDAP.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteListImportLDAP.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -1,121 +0,0 @@ -/* - * #%L - * Pollen :: Services - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero 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 Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ -package org.chorem.pollen.services.impl; - -import com.google.common.collect.Lists; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.PollAccountDAO; -import org.chorem.pollen.services.FavoriteListImport; -import org.chorem.pollen.services.PollenServiceSupport; -import org.chorem.pollen.services.exceptions.FavoriteListImportException; - -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; -import java.util.List; -import java.util.Properties; - -/** - * LDAP Import of {@link PollAccount}. The behavior is the same than the one - * from pollen 1.2.x - * <p/> - * TODO-fdesbois-2012-04-17 : tests and documentation - * <p/> - * Created: 16/04/12 - * - * @author fdesbois <desbois@codelutin.com> - * @since 1.3 - */ -public class FavoriteListImportLDAP extends PollenServiceSupport implements FavoriteListImport { - - private static final Log log = LogFactory.getLog(FavoriteListImportLDAP.class); - - @Override - public List<PollAccount> execute(String url) throws FavoriteListImportException { - - long start = System.nanoTime(); - - List<PollAccount> results = Lists.newArrayList(); - try { - - // Initialisation du contexte - Properties env = new Properties(); -// if (server != null) { -// env.put(Context.INITIAL_CONTEXT_FACTORY, -// "com.sun.jndi.ldap.LdapCtxFactory"); -// env.put(Context.PROVIDER_URL, "ldap://" + server + "/"); -// } - DirContext ictx = new InitialDirContext(env); - - // Recherche en profondeur - SearchControls control = new SearchControls(); - control.setSearchScope(SearchControls.SUBTREE_SCOPE); - - // Création des comptes avec les résultats de la recherche - NamingEnumeration<SearchResult> e = ictx.search(url, null, - control); - while (e.hasMore()) { - SearchResult r = e.next(); - - if (log.isDebugEnabled()) { - log.debug("Result: " + r.getName() + "(object: " - + r.getClassName() + ")"); - } - - Attribute nameAttr = r.getAttributes().get("cn"); - Attribute emailAttr = r.getAttributes().get("mail"); - - if (nameAttr != null) { - PollAccountDAO dao = getDAO(PollAccount.class); - PollAccount account = newInstance(dao); - account.setVotingId(nameAttr.get().toString()); - account.setEmail(emailAttr.get().toString()); - results.add(account); - - if (log.isDebugEnabled()) { - log.debug("New account - name: " - + nameAttr.get().toString() + ", email: " - + emailAttr.get().toString()); - } - } - } - } catch (NamingException ex) { - log.error("Exception de nommage lors de l'import depuis LDAP", ex); - throw new FavoriteListImportException("LDAP", ex); - } - - long duration = (System.nanoTime() - start) / 1000000000; - if (log.isInfoEnabled()) { - log.info(results.size() + " comptes importés en " + duration - + " sec."); - } - - return results; - } -} Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -23,7 +23,11 @@ package org.chorem.pollen.services.impl; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenTechnicalException; import org.chorem.pollen.business.persistence.PersonList; import org.chorem.pollen.business.persistence.PersonListDAO; @@ -33,6 +37,7 @@ import org.chorem.pollen.business.persistence.UserAccountDAO; import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.FavoriteListAlreadyExistException; +import org.chorem.pollen.services.exceptions.FavoriteListImportException; import org.chorem.pollen.services.exceptions.FavoriteListNotFoundException; import org.chorem.pollen.services.exceptions.FavoriteListNotOwnedByUserException; import org.chorem.pollen.services.exceptions.ParticipantAlreadyFoundInListException; @@ -41,11 +46,31 @@ import org.chorem.pollen.services.exceptions.UserNotFoundException; import org.nuiton.topia.TopiaException; import org.nuiton.topia.persistence.TopiaFilterPagerUtil; +import org.nuiton.util.StringUtil; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.util.List; +import java.util.Locale; +import java.util.Properties; +import static org.nuiton.i18n.I18n.l_; + public class FavoriteService extends PollenServiceSupport { + /** Logger. */ + private static final Log log = LogFactory.getLog(FavoriteService.class); + public List<PersonList> getFavoriteLists(UserAccount user, TopiaFilterPagerUtil.FilterPagerBean pager) { @@ -338,6 +363,134 @@ } } + public List<PollAccount> importFromCsvfile(String filename, + File file) throws FavoriteListImportException { + + List<PollAccount> results = Lists.newArrayList(); + Locale locale = getLocale(); + PollAccountDAO dao = getDAO(PollAccount.class); + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + String line; + int lineNumber = 0; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (StringUtils.isBlank(line) || line.startsWith("#")) { + + // comment line + continue; + } + lineNumber++; + + int spaceIndex = line.indexOf(' '); + String email; + String votingId; + if (spaceIndex == -1) { + // only email + email = line; + votingId = line; + } else { + // email + votingId + email = line.substring(0, spaceIndex); + votingId = line.substring(spaceIndex); + } + email = email.trim(); + votingId = votingId.trim(); + + if (!StringUtil.isEmail(email)) { + + // email is not valid + String error = l_(locale, "pollen.error.import.invalid.email", lineNumber, email); + throw new FavoriteListImportException(filename, error, null); + } + + PollAccount account = newInstance(dao); + account.setEmail(email); + account.setVotingId(votingId); + results.add(account); + } + + if (log.isInfoEnabled()) { + log.info(results.size() + " comptes importés."); + } + reader.close(); + + } catch (FileNotFoundException ex) { + // should never happens ? + throw new PollenTechnicalException(ex); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Could not close reader", e); + } + } finally { + IOUtils.closeQuietly(reader); + } + return results; + } + + public List<PollAccount> importFromLDAP(String url) throws FavoriteListImportException { + + long start = System.nanoTime(); + + List<PollAccount> results = Lists.newArrayList(); + try { + + // Initialisation du contexte + Properties env = new Properties(); +// if (server != null) { +// env.put(Context.INITIAL_CONTEXT_FACTORY, +// "com.sun.jndi.ldap.LdapCtxFactory"); +// env.put(Context.PROVIDER_URL, "ldap://" + server + "/"); +// } + DirContext ictx = new InitialDirContext(env); + + // Recherche en profondeur + SearchControls control = new SearchControls(); + control.setSearchScope(SearchControls.SUBTREE_SCOPE); + + // Création des comptes avec les résultats de la recherche + NamingEnumeration<SearchResult> e = ictx.search(url, null, + control); + while (e.hasMore()) { + SearchResult r = e.next(); + + if (log.isDebugEnabled()) { + log.debug("Result: " + r.getName() + "(object: " + + r.getClassName() + ")"); + } + + Attribute nameAttr = r.getAttributes().get("cn"); + Attribute emailAttr = r.getAttributes().get("mail"); + + if (nameAttr != null) { + PollAccountDAO dao = getDAO(PollAccount.class); + PollAccount account = newInstance(dao); + account.setVotingId(nameAttr.get().toString()); + account.setEmail(emailAttr.get().toString()); + results.add(account); + + if (log.isDebugEnabled()) { + log.debug("New account - name: " + + nameAttr.get().toString() + ", email: " + + emailAttr.get().toString()); + } + } + } + } catch (NamingException ex) { + log.error("Exception de nommage lors de l'import depuis LDAP", ex); + throw new FavoriteListImportException("LDAP", ex); + } + + long duration = (System.nanoTime() - start) / 1000000000; + if (log.isInfoEnabled()) { + log.info(results.size() + " comptes importés en " + duration + + " sec."); + } + + return results; + } + public PersonList newFavoriteList() { PersonListDAO dao = getDAO(PersonList.class); PersonList personList = newInstance(dao); Modified: trunk/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties =================================================================== --- trunk/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/resources/i18n/pollen-services_en_GB.properties 2012-07-31 16:17:34 UTC (rev 3576) @@ -25,8 +25,8 @@ pollen.email.voteEmail.subject=[Pollen] Vote reporting (%s) pollen.email.votingEmail.content=A new poll has been created\: "%s".\nYou can participate with the identifier %s by following this link\: \n%s pollen.email.votingEmail.subject=[Pollen] Invitation to vote (%s) -pollen.error.email.invalid=The email doesn't have the good format pollen.error.import=Error during %1$s import \: %2$s +pollen.error.import.invalid.email=At line %s, Email format is not valid '%s' pollen.feed.addChoiceContent= pollen.feed.addChoiceTitle= pollen.feed.addCommentContent=%s @@ -47,5 +47,4 @@ pollen.security.error.poll.free.creatorId.can.not.vote=Using a creator Id does not allow you to vote. pollen.security.error.poll.not.closed.and.results.not.continuous=The poll is not closed and results are not continuous pollen.security.error.poll.not.free.and.access.not.granted=You can not access to this non free poll -pollen.security.error.poll.not.running.can.not.vote=You can not vote (poll is finished or not began). pollen.security.error.poll.result.private.and.access.not.granted=Results of the poll are private and you do not have credentials to see them Modified: trunk/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties =================================================================== --- trunk/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/main/resources/i18n/pollen-services_fr_FR.properties 2012-07-31 16:17:34 UTC (rev 3576) @@ -25,8 +25,8 @@ pollen.email.voteEmail.subject=[Pollen] Notification de vote (%s) pollen.email.votingEmail.content=Un nouveau sondage a été créé \: "%s".\nVous pouvez y participer avec l'identifiant %s à l'adresse suivante \: \n%s pollen.email.votingEmail.subject=[Pollen] Invitation au vote (%s) -pollen.error.email.invalid=Email non valide pollen.error.import=Erreur pendant l'import %1$s \: %2$s +pollen.error.import.invalid.email=A la ligne %s, le courriel n'est pas valide '%s' pollen.feed.addChoiceContent= pollen.feed.addChoiceTitle= pollen.feed.addCommentContent=%s @@ -47,5 +47,4 @@ pollen.security.error.poll.free.creatorId.can.not.vote=L'utlisation du creatorId ne permet de voter. pollen.security.error.poll.not.closed.and.results.not.continuous=Vous n'avez pas accès aux résultats de ce sondage (résultats non continus et sondage non fermé) pollen.security.error.poll.not.free.and.access.not.granted=Vous n'avez pas accès à ce sondage restreint -pollen.security.error.poll.not.running.can.not.vote=Vous n'êtes pas autorisé à voter (sondage terminé ou pas encore commencé). pollen.security.error.poll.result.private.and.access.not.granted=Vous n'avez pas accès aux résultats privés de ce sondage Modified: trunk/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java =================================================================== --- trunk/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java 2012-07-31 14:19:29 UTC (rev 3575) +++ trunk/pollen-services/src/test/java/org/chorem/pollen/services/impl/FavoriteServiceTest.java 2012-07-31 16:17:34 UTC (rev 3576) @@ -22,13 +22,19 @@ */ package org.chorem.pollen.services.impl; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.apache.commons.io.FileUtils; +import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.business.persistence.PollAccount; import org.chorem.pollen.services.AbstractPollenServiceTest; +import org.chorem.pollen.services.exceptions.FavoriteListImportException; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import java.io.File; +import java.util.List; + /** * Tests the {@link FavoriteService}. * @@ -38,8 +44,6 @@ @Ignore public class FavoriteServiceTest extends AbstractPollenServiceTest { - /** Logger. */ - private static final Log log = LogFactory.getLog(FavoriteServiceTest.class); @Override @Before @@ -105,4 +109,77 @@ } + @Test(expected = PollenTechnicalException.class) + public void executeImportCsvFileNotFound() throws Exception { + + File importFile = getCsvImportFile(); + + FavoriteService service = newService(FavoriteService.class); + + service.importFromCsvfile(importFile.getName(), importFile); + } + + @Test(expected = FavoriteListImportException.class) + public void executeImportCsvInvalidEmail() throws Exception { + + FavoriteService service = newService(FavoriteService.class); + + File importFile = getCsvImportFile(); + + String importContent = "badEmail@ voterId"; + + FileUtils.write(importFile, importContent); + + service.importFromCsvfile(importFile.getName(), importFile); + } + + @Test + public void executeImportCsvEmptyContent() throws Exception { + + FavoriteService service = newService(FavoriteService.class); + + File importFile = getCsvImportFile(); + + String importContent = ""; + FileUtils.write(importFile, importContent); + + List<PollAccount> importedAccount = service.importFromCsvfile(importFile.getName(), importFile); + + Assert.assertNotNull(importedAccount); + Assert.assertTrue(importedAccount.isEmpty()); + } + + @Test + public void executeImportCsv() throws Exception { + + FavoriteService service = newService(FavoriteService.class); + + File importFile = getCsvImportFile(); + + String importContent = "myemail@mydomain.uk voterId \n\t\t myemail2@mydomain.uk\r\r\n#Comment\n\n "; + FileUtils.write(importFile, importContent); + + List<PollAccount> importedAccount = service.importFromCsvfile(importFile.getName(), importFile); + + Assert.assertNotNull(importedAccount); + Assert.assertEquals(2, importedAccount.size()); + assertPollAccount(importedAccount.get(0), "myemail@mydomain.uk", "voterId"); + assertPollAccount(importedAccount.get(1), "myemail2@mydomain.uk", "myemail2@mydomain.uk"); + } + + protected void assertPollAccount(PollAccount pollAccount, + String expectedEmail, + String expectedVotingId) { + Assert.assertNotNull(pollAccount); + Assert.assertEquals(expectedEmail, pollAccount.getEmail()); + Assert.assertEquals(expectedVotingId, pollAccount.getVotingId()); + + } + + protected File getCsvImportFile() { + File temporaryDirectory = fakeContext.getConfiguration().getTemporaryDirectory(); + File importFile = new File(temporaryDirectory, "import.csv"); + return importFile; + } + }