View Javadoc

1   /*
2    * $Id: AbstractCustomerTest.java 22401 2011-07-13 09:10:18Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom;
11  
12  import org.mule.tck.junit4.FunctionalTestCase;
13  
14  import java.io.IOException;
15  import java.util.Date;
16  
17  import javax.xml.namespace.QName;
18  
19  import org.apache.abdera.Abdera;
20  import org.apache.abdera.factory.Factory;
21  import org.apache.abdera.i18n.iri.IRI;
22  import org.apache.abdera.model.Base;
23  import org.apache.abdera.model.Element;
24  import org.apache.abdera.model.Entry;
25  import org.apache.abdera.protocol.client.AbderaClient;
26  import org.apache.abdera.protocol.client.ClientResponse;
27  import org.apache.abdera.protocol.client.RequestOptions;
28  import org.apache.abdera.writer.Writer;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  public abstract class AbstractCustomerTest extends FunctionalTestCase
33  {
34  
35      protected void testCustomerProvider(String basePath) throws Exception
36      {
37          Abdera abdera = new Abdera();
38          Factory factory = abdera.getFactory();
39  
40          AbderaClient client = new AbderaClient(abdera);
41          String base = "http://localhost:9002" + basePath + "/";
42  
43          // Testing of entry creation
44          IRI colUri = new IRI(base).resolve("customers");
45          Entry entry = factory.newEntry();
46          entry.setTitle("Hmmm this is ignored right now");
47          entry.setUpdated(new Date());
48          entry.addAuthor("Acme Industries");
49          entry.setId(factory.newUuidUri());
50          entry.setSummary("Customer document");
51  
52          Element customerEl = factory.newElement(new QName("customer"));
53          customerEl.setAttributeValue(new QName("name"), "Dan Diephouse");
54          entry.setContent(customerEl);
55  
56          RequestOptions opts = new RequestOptions();
57          opts.setContentType("application/atom+xml;type=entry");
58          ClientResponse res = client.post(colUri.toString(), entry, opts);
59          assertEquals(201, res.getStatus());
60  
61          IRI location = res.getLocation();
62          assertEquals(basePath + "/customers/1001-Dan_Diephouse", location.toString());
63  
64          // GET the entry
65          res = client.get(colUri.resolve(location.toString()).toString());
66          assertEquals(200, res.getStatus());
67  
68          org.apache.abdera.model.Document<Entry> entry_doc = res.getDocument();
69          entry = entry_doc.getRoot();
70      }
71  
72      protected void prettyPrint(Abdera abdera, Base doc) throws IOException
73      {
74          Writer writer = abdera.getWriterFactory().getWriter("prettyxml");
75          writer.writeTo(doc, System.out);
76          System.out.println();
77      }
78  }