<commit_before> /************************************************************************************
 * Copyright (C) 2001-2010 encuestame: system online surveys Copyright (C) 2009
 * encuestame Development Team.
 * Licensed under the Apache Software License version 2.0
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to  in writing,  software  distributed
 * under the License is distributed  on  an  "AS IS"  BASIS,  WITHOUT  WARRANTIES  OR
 * CONDITIONS OF ANY KIND, either  express  or  implied.  See  the  License  for  the
 * specific language governing permissions and limitations under the License.
 ************************************************************************************
 */
package org.encuestame.core.test.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.sql.BatchUpdateException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.encuestame.core.exception.EnMeDomainNotFoundException;
import org.encuestame.core.exception.EnMeExpcetion;
import org.encuestame.core.persistence.domain.CatEmailLists;
import org.encuestame.core.persistence.domain.CatEmails;
import org.encuestame.core.persistence.domain.Poll;
import org.encuestame.core.persistence.domain.PollFolder;
import org.encuestame.core.persistence.domain.Question;
import org.encuestame.core.persistence.domain.QuestionPattern;
import org.encuestame.core.persistence.domain.SecUser;
import org.encuestame.core.persistence.domain.SecUserSecondary;
import org.encuestame.core.service.IPollService;
import org.encuestame.core.service.PollService;
import org.encuestame.core.service.util.ConvertDomainBean;
import org.encuestame.core.test.service.config.AbstractBaseUnitBeans;
import org.encuestame.utils.web.UnitAnswersBean;
import org.encuestame.utils.web.UnitFolder;
import org.encuestame.utils.web.UnitLists;
import org.encuestame.utils.web.UnitPatternBean;
import org.encuestame.utils.web.UnitPoll;
import org.encuestame.utils.web.UnitQuestionBean;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

 /**
 * Test for {@link PollService}.
 * @author Morales, <NAME> <EMAIL>
 * @since 17/05/2010 19:35:36
 * @version $Id:$
 */
public class TestPollService extends AbstractBaseUnitBeans{

     /** {@link SecUser} **/
    private SecUser user;

    /** {@link Question} **/
    private Question question;

    /** {@link QuestionPattern} **/
    private QuestionPattern questionPattern;

    /** {@link IPollService} **/
    @Autowired
    private IPollService pollService;

    /** {@link Poll} **/
    private Poll poll;

    private SecUserSecondary secUserSecondary;

    /** {@link CatEmailLists} **/
    private CatEmailLists emailList;

    /** {@link CatEmails} **/
    private CatEmails emails;

    /** {@link PollFolder}. **/
    private PollFolder folder;

    /**
     * Init.
     */
    @Before
    public void serviceInit(){
        this.user = createUser("testEncuesta", "testEncuesta123");
        this.secUserSecondary = createSecondaryUser("diana", this.user);
        this.question = createQuestion("Why the roses are red?","html");
        this.questionPattern = createQuestionPattern("html");
        this.poll = createPoll(new Date(), this.question, "FDK125", this.user, Boolean.TRUE);
        this.emailList = createDefaultListEmail(this.secUserSecondary.getSecUser());
        createDefaultListEmail(this.user, "default");
        this.emails = createDefaultEmails("<EMAIL>", this.emailList);
        createDefaultEmails("<EMAIL>", this.emailList);
        this.folder = createPollFolder("folder 1", this.user);
        this.poll.setPollFolder(folder);
     }

    /**
     * Test createPoll.
     * @throws Exception exception
     */
    @Test
    public void testcreatePoll() throws Exception{
        final UnitQuestionBean question = ConvertDomainBean.convertQuestionsToBean(this.question);
        final UnitPoll unitPoll = ConvertDomainBean.convertPollDomainToBean(this.poll);
        unitPoll.setQuestionBean(question);
        this.pollService.createPoll(unitPoll, this.secUserSecondary.getUsername());
    }

