private void validateUpdateContentParams(UpdateContentParams params) {
    if (params.contentKey == null) {
      throw new IllegalArgumentException("contentKey must be specified");
    }
    if (params.updateStrategy == null) {
      throw new IllegalArgumentException("updateStrategy must be specified");
    }
    if (params.publishFrom != null
        && params.publishTo != null
        && !params.publishTo.after(params.publishFrom)) {
      throw new IllegalArgumentException("publishTo must be after publishFrom");
    }
    if (params.createNewVersion
        && params.contentData == null
        && params.updateStrategy == ContentDataInputUpdateStrategy.REPLACE_ALL) {
      throw new IllegalArgumentException(
          "contentData must be specified if you want to create new version when updateStrategy is "
              + ContentDataInputUpdateStrategy.REPLACE_ALL);
    }
    if (params.contentVersionKey != null && params.createNewVersion) {
      throw new IllegalArgumentException(
          "There is no meaning in wanting to both update one specific version and create a new version");
    }

    ContentEntity persistedContent = contentDao.findByKey(new ContentKey(params.contentKey));
    if (persistedContent == null) {
      throw new IllegalArgumentException("No content for given contentKey: " + params.contentKey);
    }

    int currentStatus = persistedContent.getMainVersion().getStatus().getKey();
    if (!params.createNewVersion
        && currentStatus != ContentStatus.DRAFT.getKey()
        && params.contentData != null) {
      throw new IllegalArgumentException(
          "Only allowed to overwrite a draft content version - create new version instead");
    }

    if (params.status != null) {
      boolean currentStatusIsApprovedOrArchived =
          currentStatus == ContentStatus.APPROVED.getKey()
              || currentStatus == ContentStatus.ARCHIVED.getKey();

      if (currentStatusIsApprovedOrArchived) {
        boolean statusChangingingToDraft = params.status == ContentStatus.DRAFT.getKey();
        boolean statusChangingToSnapshot = params.status == ContentStatus.SNAPSHOT.getKey();

        if (!params.createNewVersion && (statusChangingingToDraft || statusChangingToSnapshot)) {
          throw new IllegalArgumentException(
              "Not allowed to change status of an approved or archived content version - create new content version instead");
        }
      }
    }
  }
  @Test
  public void testUpdateContentWithBinary() {
    // exercise: updateContent

    ContentDataInput newContentData = new ContentDataInput("MyContentType");
    newContentData.add(new TextInput("myTitle", "changedtitle"));
    newContentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary"));

    UpdateContentParams params = new UpdateContentParams();
    params.contentKey = contentWithBinaryKey.toInt();
    params.contentData = newContentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.createNewVersion = false;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentVersionKey = internalClient.updateContent(params);

    ContentVersionEntity actualVersion =
        contentVersionDao.findByKey(new ContentVersionKey(contentVersionKey));
    assertEquals(
        com.enonic.cms.core.content.ContentStatus.DRAFT.getKey(),
        actualVersion.getStatus().getKey());
    assertEquals("changedtitle", actualVersion.getTitle());
    assertEquals("changedtitle", actualVersion.getContentData().getTitle());
  }
 private ContentVersionEntity createDraftVersion(String title) {
   ContentVersionEntity draftVersion =
       factory.createContentVersion("" + ContentStatus.DRAFT.getKey(), "testuser/testuserstore");
   draftVersion.setCreatedAt(new Date());
   draftVersion.setModifiedAt(new Date());
   draftVersion.setModifiedBy(fixture.findUserByName("testuser"));
   draftVersion.setTitle(title);
   return draftVersion;
 }
  @Test
  public void testCreateContentWithBinary() {
    fixture.createAndStoreNormalUserWithUserGroup("testuser", "Test user", "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));
    fixture.save(
        factory.createContentType(
            "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), standardConfig));
    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(
        factory.createCategory("MyCategory", "MyContentType", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("MyCategory", "testuser", "read,create"));

    fixture.flushAndClearHibernateSesssion();

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    ContentDataInput contentData = new ContentDataInput("MyContentType");
    contentData.add(new TextInput("myTitle", "testtitle"));
    contentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary"));

    CreateContentParams params = new CreateContentParams();
    params.categoryKey = fixture.findCategoryByName("MyCategory").getKey().toInt();
    params.contentData = contentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentKey = internalClient.createContent(params);

    fixture.flushAndClearHibernateSesssion();

    ContentEntity persistedContent = fixture.findContentByKey(new ContentKey(contentKey));
    assertNotNull(persistedContent);
    assertEquals("MyCategory", persistedContent.getCategory().getName());
    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);
    assertEquals("testtitle", persistedVersion.getTitle());
    assertEquals(
        com.enonic.cms.core.content.ContentStatus.DRAFT.getKey(),
        persistedVersion.getStatus().getKey());

    // verify binary was saved
    Set<ContentBinaryDataEntity> contentBinaryDatas = persistedVersion.getContentBinaryData();
    assertEquals(1, contentBinaryDatas.size());
    ContentBinaryDataEntity contentBinaryData = contentBinaryDatas.iterator().next();
    BinaryDataEntity binaryData = contentBinaryData.getBinaryData();
    assertEquals("dummyBinary", binaryData.getName());

    CustomContentData customContentData = (CustomContentData) persistedVersion.getContentData();
    assertNotNull(customContentData);
  }