]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-347: Added negative tests and additional (stubbed) tests for major responses...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Aug 2009 22:00:45 +0000 (22:00 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 6 Aug 2009 22:00:45 +0000 (22:00 +0000)
services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java
services/intake/client/src/test/java/org/collectionspace/services/client/test/IntakeServiceTest.java

index 167c44d1fad036d151faa711fb1861eae4bcf16c..607493911ef845e050242bc177fb527c8edb523c 100644 (file)
@@ -32,7 +32,7 @@ public class IntakeClient extends BaseServiceClient {
      * Default constructor for IntakeClient class.
      *
      */
-    private IntakeClient() {
+    public IntakeClient() {
         ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
         RegisterBuiltin.register(factory);
         intakeProxy = ProxyFactory.create(IntakeProxy.class, getBaseURL());
index 19b04a8a6e6205141e533d7d198821f1b0584ff8..3f233255088e5dc95b5c5b1d83952baad68a1f9b 100644 (file)
@@ -1,3 +1,26 @@
+/**
+ * This document is a part of the source code and related artifacts
+ * for CollectionSpace, an open source collections management system
+ * for museums and related institutions:
+ *
+ * http://www.collectionspace.org
+ * http://wiki.collectionspace.org
+ *
+ * Copyright © 2009 Regents of the University of California
+ *
+ * Licensed under the Educational Community License (ECL), Version 2.0.
+ * You may not use this file except in compliance with this License.
+ *
+ * You may obtain a copy of the ECL 2.0 License at
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.collectionspace.services.client.test;
 
 import java.util.ArrayList;
@@ -13,145 +36,479 @@ import org.testng.annotations.Test;
 import org.collectionspace.services.intake.Intake;
 import org.collectionspace.services.intake.IntakeList;
 import org.collectionspace.services.client.IntakeClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * A IntakeNuxeoServiceTest.
+ * IntakeServiceTest, carries out tests against a
+ * deployed and running Intake Service.
  * 
- * @version $Revision:$
+ * $LastChangedRevision: 511 $
+ * $LastChangedDate: 2009-08-06 20:16:16 +0000 (Thu, 06 Aug 2009) $
  */
 public class IntakeServiceTest {
 
-    private IntakeClient intakeClient = IntakeClient.getInstance();
-    private String updateId = null;
-    private String deleteId = null;
-
-    @Test
-    public void createIntake() {
-        long identifier = this.createIdentifier();
-
-        Intake intake = createIntake(identifier);
-        ClientResponse<Response> res = intakeClient.createIntake(intake);
-        verbose("createIntake: status = " + res.getStatus());
-        Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
-
-        //store updateId locally for "update" test
-        if(updateId == null){
-            updateId = extractId(res);
-        }else{
-            deleteId = extractId(res);
-            System.out.println("Set deleteId: " + deleteId);
-        }
-    }
+  private IntakeClient intakeClient = new IntakeClient();
+  private String knownIntakeId = null;
+  private final String NON_EXISTENT_ID = createNonExistentIdentifier();
+  final Logger logger = LoggerFactory.getLogger(IntakeServiceTest.class);
 
-    @Test(dependsOnMethods = {"createIntake"})
-    public void updateIntake() {
-        ClientResponse<Intake> res = intakeClient.getIntake(updateId);
-        Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
-        verbose("getIntake: status = " + res.getStatus());
-        Intake intake = res.getEntity();
-        verbose("Got Intake to update with ID: " + updateId,
-                intake, Intake.class);
-
-        //intake.setCsid("updated-" + updateId);
-        intake.setEntryNumber("updated-" + intake.getEntryNumber());
-        intake.setEntryDate("updated-" + intake.getEntryDate());
-
-        // make call to update service
-        res = intakeClient.updateIntake(updateId, intake);
-        verbose("updateIntake: status = " + res.getStatus());
-        Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
-        // check the response
-        Intake updatedIntake = res.getEntity();
-        Assert.assertEquals(updatedIntake.getEntryDate(), intake.getEntryDate());
-        verbose("updateIntake: ", updatedIntake, Intake.class);
-
-        return;
-    }
+  // ---------------------------------------------------------------
+  // Service Discovery tests
+  // ---------------------------------------------------------------
 
-    @Test(dependsOnMethods = {"createIntake"})
-    public void createCollection() {
-        for(int i = 0; i < 3; i++){
-            this.createIntake();
-        }
-    }
+  // TBA
+  
+  
+  // ---------------------------------------------------------------
+  // CRUD tests : CREATE tests
+  // ---------------------------------------------------------------
 
-    @Test(dependsOnMethods = {"createCollection"})
-    public void getIntakeList() {
-        //the resource method is expected to return at least an empty list
-        ClientResponse<IntakeList> res = intakeClient.getIntakeList();
-        verbose("getIntakeList: status = " + res.getStatus());
-        Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
-        IntakeList coList = res.getEntity();
-        List<IntakeList.IntakeListItem> coItemList = coList.getIntakeListItem();
-        int i = 0;
-        for(IntakeList.IntakeListItem pli : coItemList){
-            verbose("getIntakeList: list-item[" + i + "] csid=" + pli.getCsid());
-            verbose("getIntakeList: list-item[" + i + "] entryNumber=" + pli.getEntryNumber());
-            verbose("getIntakeList: list-item[" + i + "] URI=" + pli.getUri());
-            i++;
-        }
-    }
+  // Success outcomes
+  // ----------------
+  
+  /**
+   * Tests creation of a new Intake.
+   *
+   * Expected status code: 201 Created
+   *
+   * Also expected: The 'Location' header contains the URL for the newly created object.
+   * This is required by the extractId() utility method, below.
+   *
+   * The newly-created Intake is also used by other test(s)
+   * (e.g. update, delete) which follow, below.
+   */
+  @Test
+  public void createIntake() {
+    String identifier = this.createIdentifier();
 
-    @Test(dependsOnMethods = {"createCollection"})
-    public void deleteIntake() {
-        System.out.println("Calling deleteIntake:" + deleteId);
-        ClientResponse<Response> res = intakeClient.deleteIntake(deleteId);
-        verbose("deleteIntake: csid=" + deleteId);
-        verbose("deleteIntake: status = " + res.getStatus());
-        Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
-    }
+    Intake intake = createIntake(identifier);
+    ClientResponse<Response> res = intakeClient.createIntake(intake);
+    verbose("createIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
 
-    private Intake createIntake(long identifier) {
-        Intake intake = createIntake("entryNumber-" + identifier,
-                "entryDate-" + identifier);
+    // Store the ID returned from this create operation for additional tests below.
+    knownIntakeId = extractId(res);
+  }
 
-        return intake;
+  /**
+   * Creates two or more new Intakes.
+   *
+   * Repeatedly calls the createIntake test, above, and relies on its
+   * test assertions.
+   *
+   * Expected status code: 201 Created
+   *
+   * The newly-created Intakes are also used by other test(s)
+   * (e.g. read multiple/list) which follow, below.
+   */
+  @Test(dependsOnMethods = {"createIntake"})
+  public void createCollection() {
+    for(int i = 0; i < 3; i++){
+      this.createIntake();
     }
+  }
 
-    private Intake createIntake(String entryNumber, String entryDate) {
-        Intake intake = new Intake();
+  // Failure outcomes
+  // ----------------
 
-        intake.setEntryNumber(entryNumber);
-        intake.setEntryDate(entryDate);
+  /**
+   * Tests creation of a Intake by sending a null to the client proxy.
+   *
+   * Expected status code: (none)
+   *
+   * Expected result: IllegalArgumentException 
+   * (Make sure this is a reported exception in the called class.)
+   */
+  @Test(dependsOnMethods = {"createIntake"}, expectedExceptions = IllegalArgumentException.class)
+  public void createNullIntake() {
+    ClientResponse<Response> res = intakeClient.createIntake(null);
+  }
+               
+  /**
+   * Tests creation of an Intake by sending bad data
+   * (e.g. in a format that doesn't match the Intake schema)
+   * in the entity body of the request.
+   *
+   * Expected status code: 400 Bad Request
+   */
+/*
+  @Test(dependsOnMethods = {"createIntake"})
+  public void createIntakeWithBadData() {
+    // Currently only a stub.
+  }
+*/
 
-        return intake;
-    }
+  /**
+   * Tests creation of an Intake by a user who
+   * is not authorized to perform this action.
+   *
+   * Expected status code: 403 Forbidden
+   */
+/*
+  @Test(dependsOnMethods = {"createIntake"})
+  public void createIntakeWithUnauthorizedUser() {
+    // Currently only a stub.
+  }
+*/
 
-    private String extractId(ClientResponse<Response> res) {
-        MultivaluedMap mvm = res.getMetadata();
-        String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
-        String[] segments = uri.split("/");
-        String id = segments[segments.length - 1];
-        verbose("id=" + id);
-        return id;
-    }
+  /**
+   * Tests creation of a duplicate Intake, whose unique resource identifier
+   * duplicates that of an existing Intake.
+   * 
+   * Expected status code: 409 Conflict
+   */
+/*
+  @Test(dependsOnMethods = {"createIntake"})
+  public void createDuplicateIntake() {
+    Intake intake = createIntake(knownIntakeId);
+    ClientResponse<Response> res = 
+      intakeClient.createIntake(intake);
+    verbose("createDuplicateIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.CONFLICT.getStatusCode());
+  }
+*/
 
-    private void verbose(String msg) {
-        System.out.println("Intake Test: " + msg);
-    }
+  // ---------------------------------------------------------------
+  // CRUD tests : READ tests
+  // ---------------------------------------------------------------
+
+  // Success outcomes
+  // ----------------
+  
+  /**
+   * Tests reading (i.e. retrieval) of a Intake.
+   *
+   * Expected status code: 200 OK
+   */
+  @Test(dependsOnMethods = {"createIntake"})
+  public void getIntake() {
+    ClientResponse<Intake> res = 
+      intakeClient.getIntake(knownIntakeId);
+    verbose("getIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
+  }
+
+  // Failure outcomes
+  // ----------------
 
-    private void verbose(String msg, Object o, Class clazz) {
-        try{
-            verbose(msg);
-            JAXBContext jc = JAXBContext.newInstance(clazz);
-            Marshaller m = jc.createMarshaller();
-            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
-                    Boolean.TRUE);
-            m.marshal(o, System.out);
-        }catch(Exception e){
-            e.printStackTrace();
-        }
+  /**
+   * Tests reading (i.e. retrieval) of a Intake by a user who
+   * is not authorized to perform this action.
+   *
+   * Expected status code: 403 Forbidden
+   */
+/*
+  @Test(dependsOnMethods = {"getIntake"})
+  public void getIntakeWithUnauthorizedUser() {
+    // Currently only a stub
+  }
+*/
+
+  /**
+   * Tests reading (i.e. retrieval) of a non-existent Intake,
+   * whose resource identifier does not exist at the specified URL.
+   *
+   * Expected status code: 404 Not Found
+   */
+  @Test(dependsOnMethods = {"getIntake"})
+  public void getNonExistentIntake() {
+    ClientResponse<Intake> res = 
+      intakeClient.getIntake(NON_EXISTENT_ID);
+    verbose("getNonExistentIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
+  }
+
+
+  // ---------------------------------------------------------------
+  // CRUD tests : READ (list, or multiple) tests
+  // ---------------------------------------------------------------
+
+  // Success outcomes
+  // ----------------
+
+  /**
+   * Tests reading (i.e. retrieval) of a list of multiple Intakes.
+   *
+   * Expected status code: 200 OK
+   *
+   * Also expected: The entity body in the response contains
+   * a representation of the list of Intakes.
+   */
+  @Test(dependsOnMethods = {"createCollection"})
+  public void getIntakeList() {
+    // The resource method is expected to return at least an empty list
+    ClientResponse<IntakeList> res = intakeClient.getIntakeList();
+    IntakeList coList = res.getEntity();
+    verbose("getIntakeList: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
+
+    List<IntakeList.IntakeListItem> coItemList =
+      coList.getIntakeListItem();
+    int i = 0;
+    for(IntakeList.IntakeListItem pli : coItemList){
+      verbose("getIntakeList: list-item[" + i + "] csid=" + pli.getCsid());
+      verbose("getIntakeList: list-item[" + i + "] entryNumber=" + pli.getEntryNumber());
+      verbose("getIntakeList: list-item[" + i + "] URI=" + pli.getUri());
+      i++;
     }
+  }
+
+  /**
+   * Tests reading (i.e. retrieval) of a list of multiple Intakes
+   * when the contents of the list are expected to be empty.
+   *
+   * Expected status code: 200 OK
+   * (Note: *not* 204 No Content)
+   *
+   * Also expected: The entity body in the response contains
+   * a representation of an empty list of Intakes.
+   */
+/*
+  @Test(dependsOnMethods = {"getIntakeList"})
+  public void getIntakeEmptyList() {
+    // Currently only a stub.
+  }
+*/
+  
+  // Failure outcomes
+  // ----------------
+
+  /**
+   * Tests reading (i.e. retrieval) of a list of Intakes
+   * when sending unrecognized query parameters with the request.
+   *
+   * Expected status code: 400 Bad Request
+   */
+/*
+  @Test(dependsOnMethods = {"getIntakeList"})
+  public void getIntakeListWithBadParams() {
+    // Currently only a stub.
+  }
+*/
+
+  /**
+   * Tests reading (i.e. retrieval) of a list of Intakes by a user who
+   * is not authorized to perform this action.
+   *
+   * Expected status code: 403 Forbidden
+   */
+/*
+  @Test(dependsOnMethods = {"getIntakeList"})
+  public void getIntakeListWithUnauthorizedUser() {
+    // Currently only a stub.
+  }
+*/
+
+  
+
+  // ---------------------------------------------------------------
+  // CRUD tests : UPDATE tests
+  // ---------------------------------------------------------------
+
+  // Success outcomes
+  // ----------------
 
-    private void verboseMap(MultivaluedMap map) {
-        for(Object entry : map.entrySet()){
-            MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
-            verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
-        }
+  /**
+   * Tests updating the content of a Intake.
+   *
+   * Expected status code: 200 OK
+   *
+   * Also expected: The entity body in the response contains
+   * a representation of the updated Intake.
+   */
+  @Test(dependsOnMethods = {"createIntake"})
+  public void updateIntake() {
+    ClientResponse<Intake> res = 
+      intakeClient.getIntake(knownIntakeId);
+    verbose("getIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
+    Intake intake = res.getEntity();
+    verbose("Got Intake to update with ID: " + knownIntakeId,
+        intake, Intake.class);
+
+    //intake.setCsid("updated-" + knownIntakeId);
+    intake.setEntryNumber("updated-" + intake.getEntryNumber());
+    intake.setEntryDate("updated-" + intake.getEntryDate());
+    
+    // make call to update service
+    res = 
+      intakeClient.updateIntake(knownIntakeId, intake);
+    verbose("updateIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
+    
+    // check the response
+    Intake updatedIntake = res.getEntity();
+    Assert.assertEquals(updatedIntake.getEntryDate(), intake.getEntryDate());
+    verbose("updateIntake ", updatedIntake, Intake.class);
+  }
+
+  // Failure outcomes
+  // ----------------
+
+  /**
+   * Tests updating the content of a Intake by sending bad data
+   * (e.g. in a format that doesn't match the Intake schema)
+   * in the entity body of the request.
+   *
+   * Expected status code: 400 Bad Request
+   */
+/*
+  @Test(dependsOnMethods = {"updateIntake"})
+  public void updateIntakeWithBadData() {
+    // Currently only a stub.
+  }
+*/
+
+  /**
+   * Tests updating the content of a Intake by a user who
+   * is not authorized to perform this action.
+   *
+   * Expected status code: 403 Forbidden
+   */
+/*
+  @Test(dependsOnMethods = {"updateIntake"})
+  public void updateIntakeWithUnauthorizedUser() {
+    // Currently only a stub.
+  }
+*/
+
+  /**
+   * Tests updating the content of a non-existent Intake, whose
+   * resource identifier does not exist.
+   *
+   * Expected status code: 404 Not Found
+   */
+  @Test(dependsOnMethods = {"updateIntake"})
+  public void updateNonExistentIntake() {
+    // Note: The ID used in this 'create' call may be arbitrary.
+    // The only relevant ID may be the one used in updateIntake(), below.
+    Intake intake = createIntake(NON_EXISTENT_ID);
+    // make call to update service
+    ClientResponse<Intake> res =
+      intakeClient.updateIntake(NON_EXISTENT_ID, intake);
+    verbose("createIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
+  }
+
+
+  // ---------------------------------------------------------------
+  // CRUD tests : DELETE tests
+  // ---------------------------------------------------------------
+
+  // Success outcomes
+  // ----------------
+
+  /**
+   * Tests deleting a Intake.
+   *
+   * Expected status code: 200 OK
+   */
+  @Test(dependsOnMethods = {"createIntake", "getIntake"})
+  public void deleteIntake() {
+    verbose("Calling deleteIntake: " + knownIntakeId);
+    ClientResponse<Response> res = intakeClient.deleteIntake(knownIntakeId);
+    verbose("deleteIntake csid=" + knownIntakeId);
+    verbose("deleteIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
+  }
+
+  // Failure outcomes
+  // ----------------
+
+  /**
+   * Tests deleting a Intake by a user who
+   * is not authorized to perform this action.
+   *
+   * Expected status code: 403 Forbidden
+   */
+/*
+  @Test(dependsOnMethods = {"deleteIntake"})
+  public void deleteIntakeWithUnauthorizedUser() {
+    // Currently only a stub.
+  }
+*/
+
+  /**
+   * Tests deleting a non-existent Intake, whose
+   * resource identifier does not exist at the specified URL.
+   *
+   * Expected status code: 404 Not Found
+   */
+  @Test(dependsOnMethods = {"deleteIntake"})
+  public void deleteNonExistentIntake() {
+    verbose("Calling deleteIntake: " + NON_EXISTENT_ID);
+    ClientResponse<Response> res =
+      intakeClient.deleteIntake(NON_EXISTENT_ID);
+    verbose("deleteIntake: status = " + res.getStatus());
+    Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
+  }
+
+
+  // ---------------------------------------------------------------
+  // Utility methods used by tests above
+  // ---------------------------------------------------------------
+
+  private Intake createIntake(String identifier) {
+    Intake intake = createIntake("entryNumber-" + identifier,
+        "entryDate-" + identifier);
+
+    return intake;
+  }
+
+  private Intake createIntake(String entryNumber, String entryDate) {
+    Intake intake = new Intake();
+
+    intake.setEntryNumber(entryNumber);
+    intake.setEntryDate(entryDate);
+
+    return intake;
+  }
+
+  private String extractId(ClientResponse<Response> res) {
+    MultivaluedMap mvm = res.getMetadata();
+    String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
+    verbose("extractId:uri=" + uri);
+    String[] segments = uri.split("/");
+    String id = segments[segments.length - 1];
+    verbose("id=" + id);
+    return id;
+  }
+
+  private void verbose(String msg) {
+//    if(logger.isInfoEnabled()){
+//      logger.debug(msg);
+//    }
+    System.out.println(msg);
+  }
+
+  private void verbose(String msg, Object o, Class clazz) {
+    try{
+      verbose(msg);
+      JAXBContext jc = JAXBContext.newInstance(clazz);
+      Marshaller m = jc.createMarshaller();
+      m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
+          Boolean.TRUE);
+      m.marshal(o, System.out);
+    }catch(Exception e){
+      e.printStackTrace();
     }
+  }
 
-    private long createIdentifier() {
-        long identifier = System.currentTimeMillis();
-        return identifier;
+  private void verboseMap(MultivaluedMap map) {
+    for(Object entry : map.entrySet()){
+      MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
+      verbose("  name=" + mentry.getKey() + " value=" + mentry.getValue());
     }
+  }
+
+  private String createIdentifier() {
+    long identifier = System.currentTimeMillis();
+    return Long.toString(identifier);
+  }
+
+  private String createNonExistentIdentifier() {
+    return Long.toString(Long.MAX_VALUE);
+  }
+  
 }