1 package org.collectionspace.services.IntegrationTests.test;
4 import java.io.IOException;
5 import java.nio.charset.StandardCharsets;
7 import javax.xml.bind.DatatypeConverter;
9 import static org.testng.Assert.*;
11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.http.HttpEntity;
13 import org.apache.http.HttpHost;
14 import org.apache.http.HttpResponse;
15 import org.apache.http.StatusLine;
16 import org.apache.http.client.ClientProtocolException;
17 import org.apache.http.client.HttpResponseException;
18 import org.apache.http.client.ResponseHandler;
19 import org.apache.http.client.fluent.Executor;
20 import org.apache.http.client.fluent.Request;
21 import org.apache.http.entity.ContentType;
22 import org.testng.annotations.Test;
24 import com.fasterxml.jackson.core.JsonFactory;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
29 * Tests for sending and receiving JSON-formatted payloads.
31 public class JsonIntegrationTest {
32 public static final String HOST = "localhost";
33 public static final int PORT = 8180;
34 public static final String CLIENT_ID = "cspace-ui";
35 public static final String CLIENT_SECRET = "";
36 public static final String USERNAME = "admin@core.collectionspace.org";
37 public static final String PASSWORD = "Administrator";
38 public static final String BASE_URL = "http://" + HOST + ":" + PORT + "/cspace-services/";
39 public static final String FILE_PATH = "test-data/json/";
41 private Executor restExecutor = Executor.newInstance()
42 .auth(new HttpHost(HOST, PORT), USERNAME, PASSWORD);
44 private Executor authExecutor = Executor.newInstance()
45 .auth(new HttpHost(HOST, PORT), CLIENT_ID, CLIENT_SECRET);
47 private ObjectMapper mapper = new ObjectMapper();
48 private JsonFactory jsonFactory = mapper.getFactory();
51 public void testRecord() throws ClientProtocolException, IOException {
54 String csid = postJson("collectionobjects", "collectionobject1");
56 jsonNode = getJson("collectionobjects/" + csid);
58 assertEquals(jsonNode.at("/document/ns2:collectionspace_core/createdBy").asText(), USERNAME);
59 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5");
60 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNameList/objectNameGroup/objectName").asText(), "Test Object");
61 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").asText(), "line 1\nline 2");
62 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").asText(), "åéîøü");
64 jsonNode = putJson("collectionobjects/" + csid, "collectionobject2");
66 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5-updated");
67 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment").asText(), "™£•");
68 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").isMissingNode());
69 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").isMissingNode());
71 jsonNode = getJson("collectionobjects/" + csid);
73 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/objectNumber").asText(), "TEST2000.4.5-updated");
74 assertEquals(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment").asText(), "™£•");
75 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/0").isMissingNode());
76 assertTrue(jsonNode.at("/document/ns2:collectionobjects_common/comments/comment/1").isMissingNode());
78 delete("collectionobjects/" + csid);
82 public void testAuth() throws ClientProtocolException, IOException {
83 String base64EncodedPassword = DatatypeConverter.printBase64Binary(PASSWORD.getBytes(StandardCharsets.UTF_8));
87 jsonNode = postAuthForm("oauth/token", "grant_type=password&username=" + USERNAME + "&password=" + base64EncodedPassword);
89 assertEquals(jsonNode.at("/token_type").asText(), "bearer");
90 assertTrue(StringUtils.isNotEmpty(jsonNode.at("/access_token").asText()));
93 private String postJson(String path, String filename) throws ClientProtocolException, IOException {
94 return restExecutor.execute(Request.Post(getUrl(path))
95 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
96 .addHeader("Content-type", ContentType.APPLICATION_JSON.getMimeType())
97 .bodyFile(getFile(filename), ContentType.APPLICATION_JSON))
98 .handleResponse(new CsidFromLocationResponseHandler());
101 private JsonNode getJson(String path) throws ClientProtocolException, IOException {
102 return restExecutor.execute(Request.Get(getUrl(path))
103 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType()))
104 .handleResponse(new JsonBodyResponseHandler());
107 private JsonNode putJson(String path, String filename) throws ClientProtocolException, IOException {
108 return restExecutor.execute(Request.Put(getUrl(path))
109 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
110 .addHeader("Content-type", ContentType.APPLICATION_JSON.getMimeType())
111 .bodyFile(getFile(filename), ContentType.APPLICATION_JSON))
112 .handleResponse(new JsonBodyResponseHandler());
115 private void delete(String path) throws ClientProtocolException, IOException {
116 restExecutor.execute(Request.Delete(getUrl(path)))
117 .handleResponse(new CheckStatusResponseHandler());
120 private JsonNode postAuthForm(String path, String values) throws ClientProtocolException, IOException {
121 return authExecutor.execute(Request.Post(getUrl(path))
122 .addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
123 .addHeader("Content-type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
124 .bodyString(values, ContentType.APPLICATION_FORM_URLENCODED))
125 .handleResponse(new JsonBodyResponseHandler());
128 public class CsidFromLocationResponseHandler implements ResponseHandler<String> {
131 public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
132 StatusLine status = response.getStatusLine();
133 int statusCode = status.getStatusCode();
135 if (statusCode< 200 || statusCode > 299) {
136 throw new HttpResponseException(statusCode, status.getReasonPhrase());
139 return csidFromLocation(response.getFirstHeader("Location").getValue());
143 public class JsonBodyResponseHandler implements ResponseHandler<JsonNode> {
146 public JsonNode handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
147 StatusLine status = response.getStatusLine();
148 int statusCode = status.getStatusCode();
150 if (statusCode< 200 || statusCode > 299) {
151 throw new HttpResponseException(statusCode, status.getReasonPhrase());
154 HttpEntity entity = response.getEntity();
156 if (entity == null) {
157 throw new ClientProtocolException("response contains no content");
160 ContentType contentType = ContentType.getOrDefault(entity);
161 String mimeType = contentType.getMimeType();
163 if (!mimeType.equals(ContentType.APPLICATION_JSON.getMimeType())) {
164 throw new ClientProtocolException("unexpected content type: " + contentType);
167 return jsonFactory.createParser(entity.getContent()).readValueAsTree();
171 public class CheckStatusResponseHandler implements ResponseHandler<Integer> {
174 public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
175 StatusLine status = response.getStatusLine();
176 int statusCode = status.getStatusCode();
178 if (statusCode< 200 || statusCode > 299) {
179 throw new HttpResponseException(statusCode, status.getReasonPhrase());
186 private String csidFromLocation(String location) {
187 int index = location.lastIndexOf("/");
189 return location.substring(index + 1);
192 private String getUrl(String path) {
193 return BASE_URL + path;
196 private File getFile(String fileName) {
197 ClassLoader classLoader = getClass().getClassLoader();
199 return new File(classLoader.getResource(FILE_PATH + fileName + ".json").getFile());