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.common.relation.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 = new RelationClient();
34 final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class);
37 * Creates the relation.
40 public void createRelation() {
41 long identifier = this.createIdentifier();
43 Relation relation = new Relation();
44 fillRelation(relation, identifier);
45 ClientResponse<Response> res = relationClient.createRelation(relation);
46 verbose("createRelation: status = " + res.getStatus());
47 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
50 @Test(dependsOnMethods = { "createRelation" })
51 public void getRelation() {
53 // First, create a relation object
55 Relation relation = new Relation();
56 fillRelation(relation, createIdentifier());
57 ClientResponse<Response> response = relationClient.createRelation(relation);
58 Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
59 String relationCsid = extractId(response);
61 ClientResponse<Relation> relationResponse = relationClient.getRelation(relationCsid);
62 verbose("getRelation: status = " + response.getStatus());
63 Assert.assertEquals(relationResponse.getStatus(), Response.Status.OK.getStatusCode());
65 Relation returnedRelation = relationResponse.getEntity();
66 Assert.assertEquals(returnedRelation.getCsid(), relationCsid);
72 @Test(dependsOnMethods = {"createRelation"})
73 public void updateRelation() {
75 String relationID = this.createRelationEntity(1);
76 Assert.assertNotNull(relationID, "Could not create a new object to update.");
78 ClientResponse<Relation> res = relationClient.getRelation(relationID);
79 Relation theRelation = res.getEntity();
80 verbose("Got Relation to update with ID: " + relationID,
81 theRelation, Relation.class);
83 //relation.setCsid("updated-" + updateId);
84 theRelation.setDocumentId1("updated-" + theRelation.getDocumentId1());
85 theRelation.setDocumentType1("updated-" + theRelation.getDocumentType1());
86 theRelation.setDocumentId2("updated-" + theRelation.getDocumentId2());
87 theRelation.setDocumentType2("updated-" + theRelation.getDocumentType2());
89 // make call to update service
90 res = relationClient.updateRelation(relationID, theRelation);
93 Relation updatedRelation = res.getEntity();
94 Assert.assertEquals(updatedRelation.getDocumentId1(), theRelation.getDocumentId1());
95 Assert.assertEquals(updatedRelation.getDocumentType1(), theRelation.getDocumentType1());
96 Assert.assertEquals(updatedRelation.getDocumentId2(), theRelation.getDocumentId2());
97 Assert.assertEquals(updatedRelation.getDocumentType2(), theRelation.getDocumentType2());
99 verbose("updateRelation: ", updatedRelation, Relation.class);
105 * Creates a set of three relation objects.
107 @Test(dependsOnMethods = {"createRelation"})
108 public void createRelationList() {
109 for(int i = 0; i < 3; i++){
110 this.createRelation();
115 * Gets the relation list.
117 * @return the relation list
119 @Test(dependsOnMethods = {"createRelationList"})
120 public void getRelationList() {
121 //the resource method is expected to return at least an empty list
122 RelationList coList = relationClient.getRelationList().getEntity();
123 List<RelationList.RelationListItem> coItemList = coList.getRelationListItem();
125 for(RelationList.RelationListItem pli : coItemList){
126 verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid());
127 verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri());
129 System.out.println();
136 @Test(dependsOnMethods = {"createRelation", "updateRelation"})
137 public void deleteRelation() {
139 String relationID = this.createRelationEntity(0);
140 Assert.assertNotNull(relationID, "Could not create a new object to delete.");
142 verbose("Calling deleteRelation:" + relationID);
143 ClientResponse<Response> res = relationClient.deleteRelation(relationID);
145 verbose("deleteRelation: csid=" + relationID);
146 verbose("deleteRelation: status = " + res.getStatus());
147 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode());
150 @Test(dependsOnMethods = {"createRelation"})
151 public void relateObjects() {
158 private String createRelationEntity(long identifier) {
160 String result = null;
162 Relation relation = new Relation();
163 fillRelation(relation, identifier);
164 ClientResponse<Response> res = relationClient.createRelation(relation);
165 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode());
167 result = extractId(res);
168 String responseString = res.toString();
169 System.out.println(responseString);
175 * Fills the relation.
177 * @param identifier the identifier
179 * @return the relation
181 private void fillRelation(Relation relation, long identifier) {
182 fillRelation(relation, "Subject-" + identifier,
183 "SubjectType-" + identifier + "-type",
184 "Object-" + identifier,
185 "ObjectType-" + identifier + "-type",
186 RelationshipType.COLLECTIONOBJECT_INTAKE);
190 * Creates the relation.
192 * @param documentId1 the document id1
193 * @param documentType1 the document type1
194 * @param documentId2 the document id2
195 * @param documentType2 the document type2
198 * @return the relation
200 private void fillRelation(Relation relation, String documentId1, String documentType1,
201 String documentId2, String documentType2, RelationshipType rt)
203 relation.setDocumentId1(documentId1);
204 relation.setDocumentType1(documentType1);
205 relation.setDocumentId2(documentId2);
206 relation.setDocumentType2(documentType2);
208 relation.setRelationshipType(rt);
218 private String extractId(ClientResponse<Response> res) {
219 MultivaluedMap mvm = res.getMetadata();
220 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
221 String[] segments = uri.split("/");
222 String id = segments[segments.length - 1];
232 private void verbose(String msg) {
233 System.out.println(msg);
241 * @param clazz the clazz
243 private void verbose(String msg, Object o, Class theClass) {
246 JAXBContext jc = JAXBContext.newInstance(theClass);
247 Marshaller m = jc.createMarshaller();
248 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
250 m.marshal(o, System.out);
261 private void verboseMap(MultivaluedMap map) {
262 for(Object entry : map.entrySet()){
263 MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry;
264 verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue());
269 * Creates the identifier.
273 private long createIdentifier() {
274 long identifier = System.currentTimeMillis();