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