summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-atom-runtime/src/test/java/org/apache/tuscany/sca/binding/atom/ProviderEntryEntityTagsTestCase.java
blob: c39bee65f69db699b886a52a632af62ec13e341b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tuscany.sca.binding.atom;

import java.text.SimpleDateFormat;
import java.util.Date;

import junit.framework.Assert;

import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.i18n.iri.IRI;
import org.apache.abdera.model.Content;
import org.apache.abdera.model.Entry;
import org.apache.abdera.parser.Parser;
import org.apache.abdera.protocol.client.AbderaClient;
import org.apache.abdera.protocol.client.ClientResponse;
import org.apache.abdera.protocol.client.RequestOptions;
import org.apache.abdera.protocol.client.util.BaseRequestEntity;
import org.apache.tuscany.sca.node.Contribution;
import org.apache.tuscany.sca.node.ContributionLocationHelper;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests use of server provided entry entity tags for Atom binding in Tuscany.
 * Tests conditional gets (e.g. get if-none-match) or conditional posts (post if-match)
 * using entity tags or last modified header entries.
 * Uses the SCA provided Provider composite to act as a server.
 * Uses the Abdera provided Client to act as a client.
 *
 * @version $Rev$ $Date$
 */
public class ProviderEntryEntityTagsTestCase {
    public final static String providerURI = "http://localhost:8084/customer";

    protected static Node scaProviderNode;

    protected static CustomerClient testService;
    protected static Abdera abdera;
    protected static AbderaClient client;
    protected static Parser abderaParser;
    protected static String eTag;
    protected static Date lastModified;
    protected static final SimpleDateFormat dateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss Z" ); // RFC 822 date time

    @BeforeClass
    public static void init() throws Exception {
        //System.out.println(">>>ProviderEntryEntityTagsTestCase.init");
        String contribution = ContributionLocationHelper.getContributionLocation(ProviderEntryEntityTagsTestCase.class);

        scaProviderNode = NodeFactory.newInstance().createNode("org/apache/tuscany/sca/binding/atom/Provider.composite", new Contribution("provider", contribution));
        scaProviderNode.start();

        abdera = new Abdera();
        client = new AbderaClient(abdera);
        abderaParser = Abdera.getNewParser();
    }

    @AfterClass
    public static void destroy() throws Exception {
        //System.out.println(">>>ProviderEntryEntityTagsTestCase.destroy");
        if (scaProviderNode != null) {
            scaProviderNode.stop();
        }
    }

    @Test
    public void testPrelim() throws Exception {
        Assert.assertNotNull(scaProviderNode);
        Assert.assertNotNull( client );
    }

    @Test
    public void testEmptyCachePost() throws Exception {
        // Pseudo-code
        // 1) Example HTTP POST request (new entry put, new etag response)
        // User client post request
        //        POST /myblog/entries HTTP/1.1
        //        Slug: First Post
        //
        //        <?xml version="1.0" ?>
        //        <entry xmlns="http://www.w3.org/2005/Atom">
        //          <title>Atom-Powered Robots Run Amok</title>
        //          <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
        //          <updated>2007-02-123T17:09:02Z</updated>
        //          <author><name>Captain Lansing</name></author>
        //          <content>It's something moving... solid metal</content>
        //        </entry>

        // Expected Atom server response (note unique ETag)
        //       HTTP/1.1 201 Created
        //       Date: Fri, 23 Feb 2007 21:17:11 GMT
        //       Content-Length: nnn
        //       Content-Type: application/atom+xml;type=entry
        //       Location: http://example.org/edit/first-post.atom
        //       Content-Location: http://example.org/edit/first-post.atom
        //       ETag: "e180ee84f0671b1"
        //       Last-Modified: Fri, 25 Jul 2008 14:36:44 -0500
        //
        //        <?xml version="1.0" ?>
        //        <entry xmlns="http://www.w3.org/2005/Atom">
        //          <title>Atom-Powered Robots Run Amok</title>
        //          <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
        //          <updated>2007-02-123T17:09:02Z</updated>
        //          <author><name>Captain Lansing</name></author>
        //          <content>It's something moving... solid metal</content>
        //        </entry>

        // Testing of entry creation
        Factory factory = abdera.getFactory();
        String customerName = "Fred Farkle";
        Entry entry = factory.newEntry();
        entry.setTitle("customer " + customerName);
        entry.setUpdated(new Date());
        entry.addAuthor("Apache Tuscany");
        // ID created by collection.
        // entry.setId(id); // auto-provided
        // entry.addLink("" + id, "edit"); // auto-provided
        // entry.addLink("" + id, "alternate"); // auto-provided
        Content content = abdera.getFactory().newContent();
        content.setContentType(Content.Type.TEXT);
        content.setValue(customerName);
        entry.setContentElement(content);

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        // AtomTestCaseUtils.printRequestHeaders( "Post request headers", "   ", opts );
        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        ClientResponse res = client.post(colUri.toString(), entry, opts);

        // Assert response status code is 201-OK.
        // Assert response header Content-Type: application/atom+xml; charset=UTF-8
        // Assert response header Location: http://example.org/edit/first-post.atom
        // Assert response header Content-Location: http://example.org/edit/first-post.atom
        // Assert response header ETag: "e180ee84f0671b1"
        // Assert response header Last-Modified: Fri, 25 Jul 2008 14:36:44 -0500
        // Assert collection size is 1.
        Assert.assertEquals(201, res.getStatus());
        Assert.assertEquals(contentType, res.getContentType().toString().trim());
        // Assert.assertNotNull( res.getLocation().toString() );
        // Assert.assertEquals( "", res.getContentLocation().toString() );
        // Save eTag for subsequent tests;
        eTag = res.getHeader( "ETag" );
        Assert.assertNotNull( eTag );
        lastModified = res.getLastModified();
        Assert.assertNotNull(lastModified);
    }

