~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.test/src/org/restlet/test/jaxrs/services/tests/RequestTest.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
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.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
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
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.test.jaxrs.services.tests;
 
35
 
 
36
import java.util.Collections;
 
37
import java.util.Date;
 
38
import java.util.List;
 
39
import java.util.Set;
 
40
 
 
41
import javax.ws.rs.core.Application;
 
42
import javax.ws.rs.core.Request;
 
43
 
 
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;
 
57
 
 
58
/**
 
59
 * This test class checks if the Request.evaluatePreconditions methods works
 
60
 * fine.
 
61
 * 
 
62
 * @author Stephan Koops
 
63
 * @see RequestService
 
64
 * @see Request
 
65
 * @see CallContext
 
66
 */
 
67
public class RequestTest extends JaxRsTestCase {
 
68
    /**
 
69
     * After than 2009-01-08, 12h
 
70
     * 
 
71
     * @see EvaluatePreconditionService#getLastModificationDateFromDatastore()
 
72
     */
 
73
    @SuppressWarnings("deprecation")
 
74
    public static final Date AFTER = new Date(2009 - 1900, 0, 9); // 2009-01-09
 
75
 
 
76
    /**
 
77
     * Before 2009-01-08, 12h
 
78
     * 
 
79
     * @see EvaluatePreconditionService#getLastModificationDateFromDatastore()
 
80
     */
 
81
    @SuppressWarnings("deprecation")
 
82
    public static final Date BEFORE = new Date(2007 - 1900, 11, 31); // 2007-12-31
 
83
 
 
84
    private static final Status PREC_FAILED = Status.CLIENT_ERROR_PRECONDITION_FAILED;
 
85
 
 
86
    /**
 
87
     * @param modifiedSince
 
88
     * @param entityTag
 
89
     * @return
 
90
     */
 
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));
 
95
        return conditions;
 
96
    }
 
97
 
 
98
    public static void main(String[] args) throws Exception {
 
99
        new RequestTest().runServerUntilKeyPressed();
 
100
    }
 
101
 
 
102
    /**
 
103
     * @return
 
104
     */
 
105
    @SuppressWarnings("all")
 
106
    private Tag getDatastoreETag() {
 
107
        return org.restlet.ext.jaxrs.internal.util.Converter
 
108
                .toRestletTag(RequestService.getEntityTagFromDatastore());
 
109
    }
 
110
 
 
111
    @Override
 
112
    protected Application getApplication() {
 
113
        return new Application() {
 
114
            @Override
 
115
            @SuppressWarnings({ "unchecked", "rawtypes" })
 
116
            public Set<Class<?>> getClasses() {
 
117
                return (Set) Collections.singleton(RequestService.class);
 
118
            }
 
119
        };
 
120
    }
 
121
 
 
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());
 
126
    }
 
127
 
 
128
    /**
 
129
     * @see RequestService#put(Request)
 
130
     */
 
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());
 
135
    }
 
136
 
 
137
    public void testDateAndEntityTag2Get() throws Exception {
 
138
        final Conditions conditions = createConditions(AFTER,
 
139
                getDatastoreETag());
 
140
        final Response response = get("date", conditions);
 
141
        assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
 
142
    }
 
143
 
 
144
    public void testDateAndEntityTag2Put() throws Exception {
 
145
        final Conditions conditions = createConditions(AFTER,
 
146
                getDatastoreETag());
 
147
        final Response response = put("date", null, conditions);
 
148
        assertEquals(PREC_FAILED, response.getStatus());
 
149
    }
 
150
 
 
151
    public void testDateAndEntityTag3Get() throws Exception {
 
152
        final Conditions conditions = createConditions(BEFORE, new Tag(
 
153
                "shkhsdk"));
 
154
        final Response response = get("date", conditions);
 
155
        assertEquals(PREC_FAILED, response.getStatus());
 
156
    }
 
157
 
 
158
    public void testDateAndEntityTag3Put() throws Exception {
 
159
        final Conditions conditions = createConditions(BEFORE, new Tag(
 
160
                "shkhsdk"));
 
161
        final Response response = put("date", null, conditions);
 
162
        assertEquals(PREC_FAILED, response.getStatus());
 
163
    }
 
