Index: test/edu/ucsb/nceas/metacattest/AccessControlTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/AccessControlTest.java	(nonexistent)
+++ test/edu/ucsb/nceas/metacattest/AccessControlTest.java	(revision 2239)
@@ -0,0 +1,580 @@
+/**
+ *  '$RCSfile$'
+ *  Copyright: 2004 Regents of the University of California and the
+ *              National Center for Ecological Analysis and Synthesis
+ *  Purpose: To test the Access Controls in metacat by JUnit
+ *
+ *   '$Author$'
+ *     '$Date$'
+ * '$Revision$'
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+package edu.ucsb.nceas.metacattest;
+
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.SimpleTimeZone;
+import java.util.TimeZone;
+
+import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
+import edu.ucsb.nceas.metacat.client.Metacat;
+import edu.ucsb.nceas.metacat.client.MetacatAuthException;
+import edu.ucsb.nceas.metacat.client.MetacatException;
+import edu.ucsb.nceas.metacat.client.MetacatFactory;
+import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
+import edu.ucsb.nceas.utilities.IOUtil;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * A JUnit test for testing Step class processing
+ */
+public class AccessControlTest
+    extends TestCase {
+  private String metacatUrl = "@systemidserver@@servlet-path@";
+  private String wrongMetacatUrl =
+      "http://somepalce.somewhere.com/some/servlet/metacat";
+  private String username = "@mcuser@";
+  private String password = "@mcpassword@";
+  private String anotheruser = "@mcanotheruser@";
+  private String anotherpassword = "@mcanotherpassword@";
+  private String failpass = "uidfnkj43987yfdn";
+  private String prefix = "test";
+  private String newdocid = null;
+  private String testfile = "test/jones.204.22.xml";
+  private String queryFile = "test/query.xml";
+  private String testdocument = "";
+  private Metacat m;
+  private boolean SUCCESS = true;
+  private boolean FAILURE = false;
+
+  /**
+   * Constructor to build the test
+   *
+   * @param name the name of the test method
+   */
+  public AccessControlTest(String name) {
+    super(name);
+    newdocid = generateDocid();
+  }
+
+  /**
+   * Establish a testing framework by initializing appropriate objects
+   */
+  public void setUp() {
+    try {
+      FileReader fr = new FileReader(testfile);
+      testdocument = IOUtil.getAsString(fr, true);
+    }
+    catch (IOException ioe) {
+      fail("Can't read test data to run the test: " + testfile);
+    }
+
+    try {
+      System.err.println("Test Metacat: " + metacatUrl);
+      m = MetacatFactory.createMetacatConnection(metacatUrl);
+    }
+    catch (MetacatInaccessibleException mie) {
+      System.err.println("Metacat is: " + metacatUrl);
+      fail("Metacat connection failed." + mie.getMessage());
+    }
+  }
+
+  /**
+   * Release any objects after tests are complete
+   */
+  public void tearDown() {
+  }
+
+  /**
+   * Create a suite of tests to be run together
+   */
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTest(new AccessControlTest("initialize"));
+    // Test basic functions
+    suite.addTest(new AccessControlTest("login"));
+    suite.addTest(new AccessControlTest("insert"));
+    suite.addTest(new AccessControlTest("read"));
+    suite.addTest(new AccessControlTest("update"));
+    suite.addTest(new AccessControlTest("delete"));
+    // Tests when no access is specified
+    suite.addTest(new AccessControlTest("noAccessSpecified_Owner"));
+    suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
+    suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
+
+    return suite;
+  }
+
+  /**
+   * Run an initial test that always passes to check that the test
+   * harness is working.
+   */
+  public void initialize() {
+    assertTrue(1 == 1);
+  }
+
+  /**
+   * Test the login() function with valid credentials
+   */
+  public void login() {
+    // Try a valid login
+    try {
+      String response = m.login(username, password);
+      System.err.println("Login response: " + response);
+      assertTrue(response != null);
+      assertTrue(response.indexOf("<login>") != -1);
+      String sessionId = m.getSessionId();
+      System.err.println("Session ID: " + m.getSessionId());
+      assertTrue(sessionId != null);
+      assertTrue(response.indexOf(m.getSessionId()) != -1);
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+  }
+
+  /**
+   * Test the insert() function with a known document
+   */
+  public void insert() {
+    try {
+      String identifier = newdocid + ".1";
+      m.login(username, password);
+      String response = m.insert(identifier,
+                                 new StringReader(testdocument), null);
+      assertTrue(response.indexOf("<success>") != -1);
+      assertTrue(response.indexOf(identifier) != -1);
+      System.err.println(response);
+
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      assertTrue(1 == 1);
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+  }
+
+  /**
+   * The read() function
+   */
+  public void read() {
+    try {
+      m.login(username, password);
+      Reader r = m.read(newdocid + ".1");
+      String doc = IOUtil.getAsString(r, true);
+      doc = doc + "\n";
+      System.err.println(doc);
+      assertTrue(doc.equals(testdocument));
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+  }
+
+  /**
+   * Test the update() function with a known document
+   */
+  public void update() {
+    try {
+      String identifier = newdocid + ".2";
+      m.login(username, password);
+      String response = m.update(identifier,
+                                 new StringReader(testdocument), null);
+      assertTrue(response.indexOf("<success>") != -1);
+      assertTrue(response.indexOf(identifier) != -1);
+      System.err.println(response);
+
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+  }
+
+  /**
+   * Test the delete() function with a known document
+   */
+  public void delete() {
+    try {
+      String identifier = newdocid + ".2";
+      m.login(username, password);
+      String response = m.delete(identifier);
+      assertTrue(response.indexOf("<success>") != -1);
+      System.err.println(response);
+
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+  }
+
+  /**
+   * Test the case when no access is specified and owner is logged in
+   */
+  public void noAccessRuleSpecified_Owner() {
+    try {
+      newdocid = generateDocid();
+      String identifier = newdocid + ".1";
+      // login
+      m.login(username, password);
+
+      // insert a document - get the docid
+      insertDocid(newdocid + ".1", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
+
+      // update the document
+      updateDocid(newdocid + ".2", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS);
+
+      // update the inline data
+      updateDocid(newdocid + ".3", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS);
+
+      // update the online data
+      updateDocid(newdocid + ".4", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS);
+
+      // update the document access control
+      updateDocid(newdocid + ".5", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS);
+
+      // update the document access control for inline data
+      updateDocid(newdocid + ".6", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS);
+
+      // update the document access control for online data
+      updateDocid(newdocid + ".7", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS);
+
+      // delete the document
+      deleteDocid(newdocid + ".7", SUCCESS);
+      m.logout();
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+
+  }
+
+  /**
+   * Test the case when no access is specified and another user is logged in
+   */
+  public void noAccessRuleSpecified_Random() {
+    try {
+      newdocid = generateDocid();
+      String identifier = newdocid + ".1";
+      // login
+      m.login(username, password);
+
+      // insert a document - get the docid
+      insertDocid(newdocid + ".1", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
+
+      // logoutand login as other user
+      m.logout();
+      m.login(anotheruser, anotherpassword);
+
+      // read the document
+      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
+
+      // update the document
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the inline data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the online data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control for inline data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control for online data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // delete the document
+      deleteDocid(newdocid + ".2", SUCCESS);
+      m.logout();
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+
+  }
+
+  /**
+   * Test the case when no access is specified and public is logged in
+   */
+  public void noAccessRuleSpecified_Public() {
+    try {
+      newdocid = generateDocid();
+      String identifier = newdocid + ".1";
+      // login
+      m.login(username, password);
+
+      // insert a document - get the docid
+      insertDocid(newdocid + ".1", testdocument, SUCCESS);
+      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
+
+      // logoutand login as other user
+      m.logout();
+
+      // read the document
+      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
+
+      // update the document
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the inline data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the online data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control for inline data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // update the document access control for online data
+      updateDocid(newdocid + ".2", testdocument, FAILURE);
+
+      // delete the document
+      deleteDocid(newdocid + ".2", SUCCESS);
+      m.logout();
+    }
+    catch (MetacatAuthException mae) {
+      fail("Authorization failed:\n" + mae.getMessage());
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+
+  }
+
+  public String insertDocid(String docid, String docText, boolean result) {
+    String response = null;
+    try {
+      response = m.insert(docid,
+                          new StringReader(testdocument), null);
+      if (result) {
+        assertTrue( (response.indexOf("<success>") != -1));
+        assertTrue(response.indexOf(docid) != -1);
+      }
+      else {
+        assertTrue( (response.indexOf("<success>") == -1));
+      }
+      System.err.println(response);
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+    return response;
+  }
+
+  public String updateDocid(String docid, String docText, boolean result) {
+    String response = null;
+    try {
+      response = m.update(docid,
+                          new StringReader(docText), null);
+      if (result) {
+        assertTrue( (response.indexOf("<success>") != -1));
+        assertTrue(response.indexOf(docid) != -1);
+      }
+      else {
+        assertTrue( (response.indexOf("<success>") == -1));
+      }
+      System.err.println(response);
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+
+    return response;
+  }
+
+  public void deleteDocid(String docid, boolean result) {
+    try {
+      String response = m.delete(docid);
+      if (result) {
+        assertTrue(response.indexOf("<success>") != -1);
+      }
+      else {
+        assertTrue(response.indexOf("<success>") == -1);
+      }
+      System.err.println(response);
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+  }
+
+  public void readDocidWhichEqualsDoc(String docid, String testDoc, boolean result) {
+    try {
+      Reader r = m.read(docid);
+      String doc = IOUtil.getAsString(r, true);
+      doc = doc + "\n";
+      if (result) {
+        assertTrue(testDoc.equals(doc));
+      } else {
+        assertTrue(doc.indexOf("<error>") != -1);
+      }
+    }
+    catch (MetacatInaccessibleException mie) {
+      fail("Metacat Inaccessible:\n" + mie.getMessage());
+    }
+    catch (InsufficientKarmaException ike) {
+      fail("Insufficient karma:\n" + ike.getMessage());
+    }
+    catch (MetacatException me) {
+      fail("Metacat Error:\n" + me.getMessage());
+    }
+    catch (Exception e) {
+      fail("General exception:\n" + e.getMessage());
+    }
+
+  }
+
+  /**
+   * Create a hopefully unique docid for testing insert and update. Does
+   * not include the 'revision' part of the id.
+   *
+   * @return a String docid based on the current date and time
+   */
+  private String generateDocid() {
+    StringBuffer docid = new StringBuffer(prefix);
+    docid.append(".");
+
+    // Create a calendar to get the date formatted properly
+    String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
+    SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
+    pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
+    pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
+    Calendar calendar = new GregorianCalendar(pdt);
+    Date trialTime = new Date();
+    calendar.setTime(trialTime);
+    docid.append(calendar.get(Calendar.YEAR));
+    docid.append(calendar.get(Calendar.DAY_OF_YEAR));
+    docid.append(calendar.get(Calendar.HOUR_OF_DAY));
+    docid.append(calendar.get(Calendar.MINUTE));
+    docid.append(calendar.get(Calendar.SECOND));
+
+    return docid.toString();
+  }
+}

Property changes on: test/edu/ucsb/nceas/metacattest/AccessControlTest.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
