/**
  * Searches for the PID Identifier.
  *
  * @throws Exception
  */
 @Test
 public void testSearchForPidIdentifier() throws Exception {
   // search the item for source
   MetadataSearchQuery query = getStandardQuery();
   query.addCriterion(new MetadataSearchCriterion(CriterionType.IDENTIFIER, itemPid));
   searchAndCompareResults(query, testItem);
 }
 /**
  * Searches for the topic.
  *
  * @throws Exception
  */
 @Test
 public void testSearchForTopic() throws Exception {
   // search the item for source
   MetadataSearchQuery query = getStandardQuery();
   query.addCriterion(new MetadataSearchCriterion(CriterionType.TOPIC, abstractText));
   searchAndCompareResults(query, testItem);
 }
 /**
  * Searches for the event's title.
  *
  * @throws Exception
  */
 @Test
 public void testSearchForEvent() throws Exception {
   // search the item for source
   MetadataSearchQuery query = getStandardQuery();
   query.addCriterion(new MetadataSearchCriterion(CriterionType.EVENT, eventTitle));
   searchAndCompareResults(query, testItem);
 }
  /**
   * Searches for 2 date ranges in 2 different dates.
   *
   * @throws Exception
   */
  @Test
  public void testSearchForConcreteDates() throws Exception {

    // search the item

    MetadataSearchQuery query = getStandardQuery();

    ArrayList<CriterionType> criterionList = new ArrayList<CriterionType>();
    criterionList.add(CriterionType.DATE_ACCEPTED);
    query.addCriterion(new MetadataDateSearchCriterion(criterionList, "2008-05-01", "2008-05-31"));

    /*
    ArrayList<CriterionType> criterionList2 = new ArrayList<CriterionType>();
    criterionList2.add(CriterionType.DATE_CREATED);
    MetadataDateSearchCriterion mdsc = new MetadataDateSearchCriterion(criterionList2, "2005-04-01", "2005-04-31");
    //mdsc.setLogicalOperator(LogicalOperator.AND);
    query.addCriterion(mdsc);
    */
    /*
    ArrayList<CriterionType> criterionList2 = new ArrayList<CriterionType>();
    criterionList2.add(CriterionType.DATE_PUBLISHED_ONLINE);
    MetadataDateSearchCriterion mdsc = new MetadataDateSearchCriterion(criterionList2, "2008-01-01", "2008-01-01");
    mdsc.setLogicalOperator(LogicalOperator.AND);
    query.addCriterion(mdsc);
    */
    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.TITLE, itemTitle, LogicalOperator.AND));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for the organization.
   *
   * @throws Exception
   */
  @Test
  public void testSearchForOrganization() throws Exception {
    // search the item for source
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.ORGANIZATION, organizationName));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for the title, using the ANY criterion (simple search without file).
   *
   * @throws Exception
   */
  @Test
  public void testSimpleSearchForAny() throws Exception {

    // search the item
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.ANY, itemTitle));

    searchAndCompareResults(query, testItem);
  }
 /**
  * Searches for the escidoc Identifier.
  *
  * @throws Exception
  */
 @Test
 public void testSearchForEscidocIdentifier() throws Exception {
   // search the item for source
   MetadataSearchQuery query = getStandardQuery();
   query.addCriterion(
       new MetadataSearchCriterion(
           CriterionType.IDENTIFIER, testItem.getVersion().getObjectId(), LogicalOperator.AND));
   searchAndCompareResults(query, testItem);
 }
  /**
   * Searches for the genre.
   *
   * @throws Exception
   */
  @Test
  public void testSearchForGenre() throws Exception {
    // search the item for source
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.GENRE, genreName));
    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.TITLE, itemTitle, LogicalOperator.AND));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for creator's name and role.
   *
   * @throws Exception
   */
  @Test
  public void testSearchForCreator() throws Exception {

    // search the item for creator
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.PERSON, creatorsFamilyName));
    // query.addCriterion(new MetadataSearchCriterion(CriterionType.PERSON_ROLE,
    // creatorsRole.toString(), LogicalOperator.AND));
    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for an item title using logical operator *.
   *
   * @throws Exception
   */
  @Test
  public void testSearchWithAsteriskLogicalOperators() throws Exception {

    // remvove last characters from title and replace with logical operator
    String searchTerm = itemTitle.substring(0, itemTitle.length() - 3);
    searchTerm = searchTerm + "*";
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.TITLE, searchTerm));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for an item with file (simple search with file).
   *
   * @throws Exception
   */
  @Test
  // @Ignore
  public void testSimpleSearchWithFile() throws Exception {

    // search the item
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(
        new MetadataSearchCriterion(
            CriterionType.ANY_INCLUDE, itemTitle + " " + "\"Antoine de Saint-Exupery\""));

    searchAndCompareResults(query, testItem);
  }
  // TODO: Get the test running after release 3.8.2
  @Test
  @Ignore
  public void testSearchForSpecialCharacters() throws Exception {
    AccountUserVO user = getUserTestDepLibWithHandle();
    // create new test item

    PubItemVO myItem = getNewPubItemWithoutFiles();

    // title with special characters äöüß/@ §$%=`´'_
    String title = "Tößt. Itäm für @MPDL §23'1_2/5=6 àé 30,%" + System.nanoTime();
    myItem.getMetadata().getTitle().setValue(title);

    // creator name with escape characters
    String givenName = "Hans" + System.nanoTime();
    String familyName = "Meier-Müller (von{und&}|[evtl.]) zu^ Lauen+stein!" + System.nanoTime();
    myItem.getMetadata().getCreators().get(0).getPerson().setGivenName(givenName);
    myItem.getMetadata().getCreators().get(0).getPerson().setFamilyName(familyName);
    myItem
        .getMetadata()
        .getCreators()
        .get(0)
        .getPerson()
        .setCompleteName(givenName + " " + familyName);

    ItemVO createdItem = createItem(myItem, user);
    assertNotNull(createdItem);

    ItemVO releasedItem = submitAndReleaseItem(createdItem, user);
    assertNotNull(testItem);

    logger.info(
        "Test Item for special characters created: object id="
            + testItem.getVersion().getObjectId());

    // wait a little bit for indexing...
    // if test fails, the time given for indexing might be too short
    // (with Thread.sleep(2000) the test sometimes failed.
    Thread.sleep(5000);

    // search for title
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.TITLE, title));

    searchAndCompareResults(query, releasedItem);

    // search for creator
    query = getStandardQuery();
    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.PERSON, givenName + " " + familyName));

    searchAndCompareResults(query, releasedItem);
  }
  @Test
  public void testOrganizationalSearch() throws Exception {
    OrgUnitsSearchResult result = null;
    try {
      String testTitle = "MPS";
      MetadataSearchQuery query = getStandardQuery();
      query.addCriterion(
          new MetadataSearchCriterion(CriterionType.ANY, testTitle, LogicalOperator.AND));

      result = itemContainerSearch.searchForOrganizationalUnits(query);
    } catch (ParseException e) {
    }
  }
  /**
   * Searches for one date range in all dates.
   *
   * @throws Exception
   */
  @Test
  public void testSearchForAnyDates() throws Exception {

    // search the item
    MetadataSearchQuery query = getStandardQuery();
    ArrayList<CriterionType> criterionList = new ArrayList<CriterionType>();
    criterionList.add(CriterionType.DATE_ANY);
    query.addCriterion(new MetadataDateSearchCriterion(criterionList, "2007-05-01", "2008-05-31"));

    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.TITLE, itemTitle, LogicalOperator.AND));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for an title that should not exist.
   *
   * @throws Exception
   */
  @Test
  public void testSearchNoResults() throws Exception {
    // search the item for source
    String testTitle = "Der kleine Prinz" + System.nanoTime();
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.TITLE, testTitle, LogicalOperator.AND));

    ItemContainerSearchResult result = itemContainerSearch.searchForItemContainer(query);

    List<SearchResultElement> resultList = result.getResultList();

    assertEquals("Wrong number of search results", 0, resultList.size());
  }
  /**
   * Searches for an title that should not exist.
   *
   * @throws Exception
   */
  @Test
  public void testSearchEmptyString() throws Exception {
    // search the item for source
    ItemContainerSearchResult result;
    try {
      String testTitle = "";
      MetadataSearchQuery query = getStandardQuery();
      query.addCriterion(
          new MetadataSearchCriterion(CriterionType.TITLE, testTitle, LogicalOperator.AND));

      result = itemContainerSearch.searchForItemContainer(query);
      fail("ParseException expected");
    } catch (ParseException e) {
    }
  }
  /**
   * Searches for an item title using logical operator ?.
   *
   * @throws Exception
   */
  @Test
  public void testSearchWithQuestionmarkLogicalOperator() throws Exception {

    // remvove last characters from title and replace with logical operator
    String searchTermStart = itemTitle.substring(0, itemTitle.length() - 5);
    String searchTermEnd = itemTitle.substring(itemTitle.length() - 4, itemTitle.length());
    String searchTerm = searchTermStart + "?" + searchTermEnd;

    System.out.println("SearchTerm: " + searchTerm);
    System.out.println("NormalTerm: " + itemTitle);

    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.TITLE, searchTerm));

    searchAndCompareResults(query, testItem);
  }
  /**
   * Searches for the item with multiple metadata in the query (Advanced Search).
   *
   * @throws Exception
   */
  @Test
  // @Ignore
  public void testSearchForMultipleMetadata() throws Exception {
    // search the item for source
    MetadataSearchQuery query = getStandardQuery();
    query.addCriterion(new MetadataSearchCriterion(CriterionType.TITLE, itemTitle));
    query.addCriterion(
        new MetadataSearchCriterion(CriterionType.PERSON, creatorsFamilyName, LogicalOperator.AND));
    ArrayList<CriterionType> criterionList = new ArrayList<CriterionType>();
    criterionList.add(CriterionType.DATE_PUBLISHED_ONLINE);
    MetadataDateSearchCriterion mdsc =
        new MetadataDateSearchCriterion(criterionList, "2008-01-01", "2008-01-01");
    mdsc.setLogicalOperator(LogicalOperator.AND);
    query.addCriterion(mdsc);

    searchAndCompareResults(query, testItem);
  }