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