@Before
  public void before() {
    factory = fixture.getFactory();

    fixture.initSystemData();
    fixture.createAndStoreUserAndUserGroup(
        "testuser", "testuser fullname", UserType.NORMAL, "testuserstore");

    ServletRequestAttributes servletRequestAttributes =
        new ServletRequestAttributes(httpServletRequest);
    RequestContextHolder.setRequestAttributes(servletRequestAttributes);
    httpServletRequest.setCharacterEncoding("UTF-8");
    ServletRequestAccessor.setRequest(httpServletRequest);

    loginUserInAdmin(fixture.findUserByName("testuser").getKey());
    loginUserInPortal(fixture.findUserByName("testuser").getKey());

    imageController.setGroupDao(groupDao);
    imageController.setContentDao(contentDao);
    imageController.setSecurityService(securityService);
    imageController.setImageService(imageService);
    imageController.setDisableParamEncoding(true);

    previewService = Mockito.mock(PreviewService.class);
    Mockito.when(previewService.isInPreview()).thenReturn(false);
    Mockito.when(previewService.getPreviewContext()).thenReturn(PreviewContext.NO_PREVIEW);

    site1 = factory.createSite("MySite", new Date(), null, "en");
    fixture.save(site1);
    MenuItemEntity firstPage = createPage("Firstpage", null, "MySite");
    fixture.save(firstPage);

    site1.setFirstPage(firstPage);

    fixture.flushAndClearHibernateSesssion();

    fixture.save(
        factory.createContentHandler(
            "Image content", ContentHandlerName.IMAGE.getHandlerClassShortName()));
    fixture.save(
        factory.createContentType(
            "ImageContentType", ContentHandlerName.IMAGE.getHandlerClassShortName()));
    fixture.save(factory.createUnit("ImageUnit"));
    fixture.save(
        factory.createCategory(
            "ImageCategory", "ImageContentType", "ImageUnit", "testuser", "testuser"));
    fixture.save(
        factory.createCategoryAccessForUser("ImageCategory", "testuser", "read, create, approve"));

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

    fixture.createAndStoreUserAndUserGroup(
        "user-with-no-access", "testuser fullname", UserType.NORMAL, "testuserstore");
    loginUserInAdmin(fixture.findUserByName("user-with-no-access").getKey());

    String imageRequestPath = "_image/" + contentKey + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");

    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_NOT_FOUND, httpServletResponse.getStatus());
    assertTrue("Content Length", httpServletResponse.getContentLength() == 0);
  }
  @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());
  }