Skip to content

Commit

Permalink
60:Fix for unknown keywords in message
Browse files Browse the repository at this point in the history
  • Loading branch information
subhrajitroy committed Jun 30, 2011
1 parent 5470322 commit e144643
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.motechproject.ws.RequestParameterBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class IncomingMessage {

Expand Down Expand Up @@ -57,7 +56,7 @@ public void setTime(String time) {
this.time = time;
}

public Boolean hasInformation() {
public Boolean hasSufficientInformation() {
return isNotBlank(key) && isNotBlank(text) && isNotBlank(code);
}

Expand Down Expand Up @@ -95,4 +94,8 @@ public String requestParameters() throws UnsupportedEncodingException {
public String toString() {
return "Issue { " + text + " } sent from " + number;
}

public Boolean isFor(String key) {
return key.equalsIgnoreCase(this.key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class MessageProcessorDAOImpl implements MessageProcessorDAO {

public MessageProcessorURL urlFor(String keyword) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MessageProcessorURL.class);
criteria.add(Restrictions.eq("key",keyword));
return (MessageProcessorURL) criteria.uniqueResult();
criteria.add(Restrictions.eq("key", keyword));
Object result = criteria.uniqueResult();
return result != null ? (MessageProcessorURL) result : null;
}

public List<MessageProcessorURL> list() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class MailingConstants {

public static final String INSUFFICIENT_INFO_FOR_SUPPORT_CASE = "Welcome to MOTECH. SMS should be in format <SUPPORT> <StaffID> <Issue>";
public static final String INSUFFICIENT_INFO_FOR_SUPPORT_CASE = "Welcome to MOTECH. Your keyword is invalid. Please use the correct format to send your message";
public static final String FAILED_TO_RAISE_SUPPORT_CASE = "Welcome to MOTECH . Support case could not be raised";
public static final String SUPPORT_CASE_CREATION_ACKNOWLEDGEMENT = "Welcome to MOTECH. Your message has been submitted to the support team";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ public class IncomingMessageController {
@RequestMapping(value = "/module/motechmodule/incomingmessage",method = RequestMethod.GET)
public String redirect(IncomingMessage incomingMessage) throws UnsupportedEncodingException {
log(incomingMessage);
MessageProcessorURL url = dao.urlFor(incomingMessage.getKey());
MessageProcessorURL url = processorURL(incomingMessage);
return new StringBuilder().append(REDIRECT).append(url.toString())
.append(incomingMessage.requestParameters()).toString();
}

private MessageProcessorURL processorURL(IncomingMessage incomingMessage) {
MessageProcessorURL processorURL = dao.urlFor(incomingMessage.getKey());
if(processorURL == null){
processorURL = dao.urlFor("DEFAULT");
}
return processorURL;
}


private void log(IncomingMessage incomingMessage) {
dao.save(incomingMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ModelAndView mailToSupport(@ModelAttribute IncomingMessage incomingMessag
log.info("Received support case request " + incomingMessage);
ModelAndView response = new ModelAndView(VIEW);
try {
if (incomingMessage.hasInformation()) {
if (incomingMessage.hasSufficientInformation() && incomingMessage.isFor("SUPPORT")) {
return sendSupportMail(incomingMessage);
}
response.addObject(RESPONSE, INSUFFICIENT_INFO_FOR_SUPPORT_CASE);
Expand Down
5 changes: 5 additions & 0 deletions motech-server-omod/src/main/resources/message-processor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
<property name="resourceLoaderPath" value="classpath:org/motechproject/server/omod/web/model"/>
</bean>

<bean id="defaultURLProcessor" class="org.motechproject.server.model.MessageProcessorURL">
<constructor-arg index="0" value="SUPPORT"/>
<constructor-arg index="1" value="redirect:/module/motechmodule/supportcase.form"/>
</bean>

</beans>
10 changes: 10 additions & 0 deletions motech-server-omod/src/main/resources/sqldiff.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3284,4 +3284,14 @@
</sql>
</diff>

<diff>
<version>5.3</version>
<author>Thoughtworks</author>
<date>June 30 2011</date>
<description>Adding default URL for #60</description>
<sql>
insert into motechmodule_url(message_key,url) values('DEFAULT','/module/motechmodule/supportcase.form');
</sql>
</diff>

</sqldiff>
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,33 @@ public void shouldRedirectAccordingToKeyword() throws UnsupportedEncodingExcepti
assertEquals("redirect:/module/motechmodule/supportcase.form?text=Hi&number=%2B233123456789&key=SUPPORT&time=2011-03-03+10%3A10%3A10&code=1982",redirectedUrl);
}

@Test
public void shouldRedirectToSupportIfUrlNotMapped() throws UnsupportedEncodingException {

MessageProcessorDAO dao = createMock(MessageProcessorDAO.class);
controller.setDao(dao);

IncomingMessage message = new IncomingMessage();
message.setKey("TEST");
message.setText("Hi");

dao.save(message);
expectLastCall();

expect(dao.urlFor("TEST"))
.andReturn(null);

expect(dao.urlFor("DEFAULT"))
.andReturn(new MessageProcessorURL("DEFAULT","/module/motechmodule/supportcase.form"));

replay(dao);

String redirectedUrl = controller.redirect(message);

verify(dao);

assertEquals("redirect:/module/motechmodule/supportcase.form?text=Hi&key=TEST",redirectedUrl);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public void shouldSendErrorMessageBackIfCompleteInformationNotPresent() {
assertEquals(MailingConstants.INSUFFICIENT_INFO_FOR_SUPPORT_CASE, modelAndView.getModelMap().get("response"));
}

@Test
public void shouldSendErrorMessageBackIfKeyIsNotSupport() {
SupportCaseController controller = new SupportCaseController();
IncomingMessage message = new IncomingMessage();
message.setText("TEST 465 Hello");
message.setKey("TEST");
message.setCode("1982");
ModelAndView modelAndView = controller.mailToSupport(message);
assertEquals("/module/motechmodule/response", modelAndView.getViewName());
assertEquals(MailingConstants.INSUFFICIENT_INFO_FOR_SUPPORT_CASE, modelAndView.getModelMap().get("response"));
}

@Test
public void shouldMailToSupport() {

Expand Down

0 comments on commit e144643

Please sign in to comment.