@Test
  public void fileUriTest() throws Exception {
    String origSrc = "";
    if (sampleFilePath.contains(":/")) {
      origSrc = "file://" + sampleFilePath;
    } else {
      origSrc = "file:///" + sampleFilePath;
    }

    Dcp sip = new Dcp();
    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource(origSrc);
    file.setExtant(true);
    sip.addFile(file);
    String id = ingest.getSipStager().addSIP(sip);

    ExternalContentStager ecs = new ExternalContentStager();
    ecs.setIngestFramework(ingest);

    ecs.execute(id);

    sip = ingest.getSipStager().getSIP(id);
    DcsFile retrieved = sip.getFiles().iterator().next();
    Assert.assertTrue(origSrc != retrieved.getSource());
    Assert.assertTrue(ingest.getFileContentStager().contains(retrieved.getSource()));
  }
  public void testGET_Dcp() throws Exception {
    HttpTester request = new HttpTester();
    HttpTester response = new HttpTester();

    request.setMethod("GET");
    request.setHeader("Host", "tester");
    request.setVersion("HTTP/1.0");

    DcsModelBuilder mb = new DcsXstreamStaxModelBuilder();

    Collection<DcsFile> files = dcp.getFiles();

    request.setURI("/access/query/" + ServletUtil.encodeURLPath("entityType:File"));

    response.parse(servletContainer.getResponses(request.generate()));

    assertEquals(200, response.getStatus());
    assertEquals("application/xml", response.getContentType());
    assertNotNull(response.getContent());
    assertNotNull(response.getHeader("X-TOTAL-MATCHES"));

    assertEquals("" + files.size(), response.getHeader("X-TOTAL-MATCHES"));

    Dcp dcp = mb.buildSip(new ByteArrayInputStream(response.getContent().getBytes()));

    assertEquals(files.size(), dcp.getFiles().size());

    request.setURI("/access/entity/" + ServletUtil.encodeURLPath(":blah:"));
    response.parse(servletContainer.getResponses(request.generate()));
    assertEquals(404, response.getStatus());

    request.setURI(
        "/access/entity/"
            + ServletUtil.encodeURLPath("title:(alksdfasd-axda+!E@!QW@#$!+@!#--~+))"));
    response.parse(servletContainer.getResponses(request.generate()));
    assertEquals(404, response.getStatus());

    request.setURI("/access/entity/" + ServletUtil.encodeURLPath("title:test\\-escape)"));
    response.parse(servletContainer.getResponses(request.generate()));
    assertEquals(404, response.getStatus());

    request.setURI(
        "/access/entity/"
            + "id%3A((%22ARC%5C-11%22)+)+OR+former%3A((%22ARC%5C-11%22)+)creator%3A((%22ARC%5C-11%22)+)+OR+subject%3A((%22ARC%5C-11%22)+)+OR+title%3A((%22ARC%5C-11%22)+)+OR+type%3A((%22ARC%5C-11%22)+)+OR+metadataSearchText%3A((%22ARC%5C-11%22)+)+OR+metadataSchema%3A((%22ARC%5C-11%22)+)fileName%3A((%22ARC%5C-11%22)+)+OR+fileSource%3A((%22ARC%5C-11%22)+)+OR+format%3A((%22ARC%5C-11%22)+)+OR+formatName%3A((%22ARC%5C-11%22)+)+OR+formatSchema%3A((%22ARC%5C-11%22)+)+OR+filePath%3A((%22ARC%5C-11%22)+)+OR+fixityValue%3A((%22ARC%5C-11%22)+)parent%3A((%22ARC%5C-11%22)+)+OR+metadataRef%3A((%22ARC%5C-11%22)+)+OR+formatSchema%3A((%22ARC%5C-11%22)+)+OR+metadataSchema%3A((%22ARC%5C-11%22)+)+OR+collection%3A((%22ARC%5C-11%22)+)+OR+relatedTo%3A((%22ARC%5C-11%22)+)+OR+eventTarget%3A((%22ARC%5C-11%22)+)+OR+deliverableunit%3A((%22ARC%5C-11%22)+)+OR+fileRef%3A((%22ARC%5C-11%22)+)+OR+hasRelationship%3A((%22ARC%5C-11%22)+)?");
    response.parse(servletContainer.getResponses(request.generate()));
    assertEquals(404, response.getStatus());
  }
  @Test
  public void untouchedStagedFileTest() throws Exception {
    StagedFile staged =
        ingest.getFileContentStager().add(IOUtils.toInputStream("stagedContent"), null);
    Dcp sip = new Dcp();
    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource(staged.getReferenceURI());
    file.setExtant(true);
    sip.addFile(file);
    String id = ingest.getSipStager().addSIP(sip);

    ExternalContentStager ecs = new ExternalContentStager();
    ecs.setIngestFramework(ingest);

    ecs.execute(id);

    sip = ingest.getSipStager().getSIP(id);
    DcsFile retrieved = sip.getFiles().iterator().next();
    Assert.assertEquals(staged.getReferenceURI(), retrieved.getSource());
  }
  public void testHEAD() throws Exception {
    Collection<DcsFile> files = dcp.getFiles();

    // ServletTester has internal errors processing head requests so use http client
    // request.setMethod("GET");
    String baseurl = servletContainer.createSocketConnector(true);

    HttpClient client = new HttpClient();

    HeadMethod head =
        new HeadMethod(baseurl + "/access/query/" + ServletUtil.encodeURLPath("entityType:File"));
    int status = client.executeMethod(head);
    assertEquals(200, status);

    assertEquals("application/xml", head.getResponseHeader("Content-Type").getValue());
    assertEquals("" + files.size(), head.getResponseHeader("X-TOTAL-MATCHES").getValue());

    head = new HeadMethod(baseurl + "/access/query/badfield:");
    status = client.executeMethod(head);
    assertEquals(404, status);
  }
  @Test
  public void calculateFixityTest() throws Exception {
    String origSrc = "";
    if (sampleFilePath.contains(":/")) {
      origSrc = "file://" + sampleFilePath;
    } else {
      origSrc = "file:///" + sampleFilePath;
    }

    Dcp sip = new Dcp();
    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource(origSrc);
    file.setExtant(true);
    sip.addFile(file);
    String id = ingest.getSipStager().addSIP(sip);

    ExternalContentStager ecs = new ExternalContentStager();
    ecs.setAlwaysCalculateFixityFor("SHA-1");
    ecs.setIngestFramework(ingest);

    ecs.execute(id);

    sip = ingest.getSipStager().getSIP(id);
    DcsFile retrieved = sip.getFiles().iterator().next();

    String stagedSipRef = ingest.getFileContentStager().get(retrieved.getSource()).getSipRef();

    DcsFile stagedFile = ingest.getSipStager().getSIP(stagedSipRef).getFiles().iterator().next();

    Assert.assertEquals(1, stagedFile.getFixity().size());

    DcsEvent digestEvent =
        ingest.getEventManager().getEventByType(stagedSipRef, Events.FIXITY_DIGEST);
    Assert.assertNotNull(digestEvent);

    for (DcsEntityReference er : digestEvent.getTargets()) {
      Assert.assertEquals(stagedFile.getId(), er.getRef());
    }
  }
  @Test
  public void untouchedNonExtantFileUriTest() throws Exception {
    String origSrc = "file://" + sampleFilePath;

    Dcp sip = new Dcp();
    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource(origSrc);
    file.setExtant(false);
    sip.addFile(file);
    String id = ingest.getSipStager().addSIP(sip);

    ExternalContentStager ecs = new ExternalContentStager();
    ecs.setIngestFramework(ingest);

    ecs.execute(id);

    sip = ingest.getSipStager().getSIP(id);
    DcsFile retrieved = sip.getFiles().iterator().next();
    Assert.assertEquals(origSrc, retrieved.getSource());
    Assert.assertFalse(ingest.getFileContentStager().contains(retrieved.getSource()));
  }
  @Test
  public void downloadEventTest() throws Exception {
    String origSrc = "";
    if (sampleFilePath.contains(":/")) {
      origSrc = "file://" + sampleFilePath;
    } else {
      origSrc = "file:///" + sampleFilePath;
    }

    Dcp sip = new Dcp();
    DcsFile file = new DcsFile();
    file.setId("example:/file");
    file.setSource(origSrc);
    file.setExtant(true);
    sip.addFile(file);
    String id = ingest.getSipStager().addSIP(sip);

    ExternalContentStager ecs = new ExternalContentStager();
    ecs.setIngestFramework(ingest);

    ecs.execute(id);

    sip = ingest.getSipStager().getSIP(id);
    DcsFile retrieved = sip.getFiles().iterator().next();

    String stagedSipRef = ingest.getFileContentStager().get(retrieved.getSource()).getSipRef();

    DcsFile stagedFile = ingest.getSipStager().getSIP(stagedSipRef).getFiles().iterator().next();

    DcsEvent upload = ingest.getEventManager().getEventByType(stagedSipRef, Events.FILE_DOWNLOAD);
    Assert.assertNotNull(upload);

    for (DcsEntityReference er : upload.getTargets()) {
      Assert.assertEquals(stagedFile.getId(), er.getRef());
    }
  }