@Test
  public void
      common_content_related_to_between_two_content_is_listed_both_contents_relatedcontentkeys() {
    // setup: create same content in two different categories
    ContentKey commonChildContentKey =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory", createMyRelatedContentData("Common child"), "content-creator"));

    ContentKey contentA =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Content A", commonChildContentKey),
                "content-creator"));

    ContentKey contentB =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Content B", commonChildContentKey),
                "content-creator"));

    fixture.flushIndexTransaction();

    // setup: verify that 2 content is created
    assertEquals(3, fixture.countAllContent());

    // exercise
    DataSourceContext context = new DataSourceContext();
    context.setUser(fixture.findUserByName("content-querier"));

    String query = "title STARTS WITH 'Content '";
    String orderBy = "@title asc";
    int index = 0;
    int count = 10;
    boolean includeData = true;
    int childrenLevel = 1;
    int parentLevel = 0;

    XMLDocument xmlDocResult =
        dataSourceService.getContentByQuery(
            context, query, orderBy, index, count, includeData, childrenLevel, parentLevel, null);

    // verify
    Document jdomDocResult = xmlDocResult.getAsJDOMDocument();

    System.out.println(JDOMUtil.prettyPrintDocument(jdomDocResult));

    AssertTool.assertSingleXPathValueEquals("/contents/@totalcount", jdomDocResult, "2");
    AssertTool.assertXPathEquals(
        "/contents/content/@key", jdomDocResult, contentA.toString(), contentB.toString());
    AssertTool.assertXPathEquals(
        "/contents/content[ title = 'Content A']/relatedcontentkeys/relatedcontentkey/@key",
        jdomDocResult,
        commonChildContentKey.toString());
    AssertTool.assertXPathEquals(
        "/contents/content[ title = 'Content B']/relatedcontentkeys/relatedcontentkey/@key",
        jdomDocResult,
        commonChildContentKey.toString());
    AssertTool.assertSingleXPathValueEquals("/contents/relatedcontents/@count", jdomDocResult, "1");
    AssertTool.assertSingleXPathValueEquals(
        "/contents/relatedcontents/content/@key", jdomDocResult, commonChildContentKey.toString());
  }
  @Test
  public void
      content_queried_with_both_related_child_and_parent_having_related_content_existing_as_the_queried_content_is_still_listed_as_related_content() {
    // setup: create same content in two different categories
    ContentKey grandChildContentKey =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory", createMyRelatedContentData("Grand child"), "content-creator"));

    ContentKey sonContentKey =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Son", grandChildContentKey),
                "content-creator"));

    ContentKey daughterContentKey =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory", createMyRelatedContentData("Daughter"), "content-creator"));

    ContentKey fatherContentKey =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Father", sonContentKey, daughterContentKey),
                "content-creator"));

    fixture.flushIndexTransaction();

    // setup: verify that the content was created
    assertEquals(4, fixture.countAllContent());

    // exercise
    DataSourceContext context = new DataSourceContext();
    context.setUser(fixture.findUserByName("content-querier"));

    String query = "categorykey = " + fixture.findCategoryByName("MyCategory").getKey();
    String orderyBy = "@key desc";
    int index = 0;
    int count = 10;
    boolean includeData = true;
    int childrenLevel = 10;
    int parentLevel = 10;

    XMLDocument xmlDocResult =
        dataSourceService.getContentByQuery(
            context, query, orderyBy, index, count, includeData, childrenLevel, parentLevel, null);

    // verify
    Document jdomDocResult = xmlDocResult.getAsJDOMDocument();

    AssertTool.assertSingleXPathValueEquals("/contents/@totalcount", jdomDocResult, "4");
    AssertTool.assertXPathEquals(
        "/contents/content/@key",
        jdomDocResult,
        fatherContentKey.toString(),
        daughterContentKey.toString(),
        sonContentKey.toString(),
        grandChildContentKey.toString());

    AssertTool.assertXPathEquals(
        "/contents/content[title = 'Father']/relatedcontentkeys/relatedcontentkey [@level = 1]/@key",
        jdomDocResult,
        sonContentKey.toString(),
        daughterContentKey.toString());

    AssertTool.assertXPathEquals(
        "/contents/content[title = 'Daughter']/relatedcontentkeys/relatedcontentkey[@level = -1]/@key",
        jdomDocResult,
        fatherContentKey.toString());
    AssertTool.assertXPathEquals(
        "/contents/content[title = 'Son']/relatedcontentkeys/relatedcontentkey[@level = -1]/@key",
        jdomDocResult,
        fatherContentKey.toString());
    AssertTool.assertXPathEquals(
        "/contents/content[title = 'Son']/relatedcontentkeys/relatedcontentkey[@level = 1]/@key",
        jdomDocResult,
        grandChildContentKey.toString());
    AssertTool.assertXPathEquals(
        "/contents/content[title = 'Grand child']/relatedcontentkeys/relatedcontentkey[@level = -1]/@key",
        jdomDocResult,
        sonContentKey.toString());
    AssertTool.assertSingleXPathValueEquals("/contents/relatedcontents/@count", jdomDocResult, "4");
    AssertTool.assertXPathEquals(
        "/contents/relatedcontents/content/@key",
        jdomDocResult,
        grandChildContentKey.toString(),
        sonContentKey.toString(),
        daughterContentKey.toString(),
        fatherContentKey.toString());
  }
  @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());
  }