UploadQuestionGroupController extends QuestionnaireController { public String upload(UploadQuestionGroupForm uploadQuestionGroupForm, RequestContext requestContext) { String result = "success"; if (selectedCountryNotPresent(uploadQuestionGroupForm.getSelectedCountry())) { constructErrorMessage(requestContext.getMessageContext(), "questionnaire.error.ppi.country", "selectedCountry", "Please specify the Country"); result = "failure"; } else { try { questionnaireServiceFacade.uploadPPIQuestionGroup(uploadQuestionGroupForm.getSelectedCountry()); } catch(ValidationException e) { handleValidationException(e, requestContext); result = "failure"; } catch (SystemException e) { constructErrorMessage(requestContext.getMessageContext(), e.getKey(), "selectedCountry", e.getKey()); result = "failure"; } catch (Exception e) { constructAndLogSystemError(requestContext.getMessageContext(), new SystemException(e.getMessage(), e)); result = "failure"; } } return result; } UploadQuestionGroupController();  UploadQuestionGroupController(QuestionnaireServiceFacade questionnaireServiceFacade); List<String> getAllCountriesForPPI(); String upload(UploadQuestionGroupForm uploadQuestionGroupForm, RequestContext requestContext);  }
@Test public void testUploadQuestionGroup() { UploadQuestionGroupForm form = new UploadQuestionGroupForm(); form.setSelectedCountry("INDIA"); String result = controller.upload(form, requestContext); assertThat(result, is("success")); verify(questionnaireServiceFacade).uploadPPIQuestionGroup("INDIA"); }
@Test public void testUploadQuestionGroup_ValidateForInvalidCountry() { when(requestContext.getMessageContext()).thenReturn(messageContext); UploadQuestionGroupForm form = new UploadQuestionGroupForm(); form.setSelectedCountry(QuestionnaireConstants.SELECT_ONE); String result = controller.upload(form, requestContext); assertThat(result, is("failure")); verify(requestContext).getMessageContext(); verify(messageContext).addMessage(argThat(new MessageMatcher("questionnaire.error.ppi.country"))); }
@Test public void testUploadQuestionGroup_UploadFailureDuringPPIProcessing() { when(requestContext.getMessageContext()).thenReturn(messageContext); doThrow(new SystemException(FETCH_PPI_COUNTRY_XML_FAILED)).when(questionnaireServiceFacade).uploadPPIQuestionGroup("INDIA"); UploadQuestionGroupForm form = new UploadQuestionGroupForm(); form.setSelectedCountry("INDIA"); String result = controller.upload(form, requestContext); assertThat(result, is("failure")); verify(requestContext).getMessageContext(); verify(messageContext).addMessage(argThat(new MessageMatcher(FETCH_PPI_COUNTRY_XML_FAILED))); }
@Test public void testUploadQuestionGroup_UploadFailureDueToGenericError() { when(requestContext.getMessageContext()).thenReturn(messageContext); String exceptionCode = "Random Exception"; doThrow(new RuntimeException(exceptionCode)).when(questionnaireServiceFacade).uploadPPIQuestionGroup("INDIA"); UploadQuestionGroupForm form = new UploadQuestionGroupForm(); form.setSelectedCountry("INDIA"); String result = controller.upload(form, requestContext); assertThat(result, is("failure")); verify(requestContext).getMessageContext(); verify(messageContext).addMessage(argThat(new MessageMatcher(exceptionCode))); }
@Test public void testUploadQuestionGroup_UploadFailureDuringValidation() { when(requestContext.getMessageContext()).thenReturn(messageContext); ValidationException validationException = new ValidationException(GENERIC_VALIDATION); validationException.addChildException(new ValidationException(QUESTION_GROUP_TITLE_NOT_PROVIDED)); validationException.addChildException(new ValidationException(DUPLICATE_QUESTION_FOUND_IN_SECTION)); doThrow(validationException).when(questionnaireServiceFacade).uploadPPIQuestionGroup("INDIA"); UploadQuestionGroupForm form = new UploadQuestionGroupForm(); form.setSelectedCountry("INDIA"); String result = controller.upload(form, requestContext); assertThat(result, is("failure")); verify(requestContext).getMessageContext(); verify(messageContext).addMessage(argThat(new MessageMatcher(QUESTION_GROUP_TITLE_NOT_PROVIDED))); verify(messageContext).addMessage(argThat(new MessageMatcher(DUPLICATE_QUESTION_FOUND_IN_SECTION))); }