    /**
     * Test getPollsByFolder.
     * @throws EnMeDomainNotFoundException
     */
    @Test
    public void testgetPollsByFolder() throws EnMeDomainNotFoundException{
        getiPoll().saveOrUpdate(this.poll);
        List<UnitPoll> polls = this.pollService.getPollsByFolder(ConvertDomainBean
                              .convertFolderToBeanFolder(folder), this.secUserSecondary.getUsername());
        assertEquals(polls.size(), 1);
    }

    /**
     * Test retrieveFolderPoll.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testretrieveFolderPoll() throws EnMeDomainNotFoundException{
        List<UnitFolder> folders = this.pollService.retrieveFolderPoll(this.secUserSecondary.getUsername());
        assertEquals(folders.size(), 1);
    }

    /**
     * Test createPollFolder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testcreatePollFolder() throws EnMeDomainNotFoundException{
         this.pollService.createPollFolder("folder 2", this.secUserSecondary.getUsername());
         List<UnitFolder> folders = this.pollService.retrieveFolderPoll(this.secUserSecondary.getUsername());
         assertEquals(folders.size(), 2);
    }

    /**
     * Test updateFolderName.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testupdateFolderName() throws EnMeDomainNotFoundException{
        this.pollService.updateFolderName(this.folder.getId(), "newFolderName", this.secUserSecondary.getUsername());
        final PollFolder folder = this.getiPoll().getPollFolderById(this.folder.getId());
         assertEquals(folder.getFolderName(), "newFolderName");
    }

    /**
     * test removePollFolder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test(expected = ConstraintViolationException.class)
    public void testremovePollFolderBatchUpdateException() throws EnMeDomainNotFoundException{
        final long id = this.folder.getId();
        this.pollService.removePollFolder(id);
    }

    /**
     * Remove Poll Folder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test()
    public void testremovePollFolder() throws EnMeDomainNotFoundException{
        this.poll.setPollFolder(null);
        getiPoll().saveOrUpdate(this.poll);
        final long id = this.folder.getId();<commit_msg>Fixed failure.<commit_after> /************************************************************************************
 * Copyright (C) 2001-2010 encuestame: system online surveys Copyright (C) 2009
 * encuestame Development Team.
 * Licensed under the Apache Software License version 2.0
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to  in writing,  software  distributed
 * under the License is distributed  on  an  "AS IS"  BASIS,  WITHOUT  WARRANTIES  OR
 * CONDITIONS OF ANY KIND, either  express  or  implied.  See  the  License  for  the
 * specific language governing permissions and limitations under the License.
 ************************************************************************************
 */
package org.encuestame.core.test.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.sql.BatchUpdateException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.encuestame.core.exception.EnMeDomainNotFoundException;
import org.encuestame.core.exception.EnMeExpcetion;
import org.encuestame.core.persistence.domain.CatEmailLists;
import org.encuestame.core.persistence.domain.CatEmails;
import org.encuestame.core.persistence.domain.Poll;
import org.encuestame.core.persistence.domain.PollFolder;
import org.encuestame.core.persistence.domain.Question;
import org.encuestame.core.persistence.domain.QuestionPattern;
import org.encuestame.core.persistence.domain.SecUser;
import org.encuestame.core.persistence.domain.SecUserSecondary;
import org.encuestame.core.service.IPollService;
import org.encuestame.core.service.PollService;
import org.encuestame.core.service.util.ConvertDomainBean;
import org.encuestame.core.test.service.config.AbstractBaseUnitBeans;
import org.encuestame.utils.web.UnitAnswersBean;
import org.encuestame.utils.web.UnitFolder;
import org.encuestame.utils.web.UnitLists;
import org.encuestame.utils.web.UnitPatternBean;
import org.encuestame.utils.web.UnitPoll;
import org.encuestame.utils.web.UnitQuestionBean;
import org.hibernate.HibernateException;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

 /**
 * Test for {@link PollService}.
 * @author Morales, <NAME> <EMAIL>
 * @since 17/05/2010 19:35:36
 * @version $Id:$
 */
public class TestPollService extends AbstractBaseUnitBeans{

     /** {@link SecUser} **/
    private SecUser user;

    /** {@link Question} **/
    private Question question;

    /** {@link QuestionPattern} **/
    private QuestionPattern questionPattern;

