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 = RelationClient.getInstance();
34 final Logger logger = LoggerFactory.getLogger(RelationServiceTest.class);
37 * Creates the relation.
40 public void createRelation() {
41 long identifier = this.createIdentifier();
42 this.createRelationEntity(identifier);
48 @Test(dependsOnMethods = {"createRelation"})
49 public void updateRelation() {
51 String relationID = this.createRelationEntity(1);
52 Assert.assertNotNull(relationID, "Could not create a new object to update.");
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);
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());
65 // make call to update service
66 res = relationClient.updateRelation(relationID, theRelation);
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());
75 verbose("updateRelation: ", updatedRelation, Relation.class);
81 * Creates a set of three relation objects.
83 @Test(dependsOnMethods = {"createRelation"})
84 public void createRelationList() {
85 for(int i = 0; i < 3; i++){
86 this.createRelation();
91 * Gets the relation list.
93 * @return the relation list
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();
101 for(RelationList.RelationListItem pli : coItemList){
102 verbose("getRelationList: list-item[" + i + "] csid=" + pli.getCsid());
103 verbose("getRelationList: list-item[" + i + "] URI=" + pli.getUri());
105 System.out.println();
112 @Test(dependsOnMethods = {"createRelation", "updateRelation"})
113 public void deleteRelation() {
115 String relationID = this.createRelationEntity(0);
116 Assert.assertNotNull(relationID, "Could not create a new object to delete.");
118 verbose("Calling deleteRelation:" + relationID);
119 ClientResponse<Response> res = relationClient.deleteRelation(relationID);
121 verbose("deleteRelation: csid=" + relationID);
122 verbose("deleteRelation: status = " + res.getStatus());
123 Assert.assertEquals(res.getStatus(), Response.Status.NO_CONTENT.getStatusCode());
126 @Test(dependsOnMethods = {"createRelation"})
127 public void relateObjects() {
134 private String createRelationEntity(long identifier) {
136 String result = null;
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());
143 result = extractId(res);
144 String responseString = res.toString();
145 System.out.println(responseString);
151 * Fills the relation.
153 * @param identifier the identifier
155 * @return the relation
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));
167 * Creates the relation.
169 * @param documentId1 the document id1
170 * @param documentType1 the document type1
171 * @param documentId2 the document id2
172 * @param documentType2 the document type2
175 * @return the relation
177 private void fillRelation(Relation relation, String documentId1, String documentType1,
178 String documentId2, String documentType2, RelationshipType rt)
180 relation.setDocumentId1(documentId1);
181 relation.setDocumentType1(documentType1);
182 relation.setDocumentId2(documentId2);
183 relation.setDocumentType2(documentType2);
185 relation.setRelationshipType(rt);
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];
209 private void verbose(String msg) {
210 System.out.println(msg);
218 * @param clazz the clazz
220 private void verbose(String msg, Object o, Class theClass) {
223 JAXBContext jc = JAXBContext.newInstance(theClass);
224 Marshaller m = jc.createMarshaller();
225 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
227 m.marshal(o, System.out);
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());
246 * Creates the identifier.
250 private long createIdentifier() {
251 long identifier = System.currentTimeMillis();