private void saveNeededEntities() {
   // prepare: save needed entities
   fixture.createAndStoreUserAndUserGroup(
       "testuser", "testuser fullname", UserType.NORMAL, "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", null, "MyContentType", "MyUnit", "testuser", "testuser"));
   fixture.save(
       factory.createCategoryAccessForUser("MyCategory", "testuser", "read, create, approve"));
   fixture.flushAndClearHibernateSession();
 }
  @Before
  public void setUp() {

    factory = fixture.getFactory();

    // 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.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",
            null,
            "MyRelatedType",
            "MyUnit",
            User.ANONYMOUS_UID,
            User.ANONYMOUS_UID,
            false));
    fixture.save(
        factory.createCategory(
            "MyOtherCategory",
            null,
            "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.flushAndClearHibernateSession();
    fixture.flushIndexTransaction();
  }
  @Before
  public void before() throws IOException, JDOMException {

    factory = fixture.getFactory();

    fixture.initSystemData();

    StringBuffer configXml = new StringBuffer();
    configXml.append("<config name=\"MyContentType\" version=\"1.0\">");
    configXml.append("     <form>");

    configXml.append("         <title name=\"myTitle\"/>");

    configXml.append("         <block name=\"General\">");

    configXml.append("             <input name=\"myTitle\" required=\"true\" type=\"text\">");
    configXml.append("                 <display>My title</display>");
    configXml.append("                 <xpath>contentdata/mytitle</xpath>");
    configXml.append("             </input>");

    configXml.append("         </block>");

    configXml.append("         <block name=\"Related content\">");

    configXml.append(
        "             <input name=\"myMultipleRelatedContent\" type=\"relatedcontent\" multiple=\"true\">");
    configXml.append("                 <display>My related content</display>");
    configXml.append("                 <xpath>contentdata/myrelatedcontents</xpath>");
    configXml.append("             </input>");

    configXml.append(
        "             <input name=\"mySoleRelatedContent\" type=\"relatedcontent\" multiple=\"false\">");
    configXml.append("                 <display>My sole related content</display>");
    configXml.append("                 <xpath>contentdata/mysolerelatedcontent</xpath>");
    configXml.append("                 <contenttype name=\"MyContentType\"/>");
    configXml.append("             </input>");

    configXml.append("         </block>");
    configXml.append("     </form>");
    configXml.append("</config>");
    configEl = JDOMUtil.parseDocument(configXml.toString()).getRootElement();
    config = XMLDocumentFactory.create(configXml.toString()).getAsJDOMDocument();

    fixture.createAndStoreNormalUserWithUserGroup("testuser", "testuser fullname", "testuserstore");
    hibernateTemplate.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));
    hibernateTemplate.save(
        factory.createContentType(
            "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), config));
    hibernateTemplate.save(factory.createUnit("MyUnit"));
    hibernateTemplate.save(
        factory.createCategory(
            "MyCategory", null, "MyContentType", "MyUnit", "testuser", "testuser"));
    hibernateTemplate.save(
        factory.createCategoryAccess(
            "MyCategory", fixture.findUserByName("testuser"), "read, create, approve"));

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

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    ServletRequestAccessor.setRequest(request);
  }
  @Test
  public void testUpdateContentWithBlockGroup() {
    // 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.createCategory("Skole", null, "Skole", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("Skole", "testuser", "read,create,approve"));

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

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

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

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

    UpdateContentParams updateParams = new UpdateContentParams();
    updateParams.contentKey = contentKey.toInt();
    updateParams.createNewVersion = false;
    updateParams.updateStrategy = ContentDataInputUpdateStrategy.REPLACE_ALL;
    contentData = new ContentDataInput("Skole");
    contentData.add(new TextInput("tittel", "St. Olav Videregaende skole"));

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

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

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

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

    updateParams.contentData = contentData;
    internalClient.updateContent(updateParams);

    ContentEntity updatedContent = fixture.findContentByKey(contentKey);
    ContentVersionEntity updatedVersion = updatedContent.getMainVersion();
    CustomContentData updatedContentData = (CustomContentData) updatedVersion.getContentData();
    BlockGroupDataEntries elever = updatedContentData.getBlockGroupDataEntries("Elever");

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

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

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

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

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