2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.test.jaxrs.services.tests;
36
import java.util.Collections;
37
import java.util.Date;
38
import java.util.List;
41
import javax.ws.rs.core.Application;
42
import javax.ws.rs.core.Request;
44
import org.restlet.Response;
45
import org.restlet.data.ClientInfo;
46
import org.restlet.data.Conditions;
47
import org.restlet.data.Dimension;
48
import org.restlet.data.Language;
49
import org.restlet.data.MediaType;
50
import org.restlet.data.Method;
51
import org.restlet.data.Preference;
52
import org.restlet.data.Status;
53
import org.restlet.data.Tag;
54
import org.restlet.ext.jaxrs.internal.core.CallContext;
55
import org.restlet.test.jaxrs.services.resources.RequestService;
56
import org.restlet.test.jaxrs.util.TestUtils;
59
* This test class checks if the Request.evaluatePreconditions methods works
62
* @author Stephan Koops
67
public class RequestTest extends JaxRsTestCase {
69
* After than 2009-01-08, 12h
71
* @see EvaluatePreconditionService#getLastModificationDateFromDatastore()
73
@SuppressWarnings("deprecation")
74
public static final Date AFTER = new Date(2009 - 1900, 0, 9); // 2009-01-09
77
* Before 2009-01-08, 12h
79
* @see EvaluatePreconditionService#getLastModificationDateFromDatastore()
81
@SuppressWarnings("deprecation")
82
public static final Date BEFORE = new Date(2007 - 1900, 11, 31); // 2007-12-31
84
private static final Status PREC_FAILED = Status.CLIENT_ERROR_PRECONDITION_FAILED;
87
* @param modifiedSince
91
private static Conditions createConditions(Date modifiedSince, Tag entityTag) {
92
final Conditions conditions = new Conditions();
93
conditions.setModifiedSince(modifiedSince);
94
conditions.setMatch(TestUtils.createList(entityTag));
98
public static void main(String[] args) throws Exception {
99
new RequestTest().runServerUntilKeyPressed();
105
@SuppressWarnings("all")
106
private Tag getDatastoreETag() {
107
return org.restlet.ext.jaxrs.internal.util.Converter
108
.toRestletTag(RequestService.getEntityTagFromDatastore());
112
protected Application getApplication() {
113
return new Application() {
115
@SuppressWarnings({ "unchecked", "rawtypes" })
116
public Set<Class<?>> getClasses() {
117
return (Set) Collections.singleton(RequestService.class);
122
public void testDateAndEntityTag1Get() throws Exception {
123
final Conditions cond = createConditions(BEFORE, getDatastoreETag());
124
final Response response = get("date", cond);
125
assertEquals(Status.SUCCESS_OK, response.getStatus());
129
* @see RequestService#put(Request)
131
public void testDateAndEntityTag1Put() throws Exception {
132
final Conditions cond = createConditions(BEFORE, getDatastoreETag());
133
final Response response = put("date", null, cond);
134
assertEquals(Status.SUCCESS_OK, response.getStatus());
137
public void testDateAndEntityTag2Get() throws Exception {
138
final Conditions conditions = createConditions(AFTER,
140
final Response response = get("date", conditions);
141
assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
144
public void testDateAndEntityTag2Put() throws Exception {
145
final Conditions conditions = createConditions(AFTER,
147
final Response response = put("date", null, conditions);
148
assertEquals(PREC_FAILED, response.getStatus());
151
public void testDateAndEntityTag3Get() throws Exception {
152
final Conditions conditions = createConditions(BEFORE, new Tag(
154
final Response response = get("date", conditions);
155
assertEquals(PREC_FAILED, response.getStatus());
158
public void testDateAndEntityTag3Put() throws Exception {
159
final Conditions conditions = createConditions(BEFORE, new Tag(
161
final Response response = put("date", null, conditions);
162
assertEquals(PREC_FAILED, response.getStatus());
165
public void testDateAndEntityTag4Get() throws Exception {
166
final Conditions conditions = createConditions(AFTER,
168
final Response response = get("date", conditions);
169
assertEquals(PREC_FAILED, response.getStatus());
172
public void testDateAndEntityTag4Put() throws Exception {
173
final Conditions conditions = createConditions(AFTER,
175
final Response response = put("date", null, conditions);
176
assertEquals(PREC_FAILED, response.getStatus());
179
public void testGetDateNotModified() throws Exception {
180
final Conditions conditions = new Conditions();
181
conditions.setModifiedSince(AFTER);
182
final Response response = get("date", conditions);
183
assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
184
assertFalse(response.isEntityAvailable()
185
&& response.getEntity().getSize() != 0);
186
// from RFC 2616, Section 10.3.5
187
// The 304 response MUST include the following header fields:
188
// - ETag and/or Content-Location, if the header would have been sent
189
// in a 200 response to the same request
190
// - Expires, Cache-Control, and/or Vary, if the field-value might
191
// differ from that sent in any previous response for the same
195
public void testGetEntityTagMatch() throws Exception {
196
Conditions conditions = new Conditions();
197
conditions.setMatch(TestUtils.createList(getDatastoreETag()));
198
Response response = get("date", conditions);
199
assertEquals(Status.SUCCESS_OK, response.getStatus());
200
assertEquals(RequestService.getLastModificationDateFromDatastore(),
201
response.getEntity().getModificationDate());
202
assertEquals(getDatastoreETag(), response.getEntity().getTag());
203
assertNotNull(response.getEntity().getText());
204
assertTrue(response.getEntity().getSize() > 0);
206
conditions = new Conditions();
207
conditions.setMatch(TestUtils.createList(new Tag("affer")));
208
response = get("date", conditions);
209
assertEquals(PREC_FAILED, response.getStatus());
212
public void testGetEntityTagNoneMatch() throws Exception {
213
Conditions conditions = new Conditions();
214
conditions.setNoneMatch(TestUtils.createList(getDatastoreETag()));
215
Response response = get("date", conditions);
216
assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
218
conditions = new Conditions();
219
conditions.setNoneMatch(TestUtils.createList(new Tag("affer")));
220
response = get("date", conditions);
221
assertEquals(Status.SUCCESS_OK, response.getStatus());
225
* @see RequestService#getLastModificationDateFromDatastore()
228
public void testGetModifiedSince() throws Exception {
229
Conditions conditions = new Conditions();
230
conditions.setModifiedSince(BEFORE);
231
Response response = get("date", conditions);
232
assertEquals(Status.SUCCESS_OK, response.getStatus());
233
assertEquals(RequestService.getLastModificationDateFromDatastore(),
234
response.getEntity().getModificationDate());
235
assertEquals(getDatastoreETag(), response.getEntity().getTag());
236
assertNotNull(response.getEntity().getText());
237
assertTrue(response.getEntity().getSize() > 0);
239
conditions = new Conditions();
240
conditions.setModifiedSince(AFTER);
241
response = get("date", conditions);
242
assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
243
assertEquals(RequestService.getLastModificationDateFromDatastore(),
244
response.getEntity().getModificationDate());
245
assertEquals(getDatastoreETag(), response.getEntity().getTag());
246
assertEquals(0, response.getEntity().getSize());
249
public void testGetUnmodifiedSince() throws Exception {
250
Conditions conditions = new Conditions();
251
conditions.setUnmodifiedSince(AFTER);
252
Response response = get("date", conditions);
253
assertEquals(Status.SUCCESS_OK, response.getStatus());
254
assertEquals(RequestService.getLastModificationDateFromDatastore(),
255
response.getEntity().getModificationDate());
256
assertEquals(getDatastoreETag(), response.getEntity().getTag());
257
assertNotNull(response.getEntity().getText());
258
assertTrue(response.getEntity().getSize() > 0);
260
conditions = new Conditions();
261
conditions.setUnmodifiedSince(BEFORE);
262
response = get("date", conditions);
263
assertEquals(PREC_FAILED, response.getStatus());
265
// NICE testen, was bei ungueltigem Datum passiert:
266
// If-Unmodified-Since-Header ignorieren.
269
public void testOptions() {
270
final Response response = options();
271
final Set<Method> allowedMethods = response.getAllowedMethods();
272
assertEquals(3, allowedMethods.size());
273
assertTrue("allowedOptions must contain ABC", allowedMethods
274
.contains(Method.valueOf("ABC")));
275
assertTrue("allowedOptions must contain DEF", allowedMethods
276
.contains(Method.valueOf("DEF")));
277
assertTrue("allowedOptions must contain GHI", allowedMethods
278
.contains(Method.valueOf("GHI")));
279
assertEquals(3, allowedMethods.size());
283
* @see RequestService#getLastModificationDateFromDatastore()
286
public void testPutModifiedSince() throws Exception {
287
Conditions conditions = new Conditions();
288
conditions.setModifiedSince(BEFORE);
289
Response response = put("date", null, conditions);
290
assertEquals(Status.SUCCESS_OK, response.getStatus());
292
conditions = new Conditions();
293
conditions.setModifiedSince(AFTER);
294
response = put("date", null, conditions);
295
assertEquals(PREC_FAILED, response.getStatus());
298
public void testPutUnmodifiedSince() throws Exception {
299
Conditions conditions = new Conditions();
300
conditions.setUnmodifiedSince(AFTER);
301
Response response = put("date", null, conditions);
302
assertEquals(Status.SUCCESS_OK, response.getStatus());
304
conditions = new Conditions();
305
conditions.setUnmodifiedSince(BEFORE);
306
response = put("date", null, conditions);
307
assertEquals(PREC_FAILED, response.getStatus());
310
public void testSelectVariant() {
311
final ClientInfo clientInfo = new ClientInfo();
312
final List<Preference<Language>> accLangs = clientInfo
313
.getAcceptedLanguages();
314
accLangs.add(new Preference<Language>(Language.SPANISH, 1f));
315
accLangs.add(new Preference<Language>(new Language("de"), 0.8f));
316
clientInfo.getAcceptedMediaTypes().add(
317
new Preference<MediaType>(MediaType.TEXT_HTML, 0.5f));
319
Response response = get("selectVariants", clientInfo);
320
assertEquals(Status.SUCCESS_OK, response.getStatus());
321
assertEqualMediaType(MediaType.TEXT_HTML, response.getEntity()
323
assertEquals(new Language("de"), TestUtils.getOnlyElement(response
324
.getEntity().getLanguages()));
325
assertTrue("dimensions must contain " + Dimension.MEDIA_TYPE, response
326
.getDimensions().contains(Dimension.MEDIA_TYPE));
327
assertTrue("dimensions must contain " + Dimension.LANGUAGE, response
328
.getDimensions().contains(Dimension.LANGUAGE));
330
clientInfo.getAcceptedMediaTypes().add(
331
new Preference<MediaType>(MediaType.TEXT_PLAIN, 1f));
332
response = get("selectVariants", clientInfo);
333
assertEqualMediaType(MediaType.TEXT_PLAIN, response.getEntity()
335
assertEquals(new Language("de"), TestUtils.getOnlyElement(response
336
.getEntity().getLanguages()));
338
accLangs.add(new Preference<Language>(Language.ENGLISH, 0.9f));
339
response = get("selectVariants", clientInfo);
340
assertEqualMediaType(MediaType.TEXT_PLAIN, response.getEntity()
342
assertEquals(Language.ENGLISH, TestUtils.getOnlyElement(response
343
.getEntity().getLanguages()));