@Test public void testPrepareResponse() { System.out.println("prepareResponse"); IncomingMessage message = new IncomingMessageImpl(); mockRespDao = createMock(IncomingMessageResponseDAO.class); expect( applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class) ).andReturn(new IncomingMessageResponseImpl()); replay(applicationContext); IncomingMessageResponse result = instance.prepareResponse(message, null); verify(applicationContext); String expResult ="Invalid request"; assertNotNull(result); assertEquals(result.getContent(), expResult); message.setIncomingMessageForm(new IncomingMessageFormImpl()); message.getIncomingMessageForm().setMessageFormStatus(IncMessageFormStatus.SERVER_VALID); reset(applicationContext); expect( applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class) ).andReturn(new IncomingMessageResponseImpl()); expect( applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class) ).andReturn(mockRespDao); expectLastCall(); expect( mockRespDao.save((IncomingMessageResponse) anyObject()) ).andReturn(null); expectLastCall(); replay(applicationContext,mockRespDao); result = instance.prepareResponse(message, null); verify(applicationContext,mockRespDao); expResult = "Data saved successfully"; assertNotNull(result); assertEquals(result.getContent(), expResult); IncomingMessageFormParameter param1 = new IncomingMessageFormParameterImpl(); param1.setName("name"); param1.setErrText("wrong format"); param1.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); IncomingMessageFormParameter param2 = new IncomingMessageFormParameterImpl(); param2.setName("age"); param2.setErrText("too long"); param2.setMessageFormParamStatus(IncMessageFormParameterStatus.INVALID); message.getIncomingMessageForm().setMessageFormStatus(IncMessageFormStatus.INVALID); message.getIncomingMessageForm().setIncomingMsgFormParameters(new HashMap<String,IncomingMessageFormParameter>()); message.getIncomingMessageForm().getIncomingMsgFormParameters().put(param1.getName(),param1); message.getIncomingMessageForm().getIncomingMsgFormParameters().put(param2.getName(),param2); reset(applicationContext,mockRespDao); expect( applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class) ).andReturn(new IncomingMessageResponseImpl()); expect( applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class) ).andReturn(mockRespDao); expectLastCall(); expect( mockRespDao.save((IncomingMessageResponse) anyObject()) ).andReturn(null); expectLastCall(); replay(applicationContext,mockRespDao); result = instance.prepareResponse(message, null); verify(applicationContext,mockRespDao); expResult = "Errors:\nage=too long\nname=wrong format"; assertNotNull(result); assertEquals(result.getContent(), expResult); param2.setName("age"); param2.setErrText("server error"); param2.setMessageFormParamStatus(IncMessageFormParameterStatus.SERVER_INVALID); message.getIncomingMessageForm().setMessageFormStatus(IncMessageFormStatus.SERVER_INVALID); message.getIncomingMessageForm().setIncomingMsgFormParameters(new HashMap<String,IncomingMessageFormParameter>()); message.getIncomingMessageForm().getIncomingMsgFormParameters().put(param2.getName(),param2); reset(applicationContext, mockRespDao); expect( applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class) ).andReturn(new IncomingMessageResponseImpl()); expect( applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class) ).andReturn(mockRespDao); expectLastCall(); expect( mockRespDao.save((IncomingMessageResponse) anyObject()) ).andReturn(null); expectLastCall(); replay(applicationContext, mockRespDao); result = instance.prepareResponse(message, null); verify(applicationContext, mockRespDao); expResult = "Errors:\nage=server error"; assertNotNull(result); assertEquals(result.getContent(), expResult); }
public IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse) { IncomingMessageForm form = message.getIncomingMessageForm(); IncomingMessageResponse response = (IncomingMessageResponse) applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class); response.setDateCreated(new Date()); response.setIncomingMessage(message); if (form == null) { response.setContent("Invalid request"); return response; } if (form.getMessageFormStatus().equals(IncMessageFormStatus.SERVER_VALID)) { if(wsResponse == null || wsResponse.isEmpty()) response.setContent("Data saved successfully"); else response.setContent(wsResponse); } else { String responseText = "Errors:"; for (Entry<String, IncomingMessageFormParameter> entry : form.getIncomingMsgFormParameters().entrySet()) { if (entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.INVALID) || entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.SERVER_INVALID)) { responseText += '\n' + entry.getValue().getName() + "=" + entry.getValue().getErrText(); } } for (String error : form.getErrors()) { responseText += '\n' + error; } if (responseText.equals("Errors:")) { responseText = "An unexpected error occurred! Please try again."; } response.setContent(responseText); } response.setMessageResponseStatus(IncMessageResponseStatus.SAVED); IncomingMessageResponseDAO responseDao = (IncomingMessageResponseDAO) applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class); try { responseDao.save(response); } catch (Exception ex) { logger.error("Error initializing form", ex); } return response; }
FormCommandAction implements CommandAction, ApplicationContextAware { public IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse) { IncomingMessageForm form = message.getIncomingMessageForm(); IncomingMessageResponse response = (IncomingMessageResponse) applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class); response.setDateCreated(new Date()); response.setIncomingMessage(message); if (form == null) { response.setContent("Invalid request"); return response; } if (form.getMessageFormStatus().equals(IncMessageFormStatus.SERVER_VALID)) { if(wsResponse == null || wsResponse.isEmpty()) response.setContent("Data saved successfully"); else response.setContent(wsResponse); } else { String responseText = "Errors:"; for (Entry<String, IncomingMessageFormParameter> entry : form.getIncomingMsgFormParameters().entrySet()) { if (entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.INVALID) || entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.SERVER_INVALID)) { responseText += '\n' + entry.getValue().getName() + "=" + entry.getValue().getErrText(); } } for (String error : form.getErrors()) { responseText += '\n' + error; } if (responseText.equals("Errors:")) { responseText = "An unexpected error occurred! Please try again."; } response.setContent(responseText); } response.setMessageResponseStatus(IncMessageResponseStatus.SAVED); IncomingMessageResponseDAO responseDao = (IncomingMessageResponseDAO) applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class); try { responseDao.save(response); } catch (Exception ex) { logger.error("Error initializing form", ex); } return response; } }
FormCommandAction implements CommandAction, ApplicationContextAware { public IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse) { IncomingMessageForm form = message.getIncomingMessageForm(); IncomingMessageResponse response = (IncomingMessageResponse) applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class); response.setDateCreated(new Date()); response.setIncomingMessage(message); if (form == null) { response.setContent("Invalid request"); return response; } if (form.getMessageFormStatus().equals(IncMessageFormStatus.SERVER_VALID)) { if(wsResponse == null || wsResponse.isEmpty()) response.setContent("Data saved successfully"); else response.setContent(wsResponse); } else { String responseText = "Errors:"; for (Entry<String, IncomingMessageFormParameter> entry : form.getIncomingMsgFormParameters().entrySet()) { if (entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.INVALID) || entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.SERVER_INVALID)) { responseText += '\n' + entry.getValue().getName() + "=" + entry.getValue().getErrText(); } } for (String error : form.getErrors()) { responseText += '\n' + error; } if (responseText.equals("Errors:")) { responseText = "An unexpected error occurred! Please try again."; } response.setContent(responseText); } response.setMessageResponseStatus(IncMessageResponseStatus.SAVED); IncomingMessageResponseDAO responseDao = (IncomingMessageResponseDAO) applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class); try { responseDao.save(response); } catch (Exception ex) { logger.error("Error initializing form", ex); } return response; }  }
FormCommandAction implements CommandAction, ApplicationContextAware { public IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse) { IncomingMessageForm form = message.getIncomingMessageForm(); IncomingMessageResponse response = (IncomingMessageResponse) applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class); response.setDateCreated(new Date()); response.setIncomingMessage(message); if (form == null) { response.setContent("Invalid request"); return response; } if (form.getMessageFormStatus().equals(IncMessageFormStatus.SERVER_VALID)) { if(wsResponse == null || wsResponse.isEmpty()) response.setContent("Data saved successfully"); else response.setContent(wsResponse); } else { String responseText = "Errors:"; for (Entry<String, IncomingMessageFormParameter> entry : form.getIncomingMsgFormParameters().entrySet()) { if (entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.INVALID) || entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.SERVER_INVALID)) { responseText += '\n' + entry.getValue().getName() + "=" + entry.getValue().getErrText(); } } for (String error : form.getErrors()) { responseText += '\n' + error; } if (responseText.equals("Errors:")) { responseText = "An unexpected error occurred! Please try again."; } response.setContent(responseText); } response.setMessageResponseStatus(IncMessageResponseStatus.SAVED); IncomingMessageResponseDAO responseDao = (IncomingMessageResponseDAO) applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class); try { responseDao.save(response); } catch (Exception ex) { logger.error("Error initializing form", ex); } return response; }  IncomingMessageResponse execute(IncomingMessage message, String requesterPhone); IncomingMessageSession initializeSession(IncomingMessage message, String requesterPhone); IncomingMessageForm initializeForm(IncomingMessage message, String formCode); IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse); IncomingMessageParser getParser(); void setParser(IncomingMessageParser parser); IncomingMessageFormValidator getFormValidator(); void setFormValidator(IncomingMessageFormValidator formValidator); void setFormProcessor(FormProcessor formProcessor); String getSenderFieldName(); void setSenderFieldName(String senderFieldName); void setApplicationContext(ApplicationContext applicationContext); }
FormCommandAction implements CommandAction, ApplicationContextAware { public IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse) { IncomingMessageForm form = message.getIncomingMessageForm(); IncomingMessageResponse response = (IncomingMessageResponse) applicationContext.getBean("incomingMessageResponse", IncomingMessageResponse.class); response.setDateCreated(new Date()); response.setIncomingMessage(message); if (form == null) { response.setContent("Invalid request"); return response; } if (form.getMessageFormStatus().equals(IncMessageFormStatus.SERVER_VALID)) { if(wsResponse == null || wsResponse.isEmpty()) response.setContent("Data saved successfully"); else response.setContent(wsResponse); } else { String responseText = "Errors:"; for (Entry<String, IncomingMessageFormParameter> entry : form.getIncomingMsgFormParameters().entrySet()) { if (entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.INVALID) || entry.getValue().getMessageFormParamStatus().equals(IncMessageFormParameterStatus.SERVER_INVALID)) { responseText += '\n' + entry.getValue().getName() + "=" + entry.getValue().getErrText(); } } for (String error : form.getErrors()) { responseText += '\n' + error; } if (responseText.equals("Errors:")) { responseText = "An unexpected error occurred! Please try again."; } response.setContent(responseText); } response.setMessageResponseStatus(IncMessageResponseStatus.SAVED); IncomingMessageResponseDAO responseDao = (IncomingMessageResponseDAO) applicationContext.getBean("incomingMessageResponseDAO", IncomingMessageResponseDAO.class); try { responseDao.save(response); } catch (Exception ex) { logger.error("Error initializing form", ex); } return response; }  IncomingMessageResponse execute(IncomingMessage message, String requesterPhone); IncomingMessageSession initializeSession(IncomingMessage message, String requesterPhone); IncomingMessageForm initializeForm(IncomingMessage message, String formCode); IncomingMessageResponse prepareResponse(IncomingMessage message, String wsResponse); IncomingMessageParser getParser(); void setParser(IncomingMessageParser parser); IncomingMessageFormValidator getFormValidator(); void setFormValidator(IncomingMessageFormValidator formValidator); void setFormProcessor(FormProcessor formProcessor); String getSenderFieldName(); void setSenderFieldName(String senderFieldName); void setApplicationContext(ApplicationContext applicationContext);  }