Package com.agiletec.plugins.jpsurvey.apsadmin

Source Code of com.agiletec.plugins.jpsurvey.apsadmin.ApsAdminPluginBaseTestCase

/*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
* This file is part of Entando software.
* Entando is a free software;
* You can redistribute it and/or modify it
* under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; version 2.
*
* See the file License for the specific language governing permissions  
* and limitations under the License
*
*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
*/
package com.agiletec.plugins.jpsurvey.apsadmin;

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

import com.agiletec.ConfigTestUtils;
import com.agiletec.aps.util.ApsProperties;
import com.agiletec.apsadmin.ApsAdminBaseTestCase;
import com.agiletec.plugins.jpsurvey.PluginConfigTestUtils;
import com.agiletec.plugins.jpsurvey.aps.system.services.survey.model.Choice;
import com.agiletec.plugins.jpsurvey.aps.system.services.survey.model.Question;
import com.agiletec.plugins.jpsurvey.aps.system.services.survey.model.Survey;

/**
* BaseTestCase Class for jpsurvey apsadmin tests.
* You have to comment "getConfigUtils()"
* Methods for testing plugin into custom jAPS 2.0 project.
*/
public class ApsAdminPluginBaseTestCase extends ApsAdminBaseTestCase {
 
  @Override
  protected ConfigTestUtils getConfigUtils() {
    return new PluginConfigTestUtils();
  }

  /**
   * Create a survey object to perform the test. This object prior its usage must be saved in database
   * and then reloaded to update the internal ID's
   * @param isQuestionnaire
   * @param isActive
   * @param isFreeText
   * @return the survey object just saved in the database
   * @throws Throwable in case of error
   */
  protected Survey createFakeSurveyForTest(boolean isQuestionnaire, boolean isActive, boolean isFreeText) throws Throwable {
    Survey survey = this.getFakeActiveSurvey("testingAction", "testingAction");
    survey.setQuestionnaire(isQuestionnaire);
    survey.setActive(isActive);
    survey.getQuestions().get(0).getChoices().get(0).setFreeText(isFreeText);
    survey.getQuestions().get(0).getChoices().get(1).setFreeText(false);
    survey.getQuestions().get(1).getChoices().get(0).setFreeText(isFreeText);
    survey.getQuestions().get(1).getChoices().get(1).setFreeText(false);
    return survey;
  }
 
  /**
   * Create on the fly a survey object to perform tests routines
   * @return a publishable survey object
   * @throws Throwable in case of any error
   */
  protected Survey getFakeActiveSurvey() throws Throwable {
    Survey survey = new Survey();
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"test\">me</property><property key=\"test2\">me too</property></properties>";
    String xmlImageDescr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"en\">You should NOT see THIS in the DB!</property><property key=\"it\">NON dovresti vedere QUESTO nel database!</property></properties>";
    Question question = this.getFakeQuestion();
    Question question2 = this.getFakeQuestion();
    question2.setPos(1);
    List<Question> questions = new ArrayList<Question>();
    questions.add(question);
    questions.add(question2);
    Date date = new Date();
    ApsProperties prop = new ApsProperties();
    prop.loadFromXml(xml);
    survey.setDescriptions(prop);
    survey.setGroupName("fake");
    survey.setStartDate(date);
    survey.setEndDate(null);
    survey.setActive(true);
    survey.setPublicPartialResult(false);
    survey.setPublicResult(true);
    survey.setQuestionnaire(false);
    survey.setGatherUserInfo(true);
    survey.setRestricted(true);
    survey.setTitles(prop);
    survey.setImageId("IMGXXX");
    prop = new ApsProperties();
    prop.loadFromXml(xmlImageDescr);
    survey.setImageDescriptions(prop);
    survey.setQuestions(questions);
    return survey;
  }
 
  /**
   * This creates a fake active survey with the given title and description.
   * @param description the text string inserted in the XML portion for the description
   * @param title the text string inserted in the XML portion for the title
   * @return the requested survey object
   * @throws Throwable
   */
  protected Survey getFakeActiveSurvey(String description, String title) throws Throwable {
    Survey survey = new Survey();
    String xmlDescription="<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"en\">"+description+"_en</property><property key=\"it\">"+description+"_it</property></properties>";
    String xmlTitle="<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"en\">"+title+"_en</property><property key=\"it\">"+title+"_it+</property></properties>";
    Question question = this.getFakeQuestion();
    question.setPos(1);
    Question question2 = this.getFakeQuestion();
    question2.setPos(2);
    List<Question> questions = new ArrayList<Question>();
    questions.add(question);
    questions.add(question2);
    Date date = new Date();
    ApsProperties prop = new ApsProperties();
    prop.loadFromXml(xmlDescription);
    survey.setDescriptions(prop);
    survey.setGroupName("fake");
    survey.setStartDate(date);
    survey.setEndDate(null);
    survey.setActive(true);
    survey.setPublicPartialResult(false);
    survey.setPublicResult(true);
    survey.setQuestionnaire(false);
    survey.setGatherUserInfo(true);
    survey.setRestricted(true);
    prop = new ApsProperties();
    prop.loadFromXml(xmlTitle);
    survey.setTitles(prop);
    survey.setImageId("IMGXXX");
    prop = new ApsProperties();
    prop.loadFromXml(xmlDescription);
    survey.setImageDescriptions(prop);
    survey.setQuestions(questions);
    return survey;
  }
 
  /**
   * This will assign the fake question to the survey ID 1
   * @return a question object not existing in the database
   * @throws Throwable
   */
  protected Question getFakeQuestion() throws Throwable {
    Question question = new Question();
    String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"test\">me</property><property key=\"test2\">me too</property></properties>";
    ApsProperties prop = new ApsProperties();
    Choice choice1 = this.getFakeChoice();
    Choice choice2 = this.getFakeChoice();
    List<Choice> choiceList = new ArrayList<Choice>();
    try {
      choiceList.add(choice1);
      choiceList.add(choice2);
      prop.loadFromXml(xml);
      question.setQuestions(prop);
      question.setId(2); // this will be changed when saving...
      question.setMaxResponseNumber(2677);
      question.setMinResponseNumber(1977);
      question.setPos(0);
      question.setSingleChoice(false);
      question.setSurveyId(1);
      choice1.setId(2); // will be changed upon saving
      choice1.setPos(0);
      choice1.setQuestionId(question.getId());
      choice1.setId(2); // will be changed upon saving
      choice2.setPos(10); // this is set on purpose to test save in sorted position method
      choice2.setQuestionId(question.getId());
      question.setChoices(choiceList);
    } catch (Throwable t) {
      throw t;
    }
    return question;
  }
 
  protected Choice getFakeChoice() throws Throwable {
    Choice choice = new Choice();
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties><property key=\"test\">me</property><property key=\"test2\">me too</property></properties>";
    ApsProperties prop = new ApsProperties();
    try {
      prop.loadFromXml(xml);
      choice.setChoices(prop);
      choice.setFreeText(true);
      choice.setPos(96);
      choice.setQuestionId(2);
    } catch (Throwable t) {
      throw t;
    }
    return choice;
  }
 
}
TOP

Related Classes of com.agiletec.plugins.jpsurvey.apsadmin.ApsAdminPluginBaseTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.