@Before
  public void setUp() {
    fixture = new DomainFixture(hibernateTemplate);
    factory = new DomainFactory(fixture);

    // setup needed common data for each test
    fixture.initSystemData();

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

    MockHttpServletRequest httpRequest = new MockHttpServletRequest("GET", "/");
    ServletRequestAccessor.setRequest(httpRequest);

    dataSourceService = new DataSourceServiceImpl();
    dataSourceService.setContentService(contentService);
    dataSourceService.setSecurityService(securityService);
    dataSourceService.setTimeService(new MockTimeService(new DateTime(2010, 7, 1, 12, 0, 0, 0)));
    dataSourceService.setUserDao(userDao);

    fixture.createAndStoreNormalUserWithUserGroup("content-creator", "Creator", "testuserstore");
    fixture.createAndStoreNormalUserWithUserGroup("content-querier", "Querier", "testuserstore");

    // setup content type
    ContentTypeConfigBuilder ctyconf = new ContentTypeConfigBuilder("MyContent", "title");
    ctyconf.startBlock("MyContent");
    ctyconf.addInput("title", "text", "contentdata/title", "Title", true);
    ctyconf.addRelatedContentInput(
        "myRelatedContent", "contentdata/myRelatedContent", "My related content", false, true);
    ctyconf.endBlock();
    Document configAsXmlBytes = XMLDocumentFactory.create(ctyconf.toString()).getAsJDOMDocument();

    fixture.save(
        factory.createContentType(
            "MyRelatedType",
            ContentHandlerName.CUSTOM.getHandlerClassShortName(),
            configAsXmlBytes));
    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(
        factory.createCategory(
            "MyCategory",
            "MyRelatedType",
            "MyUnit",
            User.ANONYMOUS_UID,
            User.ANONYMOUS_UID,
            false));
    fixture.save(
        factory.createCategory(
            "MyOtherCategory",
            "MyRelatedType",
            "MyUnit",
            User.ANONYMOUS_UID,
            User.ANONYMOUS_UID,
            false));

    fixture.save(
        factory.createCategoryAccessForUser(
            "MyCategory", "content-creator", "read, create, approve, admin_browse"));
    fixture.save(
        factory.createCategoryAccessForUser("MyCategory", "content-querier", "read, admin_browse"));
    fixture.save(
        factory.createCategoryAccessForUser(
            "MyOtherCategory", "content-creator", "read, create, approve, admin_browse"));
    fixture.save(
        factory.createCategoryAccessForUser(
            "MyOtherCategory", "content-querier", "read, admin_browse"));

    fixture.flushAndClearHibernateSesssion();
  }
  @Test
  public void parentRelation_with_positive_childrenLevel_and_positive_parentLevel() {
    // setup:
    ContentKey grandSon =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory", createMyRelatedContentData("Grand son"), "content-creator"));

    ContentKey grandDaughter =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory", createMyRelatedContentData("Grand daughter"), "content-creator"));

    ContentKey son =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Son", grandSon, grandDaughter),
                "content-creator"));

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

    ContentKey father =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Father", son, daughter),
                "content-creator"));

    ContentKey grandFather =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Grand father", father),
                "content-creator"));

    ContentKey grandMother =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Grand mother", father),
                "content-creator"));

    ContentKey grandMothersMother =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Grand mothers mother", grandMother),
                "content-creator"));

    ContentKey grandMothersFather =
        contentService.createContent(
            createCreateContentCommand(
                "MyCategory",
                createMyRelatedContentData("Grand mothers father", grandMother),
                "content-creator"));

    // setup: verify content is created
    assertEquals(9, fixture.countAllContent());

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

    int[] contentKeys = new int[] {father.toInt()};
    int relation = -1;
    String query = "";
    String orderBy = "";
    int index = 0;
    int count = 100;
    boolean includeData = true;
    int childrenLevel = 10;
    int parentLevel = 10;

    XMLDocument xmlDocResult =
        dataSourceService.getRelatedContent(
            context,
            contentKeys,
            relation,
            query,
            orderBy,
            index,
            count,
            includeData,
            childrenLevel,
            parentLevel);

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

    assertXPathEquals("/contents/@totalcount", jdomDocResult, "2");
    assertXPathEquals("/contents/content/@key", jdomDocResult, grandFather, grandMother);
    assertXPathEquals(
        "/contents/content[title = 'Grand father']/relatedcontentkeys/@count", jdomDocResult, "1");
    assertXPathEquals(
        "/contents/content[title = 'Grand father']/relatedcontentkeys/relatedcontentkey [@level = 1]/@key",
        jdomDocResult,
        father);
    assertXPathEquals(
        "/contents/content[title = 'Grand mother']/relatedcontentkeys/@count", jdomDocResult, "3");
    assertXPathEquals(
        "/contents/content[title = 'Grand mother']/relatedcontentkeys/relatedcontentkey[@level = 1]/@key",
        jdomDocResult,
        father);
    assertXPathEquals(
        "/contents/content[title = 'Grand mother']/relatedcontentkeys/relatedcontentkey[@level = -1]/@key",
        jdomDocResult,
        grandMothersMother,
        grandMothersFather);

    assertXPathEquals("/contents/relatedcontents/@count", jdomDocResult, "7");
    assertXPathEquals(
        "/contents/relatedcontents/content/@key",
        jdomDocResult,
        father,
        son,
        daughter,
        grandSon,
        grandDaughter,
        grandMothersMother,
        grandMothersFather);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Father']/relatedcontentkeys/@count",
        jdomDocResult,
        "2");
    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Father']/relatedcontentkeys/relatedcontentkey[@level = 1]/@key",
        jdomDocResult,
        son,
        daughter);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Son']/relatedcontentkeys/@count",
        jdomDocResult,
        "2");
    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Son']/relatedcontentkeys/relatedcontentkey[@level = 1]/@key",
        jdomDocResult,
        grandDaughter,
        grandSon);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Daughter']/relatedcontentkeys/@count",
        jdomDocResult,
        "0");
    assertXPathNotExist(
        "/contents/relatedcontents/content[title = 'Daughter']/relatedcontentkeys/relatedcontentkey",
        jdomDocResult);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Grand son']/relatedcontentkeys/@count",
        jdomDocResult,
        "0");
    assertXPathNotExist(
        "/contents/relatedcontents/content[title = 'Grand son']/relatedcontentkeys/relatedcontentkey",
        jdomDocResult);
    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Grand daughter']/relatedcontentkeys/@count",
        jdomDocResult,
        "0");
    assertXPathNotExist(
        "/contents/relatedcontents/content[title = 'Grand daughter']/relatedcontentkeys/relatedcontentkey",
        jdomDocResult);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Grand mothers mother']/relatedcontentkeys/@count",
        jdomDocResult,
        "0");
    assertXPathNotExist(
        "/contents/relatedcontents/content[title = 'Grand mothers mother']/relatedcontentkeys/relatedcontentkey",
        jdomDocResult);

    assertXPathEquals(
        "/contents/relatedcontents/content[title = 'Grand mothers father']/relatedcontentkeys/@count",
        jdomDocResult,
        "0");
    assertXPathNotExist(
        "/contents/relatedcontents/content[title = 'Grand mothers father']/relatedcontentkeys/relatedcontentkey",
        jdomDocResult);
  }