@Test
  public void testIngest() throws Exception {
    String pid = "demo:m_ds_test";

    for (String contentLocation : copyTempFileLocations) {
      try {
        apim.ingest(getAtomObject(pid, contentLocation), ATOM1_1.uri, null);
        fail("ingest should have failed with " + contentLocation);
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("ValidationException"));
      }
      try {
        apim.ingest(getFoxmlObject(pid, contentLocation), FOXML1_1.uri, null);
        fail("ingest should have failed with " + contentLocation);
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("ObjectIntegrityException"));
      }
    }

    for (String contentLocation : uploadedLocations) {
      try {
        apim.ingest(getAtomObject(pid, contentLocation), ATOM1_1.uri, null);
        fail("ingest should have failed with " + contentLocation);
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("StreamReadException"));
      }
      try {
        apim.ingest(getFoxmlObject(pid, contentLocation), FOXML1_1.uri, null);
        fail("ingest should have failed with " + contentLocation);
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("StreamReadException"));
      }
    }
  }
Beispiel #2
0
  public String addPolicy(File policyFile) throws Exception {
    byte[] policy = DataUtils.loadFile(policyFile.getAbsolutePath());

    String policyId = getPolicyId(policy);

    String policyFileName = "file:///" + policyFile.getAbsolutePath();

    // escape any pid namespace character
    if (policyId.contains(":")) {
      policyId = policyId.replace(":", "%3A");
    }

    String pid = "demo:" + policyId;

    StringBuilder foxml = new StringBuilder();

    // basic empty object

    foxml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    foxml.append(
        "<foxml:digitalObject VERSION=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
    foxml.append("    xmlns:foxml=\"info:fedora/fedora-system:def/foxml#\"\n");
    foxml.append(
        "           xsi:schemaLocation=\""
            + Constants.FOXML.uri
            + " "
            + Constants.FOXML1_1.xsdLocation
            + "\"");
    foxml.append("\n           PID=\"" + StreamUtility.enc(pid) + "\">\n");
    foxml.append("  <foxml:objectProperties>\n");
    foxml.append(
        "    <foxml:property NAME=\"info:fedora/fedora-system:def/model#state\" VALUE=\"A\"/>\n");
    foxml.append(
        "    <foxml:property NAME=\"info:fedora/fedora-system:def/model#label\" VALUE=\""
            + StreamUtility.enc("test policy object")
            + "\"/>\n");
    foxml.append("  </foxml:objectProperties>\n");

    foxml.append(
        "<foxml:datastream ID=\""
            + FedoraPolicyStore.POLICY_DATASTREAM
            + "\" CONTROL_GROUP=\"M\">");
    foxml.append(
        "<foxml:datastreamVersion ID=\"POLICY.0\" MIMETYPE=\"text/xml\" LABEL=\"XACML policy datastream\">");

    foxml.append("  <foxml:contentLocation REF=\"" + policyFileName + "\" TYPE=\"URL\"/>");

    // foxml.append("  <foxml:xmlContent>");
    // foxml.append(policy);
    // foxml.append("    </foxml:xmlContent>");

    foxml.append("  </foxml:datastreamVersion>");
    foxml.append("</foxml:datastream>");

    foxml.append("</foxml:digitalObject>");

    apim.ingest(foxml.toString().getBytes("UTF-8"), FOXML1_1.uri, "ingesting new foxml object");

    return policyId;
  }
  @Test
  public void testModifyDatastreamByReferenceWithChecksum() throws Exception {
    String pid = "file:m_ds_test_add"; // file pid namespace should work as well as demo
    String checksumType = "MD5";
    apim.ingest(getAtomObject(pid, null), ATOM1_1.uri, null);
    File temp = null;
    try {
      temp = File.createTempFile("foo", "bar");
      String contentLocation = getFedoraClient().uploadFile(temp);
      String checksum = computeChecksum(checksumType, new FileInputStream(temp));
      String dsId = addDatastream(pid, contentLocation, checksumType, checksum);
      assertEquals("DS", dsId);

      FileOutputStream os = new FileOutputStream(temp);
      os.write("testModifyDatastreamByReferenceWithChecksum".getBytes());
      os.close();
      contentLocation = getFedoraClient().uploadFile(temp);
      checksum = computeChecksum(checksumType, new FileInputStream(temp));
      modifyDatastreamByReference(pid, contentLocation, checksumType, checksum);

      // Now ensure that bogus checksums do indeed fail
      checksum = "bogus";
      try {
        modifyDatastreamByReference(pid, contentLocation, checksumType, checksum);
        fail("Modifying datastream with bogus checksum should have failed.");
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("Checksum Mismatch"));
      }
    } finally {
      apim.purgeObject(pid, "test", false);
      if (temp != null) {
        temp.delete();
      }
    }
  }
  @Test
  public void testAddDatastreamWithChecksum() throws Exception {
    String pid = "demo:m_ds_test_add";
    String checksumType = "MD5";
    apim.ingest(getAtomObject(pid, null), ATOM1_1.uri, null);
    File temp = null;

    try {
      temp = File.createTempFile("foo", "bar");
      String contentLocation = getFedoraClient().uploadFile(temp);
      String checksum = computeChecksum(checksumType, new FileInputStream(temp));
      String dsId = addDatastream(pid, contentLocation, checksumType, checksum);
      assertEquals("DS", dsId);

      // Now ensure that bogus checksums do indeed fail
      apim.purgeDatastream(pid, dsId, null, null, null, false);
      checksum = "bogus";
      try {
        addDatastream(pid, contentLocation, checksumType, checksum);
        fail("Adding datastream with bogus checksum should have failed.");
      } catch (RemoteException e) {
        assertTrue(e.getMessage().contains("Checksum Mismatch"));
      }
    } finally {
      apim.purgeObject(pid, "test", false);
      if (temp != null) {
        temp.delete();
      }
    }
  }
  @Test
  public void testModifyDatastreamByReference() throws Exception {
    String pid = "demo:m_ds_test_add";
    String dsLocation = getBaseURL() + "/get/fedora-system:ContentModel-3.0/DC";
    apim.ingest(getAtomObject(pid, dsLocation), ATOM1_1.uri, null);

    try {
      for (String contentLocation : copyTempFileLocations) {
        try {
          modifyDatastreamByReference(pid, contentLocation);
          fail("modifyDatastreamByReference should have failed with " + contentLocation);
        } catch (RemoteException e) {
          assertTrue(e.getMessage().contains("ValidationException"));
        }
      }

      for (String contentLocation : uploadedLocations) {
        try {
          modifyDatastreamByReference(pid, contentLocation);
          fail("modifyDatastreamByReference should have failed with " + contentLocation);
        } catch (RemoteException e) {
          assertTrue(e.getMessage().contains("StreamReadException"));
        }
      }

      // A null contentLocation should cause the server to generate a
      // copy:// url
      modifyDatastreamByReference(pid, null);
    } finally {
      apim.purgeObject(pid, "test", false);
    }
  }
  @Test
  public void testAddDatastream() throws Exception {
    String pid = "demo:m_ds_test_add";

    apim.ingest(getAtomObject(pid, null), ATOM1_1.uri, null);

    try {
      for (String contentLocation : copyTempFileLocations) {
        try {
          addDatastream(pid, contentLocation);
          fail("addDatastream should have failed with " + contentLocation);
        } catch (RemoteException e) {
          assertTrue(e.getMessage().contains("ValidationException"));
        }
      }

      for (String contentLocation : uploadedLocations) {
        try {
          addDatastream(pid, contentLocation);
          fail("addDatastream should have failed with " + contentLocation);
        } catch (RemoteException e) {
          assertTrue(e.getMessage().contains("StreamReadException"));
        }
      }

    } finally {
      apim.purgeObject(pid, "test", false);
    }
  }