164
 
 
165
    public void testDateAndEntityTag4Get() throws Exception {
 
166
        final Conditions conditions = createConditions(AFTER,
 
167
                new Tag("shkhsdk"));
 
168
        final Response response = get("date", conditions);
 
169
        assertEquals(PREC_FAILED, response.getStatus());
 
170
    }
 
171
 
 
172
    public void testDateAndEntityTag4Put() throws Exception {
 
173
        final Conditions conditions = createConditions(AFTER,
 
174
                new Tag("shkhsdk"));
 
175
        final Response response = put("date", null, conditions);
 
176
        assertEquals(PREC_FAILED, response.getStatus());
 
177
    }
 
178
 
 
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
 
192
        // variant
 
193
    }
 
194
 
 
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);
 
205
 
 
206
        conditions = new Conditions();
 
207
        conditions.setMatch(TestUtils.createList(new Tag("affer")));
 
208
        response = get("date", conditions);
 
209
        assertEquals(PREC_FAILED, response.getStatus());
 
210
    }
 
211
 
 
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());
 
217
 
 
218
        conditions = new Conditions();
 
219
        conditions.setNoneMatch(TestUtils.createList(new Tag("affer")));
 
220
        response = get("date", conditions);
 
221
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
222
    }
 
223
 
 
224
    /**
 
225
     * @see RequestService#getLastModificationDateFromDatastore()
 
226
     * @throws Exception
 
227
     */
 
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);
 
238
 
 
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());
 
247
    }
 
248
 
 
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);
 
259
 
 
260
        conditions = new Conditions();
 
261
        conditions.setUnmodifiedSince(BEFORE);
 
262
        response = get("date", conditions);
 
263
        assertEquals(PREC_FAILED, response.getStatus());
 
264
 
 
265
        // NICE testen, was bei ungueltigem Datum passiert:
 
266
        // If-Unmodified-Since-Header ignorieren.
 
267
    }
 
268
 
 
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());
 
280
    }
 
281
 
 
282
    /**
 
283
     * @see RequestService#getLastModificationDateFromDatastore()
 
284
     * @throws Exception
 
285
     */
 
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());
 
291
 
 
292
        conditions = new Conditions();
 
293
        conditions.setModifiedSince(AFTER);
 
294
        response = put("date", null, conditions);
 
295
        assertEquals(PREC_FAILED, response.getStatus());
 
296
    }
 
297
 
 
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());
 
303
 
 
304
        conditions = new Conditions();
 
305
        conditions.setUnmodifiedSince(BEFORE);
 
306
        response = put("date", null, conditions);
 
307
        assertEquals(PREC_FAILED, response.getStatus());
 
308
    }
 
309
 
 
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));
 
318
 
 
319
        Response response = get("selectVariants", clientInfo);
 
320
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
321
        assertEqualMediaType(MediaType.TEXT_HTML, response.getEntity()
 
322
                .getMediaType());
 
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));
 
329
 
 
330
        clientInfo.getAcceptedMediaTypes().add(
 
331
                new Preference<MediaType>(MediaType.TEXT_PLAIN, 1f));
 
332
        response = get("selectVariants", clientInfo);
 
333
        assertEqualMediaType(MediaType.TEXT_PLAIN, response.getEntity()
 
334
                .getMediaType());
 
335
        assertEquals(new Language("de"), TestUtils.getOnlyElement(response
 
336
                .getEntity().getLanguages()));
 
337
 
 
338
        accLangs.add(new Preference<Language>(Language.ENGLISH, 0.9f));
 
339
        response = get("selectVariants", clientInfo);
 
340
        assertEqualMediaType(MediaType.TEXT_PLAIN, response.getEntity()
 
341
                .getMediaType());
 
342
        assertEquals(Language.ENGLISH, TestUtils.getOnlyElement(response
 
343
                .getEntity().getLanguages()));
 
344
    }
 
345
}