1 package org.collectionspace.services.client.test;
3 import java.util.ArrayList;
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;
13 import org.collectionspace.services.relation.Relation;
14 import org.collectionspace.services.relation.RelationList;
15 import org.collectionspace.services.relation.RelationshipType;
17 import org.collectionspace.services.client.RelationClient;
18 import org.collectionspace.services.RelationJAXBSchema;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * A RelationNuxeoServiceTest.
26 * @version $Revision:$
28 public class RelationServiceTest {
30 /** The relation client. */
31 private RelationClient relationClient = RelationClient.getInstance();
34 private String updateId = null;
37 private String deleteId = null;
40 final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class);
43 * Creates the relation.
46 public void createRelation() {
47 long identifier = this.createIdentifier();
49 Relation relation = createRelation(identifier);
50 ClientResponse<Response> res = relationClient.createRelation(relation);
51 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
53 String responseString = res.toString();
54 System.out.println(responseString);
56 //store updateId locally for "update" test
58 updateId = extractId(res);
60 deleteId = extractId(res);
61 verbose("Set deleteId: " + deleteId);
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);
75 //relation.setCsid("updated-" + updateId);
76 relation.setDocumentId1("updated-" + relation.getDocumentId1());
77 relation.setDocumentType1("updated-" + relation.getDocumentType1());
79 // make call to update service
80 res = relationClient.updateRelation(updateId, relation);
83 Relation updatedRelation = res.getEntity();
84 Assert.assertEquals(updatedRelation.getDocumentId1(), relation.getDocumentId1());
85 verbose("updateRelation: ", updatedRelation, Relation.class);
91 * Creates the collection.
93 @Test(dependsOnMethods = {"createRelation"})
94 public void createCollection() {
95 for(int i = 0; i < 3; i++){
96 this.createRelation();
101 * Gets the relation list.
103 * @return the relation list
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();
111 for(RelationList.RelationListItem pli : coItemList){
112 verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid());
113 verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri());
115 System.out.println();
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());
132 * Creates the relation.
134 * @param identifier the identifier
136 * @return the relation
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));
150 * Creates the relation.
152 * @param documentId1 the document id1
153 * @param documentType1 the document type1
154 * @param documentId2 the document id2
155 * @param documentType2 the document type2
158 * @return the relation
160 private Relation createRelation(String documentId1, String documentType1,
161 String documentId2, String documentType2, RelationshipType rt) {
162 Relation relation = new Relation();
164 relation.setDocumentId1(documentId1);
165 relation.setDocumentType1(documentType1);
166 relation.setDocumentId2(documentId2);
167 relation.setDocumentType2(documentType2);
169 relation.setRelationshipType(rt);
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];
195 private void verbose(String msg) {
196 // if(logger.isInfoEnabled()){
197 // logger.debug(msg);
199 System.out.println(msg);
207 * @param clazz the clazz
209 private void verbose(String msg, Object o, Class clazz) {
212 JAXBContext jc = JAXBContext.newInstance(clazz);
213 Marshaller m = jc.createMarshaller();
214 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
216 m.marshal(o, System.out);
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());
235 * Creates the identifier.
239 private long createIdentifier() {
240 long identifier = System.currentTimeMillis();