Skip to content

Commit

Permalink
fixed issue with decoded URL to fetch the question_Text (issue #15).
Browse files Browse the repository at this point in the history
added a test.
  • Loading branch information
shravanjc committed Nov 4, 2013
1 parent 867a6e1 commit 4227fe8
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;

import flexjson.JSON;

public interface AnswerIntf extends Serializable {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "?";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -212,7 +213,7 @@ private void outBoundSMSCallXMLTest( Map<String, String> 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 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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<String, String> addressNameMap = new HashMap<String, String>();
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
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Loading

0 comments on commit 4227fe8

Please sign in to comment.