/*TODO: These can be refactored into one method that performs and http get on the id.
   * The user will need to be added and retrieved from the state in this instance.
   * A return of 200 will mean the object is found, 400 not found, anything else should throw an exception.
   */
  private boolean checkProjectExists(String id) throws BizPolicyException {
    boolean found = false;
    Project project = projectBizService.getProject(id);
    if (project != null) {
      found = true;
    }

    return found;
  }
  /**
   * Test getting all the projects for a user
   *
   * @throws InvalidXmlException
   * @throws BizInternalException
   * @throws BizPolicyException
   * @throws IOException
   * @throws ServletException
   */
  @Test
  public void testGetAllProjects()
      throws InvalidXmlException, BizPolicyException, IOException, ServletException {

    // Test getting the list of project without logging in
    final String mimeType = "application/xml";
    final MockHttpServletRequest mockReq = newMockRequest("GET", "/project", "test.org", 80);
    MockHttpServletResponse resp = new MockHttpServletResponse();

    controller.handleEmptyGetRequest(mimeType, null, mockReq, resp);

    assertNotNull(resp);
    assertEquals(401, resp.getStatus());

    // Test getting a single project
    ProjectBizService bizService = mock(ProjectBizService.class);
    HashSet<Project> projects = new HashSet<Project>();
    projects.add(projectOne);

    when(bizService.findByAdmin(admin)).thenReturn(projects);
    controller.setBizService(bizService);

    controller.setAuthenticatedUser(admin);
    resp = new MockHttpServletResponse();
    controller.handleEmptyGetRequest(mimeType, null, mockReq, resp);
    assertNotNull(resp);
    assertEquals(200, resp.getStatus());

    Bop bop =
        businessObjectBuilder.buildBusinessObjectPackage(
            new ByteArrayInputStream(resp.getContentAsByteArray()));
    assertNotNull(bop);
    assertEquals(1, bop.getProjects().size());

    // Test a query that returns no projects

    projects.clear();

    when(bizService.findByAdmin(user)).thenReturn(projects);
    controller.setAuthenticatedUser(user);
    resp = new MockHttpServletResponse();
    controller.handleEmptyGetRequest(mimeType, null, mockReq, resp);
    assertNotNull(resp);
    assertEquals(200, resp.getStatus());

    bop =
        businessObjectBuilder.buildBusinessObjectPackage(
            new ByteArrayInputStream(resp.getContentAsByteArray()));
    assertNotNull(bop);
    assertEquals(0, bop.getProjects().size());

    // Test admin sees all projects in the system
    Project newProject = new Project();
    newProject.setId(testProjectIdTwo);
    newProject.setName("Second_Test_Project");
    newProject.setDescription("foo");
    List<String> numbers = new ArrayList<String>();
    numbers.add("54321");
    numbers.add("9876");
    newProject.setNumbers(numbers);
    newProject.setFundingEntity("moo");
    newProject.setStartDate(new DateTime(2012, 5, 4, 0, 0));
    newProject.setEndDate(new DateTime(2013, 12, 23, 0, 0));
    newProject.addPi(user.getId());
    projectService.create(newProject);

    controller.setBizService(projectBizService);
    controller.setAuthenticatedUser(admin);
    resp = new MockHttpServletResponse();
    controller.handleEmptyGetRequest(mimeType, null, mockReq, resp);
    assertNotNull(resp);
    assertEquals(200, resp.getStatus());

    bop =
        businessObjectBuilder.buildBusinessObjectPackage(
            new ByteArrayInputStream(resp.getContentAsByteArray()));
    assertNotNull(bop);
    assertEquals(projectService.getAll().size(), bop.getProjects().size());
  }