]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fb14d63087d6a1e13d29dc8378c73a35be8435f6
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.IntegrationTests.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.charset.StandardCharsets;
6
7 import javax.xml.bind.DatatypeConverter;
8
9 import static org.testng.Assert.*;
10
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;
23
24 import com.fasterxml.jackson.core.JsonFactory;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 /**
29  * Tests for sending and receiving JSON-formatted payloads.
30  */
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/";
40     
41     private Executor restExecutor = Executor.newInstance()
42             .auth(new HttpHost(HOST, PORT), USERNAME, PASSWORD);
43
44     private Executor authExecutor = Executor.newInstance()
45             .auth(new HttpHost(HOST, PORT), CLIENT_ID, CLIENT_SECRET);
46     
47     private ObjectMapper mapper = new ObjectMapper();
48     private JsonFactory jsonFactory = mapper.getFactory();
49
50     @Test
51     public void testRecord() throws ClientProtocolException, IOException {
52         JsonNode jsonNode;
53         
54         String csid = postJson("collectionobjects", "collectionobject1");
55         
56         jsonNode = getJson("collectionobjects/" + csid);
57         
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(), "åéîøü");
63
64         jsonNode = putJson("collectionobjects/" + csid, "collectionobject2");
65
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());
70
71         jsonNode = getJson("collectionobjects/" + csid);
72
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());
77
78         delete("collectionobjects/" + csid);
79     }
80     
81     @Test
82     public void testAuth() throws ClientProtocolException, IOException {
83         String base64EncodedPassword = DatatypeConverter.printBase64Binary(PASSWORD.getBytes(StandardCharsets.UTF_8));
84         
85         JsonNode jsonNode;
86         
87         jsonNode = postAuthForm("oauth/token", "grant_type=password&username=" + USERNAME + "&password=" + base64EncodedPassword);
88
89         assertEquals(jsonNode.at("/token_type").asText(), "bearer");
90         assertTrue(StringUtils.isNotEmpty(jsonNode.at("/access_token").asText()));
91     }
92     
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());
99     }
100     
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());
105     }
106     
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());
113     }
114     
115     private void delete(String path) throws ClientProtocolException, IOException {
116         restExecutor.execute(Request.Delete(getUrl(path)))
117             .handleResponse(new CheckStatusResponseHandler());
118     }
119
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());
126     }
127
128     public class CsidFromLocationResponseHandler implements ResponseHandler<String> {
129
130         @Override
131         public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
132             StatusLine status = response.getStatusLine();
133             int statusCode = status.getStatusCode();
134             
135             if (statusCode< 200 || statusCode > 299) {
136                 throw new HttpResponseException(statusCode, status.getReasonPhrase());
137             }
138             
139             return csidFromLocation(response.getFirstHeader("Location").getValue());
140         }
141     }
142     
143     public class JsonBodyResponseHandler implements ResponseHandler<JsonNode> {
144
145         @Override
146         public JsonNode handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
147             StatusLine status = response.getStatusLine();
148             int statusCode = status.getStatusCode();
149             
150             if (statusCode< 200 || statusCode > 299) {
151                 throw new HttpResponseException(statusCode, status.getReasonPhrase());
152             }
153             
154             HttpEntity entity = response.getEntity();
155             
156             if (entity == null) {
157                 throw new ClientProtocolException("response contains no content");
158             }
159
160             ContentType contentType = ContentType.getOrDefault(entity);
161             String mimeType = contentType.getMimeType();
162             
163             if (!mimeType.equals(ContentType.APPLICATION_JSON.getMimeType())) {
164                 throw new ClientProtocolException("unexpected content type: " + contentType);
165             }
166
167             return jsonFactory.createParser(entity.getContent()).readValueAsTree();
168         }
169     }
170
171     public class CheckStatusResponseHandler implements ResponseHandler<Integer> {
172
173         @Override
174         public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
175             StatusLine status = response.getStatusLine();
176             int statusCode = status.getStatusCode();
177             
178             if (statusCode< 200 || statusCode > 299) {
179                 throw new HttpResponseException(statusCode, status.getReasonPhrase());
180             }
181             
182             return statusCode;
183         }
184     }
185
186     private String csidFromLocation(String location) {
187         int index = location.lastIndexOf("/");
188         
189         return location.substring(index + 1);
190     }
191     
192     private String getUrl(String path) {
193         return BASE_URL + path;
194     }
195     
196     private File getFile(String fileName) {
197         ClassLoader classLoader = getClass().getClassLoader();
198         
199         return new File(classLoader.getResource(FILE_PATH + fileName + ".json").getFile());
200     }
201 }