@Test
  public void executeFromManageItems() throws ServletException, IOException {
    final Site site = TestUtil.createSite();
    TestUtil.createUserAndUserOnSiteRightAndLogin(site, SiteAccessLevel.ADMINISTRATOR);

    final ContactUs contactUs = TestUtil.createContactUs(site);

    final CreateContactUsWidgetRequest request = new CreateContactUsWidgetRequest();
    request.setContactUsName("name");
    request.setDisplayHeader(true);
    request.setEmail("[email protected]");
    request.setHeader("header");
    request.setContactUsId(contactUs.getId());

    DraftFormItem item = new DraftFormItem();
    item.setPosition(0);
    item.setRequired(true);
    item.setFormItemName(FormItemName.FIRST_NAME);
    ServiceLocator.getPersistance().putFormItem(item);
    List<DraftFormItem> items = new ArrayList<DraftFormItem>();
    items.add(item);
    request.setFormItems(items);

    final FunctionalWidgetInfo response = service.execute(request);

    Assert.assertNotNull(response);
    Assert.assertNull(response.getWidget());
    Assert.assertEquals("name", contactUs.getName());
    Assert.assertEquals("header", contactUs.getDescription());
    Assert.assertTrue(contactUs.isShowDescription());
    Assert.assertEquals(FormType.CONTACT_US, contactUs.getType());
  }
  @Test(expected = InvalidContactUsEmailException.class)
  public void executeWithoutEmail() throws ServletException, IOException {
    final Site site = TestUtil.createSite();
    TestUtil.createUserAndUserOnSiteRightAndLogin(site, SiteAccessLevel.ADMINISTRATOR);

    final ContactUs contactUs = TestUtil.createContactUs(site);

    final CreateContactUsWidgetRequest request = new CreateContactUsWidgetRequest();
    request.setContactUsName("name");
    request.setDisplayHeader(true);
    request.setEmail(null);
    request.setHeader("header");
    request.setContactUsId(contactUs.getId());

    DraftFormItem item = new DraftFormItem();
    item.setPosition(0);
    item.setRequired(true);
    item.setFormItemName(FormItemName.FIRST_NAME);
    ServiceLocator.getPersistance().putFormItem(item);
    List<DraftFormItem> items = new ArrayList<DraftFormItem>();
    items.add(item);
    request.setFormItems(items);

    service.execute(request);
  }
  @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());
  }