    @Test
    public void testDirtyCachePut() throws Exception {
        // 2) Conditional PUT request (post with etag. entry provided is stale)
        // User client PUT request
        //        PUT /edit/first-post.atom HTTP/1.1
        // >      If-Match: "e180ee84f0671b1"
        //
        //        <?xml version="1.0" ?>
        //        <entry xmlns="http://www.w3.org/2005/Atom">
        //         <title>Atom-Powered Robots Run Amok</title>
        //         <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
        //         <updated>2007-02-24T16:34:06Z</updated>
        //         <author><name>Captain Lansing</name></author>
        //         <content>Update: it's a hoax!</content>
        //       </entry>
        // Testing of entry creation
        Factory factory = abdera.getFactory();
        String customerName = "Molly Ringwald";
        Entry entry = factory.newEntry();
        entry.setTitle("customer " + customerName);
        entry.setUpdated( new Date());
        entry.addAuthor("Apache Tuscany");
        String id = eTag.substring( 1, eTag.length()-1);
        entry.setId(id); // auto-provided
        // entry.addLink("" + id, "edit"); // auto-provided
        // entry.addLink("" + id, "alternate"); // auto-provided
        Content content = abdera.getFactory().newContent();
        content.setContentType(Content.Type.TEXT);
        content.setValue(customerName);
        entry.setContentElement(content);

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-None-Match", eTag);

        // AtomTestCaseUtils.printRequestHeaders( "Put request headers", "   ", opts );
        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.put(colUri.toString() + "/" + id, new BaseRequestEntity( entry ), opts);
        // Expected Atom server response (item was edited by another user)
        // >      HTTP/1.1 412 Precondition Failed
        //       Date: Sat, 24 Feb 2007 16:34:11 GMT

        // If-Match Assert response status code is 412. Precondition failed.
        // If-None-Match Assert response status code is 200. OK
        Assert.assertEquals(200, res.getStatus());
        // Put provides null body and no etags.
        res.release();
    }

    @Test
    public void testETagMissGet() throws Exception {
        // 4) Conditional GET example (get with etag. etag not in cache)
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        // >      If-None-Match: "e180ee84f0671b1"

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-None-Match", "123456");
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);
        // Expected Atom server response (item was edited by another user)
        // >      HTTP/1.1 412 Precondition Failed
        //       Date: Sat, 24 Feb 2007 16:34:11 GMT

        // Atom server response (item was up to date)
        // >      HTTP/1.1 200 OK
        //        Date: Sat, 24 Feb 2007 13:17:11 GMT
        // >      ETag: "bb4f5e86e92ddb8549604a0df0763581"
        // >      Last-Modified: Mon, 28 Jul 2008 10:25:37 -0500

