1 package org.collectionspace.services.IntegrationTests.test;
4 import java.io.IOException;
6 import static org.testng.Assert.*;
8 import org.apache.commons.lang3.StringUtils;
9 import org.apache.http.HttpEntity;
10 import org.apache.http.HttpHost;
11 import org.apache.http.HttpResponse;
12 import org.apache.http.StatusLine;
13 import org.apache.http.client.ClientProtocolException;
14 import org.apache.http.client.HttpResponseException;
15 import org.apache.http.client.ResponseHandler;
16 import org.apache.http.client.fluent.Executor;
17 import org.apache.http.client.fluent.Request;
18 import org.apache.http.entity.ContentType;
19 import org.testng.annotations.Test;
21 import com.fasterxml.jackson.core.JsonFactory;
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.ObjectMapper;
26 * Tests for sending and receiving JSON-formatted payloads.
28 public class JsonIntegrationTest {
29 public static final String HOST = "localhost";
30 public static final int PORT = 8180;
31 public static final String CLIENT_ID = "cspace-ui";
32 public static final String CLIENT_SECRET = "";
33 public static final String USERNAME = "admin@core.collectionspace.org";
34 public static final String PASSWORD = "Administrator";
35 public static final String BASE_URL = "http://" + HOST + ":" + PORT + "/cspace-services/";
36 public static final String FILE_PATH = "test-data/json/";
38 private Executor restExecutor = Executor.newInstance()
39 .auth(new HttpHost(HOST, PORT), USERNAME, PASSWORD);
41 private Executor authExecutor = Executor.newInstance()
42 .auth(new HttpHost(HOST, PORT), CLIENT_ID, CLIENT_SECRET);
44 private ObjectMapper mapper = new ObjectMapper();
45 private JsonFactory jsonFactory = mapper.getFactory();
48 public void testRecord() throws ClientProtocolException, IOException {
51 String csid = postJson("collectionobjects", "collectionobject1");
53 jsonNode = getJson("collectionobjects/" + csid);
55 assertEquals(jsonNode.at("/document/ns2:collectionspace_core/createdBy").asText(), USERNAME);
56 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5");
57 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNameList/objectNameGroup/objectName").asText(), "Test Object");
58 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").asText(), "line 1\nline 2");
59 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").asText(), "åéîøü");
61 jsonNode = putJson("collectionobjects/" + csid, "collectionobject2");
63 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5-updated");
64 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment").asText(), "™£•");
65 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").isMissingNode());
66 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").isMissingNode());
68 jsonNode = getJson("collectionobjects/" + csid);
70 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5-updated");
71 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment").asText(), "™£•");
72 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").isMissingNode());
73 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").isMissingNode());
75 delete("collectionobjects/" + csid);
79 public void testAuth() throws ClientProtocolException, IOException {
82 jsonNode = postAuthForm("oauth/token", "grant_type=password&username=" + USERNAME + "&password=" + PASSWORD);
84 assertEquals(jsonNode.at("/token_type").asText(), "bearer");
85 assertTrue(StringUtils.isNotEmpty(jsonNode.at("/access_token").asText()));
88 private String postJson(String path, String filename) throws ClientProtocolException, IOException {
89 return restExecutor.execute(Request.Post(getUrl(path))
90 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
91 .addHeader("Content-type", ContentType.APPLICATION_JSON.getMimeType())
92 .bodyFile(getFile(filename), ContentType.APPLICATION_JSON))
93 .handleResponse(new CsidFromLocationResponseHandler());
96 private JsonNode getJson(String path) throws ClientProtocolException, IOException {
97 return restExecutor.execute(Request.Get(getUrl(path))
98 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType()))
99 .handleResponse(new JsonBodyResponseHandler());
102 private JsonNode putJson(String path, String filename) throws ClientProtocolException, IOException {
103 return restExecutor.execute(Request.Put(getUrl(path))
104 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
105 .addHeader("Content-type", ContentType.APPLICATION_JSON.getMimeType())
106 .bodyFile(getFile(filename), ContentType.APPLICATION_JSON))
107 .handleResponse(new JsonBodyResponseHandler());
110 private void delete(String path) throws ClientProtocolException, IOException {
111 restExecutor.execute(Request.Delete(getUrl(path)))
112 .handleResponse(new CheckStatusResponseHandler());
115 private JsonNode postAuthForm(String path, String values) throws ClientProtocolException, IOException {
116 return authExecutor.execute(Request.Post(getUrl(path))
117 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
118 .addHeader("Content-type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
119 .bodyString(values, ContentType.APPLICATION_FORM_URLENCODED))
120 .handleResponse(new JsonBodyResponseHandler());
123 public class CsidFromLocationResponseHandler implements ResponseHandler<String> {
126 public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
127 StatusLine status = response.getStatusLine();
128 int statusCode = status.getStatusCode();
130 if (statusCode< 200 || statusCode > 299) {
131 throw new HttpResponseException(statusCode, status.getReasonPhrase());
134 return csidFromLocation(response.getFirstHeader("Location").getValue());
138 public class JsonBodyResponseHandler implements ResponseHandler<JsonNode> {
141 public JsonNode handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
142 StatusLine status = response.getStatusLine();
143 int statusCode = status.getStatusCode();
145 if (statusCode< 200 || statusCode > 299) {
146 throw new HttpResponseException(statusCode, status.getReasonPhrase());
149 HttpEntity entity = response.getEntity();
151 if (entity == null) {
152 throw new ClientProtocolException("response contains no content");
155 ContentType contentType = ContentType.getOrDefault(entity);
156 String mimeType = contentType.getMimeType();
158 if (!mimeType.equals(ContentType.APPLICATION_JSON.getMimeType())) {
159 throw new ClientProtocolException("unexpected content type: " + contentType);
162 return jsonFactory.createParser(entity.getContent()).readValueAsTree();
166 public class CheckStatusResponseHandler implements ResponseHandler<Integer> {
169 public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
170 StatusLine status = response.getStatusLine();
171 int statusCode = status.getStatusCode();
173 if (statusCode< 200 || statusCode > 299) {
174 throw new HttpResponseException(statusCode, status.getReasonPhrase());
181 private String csidFromLocation(String location) {
182 int index = location.lastIndexOf("/");
184 return location.substring(index + 1);
187 private String getUrl(String path) {
188 return BASE_URL + path;
191 private File getFile(String fileName) {
192 ClassLoader classLoader = getClass().getClassLoader();
194 return new File(classLoader.getResource(FILE_PATH + fileName + ".json").getFile());