@Test
  public void testGetSortedFilledFormsFromSession() {
    User user = TestUtil.createUserAndLogin("aa");

    Site site1 = TestUtil.createSite("title1", "url1");
    TestUtil.createUserOnSiteRightActive(user, site1, SiteAccessLevel.ADMINISTRATOR);

    List<DraftFormItem> items = TestUtil.createDefaultFormItemsForGallery();
    final DraftCustomForm customForm = TestUtil.createCustomForm(site1.getSiteId(), "form1", items);
    DraftGallery gallery =
        TestUtil.createGallery(site1.getSiteId(), "galleryName1", "commentsNotes1", customForm);
    gallery.setRows(1);
    gallery.setColumns(5);

    // ---------------------------------------------filled
    // forms---------------------------------------------
    List<FilledForm> filledForms = new ArrayList<FilledForm>();
    for (int i = 0; i < 30; i++) {
      final List<FilledFormItem> filledFormItems = new ArrayList<FilledFormItem>();
      for (DraftFormItem formItem : items) {
        FilledFormItem filledFormItem =
            TestUtil.createFilledFormItem(
                formItem.getFormItemId(), formItem.getFormItemName(), formItem.getItemName() + i);
        filledFormItems.add(filledFormItem);
      }
      filledForms.add(TestUtil.createFilledFormByFormId(customForm.getFormId(), filledFormItems));
    }
    // ---------------------------------------------filled
    // forms---------------------------------------------

    gallery.setFirstSortItemId(items.get(0).getFormItemId());
    gallery.setSecondSortItemId(items.get(1).getFormItemId());

    gallery.setFirstSortType(GallerySortOrder.ASCENDING);
    gallery.setSecondSortType(GallerySortOrder.DESCENDING);

    Assert.assertNull(filledFormsInSession.get(gallery, null));
    filledFormsInSession.set(gallery, filledForms, null);
    List<Integer> filledFormsIds = filledFormsInSession.get(gallery, null);
    Assert.assertNotNull(filledFormsIds);
    Assert.assertEquals(30, filledFormsIds.size());

    Assert.assertNull(filledFormsInSession.get(null, null));
    Assert.assertNull(filledFormsInSession.get(new DraftGallery(), null));

    Assert.assertEquals(filledForms.get(0).getFilledFormId(), ((int) filledFormsIds.get(0)));
    Assert.assertEquals(filledForms.get(1).getFilledFormId(), ((int) filledFormsIds.get(1)));
    Assert.assertEquals(filledForms.get(2).getFilledFormId(), ((int) filledFormsIds.get(2)));

    Assert.assertEquals(
        5,
        filledFormsInSession
            .reduceSortedItems(filledFormsIds, (gallery.getRows() * gallery.getColumns()), 3)
            .size());
  }
  @Test
  public void execute() throws Exception {
    TestUtil.createUserAndLogin();
    final Site site = TestUtil.createSite();

    final PageManager pageVersion = TestUtil.createPageVersionSiteUser();
    Widget widgetCustomForm = TestUtil.createCustomFormWidget(pageVersion);
    final DraftCustomForm customForm = TestUtil.createCustomForm(site);

    final List<FilledFormItem> filledFormItems =
        TestUtil.createFilledFormItems(FormItemName.FIRST_NAME, FormItemName.LAST_NAME);
    filledFormItems.get(0).setValue("FN1");
    filledFormItems.get(1).setValue("LN1");

    final SubmitCustomFormRequest request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(widgetCustomForm.getWidgetId());
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setVerificationCode("aaaaaa");
    request.setFilledFormItems(filledFormItems);
    ServiceLocator.getSessionStorage()
        .setNoBotCode(null, "customForm" + widgetCustomForm.getWidgetId(), "aaaaaa");

    final SubmitCustomFormResponse response = service.execute(request);

    Assert.assertNull(response.getNextPageHtml());

    final FilledForm filledCustomForm =
        ServiceLocator.getPersistance().getFilledFormById(response.getFilledFormId());
    Assert.assertNotNull(filledCustomForm);
    Assert.assertEquals(
        filledFormItems.get(0).getValues(),
        filledCustomForm.getFilledFormItems().get(0).getValues());
    Assert.assertEquals(
        filledFormItems.get(1).getValues(),
        filledCustomForm.getFilledFormItems().get(1).getValues());
  }
  @Test(expected = WrongVerificationCodeException.class)
  public void executeWithWrongVerificationCode() throws Exception {
    TestUtil.createUserAndLogin();
    final Site site = TestUtil.createSite();

    final PageManager pageVersion = TestUtil.createPageVersionSiteUser();
    Widget widgetCustomForm = TestUtil.createCustomFormWidget(pageVersion);
    final DraftCustomForm customForm = TestUtil.createCustomForm(site);

    final List<FilledFormItem> filledFormItems =
        TestUtil.createFilledFormItems(FormItemName.FIRST_NAME, FormItemName.LAST_NAME);
    filledFormItems.get(0).setValue("FN1");
    filledFormItems.get(1).setValue("LN1");

    final SubmitCustomFormRequest request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(widgetCustomForm.getWidgetId());
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setFilledFormItems(filledFormItems);
    ServiceLocator.getSessionStorage()
        .setNoBotCode(null, "customForm" + widgetCustomForm.getWidgetId(), "aaaaaa");

    service.execute(request);
  }
  @Test
  public void executeWithPageBreaks() throws Exception {
    TestUtil.createUserAndLogin();
    final Site site = TestUtil.createSite();

    final PageManager pageVersion = TestUtil.createPageVersionSiteUser();
    Widget widgetCustomForm = TestUtil.createCustomFormWidget(pageVersion);
    final DraftCustomForm customForm = TestUtil.createCustomForm(site);

    final List<DraftFormItem> formItems = new ArrayList<DraftFormItem>();
    formItems.add(TestUtil.createFormItem(FormItemName.FIRST_NAME, 0));
    formItems.add(TestUtil.createFormItem(FormItemName.PAGE_BREAK, 1));
    formItems.add(TestUtil.createFormItem(FormItemName.LAST_NAME, 2));
    customForm.setFormItems(formItems);

    // FIRST PAGE
    List<FilledFormItem> filledFormItems = TestUtil.createFilledFormItems(FormItemName.FIRST_NAME);
    filledFormItems.get(0).setValue("FN1");

    SubmitCustomFormRequest request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(widgetCustomForm.getWidgetId());
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setRequestNextPage(true);
    request.setPageBreaksToPass(1);
    request.setVerificationCode("aaaaaa");
    request.setFilledFormItems(filledFormItems);
    ServiceLocator.getSessionStorage()
        .setNoBotCode(null, "customForm" + widgetCustomForm.getWidgetId(), "aaaaaa");

    SubmitCustomFormResponse response = service.execute(request);

    Assert.assertNotNull(response.getNextPageHtml());

    FilledForm filledContactUsForm =
        ServiceLocator.getPersistance().getFilledFormById(response.getFilledFormId());
    Assert.assertNotNull(filledContactUsForm);
    Assert.assertEquals(
        filledFormItems.get(0).getValues(),
        filledContactUsForm.getFilledFormItems().get(0).getValues());

    // SECOND PAGE
    filledFormItems = TestUtil.createFilledFormItems(FormItemName.LAST_NAME);
    filledFormItems.get(0).setValue("LN1");

    request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(widgetCustomForm.getWidgetId());
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setRequestNextPage(true);
    request.setFilledFormId(response.getFilledFormId());
    request.setPageBreaksToPass(2);
    request.setFilledFormItems(filledFormItems);

    response = service.execute(request);

    // Ok, now after we have filled out last form page we need to show first form page again with
    // the
    // message about successfull form submit.
    Assert.assertNotNull(response.getNextPageHtml());
    Assert.assertTrue(response.isShowSuccessfullSubmitMessage());

    filledContactUsForm =
        ServiceLocator.getPersistance().getFilledFormById(response.getFilledFormId());
    Assert.assertEquals(
        filledFormItems.get(0).getValues(),
        filledContactUsForm.getFilledFormItems().get(1).getValues());
  }
  @Test
  public void executeWithPageBreaksFromAddRecord() throws Exception {
    TestUtil.createUserAndLogin();
    final Site site = TestUtil.createSite();

    final DraftCustomForm customForm = TestUtil.createCustomForm(site);

    final List<DraftFormItem> formItems = new ArrayList<DraftFormItem>();
    formItems.add(TestUtil.createFormItem(FormItemName.FIRST_NAME, 0));
    formItems.add(TestUtil.createFormItem(FormItemName.PAGE_BREAK, 1));
    formItems.add(TestUtil.createFormItem(FormItemName.LAST_NAME, 2));
    customForm.setFormItems(formItems);

    // FIRST PAGE
    List<FilledFormItem> filledFormItems = TestUtil.createFilledFormItems(FormItemName.FIRST_NAME);
    filledFormItems.get(0).setValue("FN1");

    SubmitCustomFormRequest request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(0);
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setRequestNextPage(true);
    request.setPageBreaksToPass(1);
    request.setVerificationCode("aaaaaa");
    request.setFilledFormItems(filledFormItems);
    request.setShowFromAddRecord(true);

    SubmitCustomFormResponse response = service.execute(request);
    Assert.assertEquals(
        "/site/showCustomForm.action?formId="
            + request.getFormId()
            + "&showFromAddRecord=true"
            + "&pageBreaksToPass="******"&filledFormToUpdateId="
            + response.getFilledFormId(),
        response.getNextPageHtml());

    FilledForm filledContactUsForm =
        ServiceLocator.getPersistance().getFilledFormById(response.getFilledFormId());
    Assert.assertNotNull(filledContactUsForm);
    Assert.assertEquals(
        filledFormItems.get(0).getValues(),
        filledContactUsForm.getFilledFormItems().get(0).getValues());

    // SECOND PAGE
    filledFormItems = TestUtil.createFilledFormItems(FormItemName.LAST_NAME);
    filledFormItems.get(0).setValue("LN1");

    request = new SubmitCustomFormRequest();
    request.setFormId(customForm.getFormId());
    request.setWidgetId(0);
    request.setFilledFormItems(new ArrayList<FilledFormItem>());
    request.setShowFromAddRecord(true);
    request.setRequestNextPage(true);
    request.setFilledFormId(response.getFilledFormId());
    request.setPageBreaksToPass(2);
    request.setFilledFormItems(filledFormItems);

    response = service.execute(request);

    Assert.assertNull(response.getNextPageHtml());

    filledContactUsForm =
        ServiceLocator.getPersistance().getFilledFormById(response.getFilledFormId());
    Assert.assertEquals(
        filledFormItems.get(0).getValues(),
        filledContactUsForm.getFilledFormItems().get(1).getValues());
  }