/*
*
* 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.aps;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.agiletec.ConfigTestUtils;
import com.agiletec.aps.BaseTestCase;
import com.agiletec.aps.util.ApsProperties;
import com.agiletec.plugins.jpsurvey.PluginConfigTestUtils;
import com.agiletec.plugins.jpsurvey.aps.system.services.survey.ISurveyManager;
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;
public class ApsPluginBaseTestCase extends BaseTestCase {
@Override
protected ConfigTestUtils getConfigUtils() {
return new PluginConfigTestUtils();
}
@Override
protected void setUp() throws Exception {
super.setUp();
this.initServices();
}
private void initServices() {
this._surveyManager = (ISurveyManager) this.getService("jpsurveySurveyManager"); // FIXME use constants
}
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 xmlBarrali="<?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(xmlBarrali);
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 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(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;
}
public ISurveyManager getSurveyManager() {
return _surveyManager;
}
private ISurveyManager _surveyManager;
}