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