@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());
  }
  private ContentKey storeSimpleContent(String title) {

    ContentEntity content = factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    ContentVersionEntity version = factory.createContentVersion("0", "testuser");

    ContentTypeConfig contentTypeConfig =
        ContentTypeConfigParser.parse(ContentHandlerName.CUSTOM, createSimpleContentTypeConfig());

    CustomContentData contentData = new CustomContentData(contentTypeConfig);

    TextDataEntryConfig titleConfig =
        new TextDataEntryConfig("myTitle", true, title, "contentdata/mytitle");
    contentData.add(new TextDataEntry(titleConfig, "relatedconfig"));

    version.setContentData(contentData);

    UserEntity runningUser = fixture.findUserByName("testuser");

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    createContentCommand.setBinaryDatas(new ArrayList<BinaryDataAndBinary>());
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    ContentKey contentKey = contentService.createContent(createContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    return contentKey;
  }
  @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);
  }
  @Test
  public void response_ok_for_request_to_content_image_that_binary_is_not_on_main_version()
      throws Exception {
    // setup: content
    byte[] bytes = loadImage("Arn.JPG");
    ContentKey contentKey =
        createImageContent(
            "MyImage.jpg", 2, bytes, "ImageCategory", new DateTime(2011, 6, 27, 10, 0, 0, 0), null);

    // setup: draft version of content
    ContentEntity content = fixture.findContentByKey(contentKey);
    ContentVersionEntity draftVersion = createDraftVersion("Arn.JPG");
    draftVersion.setContentDataXml(content.getMainVersion().getContentDataAsXmlString());
    content.setDraftVersion(draftVersion);
    content.addVersion(draftVersion);
    fixture.save(draftVersion);

    BinaryDataEntity binaryDataForDraftVersion = factory.createBinaryData("Arn.JPG", bytes.length);
    binaryDataForDraftVersion.setBlobKey(
        content.getMainVersion().getBinaryData("source").getBlobKey());
    fixture.save(binaryDataForDraftVersion);

    ContentBinaryDataEntity contentBinaryDataForDraftVersion =
        factory.createContentBinaryData("source", binaryDataForDraftVersion, draftVersion);
    draftVersion.addContentBinaryData(contentBinaryDataForDraftVersion);
    fixture.save(contentBinaryDataForDraftVersion);

    fixture.flushAndClearHibernateSesssion();

    // exercise & verify
    String imageRequestPath = "_image/" + contentKey + "/label/source.jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_version", draftVersion.getKey().toString());
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() > 0);
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  @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());
  }
  private void createUpdateContentWithBinary() {
    UserEntity runningUser = fixture.findUserByName("testuser");

    // prepare: save a new content
    ContentEntity content = new ContentEntity();
    content.setPriority(0);
    content.setAvailableFrom(null);
    content.setAvailableTo(null);
    content.setCategory(fixture.findCategoryByName("MyCategory"));
    content.setLanguage(fixture.findLanguageByCode("en"));
    content.setName("testcontentwithbinary");

    ContentVersionEntity version = new ContentVersionEntity();
    version.setContent(content);
    version.setModifiedBy(runningUser);
    version.setStatus(com.enonic.cms.core.content.ContentStatus.DRAFT);
    version.setContent(content);
    CustomContentData contentData =
        new CustomContentData(
            fixture.findContentTypeByName("MyContentType").getContentTypeConfig());

    TextDataEntryConfig titleConfig =
        (TextDataEntryConfig)
            contentData.getContentTypeConfig().getForm().getInputConfig("myTitle");
    TextDataEntryConfig updateFieldConfig =
        (TextDataEntryConfig)
            contentData.getContentTypeConfig().getForm().getInputConfig("fieldToUpdate");
    BinaryDataEntryConfig binaryConfig =
        (BinaryDataEntryConfig)
            contentData.getContentTypeConfig().getForm().getInputConfig("myBinaryfile");

    contentData.add(new TextDataEntry(titleConfig, "testitle"));
    contentData.add(new TextDataEntry(updateFieldConfig, "foobar"));
    contentData.add(new BinaryDataEntry(binaryConfig, "%0"));

    version.setContentData(contentData);
    version.setTitle(contentData.getTitle());

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    List<BinaryDataAndBinary> binaryDatas = new ArrayList<BinaryDataAndBinary>();
    binaryDatas.add(factory.createBinaryDataAndBinary("dummyBinary", dummyBinary));

    createContentCommand.setBinaryDatas(binaryDatas);
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    contentWithBinaryKey = contentService.createContent(createContentCommand);

    fixture.flushAndClearHibernateSession();
  }
  private void createUpdateContent() {
    UserEntity runningUser = fixture.findUserByName("testuser");

    // prepare: save a new content
    ContentEntity content = new ContentEntity();
    content.setPriority(0);
    content.setAvailableFrom(new Date());
    content.setAvailableTo(null);
    content.setCategory(fixture.findCategoryByName("MyCategory"));
    content.setLanguage(fixture.findLanguageByCode("en"));
    content.setName("testcontent");

    ContentVersionEntity version = new ContentVersionEntity();
    version.setContent(content);
    version.setModifiedBy(runningUser);
    version.setStatus(com.enonic.cms.core.content.ContentStatus.APPROVED);
    version.setContent(content);
    CustomContentData contentData =
        new CustomContentData(
            fixture.findContentTypeByName("MyContentType").getContentTypeConfig());

    TextDataEntryConfig titleConfig =
        (TextDataEntryConfig)
            contentData.getContentTypeConfig().getForm().getInputConfig("myTitle");
    TextDataEntryConfig updateFieldConfig =
        (TextDataEntryConfig)
            contentData.getContentTypeConfig().getForm().getInputConfig("fieldToUpdate");
    contentData.add(new TextDataEntry(titleConfig, "testitle"));
    contentData.add(new TextDataEntry(updateFieldConfig, "foobar"));

    version.setContentData(contentData);
    version.setTitle(contentData.getTitle());

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);
    createContentCommand.setAccessRightsStrategy(AccessRightsStrategy.USE_GIVEN);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    updateContentKey = contentService.createContent(createContentCommand);

    fixture.flushAndClearHibernateSession();
  }
  public int updateFileContent(UpdateFileContentParams params) {
    validateUpdateFileContentParams(params);

    ContentVersionKey contentVersionKey =
        resolveContentVersionKey(
            params.createNewVersion, params.contentKey, params.contentVersionKey);

    UpdateContentCommand command;
    if (params.createNewVersion) {
      command = UpdateContentCommand.storeNewVersionEvenIfUnchanged(contentVersionKey);
    } else {
      command = UpdateContentCommand.updateExistingVersion2(contentVersionKey);
    }

    command.setContentKey(new ContentKey(params.contentKey));
    command.setSyncRelatedContent(false);
    command.setSyncAccessRights(false);
    command.setModifier(securityService.getImpersonatedPortalUser());
    command.setAvailableFrom(params.publishFrom);
    command.setAvailableTo(params.publishTo);
    command.setStatus(ContentStatus.get(params.status));
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    LegacyFileContentData newContentData;
    List<BinaryDataAndBinary> binariesToAdd = null;
    List<BinaryDataKey> binariesToRemove = null;
    if (params.fileContentData != null) {
      newContentData =
          (LegacyFileContentData) fileContentResolver.resolveContentdata(params.fileContentData);
      command.setContentData(newContentData);
      if (!params.createNewVersion) {
        // only delete previous binaries if we are overwriting current version
        ContentVersionEntity persistedVersion = contentVersionDao.findByKey(contentVersionKey);
        LegacyFileContentData previousContentData =
            (LegacyFileContentData) persistedVersion.getContentData();
        binariesToRemove = previousContentData.getRemovedBinaries(newContentData);
      }
      // Find new binaries
      binariesToAdd = newContentData.getBinaryDataAndBinaryList();
    } else {
      // only update the meta data in this case..
    }

    command.setUpdateAsMainVersion(params.setAsCurrentVersion);
    command.setUseCommandsBinaryDataToAdd(true);
    command.setBinaryDataToAdd(binariesToAdd);

    command.setUseCommandsBinaryDataToRemove(true);
    command.setBinaryDataToRemove(binariesToRemove);
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    UpdateContentResult updateContentResult = contentService.updateContent(command);

    if (updateContentResult.isAnyChangesMade()) {
      new PageCacheInvalidatorForContent(pageCacheService)
          .invalidateForContent(updateContentResult.getTargetedVersion());
    }

    return updateContentResult.getTargetedVersionKey().toInt();
  }
  public int updateContent(UpdateContentParams params) {
    validateUpdateContentParams(params);

    final ContentVersionKey contentVersionKey =
        resolveContentVersionKey(
            params.createNewVersion, params.contentKey, params.contentVersionKey);

    UpdateContentCommand command;
    if (params.createNewVersion) {
      command = UpdateContentCommand.storeNewVersionEvenIfUnchanged(contentVersionKey);
    } else {
      command = UpdateContentCommand.updateExistingVersion2(contentVersionKey);
    }

    command.setContentName(params.name);
    command.setSyncRelatedContent(true);
    command.setSyncAccessRights(false);
    command.setModifier(securityService.getImpersonatedPortalUser());
    command.setUpdateAsMainVersion(params.setAsCurrentVersion);
    command.setContentKey(new ContentKey(params.contentKey));
    command.setAvailableFrom(params.publishFrom);
    command.setAvailableTo(params.publishTo);
    command.setStatus(ContentStatus.get(params.status));
    command.setUseCommandsBinaryDataToRemove(true);
    command.setUseCommandsBinaryDataToAdd(true);
    command.setChangeComment(params.changeComment);
    if (params.siteKey != null) {
      command.setSiteKey(new SiteKey(params.siteKey));
    }

    if (params.contentData != null) {
      final ContentTypeEntity contentType = resolveContentType(params.contentKey);
      final ContentDataResolver customContentResolver = new ContentDataResolver();
      final CustomContentData newContentData =
          customContentResolver.resolveContentdata(params.contentData, contentType);
      command.setContentData(newContentData);
      if (!params.createNewVersion) {
        // only delete previous binaries if we are overwriting current version
        final ContentVersionEntity persistedVersion =
            contentVersionDao.findByKey(contentVersionKey);
        final CustomContentData persistedContentData =
            (CustomContentData) persistedVersion.getContentData();
        final List<BinaryDataEntry> deletedBinaries =
            persistedContentData.getRemovedBinaryDataEntries(newContentData);
        command.setBinaryDataToRemove(BinaryDataEntry.createBinaryDataKeyList(deletedBinaries));
        command.setUseCommandsBinaryDataToRemove(true);
      }

      // Find new binaries
      final List<BinaryDataEntry> binaryEntries = newContentData.getBinaryDataEntryList();
      command.setBinaryDataToAdd(BinaryDataAndBinary.convert(binaryEntries));
      command.setUseCommandsBinaryDataToAdd(true);
    } else {
      // only update the meta data in this case..
    }

    if (params.updateStrategy == ContentDataInputUpdateStrategy.REPLACE_NEW) {
      command.setUpdateStrategy(UpdateStrategy.MODIFY);
    }

    final UpdateContentResult updateContentResult = contentService.updateContent(command);

    if (updateContentResult.isAnyChangesMade()) {
      new PageCacheInvalidatorForContent(pageCacheService)
          .invalidateForContent(updateContentResult.getTargetedVersion());
    }

    return updateContentResult.getTargetedVersionKey().toInt();
  }
