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