public void testFindByPrimaryKeyExisting() throws Exception {
    Content content = addContent();

    Content existingContent = _persistence.findByPrimaryKey(content.getContentId());

    assertTrue(existingContent.equals(content));
  }
  public void testUpdateNew() throws Exception {
    Content content = addContent();

    Blob data = content.getData();

    assertFalse(data.getClass().equals(OutputBlob.class));
    assertEquals(1024, data.length());
  }
  protected Content addContent() throws Exception {
    Content content = createContent();

    _persistence.update(content);

    // Reload for a fresh Blob

    content = _persistence.findByPrimaryKey(content.getContentId());

    return content;
  }
  public void testUpdateExisting() throws Exception {
    Content content = addContent();

    byte[] bytes = new byte[1024];

    Blob data = new OutputBlob(new ByteArrayInputStream(bytes), bytes.length);

    Content newContent =
        new Content(
            content.getContentId(),
            nextLong(),
            randomString(),
            nextLong(),
            nextLong(),
            randomString(),
            randomString(),
            data,
            data.length());

    _persistence.update(newContent);

    Content existingContent = _persistence.findByPrimaryKey(newContent.getContentId());

    assertTrue(existingContent.equals(newContent));
  }
  public void testRemoveByC_R_P_V() throws Exception {
    Content content = addContent();

    Content existingContent = _persistence.fetchByPrimaryKey(content.getContentId());

    assertNotNull(existingContent);

    boolean removed =
        _persistence.removeByC_R_P_V(
            existingContent.getCompanyId(), existingContent.getRepositoryId(),
            existingContent.getPath(), existingContent.getVersion());

    assertTrue(removed);

    existingContent = _persistence.fetchByPrimaryKey(content.getContentId());

    assertNull(existingContent);
  }
  public void testRemove() throws Exception {
    Content content = addContent();

    Content existingContent = _persistence.fetchByPrimaryKey(content.getContentId());

    assertNotNull(existingContent);
    assertTrue(existingContent.equals(content));

    _persistence.remove(content.getContentId());

    existingContent = _persistence.fetchByPrimaryKey(content.getContentId());

    assertNull(existingContent);
  }
  public void testFetchByC_R_P() throws Exception {
    Content content1 = addContent();

    long contentId2 = content1.getContentId() + 1;

    String version2 = content1.getVersion().concat("-2");

    int size = (int) content1.getSize();

    Blob data = new OutputBlob(new ByteArrayInputStream(new byte[size]), size);

    Content content2 =
        new Content(
            contentId2,
            content1.getCompanyId(),
            content1.getPortletId(),
            content1.getGroupId(),
            content1.getRepositoryId(),
            content1.getPath(),
            version2,
            data,
            size);

    _persistence.update(content2);

    Content existingContent2 =
        _persistence.fetchByC_R_P(
            content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

    assertTrue(existingContent2.equals(content2));

    _persistence.remove(existingContent2.getContentId());

    Content existingContent1 =
        _persistence.fetchByC_R_P(
            content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

    assertTrue(existingContent1.equals(content1));

    _persistence.remove(existingContent1.getContentId());

    Content missingContent =
        _persistence.fetchByC_R_P(
            content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

    assertNull(missingContent);
  }
  public void testCountByC_R_P_V() throws Exception {
    Content content = createContent();

    int count =
        _persistence.countByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), content.getVersion());

    assertEquals(0, count);

    _persistence.update(content);

    count =
        _persistence.countByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), content.getVersion());

    assertEquals(1, count);

    _persistence.remove(content.getContentId());

    count =
        _persistence.countByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), content.getVersion());

    assertEquals(0, count);
  }
  public void testFindByC_R_P_V() throws Exception {
    Content content = addContent();

    Content existingContent =
        _persistence.findByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), content.getVersion());

    assertTrue(existingContent.equals(content));

    _persistence.remove(existingContent.getContentId());

    try {
      _persistence.findByC_R_P_V(
          content.getCompanyId(), content.getRepositoryId(), content.getPath(), randomString());

      fail("Missing entity did not throw NoSuchContentException");
    } catch (NoSuchContentException nsce) {
    }
  }
  public void testFindByC_R_P() throws Exception {
    Content content1 = addContent();

    long contentId2 = content1.getContentId() + 1;

    String version2 = content1.getVersion().concat("-2");

    int size = (int) content1.getSize();

    Blob data = new OutputBlob(new ByteArrayInputStream(new byte[size]), size);

    Content content2 =
        new Content(
            contentId2,
            content1.getCompanyId(),
            content1.getPortletId(),
            content1.getGroupId(),
            content1.getRepositoryId(),
            content1.getPath(),
            version2,
            data,
            size);

    _persistence.update(content2);

    Content existingContent2 =
        _persistence.findByC_R_P(
            content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

    assertTrue(existingContent2.equals(content2));

    _persistence.remove(existingContent2.getContentId());

    Content existingContent1 =
        _persistence.findByC_R_P(
            content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

    assertTrue(existingContent1.equals(content1));

    _persistence.remove(existingContent1.getContentId());

    try {
      _persistence.findByC_R_P(
          content1.getCompanyId(), content1.getRepositoryId(), content1.getPath());

      fail("Missing entity did not throw NoSuchContentException");
    } catch (NoSuchContentException nsce) {
    }
  }
  public void testFetchByC_R_P_V() throws Exception {
    Content content = addContent();

    Content existingContent =
        _persistence.fetchByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), content.getVersion());

    assertTrue(existingContent.equals(content));

    _persistence.remove(existingContent.getContentId());

    Content missingContent =
        _persistence.fetchByC_R_P_V(
            content.getCompanyId(), content.getRepositoryId(),
            content.getPath(), randomString());

    assertNull(missingContent);
  }