@Before
  public void setup() throws ArchiveServiceException, RelationshipConstraintException {

    final PersonBizPolicyConsultant pc = userService.getPolicyConsultant();
    userService.setPolicyConsultant(
        new PersonBizPolicyConsultant() {
          @Override
          public boolean enforceRegistrationStatusOnCreate() {
            return pc.enforceRegistrationStatusOnCreate();
          }

          @Override
          public List<RegistrationStatus> allowedRegistrationStatusOnCreate() {
            return Arrays.asList(RegistrationStatus.PENDING, RegistrationStatus.APPROVED);
          }

          @Override
          public RegistrationStatus getDefaultRegistrationStatus() {
            return pc.getDefaultRegistrationStatus();
          }

          @Override
          public List<Role> getRolesForRegistrationStatus(RegistrationStatus status) {
            return pc.getRolesForRegistrationStatus(status);
          }
        });

    requestUtil = mock(RequestUtil.class);
    when(requestUtil.buildRequestUrl(any(HttpServletRequest.class))).thenReturn(projectOne.getId());

    String idPrefix;
    if (requestUtil.isAlwaysIncludePort()) {
      idPrefix = ID_PREFIX_WITH_PORT;
    } else {
      idPrefix = ID_PREFIX;
    }

    testProjectIdOne = idPrefix + TEST_ID_PART;
    testProjectIdTwo = idPrefix + TEST_ID_PART_TWO;

    controller =
        new ProjectController(
            projectService,
            projectBizService,
            userService,
            businessObjectBuilder,
            requestUtil,
            authorizationService);
  }
  /**
   * Tests getting the project's collections
   *
   * @throws InvalidXmlException
   * @throws BizInternalException
   * @throws BizPolicyException
   * @throws IOException
   */
  @Test
  public void testGetProjectsCollections()
      throws InvalidXmlException, BizInternalException, BizPolicyException, IOException {

    final String mimeType = "application/xml";
    final MockHttpServletRequest mockReq =
        newMockRequest("GET", projectOne.getId() + "/collections", "test.org", 80);
    MockHttpServletResponse resp = new MockHttpServletResponse();

    when(requestUtil.buildRequestUrl(any(HttpServletRequest.class)))
        .thenReturn(projectOne.getId() + "/collections");

    controller.setAuthenticatedUser(admin);
    controller.handleProjectCollectionsGetRequest(
        projectOne.getId(), mimeType, null, mockReq, resp);

    assertNotNull(resp);
    assertEquals(resp.getErrorMessage(), 200, resp.getStatus());
    Bop returnedBop =
        businessObjectBuilder.buildBusinessObjectPackage(
            new ByteArrayInputStream(resp.getContentAsByteArray()));
    assertNotNull(returnedBop);

    Set<Collection> collections = returnedBop.getCollections();
    assertNotNull(collections);
    assertEquals(1, collections.size());
  }
  /**
   * Tests the authentication of different calls. Currently tests adding a project, updating a
   * project and getting collections without correct permissions.
   *
   * @throws InvalidXmlException
   * @throws BizInternalException
   * @throws BizPolicyException
   * @throws IOException
   */
  @Test
  public void testAuthentication()
      throws InvalidXmlException, BizInternalException, BizPolicyException, IOException {
    Project newProject = new Project(projectOne);
    newProject.setName("Update");
    newProject.setDescription("foo-update");
    newProject.setEndDate(new DateTime(2014, 2, 10, 0, 0));

    String mimeType = "application/xml";

    // Test updating a project without proper permissions
    MockHttpServletRequest mockReq = newMockRequest("PUT", projectOne.getId(), "test.org", 80);
    MockHttpServletResponse resp = new MockHttpServletResponse();

    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    businessObjectBuilder.buildProject(newProject, sink);

    controller.setAuthenticatedUser(pendingUser);
    controller.handleUpdateProjectRequest(
        projectOne.getId(), mimeType, sink.toByteArray(), mockReq, resp);
    assertNotNull(resp);
    assertEquals(403, resp.getStatus());

    // Test adding a project without proper permissions
    mimeType = "application/xml";
    mockReq = newMockRequest("POST", "/project", "test.org", 80);
    resp = new MockHttpServletResponse();

    sink = new ByteArrayOutputStream();
    newProject.setId("http://test.org/project/2");
    businessObjectBuilder.buildProject(newProject, sink);

    controller.setAuthenticatedUser(pendingUser);
    controller.handleProjectPostRequest(mimeType, sink.toByteArray(), mockReq, resp);
    assertNotNull(resp);
    assertEquals(403, resp.getStatus());

    // Test getting the collections for a project without proper permissions
    mimeType = "application/xml";
    mockReq = newMockRequest("GET", projectOne.getId() + "/collections", "test.org", 80);
    resp = new MockHttpServletResponse();

    when(requestUtil.buildRequestUrl(any(HttpServletRequest.class)))
        .thenReturn(projectOne.getId() + "/collections");

    controller.setAuthenticatedUser(pendingUser);
    controller.handleProjectCollectionsGetRequest(
        projectOne.getId(), mimeType, null, mockReq, resp);
    assertNotNull(resp);
    assertEquals(403, resp.getStatus());
  }
  /**
   * Test getting a project that does not exist
   *
   * @throws InvalidXmlException
   * @throws BizInternalException
   * @throws BizPolicyException
   * @throws IOException
   */
  @Test
  public void testGetNonExistingProject()
      throws InvalidXmlException, BizInternalException, BizPolicyException, IOException {

    when(requestUtil.buildRequestUrl(any(HttpServletRequest.class))).thenReturn("  ");

    final String mimeType = "application/xml";
    final MockHttpServletRequest mockReq =
        newMockRequest("GET", "/project/" + "cowsarethebest", "test.org", 80);
    MockHttpServletResponse resp = new MockHttpServletResponse();

    controller.setAuthenticatedUser(admin);
    controller.handleProjectGetRequest(projectOne.getId(), mimeType, null, mockReq, resp);

    assertNotNull(resp);
    assertEquals(404, resp.getStatus());
  }