]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3cb68e35aae3b2bf0e2bdad4cdd13728ae050c90
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client.test;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import javax.ws.rs.core.MultivaluedMap;
6 import javax.ws.rs.core.Response;
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.Marshaller;
9 import org.jboss.resteasy.client.ClientResponse;
10 import org.testng.Assert;
11 import org.testng.annotations.Test;
12
13 import org.collectionspace.services.relation.Relation;
14 import org.collectionspace.services.relation.RelationList;
15 import org.collectionspace.services.relation.RelationshipType;
16
17 import org.collectionspace.services.client.RelationClient;
18 import org.collectionspace.services.common.relation.RelationJAXBSchema;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * A RelationNuxeoServiceTest.
25  * 
26  * @version $Revision:$
27  */
28 public class RelationServiceTest {
29
30     /** The relation client. */
31     private RelationClient relationClient = new RelationClient();
32     
33     /** The logger. */
34     final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class);
35
36     /**
37      * Creates the relation.
38      */
39     @Test
40     public void createRelation() {
41         long identifier = this.createIdentifier();
42         
43         Relation relation = new Relation();
44         fillRelation(relation, identifier);
45         ClientResponse<Response> res = relationClient.createRelation(relation);
46         verbose("createRelation: status = " + res.getStatus());
47         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
48     }    
49
50     @Test(dependsOnMethods = { "createRelation" })
51         public void getRelation() {
52         //
53         //      First, create a relation object
54         //
55         Relation relation = new Relation();
56         fillRelation(relation, createIdentifier());
57         ClientResponse<Response> response = relationClient.createRelation(relation);
58         Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
59         String relationCsid = extractId(response);
60
61         ClientResponse<Relation> relationResponse = relationClient.getRelation(relationCsid);
62                 verbose("getRelation: status = " + response.getStatus());
63                 Assert.assertEquals(relationResponse.getStatus(), Response.Status.OK.getStatusCode());
64
65                 Relation returnedRelation = relationResponse.getEntity();
66                 Assert.assertEquals(returnedRelation.getCsid(), relationCsid);
67         }
68     
69     /**
70      * Update relation.
71      */
72     @Test(dependsOnMethods = {"createRelation"})
73     public void updateRelation() {
74         
75         String relationID = this.createRelationEntity(1);
76         Assert.assertNotNull(relationID, "Could not create a new object to update.");
77         
78         ClientResponse<Relation> res = relationClient.getRelation(relationID);
79         Relation theRelation = res.getEntity();
80         verbose("Got Relation to update with ID: " + relationID,
81                         theRelation, Relation.class);
82
83         //relation.setCsid("updated-" + updateId);
84         theRelation.setDocumentId1("updated-" + theRelation.getDocumentId1());
85         theRelation.setDocumentType1("updated-" + theRelation.getDocumentType1());
86         theRelation.setDocumentId2("updated-" + theRelation.getDocumentId2());
87         theRelation.setDocumentType2("updated-" + theRelation.getDocumentType2());
88
89         // make call to update service
90         res = relationClient.updateRelation(relationID, theRelation);
91
92         // check the response
93         Relation updatedRelation = res.getEntity();
94         Assert.assertEquals(updatedRelation.getDocumentId1(), theRelation.getDocumentId1());
95         Assert.assertEquals(updatedRelation.getDocumentType1(), theRelation.getDocumentType1());
96         Assert.assertEquals(updatedRelation.getDocumentId2(), theRelation.getDocumentId2());
97         Assert.assertEquals(updatedRelation.getDocumentType2(), theRelation.getDocumentType2());
98         
99         verbose("updateRelation: ", updatedRelation, Relation.class);
100
101         return;
102     }
103
104     /**
105      * Creates a set of three relation objects.
106      */
107     @Test(dependsOnMethods = {"createRelation"})
108     public void createRelationList() {
109         for(int i = 0; i < 3; i++){
110             this.createRelation();
111         }
112     }
113
114     /**
115      * Gets the relation list.
116      * 
117      * @return the relation list
118      */
119     @Test(dependsOnMethods = {"createRelationList"})
120     public void getRelationList() {
121         //the resource method is expected to return at least an empty list
122         RelationList coList = relationClient.getRelationList().getEntity();
123         List<RelationList.RelationListItem> coItemList = coList.getRelationListItem();
124         int i = 0;
125         for(RelationList.RelationListItem pli : coItemList){
126             verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid());
127             verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri());
128             i++;
129             System.out.println();
130         }
131     }
132
133     /**
134      * Delete relation.
135      */
136     @Test(dependsOnMethods = {"createRelation", "updateRelation"})
137     public void deleteRelation() {
138         
139         String relationID = this.createRelationEntity(0);
140         Assert.assertNotNull(relationID, "Could not create a new object to delete.");
141         
142         verbose("Calling deleteRelation:" + relationID);
143         ClientResponse<Response> res = relationClient.deleteRelation(relationID);
144         
145         verbose("deleteRelation: csid=" + relationID);
146         verbose("deleteRelation: status = " + res.getStatus());
147         Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
148     }
149     
150     @Test(dependsOnMethods = {"createRelation"})
151     public void relateObjects() {
152     }
153
154     /*
155      * Private Methods
156      */
157     
158     private String createRelationEntity(long identifier) {
159
160         String result = null;
161         
162         Relation relation = new Relation();
163         fillRelation(relation, identifier);
164         ClientResponse<Response> res = relationClient.createRelation(relation);
165         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
166         
167         result = extractId(res);
168         String responseString = res.toString();
169         System.out.println(responseString);
170         
171         return result;
172     }
173     
174     /**
175      * Fills the relation.
176      * 
177      * @param identifier the identifier
178      * 
179      * @return the relation
180      */
181     private void fillRelation(Relation relation, long identifier) {
182         fillRelation(relation, "Subject-" + identifier,
183                 "SubjectType-" + identifier + "-type",
184                 "Object-" + identifier,
185                 "ObjectType-" + identifier + "-type",
186                 RelationshipType.COLLECTIONOBJECT_INTAKE);
187     }
188
189     /**
190      * Creates the relation.
191      * 
192      * @param documentId1 the document id1
193      * @param documentType1 the document type1
194      * @param documentId2 the document id2
195      * @param documentType2 the document type2
196      * @param rt the rt
197      * 
198      * @return the relation
199      */
200     private void fillRelation(Relation relation, String documentId1, String documentType1,
201                 String documentId2, String documentType2, RelationshipType rt)
202     {
203         relation.setDocumentId1(documentId1);
204         relation.setDocumentType1(documentType1);
205         relation.setDocumentId2(documentId2);
206         relation.setDocumentType2(documentType2);
207         
208         relation.setRelationshipType(rt);
209     }
210
211     /**
212      * Extract id.
213      * 
214      * @param res the res
215      * 
216      * @return the string
217      */
218     private String extractId(ClientResponse<Response> res) {
219         MultivaluedMap mvm = res.getMetadata();
220         String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
221         String[] segments = uri.split("/");
222         String id = segments[segments.length - 1];
223         verbose("id=" + id);
224         return id;
225     }
226
227     /**
228      * Verbose.
229      * 
230      * @param msg the msg
231      */
232     private void verbose(String msg) {
233         System.out.println(msg);
234     }
235
236     /**
237      * Verbose.
238      * 
239      * @param msg the msg
240      * @param o the o
241      * @param clazz the clazz
242      */
243     private void verbose(String msg, Object o, Class theClass) {
244         try{
245             verbose(msg);
246             JAXBContext jc = JAXBContext.newInstance(theClass);
247             Marshaller m = jc.createMarshaller();
248             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
249                     Boolean.TRUE);
250             m.marshal(o, System.out);
251         }catch(Exception e){
252             e.printStackTrace();
253         }
254     }
255
256     /**
257      * Verbose map.
258      * 
259      * @param map the map
260      */
261     private void verboseMap(MultivaluedMap map) {
262         for(Object entry : map.entrySet()){
263             MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
264             verbose("    name=" + mentry.getKey() + " value=" + mentry.getValue());
265         }
266     }
267
268     /**
269      * Creates the identifier.
270      * 
271      * @return the long
272      */
273     private long createIdentifier() {
274         long identifier = System.currentTimeMillis();
275         return identifier;
276     }
277 }