        // Assert response status code is 200 OK.
        // Assert header Content-Type: application/atom+xml;type=entry
        // Assert header Location: http://example.org/edit/first-post.atom
        // Assert header Content-Location: http://example.org/edit/first-post.atom
        // Assert header ETag: "555555" (etag response != etag request)
        // Assert header Last-Modified: Fri, 25 Jul 2008 14:36:44 -0500
        Assert.assertEquals(200, res.getStatus());
        Assert.assertEquals(contentType, res.getContentType().toString().trim());
        // Assert.assertNotNull( res.getLocation().toString() );
        // Assert.assertEquals( "", res.getContentLocation().toString() );
        Assert.assertNotNull( res.getHeader( "ETag" ) );
        lastModified = res.getLastModified();
        Assert.assertNotNull(lastModified);
        res.release();
    }

    @Test
    public void testETagHitGet() throws Exception {
        // 3) Conditional GET example (get with etag. etag match)
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        // >      If-None-Match: "e180ee84f0671b1"

        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-None-Match", eTag);
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);
        // Atom server response (item was up to date)
        // >      HTTP/1.1 304 Not Modified
        //       Date: Sat, 24 Feb 2007 13:17:11 GMT

        // Assert response status code is 304 Not Modified.
        // Assert header ETag: "e180ee84f0671b1"
        // Assert header Last-Modified: Fri, 25 Jul 2008 14:36:44 -0500
        // Assert.assertEquals(304, res.getStatus());
        res.release();
    }


    @Test
    public void testUpToDateGet() throws Exception {
        // 3) Conditional GET example (get with If-Mod. entry is up to date)
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        // >      If-Modified-Since: Sat, 29 Oct 2025 19:43:31 GMT
        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Modified-Since", "Sat, 29 Oct 2025 19:43:31 GMT"); // "EEE, dd MMM yyyy HH:mm:ss Z // RFC 822 Date
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.get(colUri.toString() + "/" + id, opts);
        ClientResponse res = client.execute( "GET", colUri.toString(), (BaseRequestEntity)null, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 304 Not Modified
        //       Date: Sat, 24 Feb 2007 13:17:11 GMT

        // Assert response status code is 304 Not Modified.
        Assert.assertEquals(304, res.getStatus());
        res.release();
    }

    @Test
    public void testOutOfDateGet() throws Exception {
        // 4) Conditional GET example (get with If-Mod. entry is not to date)
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        // >      If-Modified-Since: Sat, 29 Oct 1844 19:43:31 GMT
        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Modified-Since", "Sat, 29 Oct 1844 19:43:31 GMT"); // "EEE, dd MMM yyyy HH:mm:ss Z // RFC 822 Date
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 200 OK
        //        Date: Sat, 24 Feb 2007 13:17:11 GMT
        // >      ETag: "bb4f5e86e92ddb8549604a0df0763581"
        // >      Last-Modified: Mon, 28 Jul 2008 10:25:37 -0500

        // Assert response status code is 200 OK.
        // Assert header ETag: "e180ee84f0671b1"
        // Assert header Last-Modified: Greater than If-Mod
        Assert.assertEquals(200, res.getStatus());
        Assert.assertEquals(contentType, res.getContentType().toString().trim());
        // Assert.assertNotNull( res.getLocation().toString() );
        // Assert.assertEquals( "", res.getContentLocation().toString() );
        Assert.assertNotNull( res.getHeader( "ETag" ) );
        lastModified = res.getLastModified();
        Assert.assertNotNull(lastModified);
        res.release();
    }

    @Test
    public void testUpToDateUnModGet() throws Exception {
        // 3) Conditional GET example (get with If-Unmod. entry is not modified (< predicate date).
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        // >      If-Unmodified-Since: Sat, 29 Oct 2025 19:43:31 GMT
        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Unmodified-Since", "Sat, 29 Oct 2050 19:43:31 GMT" );
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 200 OK
        //        Date: Sat, 24 Feb 2007 13:17:11 GMT
        // >      ETag: "bb4f5e86e92ddb8549604a0df0763581"
        // >      Last-Modified: Mon, 28 Jul 2008 10:25:37 -0500

        // Assert response status code is 200 OK.
        // Assert header Content-Type: application/atom+xml;type=entry
        // Assert header Location: http://example.org/edit/first-post.atom
        // Assert header Content-Location: http://example.org/edit/first-post.atom
        // Assert header ETag: "e180ee84f0671b1"
        // Assert header Last-Modified: Less than If-Unmod
        Assert.assertEquals(200, res.getStatus());
        res.release();
    }

    @Test
    public void testOutOfDateUnModGet() throws Exception {
        // 4) Conditional GET example (get with If-Unmod. entry is modified (> predicate date)
        // User client GET request
        //       GET /edit/first-post.atom HTTP/1.1
        //        Host: example.org
        // >      If-Unmodified-Since: Sat, 29 Oct 1844 19:43:31 GMT
        RequestOptions opts = new RequestOptions();
        final String contentType = "application/atom+xml";
        opts.setContentType(contentType);
        opts.setHeader( "If-Unmodified-Since", "Sat, 29 Oct 1844 19:43:31 GMT" );
        opts.setHeader( "Pragma", "no-cache"); // turn off client caching

        IRI colUri = new IRI(providerURI).resolve("customer");
        // res = client.post(colUri.toString() + "?test=foo", entry, opts);
        String id = eTag.substring( 1, eTag.length()-1);
        // Warning. AbderaClient.put(String uri,Base base,RequestOptions options) caches on the client side.
        // ClientResponse res = client.put(colUri.toString() + id, entry, opts);
        ClientResponse res = client.get(colUri.toString() + "/" + id, opts);

        // Atom server response (item was up to date)
        // >      HTTP/1.1 304 Not Modified
        //       Date: Sat, 24 Feb 2007 13:17:11 GMT

        // Assert response status code is 304 Not Modified.
        Assert.assertEquals(304, res.getStatus());
        res.release();
    }
}