Пример #12
0
 public ContentVersionEntity createContentVersion(String status, String modiferQualifiedName) {
   ContentVersionEntity version = new ContentVersionEntity();
   version.setStatus(ContentStatus.get(Integer.valueOf(status)));
   version.setModifiedBy(fixture.findUserByName(modiferQualifiedName));
   return version;
 }
  @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());
  }
  @Test
  public void testUpdateCurrentVersion() {
    ContentKey relatedContentKey1 = storeSimpleContent("rel1");
    ContentKey relatedContentKey2 = storeSimpleContent("rel2");
    ContentKey relatedContentKey3 = storeSimpleContent("rel3");
    ContentKey relatedContentKey4 = storeSimpleContent("rel4");
    ContentKey relatedContentKey5 = storeSimpleContent("rel5");

    ContentEntity content = factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    ContentVersionEntity version = factory.createContentVersion("0", "testuser");

    ContentTypeConfig contentTypeConfig =
        ContentTypeConfigParser.parse(ContentHandlerName.CUSTOM, configEl);
    CustomContentData contentData = new CustomContentData(contentTypeConfig);
    TextDataEntryConfig titleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    contentData.add(new TextDataEntry(titleConfig, "test title"));

    RelatedContentDataEntryConfig multipleRelatedContentsConfig =
        (RelatedContentDataEntryConfig)
            contentTypeConfig.getInputConfig("myMultipleRelatedContent");

    contentData.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey1))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey2)));

    RelatedContentDataEntryConfig soleRelatedConfig =
        (RelatedContentDataEntryConfig) contentTypeConfig.getInputConfig("mySoleRelatedContent");

    contentData.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey3));

    version.setContentData(contentData);

    UserEntity runningUser = fixture.findUserByName("testuser");

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    createContentCommand.setBinaryDatas(new ArrayList<BinaryDataAndBinary>());
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    ContentKey contentKey = contentService.createContent(createContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity persistedContent = contentDao.findByKey(contentKey);
    assertNotNull(persistedContent);

    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);

    assertEquals(3, persistedVersion.getRelatedChildren(true).size());

    ContentEntity changedContent =
        factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    changedContent.setKey(contentKey);
    ContentVersionEntity changedVersion = factory.createContentVersion("0", "testuser");
    changedVersion.setKey(persistedVersion.getKey());

    CustomContentData changedCD = new CustomContentData(contentTypeConfig);

    TextDataEntryConfig changedTitleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    changedCD.add(new TextDataEntry(changedTitleConfig, "changed title"));

    changedCD.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey3))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey5)));

    changedCD.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey4));

    changedVersion.setContentData(changedCD);

    UpdateContentCommand updateContentCommand =
        UpdateContentCommand.updateExistingVersion2(persistedVersion.getKey());
    updateContentCommand.setModifier(runningUser);
    updateContentCommand.setUpdateAsMainVersion(false);

    updateContentCommand.populateContentValuesFromContent(persistedContent);
    updateContentCommand.populateContentVersionValuesFromContentVersion(changedVersion);

    contentService.updateContent(updateContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity contentAfterUpdate = contentDao.findByKey(contentKey);
    ContentVersionEntity versionAfterUpdate =
        contentVersionDao.findByKey(persistedVersion.getKey());

    Document contentDataXmlAfterUpdate = versionAfterUpdate.getContentDataAsJDomDocument();

    AssertTool.assertXPathEquals(
        "/contentdata/mysolerelatedcontent/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey4.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[1]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey3.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[2]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey5.toString());

    assertEquals(3, versionAfterUpdate.getRelatedChildren(true).size());
  }