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