]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d77d03b3d376a0b7bcf69f70e258e8c9ea4ec92d
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 package org.collectionspace.services.PerformanceTests.test;
24
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27
28 import org.testng.Assert;
29 import org.testng.annotations.AfterClass;
30 import org.testng.annotations.Test;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.PayloadOutputPart;
41 import org.collectionspace.services.client.PoxPayloadOut;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43
44 /**
45  * A test related to Issue CSPACE-1591, which creates a large number of
46  * records in a variation where:
47  * - A new client object is instantiated for every 'create' request submitted.
48  * - The client's HTTP connection is formally closed, and its resources
49  *   released, after each 'create' request.
50  * 
51  * @version $Revision:$
52  */
53 public class I1591Multiple extends CollectionSpacePerformanceTest {
54
55     final Logger logger = LoggerFactory.getLogger(I1591Multiple.class);
56     private final String COLLECTION_OBJECT_COMMON_PART_NAME =
57         getCollectionObjectCommonPartName();
58     private static int MAX_RECORDS = 500;
59     String[] coList = new String[MAX_RECORDS];
60
61     private String getCollectionObjectCommonPartName() {
62         return new CollectionObjectClient().getCommonPartName();
63     }
64
65     @Test
66     public void testCreateWithMultipleClientInstantiations() {
67         coList = this.createCollectionObjects(MAX_RECORDS);
68     }
69
70     /**
71      * Creates a single collection object, instantiating a new instance
72      * of the CollectionObjectClient (a RESTEasy client proxy)
73      * each time this method is called.
74      *
75      * @param identifier A arbitrary identifier to use when filling
76      *                   the CollectionObject's fields with values.
77      * @return A resource ID for the newly-created object.
78      */
79     private String createCollectionObject(long identifier) throws AssertionError {
80
81         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
82         String resourceId = null;
83
84         // Create a CollectionObject instance.
85         CollectionobjectsCommon co = new CollectionobjectsCommon();
86         fillCollectionObject(co, Long.toString(identifier));
87
88         // Assign it to the Common part of a multipart payload.
89         PoxPayloadOut multipart = new PoxPayloadOut(CollectionObjectClient.SERVICE_PAYLOAD_NAME);
90         PayloadOutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
91         commonPart.setLabel(collectionObjectClient.getCommonPartName());
92
93         // Make a create call with that payload and check the response.
94         ClientResponse<Response> response = collectionObjectClient.create(multipart);
95         try {
96             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
97             resourceId = extractId(response);
98         // Since failed Asserts can throw an Exception, ensure
99         // that the underlying HTTP connection is explicitly closed
100         // under all circumstances.
101         } finally {
102             response.releaseConnection();
103         }
104
105         // Return the ID of the newly-created CollectionObject resource.
106         return resourceId;
107     }
108
109     /**
110      * Creates multiple CollectionObject resources.
111      *
112      * @param numberOfObjects The number of CollectionObject resources to create.
113      * @return A list of the resource IDs of the newly-created object resources.
114      */
115     public String[] createCollectionObjects(int numberOfObjects) {
116
117             long identifier = 0;
118             int i = 0;
119
120             try {
121                 for (i = 0; i <= numberOfObjects; i++) {
122                     identifier = System.currentTimeMillis();
123                     coList[i] = createCollectionObject(identifier);
124                     if (logger.isDebugEnabled() == true) {
125                         logger.debug("Created CollectionObject #: " + i);
126                     }
127                 }
128             } catch (AssertionError e) {
129                 if (logger.isDebugEnabled() == true) {
130                     logger.debug("FAILURE: Created " + i +
131                         " of " + numberOfObjects +
132                         " before failing.");
133                 }
134                 Assert.assertTrue(false);
135             }
136
137             return coList;
138     }
139
140     @AfterClass(alwaysRun=true)
141     public void cleanUp() {
142
143         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
144         String resourceId = "";
145
146         if (logger.isDebugEnabled() == true) {
147             logger.debug("Cleaing up CollectionObject records created during testing ...");
148         }
149
150         for (int i = 0; i < coList.length; i++) {
151             resourceId = coList[i];
152             ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
153             if (logger.isDebugEnabled() == true) {
154                 logger.debug("Deleted CollectionObject #: " + i);
155             }
156             res.releaseConnection();
157         }
158         
159         if (logger.isDebugEnabled()) {
160             logger.debug("Deleted " + coList.length + " CollectionObjects.");
161         }
162     }
163
164 }