@Test
  public void testUpdateContentWithReplaceNew_WithNullValue() {
    ContentDataInput cdi = getContentData(true);
    UpdateContentParams upcd = getUpdateContentParams(cdi);
    upcd.updateStrategy = ContentDataInputUpdateStrategy.REPLACE_NEW;

    // update content with all fields set
    internalClient.updateContent(upcd);

    // get updated content - make sure all fiels are set
    String xml = getUpdatedContentXMLWithDao();
    assertTrue(
        "XML inneholder ikke <mytitle>updateTest</mytitle>",
        xml.contains("<mytitle>updateTest</mytitle>"));
    assertTrue(
        "XML inneholder ikke <updatefield>foobar</updatefield>",
        xml.contains("<updatefield>foobar</updatefield>"));

    // update content with missing field
    // update content with missing field
    cdi = new ContentDataInput("MyContentType");
    cdi.add(new TextInput("myTitle", "updateTest"));
    cdi.add(new TextInput("fieldToUpdate", null));
    upcd.contentData = cdi;
    internalClient.updateContent(upcd);

    // get updated content
    ContentEntity entity = contentDao.findByKey(updateContentKey);
    ContentVersionEntity version = entity.getMainVersion();
    CustomContentData customContentData = (CustomContentData) version.getContentData();
    TextDataEntry myTitle = (TextDataEntry) customContentData.getEntry("myTitle");
    assertEquals("updateTest", myTitle.getValue());
    TextDataEntry fieldToUpdate = (TextDataEntry) customContentData.getEntry("fieldToUpdate");
    assertFalse(fieldToUpdate.hasValue());
  }
  @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());
  }
  @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);
  }
  private ContentDataInput getContentData(boolean all) {
    ContentDataInput cdi = new ContentDataInput("MyContentType");

    TextInput updatefield = new TextInput("fieldToUpdate", "foobar");
    TextInput title = new TextInput("myTitle", "updateTest");

    cdi.add(title);
    if (all) {
      cdi.add(updatefield);
    }

    return cdi;
  }
  @Test
  public void testUpdateContentWithReplaceNew_WithMissingValue() {
    ContentDataInput cdi = getContentData(true);
    UpdateContentParams upcd = getUpdateContentParams(cdi);
    upcd.updateStrategy = ContentDataInputUpdateStrategy.REPLACE_NEW;

    // update content with all fields set
    internalClient.updateContent(upcd);

    // get updated content - make sure all fiels are set
    String xml = getUpdatedContentXMLWithDao();
    assertTrue(
        "XML inneholder ikke <mytitle>updateTest</mytitle>",
        xml.contains("<mytitle>updateTest</mytitle>"));
    assertTrue(
        "XML inneholder ikke <updatefield>foobar</updatefield>",
        xml.contains("<updatefield>foobar</updatefield>"));

    // update content with missing field
    cdi = new ContentDataInput("MyContentType");
    cdi.add(new TextInput("myTitle", "updateTest"));
    upcd.contentData = cdi;
    internalClient.updateContent(upcd);

    // get updated content
    xml = getUpdatedContentXMLWithDao();
    assertTrue(
        "XML inneholder ikke <mytitle>updateTest</mytitle>",
        xml.contains("<mytitle>updateTest</mytitle>"));
    assertTrue(
        "XML inneholder ikke <updatefield>foobar</updatefield>",
        xml.contains("<updatefield>foobar</updatefield>"));
  }
  @Test
  public void testUpdateContentDoNotChangeAssignment() {
    // exercise: updateContent

    AssignContentCommand assignContentCommand = new AssignContentCommand();
    assignContentCommand.setAssignerKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssigneeKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssignmentDescription("test assignment");
    assignContentCommand.setAssignmentDueDate(new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate());
    assignContentCommand.setContentKey(contentWithBinaryKey);
    contentService.assignContent(assignContentCommand);

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

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

    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);

    fixture.flushAndClearHibernateSession();

    ContentVersionEntity actualVersion =
        contentVersionDao.findByKey(new ContentVersionKey(contentVersionKey));
    ContentEntity persistedContent = contentDao.findByKey(actualVersion.getContent().getKey());

    assertEquals(runningUser, persistedContent.getAssignee());
    assertEquals(runningUser, persistedContent.getAssigner());
    assertEquals("test assignment", persistedContent.getAssignmentDescription());
    assertEquals(
        new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate(), persistedContent.getAssignmentDueDate());
  }
  @Test
  public void
      update_existing_version_with_publishFrom_without_contentdata_changes_only_publishFrom() {
    CreateContentParams createParams = new CreateContentParams();
    createParams.categoryKey = fixture.findCategoryByName("MyCategory").getKey().toInt();
    createParams.publishFrom = new DateTime(2100, 1, 1, 0, 0, 0, 0).toDate();
    createParams.status = ContentStatus.STATUS_APPROVED;
    ContentDataInput contentdataForCreate = new ContentDataInput("MyContentType");
    contentdataForCreate.add(new TextInput("myTitle", "title from creation"));
    createParams.contentData = contentdataForCreate;
    ContentKey contentKeyToUpdate = new ContentKey(internalClient.createContent(createParams));

    UpdateContentParams updateParams = new UpdateContentParams();
    updateParams.updateStrategy = ContentDataInputUpdateStrategy.REPLACE_NEW;
    updateParams.contentKey = contentKeyToUpdate.toInt();
    updateParams.createNewVersion = false;
    updateParams.publishFrom = new DateTime(2010, 1, 1, 0, 0, 0, 0).toDate();
    updateParams.setAsCurrentVersion = true;
    updateParams.status = null;

    ContentVersionKey versionKey =
        new ContentVersionKey(internalClient.updateContent(updateParams));
    assertNotNull(versionKey);

    assertEquals(1, fixture.countContentVersionsByTitle("title from creation"));
    assertEquals(
        versionKey, fixture.findFirstContentVersionByTitle("title from creation").getKey());
    assertEquals(
        new DateTime(2010, 1, 1, 0, 0, 0, 0).toDate(),
        fixture
            .findFirstContentVersionByTitle("title from creation")
            .getContent()
            .getAvailableFrom());
    assertEquals(
        1,
        fixture
            .findFirstContentVersionByTitle("title from creation")
            .getContent()
            .getVersions()
            .size());
  }
  @Test
  public void testCreateContentWithBlockGroup() {
    fixture.createAndStoreUserAndUserGroup(
        "testuser", "testuser fullname", UserType.NORMAL, "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));

    // setup content type
    ContentTypeConfigBuilder ctyconf = new ContentTypeConfigBuilder("Skole", "tittel");
    ctyconf.startBlock("Skole");
    ctyconf.addInput("tittel", "text", "contentdata/tittel", "Tittel", true);
    ctyconf.endBlock();
    ctyconf.startBlock("Elever", "contentdata/elever");
    ctyconf.addInput("elev-navn", "text", "navn", "Navn");
    ctyconf.addInput("elev-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    ctyconf.startBlock("Laerere", "contentdata/laerere");
    ctyconf.addInput("laerer-navn", "text", "navn", "Navn");
    ctyconf.addInput("laerer-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    Document configAsXmlBytes = XMLDocumentFactory.create(ctyconf.toString()).getAsJDOMDocument();
    fixture.save(
        factory.createContentType(
            "Skole", ContentHandlerName.CUSTOM.getHandlerClassShortName(), configAsXmlBytes));

    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(factory.createCategory("Skole", "Skole", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("Skole", "testuser", "read,create,approve"));

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

    CreateContentParams content = new CreateContentParams();
    content.categoryKey = fixture.findCategoryByName("Skole").getKey().toInt();
    content.publishFrom = new Date();
    content.status = ContentStatus.STATUS_APPROVED;

    ContentDataInput contentData = new ContentDataInput("Skole");
    contentData.add(new TextInput("tittel", "St. Olav Videregaende skole"));

    GroupInput groupInputElev1 = contentData.addGroup("Elever");
    groupInputElev1.add(new TextInput("elev-navn", "Vegar Jansen"));
    groupInputElev1.add(new TextInput("elev-karakter", "S"));

    GroupInput groupInputElev2 = contentData.addGroup("Elever");
    groupInputElev2.add(new TextInput("elev-navn", "Thomas Sigdestad"));
    groupInputElev2.add(new TextInput("elev-karakter", "M"));

    GroupInput groupInputLaerer1 = contentData.addGroup("Laerere");
    groupInputLaerer1.add(new TextInput("laerer-navn", "Mutt Hansen"));
    groupInputLaerer1.add(new TextInput("laerer-karakter", "LG"));

    GroupInput groupInputLaerer2 = contentData.addGroup("Laerere");
    groupInputLaerer2.add(new TextInput("laerer-navn", "Striks Jansen"));
    groupInputLaerer2.add(new TextInput("laerer-karakter", "M"));

    content.contentData = contentData;
    ContentKey contentKey = new ContentKey(internalClient.createContent(content));

    ContentEntity createdContent = fixture.findContentByKey(contentKey);
    ContentVersionEntity createdVersion = createdContent.getMainVersion();
    CustomContentData createdContentData = (CustomContentData) createdVersion.getContentData();
    BlockGroupDataEntries elever = createdContentData.getBlockGroupDataEntries("Elever");

    GroupDataEntry elev1 = elever.getGroupDataEntry(1);
    assertEquals("Vegar Jansen", ((TextDataEntry) elev1.getEntry("elev-navn")).getValue());
    assertEquals("S", ((TextDataEntry) elev1.getEntry("elev-karakter")).getValue());

    GroupDataEntry elev2 = elever.getGroupDataEntry(2);
    assertEquals("Thomas Sigdestad", ((TextDataEntry) elev2.getEntry("elev-navn")).getValue());
    assertEquals("M", ((TextDataEntry) elev2.getEntry("elev-karakter")).getValue());

    BlockGroupDataEntries laerere = createdContentData.getBlockGroupDataEntries("Laerere");

    GroupDataEntry laerer1 = laerere.getGroupDataEntry(1);
    assertEquals("Mutt Hansen", ((TextDataEntry) laerer1.getEntry("laerer-navn")).getValue());
    assertEquals("LG", ((TextDataEntry) laerer1.getEntry("laerer-karakter")).getValue());

    GroupDataEntry laerer2 = laerere.getGroupDataEntry(2);
    assertEquals("Striks Jansen", ((TextDataEntry) laerer2.getEntry("laerer-navn")).getValue());
    assertEquals("M", ((TextDataEntry) laerer2.getEntry("laerer-karakter")).getValue());
  }