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.collectionobject.CollectionObject;
19 import org.collectionspace.services.common.relation.RelationJAXBSchema;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * A RelationNuxeoServiceTest.
27 * @version $Revision:$
29 public class RelationServiceTest {
31 /** The relation client. */
32 private RelationClient relationClient = new RelationClient();
35 final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class);
38 * Creates the relation.
41 public void createRelation() {
42 long identifier = this.createIdentifier();
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());
51 @Test(dependsOnMethods = { "createRelation" })
52 public void getRelation() {
54 // First, create a relation object
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);
62 ClientResponse<Relation> relationResponse = relationClient.getRelation(relationCsid);
63 verbose("getRelation: status = " + response.getStatus());
64 Assert.assertEquals(relationResponse.getStatus(), Response.Status.OK.getStatusCode());
66 Relation returnedRelation = relationResponse.getEntity();
67 Assert.assertEquals(returnedRelation.getCsid(), relationCsid);
73 @Test(dependsOnMethods = {"createRelation"})
74 public void updateRelation() {
76 String relationID = this.createRelationEntity(1);
77 Assert.assertNotNull(relationID, "Could not create a new object to update.");
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);
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());
90 // make call to update service
91 res = relationClient.updateRelation(relationID, theRelation);
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());
100 verbose("updateRelation: ", updatedRelation, Relation.class);
106 * Creates a set of three relation objects.
108 @Test(dependsOnMethods = {"createRelation"})
109 public void createRelationList() {
110 for(int i = 0; i < 3; i++){
111 this.createRelation();
116 * Gets the relation list.
118 * @return the relation list
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();
126 for(RelationList.RelationListItem pli : coItemList){
127 verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid());
128 verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri());
130 System.out.println();
137 @Test(dependsOnMethods = {"createRelation", "updateRelation"})
138 public void deleteRelation() {
140 String relationID = this.createRelationEntity(0);
141 Assert.assertNotNull(relationID, "Could not create a new object to delete.");
143 verbose("Calling deleteRelation:" + relationID);
144 ClientResponse<Response> res = relationClient.deleteRelation(relationID);
146 verbose("deleteRelation: csid=" + relationID);
147 verbose("deleteRelation: status = " + res.getStatus());
148 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
151 @Test(dependsOnMethods = {"createRelation"})
152 public void relateObjects() {
159 private String createRelationEntity(long identifier) {
161 String result = null;
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());
168 result = extractId(res);
169 String responseString = res.toString();
170 System.out.println(responseString);
176 * Fills the relation.
178 * @param identifier the identifier
180 * @return the relation
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);
191 * Creates the relation.
193 * @param documentId1 the document id1
194 * @param documentType1 the document type1
195 * @param documentId2 the document id2
196 * @param documentType2 the document type2
199 * @return the relation
201 private void fillRelation(Relation relation, String documentId1, String documentType1,
202 String documentId2, String documentType2, RelationshipType rt)
204 relation.setDocumentId1(documentId1);
205 relation.setDocumentType1(documentType1);
206 relation.setDocumentId2(documentId2);
207 relation.setDocumentType2(documentType2);
209 relation.setRelationshipType(rt);
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];
233 private void verbose(String msg) {
234 System.out.println(msg);
242 * @param clazz the clazz
244 private void verbose(String msg, Object o, Class theClass) {
247 JAXBContext jc = JAXBContext.newInstance(theClass);
248 Marshaller m = jc.createMarshaller();
249 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
251 m.marshal(o, System.out);
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());
270 * Creates the identifier.
274 private long createIdentifier() {
275 long identifier = System.currentTimeMillis();