diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/TextServlet.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/TextServlet.java index 921ceef2..776d48f0 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/TextServlet.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/TextServlet.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.net.URI; import java.net.URL; +import java.net.URLDecoder; import java.nio.CharBuffer; import java.util.ArrayList; import java.util.HashMap; @@ -423,7 +424,8 @@ protected int processMessage(TextMessage msg) throws Exception Question.getRetryCount( sessionKey ) ); } Return replystr = formQuestion(question, config.getConfigId(),address); - escapeInput.reply = replystr.reply; + //fix for bug: #15 https://github.com/almende/dialog/issues/15 + escapeInput.reply = URLDecoder.decode(replystr.reply); question = replystr.question; fromName = getNickname(question); diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/tools/Broadsoft.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/tools/Broadsoft.java index 9b32e67e..0dfb0168 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/tools/Broadsoft.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/adapter/tools/Broadsoft.java @@ -14,7 +14,6 @@ import com.almende.dialog.Settings; import com.almende.dialog.accounts.AdapterConfig; -import com.almende.dialog.util.PhoneNumberUtils; import com.almende.util.ParallelInit; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; @@ -49,15 +48,16 @@ public Broadsoft(AdapterConfig config) { public String startCall(String address) { String formattedAddress = new String(address); - try - { - formattedAddress = PhoneNumberUtils.formatNumber( address, null ); - } - catch ( Exception e ) - { - log.severe( String.format( "Phonenumber: %s is not valid", address ) ); - return null; - } +// try +// { +// //trim address +// formattedAddress = PhoneNumberUtils.formatNumber( address, null ); +// } +// catch ( Exception e ) +// { +// log.severe( String.format( "Phonenumber: %s is not valid", address ) ); +// return null; +// } WebResource webResource = client.resource(XSI_URL+XSI_ACTIONS+user+XSI_START_CALL); webResource.addFilter(this.auth); diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/Question.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/Question.java index b09bfaf4..0ed75581 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/Question.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/Question.java @@ -124,7 +124,7 @@ public static Question fromJSON(String json, String adapterID) { if(json!=null) { try { question = om.readValue(json, Question.class); - question.setQuestion_text( URLDecoder.decode( question.getQuestion_text(), "UTF-8" ) ); +// question.setQuestion_text( URLDecoder.decode( question.getQuestion_text(), "UTF-8" ) ); log.info( "question from JSON: %s" + json ); } catch (Exception e) { log.severe(e.toString()); @@ -140,12 +140,31 @@ public String toJSON() { return toJSON(false); } - @JSON(include = false) - @JsonIgnore - public String toJSON(boolean expanded_texts) { - return new JSONSerializer().exclude("*.class").transform(new QuestionTextTransformer(expanded_texts), "question_text", "question_expandedtext", "answer_text", "answer_expandedtext", "answers.answer_text", "answers.answer_expandedtext") - .include("answers", "event_callbacks").serialize(this); - } + @JSON( include = false ) + @JsonIgnore + public String toJSON( boolean expanded_texts ) + { + if ( ServerUtils.isInUnitTestingEnvironment() ) + { + try + { + return ServerUtils.serialize( this ); + } + catch ( Exception e ) + { + e.printStackTrace(); + return null; + } + } + else + { + return new JSONSerializer() + .exclude( "*.class" ) + .transform( new QuestionTextTransformer( expanded_texts ), "question_text", "question_expandedtext", + "answer_text", "answer_expandedtext", "answers.answer_text", "answers.answer_expandedtext" ) + .include( "answers", "event_callbacks" ).serialize( this ); + } + } @JsonIgnore @JSON(include = false) diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/impl/Q_fields.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/impl/Q_fields.java index 430bd2e5..856ecb59 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/impl/Q_fields.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/impl/Q_fields.java @@ -4,14 +4,17 @@ import java.util.HashMap; import java.util.logging.Logger; +import com.almende.dialog.TestFramework; import com.almende.dialog.model.Answer; import com.almende.dialog.model.EventCallback; import com.almende.dialog.model.intf.QuestionIntf; +import com.almende.dialog.util.ServerUtils; import com.almende.util.ParallelInit; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; +import com.thetransactioncompany.cors.HTTPMethod; public class Q_fields implements QuestionIntf { private static final long serialVersionUID = 748817624285821262L; @@ -124,13 +127,23 @@ public String getQuestion_expandedtext(String language) { url+=url.indexOf("?")>0?"&":"?"; url+="preferred_language="+language; } - WebResource webResource = client.resource(url); String text = ""; - try { - text = webResource.type("text/plain").get(String.class); - } catch (Exception e){ - log.severe(e.toString()); - } + try + { + if(!ServerUtils.isInUnitTestingEnvironment()) + { + WebResource webResource = client.resource(url); + text = webResource.type( "text/plain" ).get( String.class ); + } + else + { + text = TestFramework.fetchResponse( HTTPMethod.GET, url, null ); + } + } + catch ( Exception e ) + { + log.severe( e.toString() ); + } return text; } @Override diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/intf/AnswerIntf.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/intf/AnswerIntf.java index 6f8f3a41..f8c1591d 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/model/intf/AnswerIntf.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/model/intf/AnswerIntf.java @@ -2,6 +2,8 @@ import java.io.Serializable; +import com.fasterxml.jackson.annotation.JsonIgnore; + import flexjson.JSON; public interface AnswerIntf extends Serializable { @@ -10,8 +12,10 @@ public interface AnswerIntf extends Serializable { public String getCallback(); @JSON(include = false) + @JsonIgnore public String getAnswer_expandedtext(); @JSON(include = false) + @JsonIgnore public String getAnswer_expandedtext(String language); public void setAnswer_id(String answer_id); diff --git a/Charlotte - Java dialog tooling/src/com/almende/dialog/util/ServerUtils.java b/Charlotte - Java dialog tooling/src/com/almende/dialog/util/ServerUtils.java index de5702f5..2a8f5a94 100644 --- a/Charlotte - Java dialog tooling/src/com/almende/dialog/util/ServerUtils.java +++ b/Charlotte - Java dialog tooling/src/com/almende/dialog/util/ServerUtils.java @@ -3,6 +3,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.net.URLEncoder; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -115,19 +116,20 @@ public static boolean isInUnitTestingEnvironment() * already seen in the url * @return */ - public static String getURLWithQueryParams(String url, String queryKey, String queryValue) + public static String getURLWithQueryParams( String url, String queryKey, String queryValue ) { - String copyURL = new String(url); - if(copyURL.endsWith( "/" )) + String copyURL = new String( url ); + if ( copyURL.endsWith( "/" ) || copyURL.endsWith( URLEncoder.encode( "/" ) ) ) { copyURL = copyURL.substring( 0, copyURL.length() - 1 ); } - - if(copyURL.indexOf( "?" ) > 0) + + if ( ( copyURL.indexOf( "?" ) > 0 || copyURL.indexOf( URLEncoder.encode( "?" ) ) > 0 ) + && !copyURL.endsWith( "?" ) ) { copyURL = copyURL + "&"; } - else + else { copyURL = copyURL + "?"; } diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/TestFramework.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/TestFramework.java index 553ad462..efa65d34 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/TestFramework.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/TestFramework.java @@ -288,7 +288,7 @@ private static String getTextContent( BodyPart part ) throws MessagingException, private ServletRunner setupTestServlet() { ServletRunner servletRunner = new ServletRunner(); - servletRunner.registerServlet( "unitTestServlet", TestServlet.class.getName() ); + servletRunner.registerServlet( "/unitTestServlet/*", TestServlet.class.getName() ); return servletRunner; } @@ -307,7 +307,10 @@ public static void log(Object log) public static void storeResponseQuestionInThread(String questionText) { - responseQuestionString.set(questionText); + if(questionText != null && !questionText.isEmpty()) + { + responseQuestionString.set(questionText); + } } protected javax.mail.Message getMessageFromDetails(String remoteAddress, String localAddress, String messageText, diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/CMServletTest.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/CMServletTest.java index dc43a389..b9537500 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/CMServletTest.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/CMServletTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; import java.lang.reflect.Method; +import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; @@ -212,7 +213,7 @@ private void outBoundSMSCallXMLTest( Map addressNameMap, Adapter { String url = ServerUtils.getURLWithQueryParams( TestServlet.TEST_SERVLET_PATH, "questionType", questionInRequest.name() ); - url = ServerUtils.getURLWithQueryParams( url, "question", simpleQuestion ); + url = ServerUtils.getURLWithQueryParams( url, "question", URLEncoder.encode( simpleQuestion, "UTF-8" )); DialogAgent dialogAgent = new DialogAgent(); if ( addressNameMap.size() > 1 ) { diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/MailServletTest.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/MailServletTest.java index b82a5b8c..82ecc035 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/MailServletTest.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/MailServletTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertTrue; import java.lang.reflect.Method; +import java.net.URLEncoder; import java.util.Arrays; import java.util.HashMap; import java.util.Properties; @@ -59,8 +60,11 @@ public void sendDummyMessageTest() throws Exception @Test public void MailServletReceiveDummyMessageTest() throws Exception { + String initialAgentURL = ServerUtils.getURLWithQueryParams( TestServlet.TEST_SERVLET_PATH, "questionType", QuestionInRequest.APPOINTMENT.name()); + initialAgentURL = ServerUtils.getURLWithQueryParams( initialAgentURL, "question", "start" ); + //create mail adapter - AdapterConfig adapterConfig = createAdapterConfig( "MAIL", TEST_PUBLIC_KEY, localAddressMail, "" ); + AdapterConfig adapterConfig = createAdapterConfig( "MAIL", TEST_PUBLIC_KEY, localAddressMail, initialAgentURL ); //create session getOrCreateSession( adapterConfig, remoteAddressEmail ); @@ -181,6 +185,33 @@ public void RejectAppointmentExistingSessionMessageTest() TextMessage textMessage = mailAppointmentInteraction(TestServlet.APPOINTMENT_NO_ANSWER); assertOutgoingTextMessage(textMessage); } + + /** + * test if a URL passed into the question_text is parsed normally + * @throws Exception + */ + @Test + public void QuestionTextWithURLDoesNotCreateIssuesTest() throws Exception + { + String textMessage = "How are you doing?"; + //create mail adapter + AdapterConfig adapterConfig = createAdapterConfig( "MAIL", TEST_PUBLIC_KEY, localAddressMail, "" ); + //create session + getOrCreateSession( adapterConfig, remoteAddressEmail ); + + //fetch and invoke the receieveMessage method + HashMap addressNameMap = new HashMap(); + addressNameMap.put( remoteAddressEmail, "Test" ); + String url = TestServlet.TEST_SERVLET_PATH + TestServlet.OPEN_QUESTION_URL_WITH_SPACES + "/" + + URLEncoder.encode( textMessage, "UTF-8"); + + MailServlet mailServlet = new MailServlet(); + mailServlet.startDialog( addressNameMap, url, "test", "sendDummyMessageTest", adapterConfig ); + + Message message = super.getMessageFromDetails( remoteAddressEmail, localAddressMail, textMessage, + "sendDummyMessageTest" ); + assertOutgoingTextMessage( message ); + } /** * this is a test to test if the old message block is trimmed off @@ -233,7 +264,6 @@ private void assertOutgoingTextMessage(TextMessage textMessage) throws Exception { javax.mail.Message messageFromDetails = getMessageFromDetails(textMessage.getAddress(), textMessage.getLocalAddress(), responseQuestionString.get(), ""); - assertTrue(logObject.get() instanceof javax.mail.Message); javax.mail.Message messageLogged = (javax.mail.Message) logObject.get(); assertEquals(messageFromDetails.getFrom(), messageLogged.getFrom()); diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/VoiceXMLServletTest.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/VoiceXMLServletTest.java index f00beef6..12ed1111 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/VoiceXMLServletTest.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/VoiceXMLServletTest.java @@ -46,7 +46,7 @@ public void inbountPhoneCall_WithOpenQuestion_MissingAnswerTest() throws Excepti { String senderName = "TestUser"; String url = ServerUtils.getURLWithQueryParams( TestServlet.TEST_SERVLET_PATH, "questionType", - QuestionInRequest.OPEN_QUESTION.name() ); + QuestionInRequest.OPEN_QUESION_WITHOUT_ANSWERS.name() ); url = ServerUtils.getURLWithQueryParams( url, "question", COMMENT_QUESTION_AUDIO ); //create SMS adapter AdapterConfig adapterConfig = createAdapterConfig( "broadsoft", TEST_PUBLIC_KEY, localAddressBroadsoft, url ); @@ -79,12 +79,12 @@ public void inbountPhoneCall_WithOpenQuestion_MissingAnswerTest() throws Excepti if(mediaPropertyValue != null) { assertTrue( retryCount < i ); - assertTrue( retryCount == Integer.parseInt( mediaPropertyValue ) ); + assertTrue( retryCount == Integer.parseInt( mediaPropertyValue )); } else { assertTrue( retryCount < i ); - assertTrue( retryCount == Question.DEFAULT_MAX_QUESTION_LOAD ); + assertEquals( new Integer(Question.DEFAULT_MAX_QUESTION_LOAD), retryCount ); } } diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/XMPPServletTest.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/XMPPServletTest.java index aea0c18c..a13f66d0 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/XMPPServletTest.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/adapter/XMPPServletTest.java @@ -24,8 +24,11 @@ public class XMPPServletTest extends TestFramework @Test public void ReceiveHelpMessageTest() throws Exception { + String initialAgentURL = ServerUtils.getURLWithQueryParams( TestServlet.TEST_SERVLET_PATH, "questionType", QuestionInRequest.APPOINTMENT.name()); + initialAgentURL = ServerUtils.getURLWithQueryParams( initialAgentURL, "question", "start" ); + //create mail adapter - AdapterConfig adapterConfig = createAdapterConfig( "XMPP", TEST_PUBLIC_KEY, localAddressChat, "" ); + AdapterConfig adapterConfig = createAdapterConfig( "XMPP", TEST_PUBLIC_KEY, localAddressChat, initialAgentURL ); //create session getOrCreateSession( adapterConfig, remoteAddressEmail ); diff --git a/Charlotte - Java dialog tooling/test/com/almende/dialog/test/TestServlet.java b/Charlotte - Java dialog tooling/test/com/almende/dialog/test/TestServlet.java index 390b55b7..648578d9 100644 --- a/Charlotte - Java dialog tooling/test/com/almende/dialog/test/TestServlet.java +++ b/Charlotte - Java dialog tooling/test/com/almende/dialog/test/TestServlet.java @@ -1,6 +1,8 @@ package com.almende.dialog.test; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -11,7 +13,7 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.MediaType; -import junit.framework.Assert; +import org.junit.Assert; import com.almende.dialog.TestFramework; import com.almende.dialog.model.Answer; @@ -23,6 +25,8 @@ public class TestServlet extends HttpServlet private static final long serialVersionUID = 1L; public static final String TEST_SERVLET_PATH = "http://localhost:9000/unitTestServlet"; public static final String APPOINTMENT_MAIN_QUESTION = "Are you available today?"; + public static final String OPEN_QUESTION_URL_WITH_SPACES = "/URL WITH SPACES"; + public static final String PLAIN_TEXT_QUESTION = "/PLAIN TEXT"; public static final String APPOINTMENT_YES_ANSWER = "Yup"; public static final String APPOINTMENT_NO_ANSWER = "Nope"; public static final String APPOINTMENT_FREE_ANSWER = "Free"; @@ -30,9 +34,13 @@ public class TestServlet extends HttpServlet public static final String APPOINTMENT_REJECT_RESPONSE = "Thanks for responding to the invitation!"; public static final String APPOINTMENT_ACCEPTANCE_RESPONSE = "Thanks for accepting the invitation!"; + /** + * simple enum to generate different questions formats + * @author Shravan + */ public enum QuestionInRequest { - APPOINTMENT, SIMPLE_COMMENT, OPEN_QUESTION; + APPOINTMENT, SIMPLE_COMMENT, OPEN_QUESTION, OPEN_QUESION_WITHOUT_ANSWERS, URL_QUESTION_TEXT, PLAIN_TEXT_QUESION; } @Override @@ -40,22 +48,51 @@ protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String result = ""; - switch ( QuestionInRequest.valueOf( req.getParameter( "questionType" ) )) - { - case APPOINTMENT: - result = getAppointmentQuestion( req.getParameter( "question" ) ); - break; - case SIMPLE_COMMENT: - result = getJsonSimpleCommentQuestion( req.getParameter( "question" ) ); - break; - case OPEN_QUESTION: - result = getJsonSimpleOpenQuestion( req.getParameter( "question" ) ); - break; - default: - break; + String questionType = req.getParameter( "questionType" ); + if ( questionType != null ) + { + switch ( QuestionInRequest.valueOf(questionType) ) + { + case APPOINTMENT: + result = getAppointmentQuestion( req.getParameter( "question" ) ); + break; + case SIMPLE_COMMENT: + result = getJsonSimpleCommentQuestion( req.getParameter( "question" ) ); + break; + case OPEN_QUESTION: + result = getJsonSimpleOpenQuestion( req.getParameter( "question" ) ); + break; + case PLAIN_TEXT_QUESION: + result = req.getParameter( "question" ); + break; + case OPEN_QUESION_WITHOUT_ANSWERS: + result = getJsonSimpleOpenQuestionWithoutAnswers( req.getParameter( "question" ) ); + default: + break; + } } //store all the questions loaded in the TestFramework - TestFramework.storeResponseQuestionInThread(getResponseQuestionWithOptionsInString(result)); + if(result != null && !result.isEmpty()) + { + try + { + TestFramework.storeResponseQuestionInThread(getResponseQuestionWithOptionsInString(result)); + } + catch ( Exception e ) + { + Assert.fail( "Exception is not expected to be thrown. "+ e.getLocalizedMessage() ); + } + } + if ( result == null || result.isEmpty() + && req.getPathInfo().startsWith( OPEN_QUESTION_URL_WITH_SPACES ) ) + { + String message = req.getPathInfo().substring( OPEN_QUESTION_URL_WITH_SPACES.length() + 1 ); + result = getJsonSimpleOpenQuestion( TEST_SERVLET_PATH + PLAIN_TEXT_QUESTION + "/" + message); + } + else if ( result == null || result.isEmpty() && req.getPathInfo().startsWith( PLAIN_TEXT_QUESTION ) ) + { + result = req.getPathInfo().substring( PLAIN_TEXT_QUESTION.length() + 1 ); + } resp.getWriter().write( result ); resp.setHeader( "Content-Type", MediaType.APPLICATION_JSON ); } @@ -66,9 +103,23 @@ protected void doPost( HttpServletRequest req, HttpServletResponse resp ) { String result = ""; String appointmentTag = req.getParameter( "appointment" ); - result = getAppointmentQuestion( appointmentTag ); - //store all the questions loaded in the TestFramework - TestFramework.storeResponseQuestionInThread(getResponseQuestionWithOptionsInString(result)); + if(appointmentTag != null) + { + result = getAppointmentQuestion( appointmentTag ); + //store all the questions loaded in the TestFramework + try + { + TestFramework.storeResponseQuestionInThread(getResponseQuestionWithOptionsInString(result)); + } + catch ( Exception e ) + { + Assert.fail( "Exception is not expected to be thrown. "+ e.getLocalizedMessage() ); + } + } + else if(req.getParameter( "questionType") != null && req.getParameter( "questionType").equals( QuestionInRequest.SIMPLE_COMMENT.name() )) + { + result = getJsonSimpleCommentQuestion( req.getParameter( "question" ) ); + } resp.getWriter().write( result ); resp.setHeader( "Content-Type", MediaType.APPLICATION_JSON ); } @@ -85,10 +136,42 @@ public static String getJsonSimpleCommentQuestion(String questionText) Question question = new Question(); question.setQuestion_id( "1" ); question.setType( "comment" ); - question.setQuestion_text( "text://" + questionText ); + try + { + question.setQuestion_text( "text://" + URLDecoder.decode( questionText, "UTF-8" )); + } + catch ( UnsupportedEncodingException e ) + { + Assert.fail( e.getLocalizedMessage() ); + } return question.toJSON(); } + private String getJsonSimpleOpenQuestionWithoutAnswers( String questionText ) + { + Question question = new Question(); + question.setQuestion_id( "1" ); + question.setType( "open" ); + if(questionText.startsWith( "http://" )) + { + question.setQuestion_text( questionText ); + } + else + { + question.setQuestion_text( "text://" + questionText ); + } + question.generateIds(); + try + { + return ServerUtils.serialize( question ); + } + catch ( Exception e ) + { + Assert.fail("exception not expected. "+ e.getLocalizedMessage()); + return null; + } + } + private String getJsonSimpleOpenQuestion( String questionText ) { Question question = new Question(); @@ -102,9 +185,21 @@ private String getJsonSimpleOpenQuestion( String questionText ) { question.setQuestion_text( "text://" + questionText ); } - return question.toJSON(); + String callback = ServerUtils.getURLWithQueryParams( TEST_SERVLET_PATH , "questionType", QuestionInRequest.SIMPLE_COMMENT.name() ); + callback = ServerUtils.getURLWithQueryParams( callback, "question", "Simple%20Comment" ); + question.setAnswers( new ArrayList( Arrays.asList( new Answer( "Test answer", callback )))); + question.generateIds(); + try + { + return ServerUtils.serialize( question ); + } + catch ( Exception e ) + { + Assert.fail("exception not expected. "+ e.getLocalizedMessage()); + return null; + } } - + public static String getJsonAppointmentQuestion() { Question question = new Question(); @@ -192,34 +287,33 @@ else if ( appointmentTag.equals( APPOINTMENT_FREE_ANSWER ) ) [ Yup | Nope ] * @param questionJSON * @return + * @throws Exception */ - public static String getResponseQuestionWithOptionsInString( String questionJSON ) + public static String getResponseQuestionWithOptionsInString( String questionJSON ) throws Exception { - Question question = null; - try + Question question = ServerUtils.deserialize( questionJSON, false, Question.class ); + if ( question != null ) { - question = ServerUtils.deserialize(questionJSON, Question.class); - } - catch (Exception e) - { - Assert.fail("Serialization should be OK. Exception is not expected to be thrown"); - } - String result = question.getQuestion_expandedtext(); - if(question.getAnswers() != null && question.getType().equals("closed")) - { - result += "\n[ "; - Iterator answerIterator = question.getAnswers().iterator(); - while (answerIterator.hasNext()) + String result = question.getQuestion_expandedtext(); + if ( question.getAnswers() != null && question.getType().equals( "closed" ) ) { - result += answerIterator.next().getAnswer_expandedtext(); - if(answerIterator.hasNext()) + result += "\n[ "; + Iterator answerIterator = question.getAnswers().iterator(); + while ( answerIterator.hasNext() ) { - result += " | "; + result += answerIterator.next().getAnswer_expandedtext(); + if ( answerIterator.hasNext() ) + { + result += " | "; + } } + result += " ]"; } - result += " ]"; + return result; + } + else + { + return questionJSON; } - return result; } - }