    /** {@link IPollService} **/
    @Autowired
    private IPollService pollService;

    /** {@link Poll} **/
    private Poll poll;

    private SecUserSecondary secUserSecondary;

    /** {@link CatEmailLists} **/
    private CatEmailLists emailList;

    /** {@link CatEmails} **/
    private CatEmails emails;

    /** {@link PollFolder}. **/
    private PollFolder folder;

    /**
     * Init.
     */
    @Before
    public void serviceInit(){
        this.user = createUser("testEncuesta", "testEncuesta123");
        this.secUserSecondary = createSecondaryUser("diana", this.user);
        this.question = createQuestion("Why the roses are red?","html");
        this.questionPattern = createQuestionPattern("html");
        this.poll = createPoll(new Date(), this.question, "FDK125", this.user, Boolean.TRUE);
        this.emailList = createDefaultListEmail(this.secUserSecondary.getSecUser());
        createDefaultListEmail(this.user, "default");
        this.emails = createDefaultEmails("<EMAIL>", this.emailList);
        createDefaultEmails("<EMAIL>", this.emailList);
        this.folder = createPollFolder("folder 1", this.user);
        this.poll.setPollFolder(folder);
     }

    /**
     * Test createPoll.
     * @throws Exception exception
     */
    @Test
    public void testcreatePoll() throws Exception{
        final UnitQuestionBean question = ConvertDomainBean.convertQuestionsToBean(this.question);
        final UnitPoll unitPoll = ConvertDomainBean.convertPollDomainToBean(this.poll);
        unitPoll.setQuestionBean(question);
        this.pollService.createPoll(unitPoll, this.secUserSecondary.getUsername());
    }

    /**
     * Test getPollsByFolder.
     * @throws EnMeDomainNotFoundException
     */
    @Test
    public void testgetPollsByFolder() throws EnMeDomainNotFoundException{
        getiPoll().saveOrUpdate(this.poll);
        List<UnitPoll> polls = this.pollService.getPollsByFolder(ConvertDomainBean
                              .convertFolderToBeanFolder(folder), this.secUserSecondary.getUsername());
        assertEquals(polls.size(), 1);
    }

    /**
     * Test retrieveFolderPoll.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testretrieveFolderPoll() throws EnMeDomainNotFoundException{
        List<UnitFolder> folders = this.pollService.retrieveFolderPoll(this.secUserSecondary.getUsername());
        assertEquals(folders.size(), 1);
    }

    /**
     * Test createPollFolder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testcreatePollFolder() throws EnMeDomainNotFoundException{
         this.pollService.createPollFolder("folder 2", this.secUserSecondary.getUsername());
         List<UnitFolder> folders = this.pollService.retrieveFolderPoll(this.secUserSecondary.getUsername());
         assertEquals(folders.size(), 2);
    }

    /**
     * Test updateFolderName.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test
    public void testupdateFolderName() throws EnMeDomainNotFoundException{
        this.pollService.updateFolderName(this.folder.getId(), "newFolderName", this.secUserSecondary.getUsername());
        final PollFolder folder = this.getiPoll().getPollFolderById(this.folder.getId());
         assertEquals(folder.getFolderName(), "newFolderName");
    }

    /**
     * test removePollFolder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test(expected = HibernateException.class)
    public void testremovePollFolderBatchUpdateException() throws EnMeDomainNotFoundException{
        final long id = this.folder.getId();
        this.pollService.removePollFolder(id);
    }

    /**
     * Remove Poll Folder.
     * @throws EnMeDomainNotFoundException exception
     */
    @Test()
    public void testremovePollFolder() throws EnMeDomainNotFoundException{
        this.poll.setPollFolder(null);
        getiPoll().saveOrUpdate(this.poll);
        final long id = this.folder.getId();
{'fraction_non_alphanumeric': 0.1284485315929991, 'fraction_numerical': 0.00608128151883714, 'mean_word_length': 3.9054201527828303, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3024482', 'n_tokens_mistral': 3830, 'n_tokens_neox': 3706, 'n_words': 756}