QuestionController extends QuestionnaireController { public String addQuestion(QuestionForm questionForm, RequestContext requestContext, boolean createMode) { MessageContext context = requestContext.getMessageContext(); boolean result = validateQuestion(questionForm, context, createMode); if (result) { questionForm.addCurrentQuestion(); } return result? "success": "failure"; } @SuppressWarnings({"UnusedDeclaration"})  QuestionController();  QuestionController(QuestionnaireServiceFacade questionnaireServiceFacade); @RequestMapping("/viewQuestions.ftl") String getAllQuestions(ModelMap model); String addQuestion(QuestionForm questionForm, RequestContext requestContext, boolean createMode); String addSmartChoiceTag(QuestionForm questionForm, RequestContext requestContext, int choiceIndex); void removeQuestion(QuestionForm questionForm, String questionTitle); String createQuestions(QuestionForm questionForm, RequestContext requestContext);  }
@Test public void testAddQuestionForFailureWhenQuestionTitleNotProvided() throws Exception { QuestionForm questionForm = new QuestionForm(); questionForm.setValidator(validator); when(requestContext.getMessageContext()).thenReturn(messageContext); when(messageContext.hasErrorMessages()).thenReturn(true); String result = questionController.addQuestion(questionForm, requestContext, true); Assert.assertThat(questionForm.getQuestions().size(), is(0)); Assert.assertThat(result, is(notNullValue())); Assert.assertThat(result, is("failure")); Mockito.verify(requestContext).getMessageContext(); Mockito.verify(messageContext).addMessage(argThat(new MessageMatcher("NotNull.QuestionForm.currentQuestion.text"))); }
@Test public void testAddQuestionForFailureWhenInvalidNumericBoundsGiven() throws Exception { QuestionForm questionForm = getQuestionForm(TITLE, "number"); questionForm.getCurrentQuestion().setNumericMin(100); questionForm.getCurrentQuestion().setNumericMax(10); questionForm.setValidator(validator); when(requestContext.getMessageContext()).thenReturn(messageContext); when(messageContext.hasErrorMessages()).thenReturn(false); String result = questionController.addQuestion(questionForm, requestContext, true); Assert.assertThat(questionForm.getQuestions().size(), is(0)); Assert.assertThat(result, is("failure")); Mockito.verify(requestContext).getMessageContext(); Mockito.verify(messageContext).addMessage(argThat(new MessageMatcher(QuestionnaireConstants.INVALID_NUMERIC_BOUNDS))); }
@Test public void testAddQuestionForFailureWhenQuestionTitleProvidedWithAllBlanks() throws Exception { QuestionForm qform = new QuestionForm(); qform.setValidator(validator); qform.getCurrentQuestion().setText(" "); qform.getCurrentQuestion().setType("freeText"); when(requestContext.getMessageContext()).thenReturn(messageContext); when(messageContext.hasErrorMessages()).thenReturn(true); String result = questionController.addQuestion(qform, requestContext, true); Assert.assertThat(qform.getQuestions().size(), is(0)); Assert.assertThat(result, is(notNullValue())); Assert.assertThat(result, is("failure")); Mockito.verify(requestContext).getMessageContext(); Mockito.verify(messageContext).addMessage(argThat(new MessageMatcher("Pattern.QuestionForm.currentQuestion.text"))); }
@Test public void testAddQuestionForFailureWhenQuestionTitleIsDuplicateInForm() throws Exception { QuestionForm questionForm = new QuestionForm(); questionForm.getCurrentQuestion().setText(" " + TITLE + " "); questionForm.setQuestions(Arrays.asList(getQuestion("0", TITLE, "number"))); when(requestContext.getMessageContext()).thenReturn(messageContext); String result = questionController.addQuestion(questionForm, requestContext, true); Assert.assertThat(questionForm.getQuestions().size(), is(1)); Assert.assertThat(result, is(notNullValue())); Assert.assertThat(result, is("failure")); Mockito.verify(requestContext).getMessageContext(); Mockito.verify(messageContext).addMessage(argThat(new MessageMatcher("questionnaire.error.question.duplicate"))); }
@Test public void testAddQuestionForFailureWhenLessThanTwoChoicesAreGivenForMultiSelect() throws Exception { QuestionForm questionForm = new QuestionForm(); questionForm.getCurrentQuestion().setText(" " + TITLE + " "); questionForm.getCurrentQuestion().setType("multiSelect"); questionForm.getCurrentQuestion().setCurrentChoice("C1"); questionForm.getCurrentQuestion().addAnswerChoice(); when(requestContext.getMessageContext()).thenReturn(messageContext); String result = questionController.addQuestion(questionForm, requestContext, true); Assert.assertThat(result, is(notNullValue())); Assert.assertThat(result, is("failure")); Mockito.verify(requestContext).getMessageContext(); Mockito.verify(messageContext).addMessage(argThat(new MessageMatcher("questionnaire.error.question.choices"))); }
@Test public void testAddQuestionForSuccessWhenTwoChoicesAreGivenForSigleSelect() throws Exception { QuestionForm questionForm = new QuestionForm(); questionForm.getCurrentQuestion().setText(" " + TITLE + " "); questionForm.getCurrentQuestion().setType("singleSelect"); questionForm.getCurrentQuestion().setCurrentChoice("C1"); questionForm.getCurrentQuestion().addAnswerChoice(); questionForm.getCurrentQuestion().setCurrentChoice("C2"); questionForm.getCurrentQuestion().addAnswerChoice(); when(requestContext.getMessageContext()).thenReturn(messageContext); String result = questionController.addQuestion(questionForm, requestContext, true); Assert.assertThat(result, is(notNullValue())); Assert.assertThat(result, is("success")); Assert.assertThat(questionForm.getQuestions().size(), is(1)); }