public CreateContentPoolServletTest()
     throws ClientPoolException, StorageClientException, AccessDeniedException,
         ClassNotFoundException {
   MockitoAnnotations.initMocks(this);
   BaseMemoryRepository baseMemoryRepository = new BaseMemoryRepository();
   repository = baseMemoryRepository.getRepository();
   Session session = repository.loginAdministrative();
   AuthorizableManager authorizableManager = session.getAuthorizableManager();
   authorizableManager.createUser("ieb", "Ian Boston", "test", ImmutableMap.of("x", (Object) "y"));
   org.sakaiproject.nakamura.api.lite.authorizable.Authorizable authorizable =
       authorizableManager.findAuthorizable("ieb");
   System.err.println("Got ieb as " + authorizable);
   session.logout();
 }
  @Test
  public void testCreate() throws Exception {

    // activate
    when(slingRepository.loginAdministrative(null)).thenReturn(adminSession);

    when(request.getResourceResolver()).thenReturn(resourceResolver);
    when(resourceResolver.adaptTo(javax.jcr.Session.class)).thenReturn(jcrSesson);
    Session session = repository.loginAdministrative("ieb");
    when(jcrSesson.getUserManager()).thenReturn(sparseMapUserManager);
    when(sparseMapUserManager.getSession()).thenReturn(session);
    when(clusterTrackingService.getClusterUniqueId())
        .thenReturn(String.valueOf(System.currentTimeMillis()));

    when(request.getRequestPathInfo()).thenReturn(requestPathInfo);
    when(requestPathInfo.getExtension()).thenReturn(null);

    when(adminSession.getUserManager()).thenReturn(userManager);
    when(adminSession.getPrincipalManager()).thenReturn(principalManager);
    when(adminSession.getAccessControlManager()).thenReturn(accessControlManager);
    when(request.getRemoteUser()).thenReturn("ieb");

    when(request.getRequestParameterMap()).thenReturn(requestParameterMap);
    Map<String, RequestParameter[]> map = new HashMap<String, RequestParameter[]>();

    RequestParameter[] requestParameters =
        new RequestParameter[] {
          requestParameter1, requestParameterNot, requestParameter2,
        };
    map.put("files", requestParameters);

    when(requestParameterMap.entrySet()).thenReturn(map.entrySet());

    when(requestParameter1.isFormField()).thenReturn(false);
    when(requestParameter1.getContentType()).thenReturn("application/pdf");
    when(requestParameter1.getFileName()).thenReturn("testfilename.pdf");
    InputStream input1 = new ByteArrayInputStream(new byte[10]);
    when(requestParameter1.getInputStream()).thenReturn(input1);

    when(requestParameter2.isFormField()).thenReturn(false);
    when(requestParameter2.getContentType()).thenReturn("text/html");
    when(requestParameter2.getFileName()).thenReturn("index.html");
    InputStream input2 = new ByteArrayInputStream(new byte[10]);
    when(requestParameter2.getInputStream()).thenReturn(input2);

    when(requestParameterNot.isFormField()).thenReturn(true);

    // deep create
    // when(adminSession.nodeExists(CreateContentPoolServlet.POOLED_CONTENT_ROOT)).thenReturn(true);
    when(adminSession.itemExists(Mockito.anyString())).thenReturn(true);

    // Because the pooled content paths are generated by a private method,
    // mocking the repository is more problematic than usual. The test
    // therefore relies on inside knowledge that there should be three
    // calls to deepGetOrCreateNode for each file: one for the pooled content
    // node, one for its members node, and one for the manager node.
    when(adminSession.getItem(Mockito.anyString()))
        .thenAnswer(
            new Answer<Item>() {
              public Item answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                String path = (String) args[0];
                if (path.endsWith(POOLED_CONTENT_MEMBERS_NODE)) {
                  return membersNode;
                } else if (path.endsWith("ieb")) {
                  return memberNode;
                } else {
                  return parentNode;
                }
              }
            });

    when(parentNode.addNode(JCR_CONTENT, NT_RESOURCE)).thenReturn(resourceNode);
    when(adminSession.getValueFactory()).thenReturn(valueFactory);
    when(valueFactory.createBinary(Mockito.any(InputStream.class))).thenReturn(binary);

    // access control utils
    accessControlList =
        new AccessControlList() {

          // Add an "addEntry" method so AccessControlUtil can execute something.
          // This method doesn't do anything useful.
          @SuppressWarnings("unused")
          public boolean addEntry(Principal principal, Privilege[] privileges, boolean isAllow)
              throws AccessControlException {
            return true;
          }

          public void removeAccessControlEntry(AccessControlEntry ace)
              throws AccessControlException, RepositoryException {}

          public AccessControlEntry[] getAccessControlEntries() throws RepositoryException {
            return new AccessControlEntry[0];
          }

          public boolean addAccessControlEntry(Principal principal, Privilege[] privileges)
              throws AccessControlException, RepositoryException {
            return false;
          }
        };
    when(accessControlManager.privilegeFromName(Mockito.anyString())).thenReturn(allPrivilege);
    AccessControlPolicy[] acp = new AccessControlPolicy[] {accessControlList};
    when(accessControlManager.getPolicies(Mockito.anyString())).thenReturn(acp);

    StringWriter stringWriter = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(stringWriter));

    CreateContentPoolServlet cp = new CreateContentPoolServlet();
    cp.eventAdmin = eventAdmin;
    cp.clusterTrackingService = clusterTrackingService;
    cp.sparseRepository = repository;

    cp.doPost(request, response);

    // Verify that we created all the nodes.

    JSONObject jsonObject = new JSONObject(stringWriter.toString());
    Assert.assertNotNull(jsonObject.getString("testfilename.pdf"));
    Assert.assertNotNull(jsonObject.getString("index.html"));
    Assert.assertEquals(2, jsonObject.length());
  }