]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
53c1bb06439cb072f806e30c8b94b4ed2a0c8c0e
[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.collectionobject.CollectionobjectsCommon;
41
42 /**
43  * A test related to Issue CSPACE-1591, which creates a large number of
44  * records in a variation where:
45  * - A new client object is instantiated for every 'create' request submitted.
46  * - The client's HTTP connection is formally closed, and its resources
47  *   released, after each 'create' request.
48  * 
49  * @version $Revision:$
50  */
51 public class I1591Multiple extends CollectionSpacePerformanceTest {
52
53     final Logger logger = LoggerFactory.getLogger(I1591Multiple.class);
54     private final String COLLECTION_OBJECT_COMMON_PART_NAME =
55         getCollectionObjectCommonPartName();
56     private static int MAX_RECORDS = 500;
57     String[] coList = new String[MAX_RECORDS];
58
59     private String getCollectionObjectCommonPartName() {
60         return new CollectionObjectClient().getCommonPartName();
61     }
62
63     @Test
64     public void testCreateWithMultipleClientInstantiations() {
65         coList = this.createCollectionObjects(MAX_RECORDS);
66     }
67
68     /**
69      * Creates a single collection object, instantiating a new instance
70      * of the CollectionObjectClient (a RESTEasy client proxy)
71      * each time this method is called.
72      *
73      * @param identifier A arbitrary identifier to use when filling
74      *                   the CollectionObject's fields with values.
75      * @return A resource ID for the newly-created object.
76      */
77     private String createCollectionObject(long identifier) throws AssertionError {
78
79         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
80         String resourceId = null;
81
82         // Create a CollectionObject instance.
83         CollectionobjectsCommon co = new CollectionobjectsCommon();
84         fillCollectionObject(co, Long.toString(identifier));
85
86         // Assign it to the Common part of a multipart payload.
87         MultipartOutput multipart = new MultipartOutput();
88         OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
89         commonPart.getHeaders().add("label", COLLECTION_OBJECT_COMMON_PART_NAME);
90
91         // Make a create call with that payload and check the response.
92         ClientResponse<Response> response = collectionObjectClient.create(multipart);
93         try {
94             Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
95             resourceId = extractId(response);
96         // Since failed Asserts can throw an Exception, ensure
97         // that the underlying HTTP connection is explicitly closed
98         // under all circumstances.
99         } finally {
100             response.releaseConnection();
101         }
102
103         // Return the ID of the newly-created CollectionObject resource.
104         return resourceId;
105     }
106
107     /**
108      * Creates multiple CollectionObject resources.
109      *
110      * @param numberOfObjects The number of CollectionObject resources to create.
111      * @return A list of the resource IDs of the newly-created object resources.
112      */
113     public String[] createCollectionObjects(int numberOfObjects) {
114
115             long identifier = 0;
116             int i = 0;
117
118             try {
119                 for (i = 0; i <= numberOfObjects; i++) {
120                     identifier = System.currentTimeMillis();
121                     coList[i] = createCollectionObject(identifier);
122                     if (logger.isDebugEnabled() == true) {
123                         logger.debug("Created CollectionObject #: " + i);
124                     }
125                 }
126             } catch (AssertionError e) {
127                 if (logger.isDebugEnabled() == true) {
128                     logger.debug("FAILURE: Created " + i +
129                         " of " + numberOfObjects +
130                         " before failing.");
131                 }
132                 Assert.assertTrue(false);
133             }
134
135             return coList;
136     }
137
138     @AfterClass(alwaysRun=true)
139     public void cleanUp() {
140
141         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
142         String resourceId = "";
143
144         if (logger.isDebugEnabled() == true) {
145             logger.debug("Cleaing up CollectionObject records created during testing ...");
146         }
147
148         for (int i = 0; i < coList.length; i++) {
149             resourceId = coList[i];
150             ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
151             if (logger.isDebugEnabled() == true) {
152                 logger.debug("Deleted CollectionObject #: " + i);
153             }
154             res.releaseConnection();
155         }
156         
157         if (logger.isDebugEnabled()) {
158             logger.debug("Deleted " + coList.length + " CollectionObjects.");
159         }
160     }
161
162 }