]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ea0623939ac176ce89723b151e0934cc11559ece
[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 only once, and re-used when submitting every
46  *   new 'create' request.
47  * - The client's HTTP connection is formally closed, and its resources
48  *   released, after each 'create' request.
49  * 
50  * @version $Revision:$
51  */
52 public class I1591OneInst extends CollectionSpacePerformanceTest {
53
54     final Logger logger = LoggerFactory.getLogger(I1591OneInst.class);
55     private final String COLLECTION_OBJECT_COMMON_PART_NAME =
56         getCollectionObjectCommonPartName();
57     private static int MAX_RECORDS = 500;
58     String[] coList = new String[MAX_RECORDS];
59
60     private String getCollectionObjectCommonPartName() {
61         return new CollectionObjectClient().getCommonPartName();
62     }
63
64     @Test
65     public void testCreateWithSingleClientInstantiation() {
66         createCollectionObjects(MAX_RECORDS);
67     }
68
69     /**
70      * Creates multiple CollectionObject resources.
71      *
72      * @param numberOfObjects The number of CollectionObject resources to create.
73      */
74     public void createCollectionObjects(int numberOfObjects) {
75
76         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
77
78         long identifier = 0;
79
80         for (int i = 0; i <= numberOfObjects; i++) {
81
82             // Create a CollectionObject instance.
83             CollectionobjectsCommon co = new CollectionobjectsCommon();
84             identifier = System.currentTimeMillis();
85             fillCollectionObject(co, Long.toString(identifier));
86
87             // Assign it to the Common part of a multipart payload.
88             MultipartOutput multipart = new MultipartOutput();
89             OutputPart commonPart = multipart.addPart(co, MediaType.APPLICATION_XML_TYPE);
90             commonPart.getHeaders().add("label", COLLECTION_OBJECT_COMMON_PART_NAME);
91
92             // Make a create call with that payload and check the response.
93             ClientResponse<Response> response = collectionObjectClient.create(multipart);
94             try {
95                 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
96                 coList[i] = extractId(response);
97             } catch (AssertionError e) {
98                 if (logger.isDebugEnabled() == true) {
99                     logger.debug("FAILURE: Created " + i +
100                         " of " + numberOfObjects +
101                         " before failing.");
102                 }
103                 Assert.assertTrue(false);
104             // Since failed Asserts can throw an Exception, ensure
105             // that the underlying HTTP connection is explicitly closed
106             // under all circumstances.
107             } finally {
108                 response.releaseConnection();
109             }
110
111             if (logger.isDebugEnabled() == true) {
112                 logger.debug("Created CollectionObject #: " + i);
113             }
114         }
115     }
116
117     @AfterClass(alwaysRun=true)
118     public void cleanUp() {
119
120         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
121         String resourceId = "";
122
123         if (logger.isDebugEnabled() == true) {
124             logger.debug("Cleaing up CollectionObject records created during testing ...");
125         }
126
127         for (int i = 0; i < coList.length; i++) {
128             resourceId = coList[i];
129             ClientResponse<Response> res = collectionObjectClient.delete(resourceId);
130             if (logger.isDebugEnabled() == true) {
131                 logger.debug("Deleted CollectionObject #: " + i);
132             }
133             res.releaseConnection();
134         }
135
136         if (logger.isDebugEnabled()) {
137             logger.debug("Deleted " + coList.length + " CollectionObjects.");
138         }
139     }
140
141 }