cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF Documentation > JAXRS Testing
Date Thu, 28 Jun 2012 12:50:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CXF20DOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing">JAXRS
Testing</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://cwiki.apache.org/confluence/display/~sergey_beryozkin">Sergey
Beryozkin</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <p><span style="font-size:2em;font-weight:bold"> JAX-RS Testing</span></p>


<div>
<ul>
    <li><a href='#JAXRSTesting-EmbeddedJetty'>Embedded Jetty</a></li>
    <li><a href='#JAXRSTesting-LocalTransport'>Local Transport</a></li>
</ul></div>

<p>JAX-RS endpoints can be easily tested using the embedded Jetty or CXF Local Transport.</p>

<h1><a name="JAXRSTesting-EmbeddedJetty"></a>Embedded Jetty</h1>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">import</span> javax.ws.rs.core.Response;

<span class="code-keyword">import</span> org.apache.cxf.endpoint.Server;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.client.WebClient;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;

<span class="code-keyword">import</span> org.junit.Test;
<span class="code-keyword">import</span> org.junit.Assert;
<span class="code-keyword">import</span> org.junit.BeforeClass;
<span class="code-keyword">import</span> org.junit.AfterClass;

<span class="code-keyword">import</span> custom.MyJaxrsResource;
<span class="code-keyword">import</span> custom.Book;

<span class="code-keyword">public</span> class JAXRSTest <span class="code-keyword">extends</span>
Assert {

<span class="code-keyword">private</span> <span class="code-keyword">final</span>
<span class="code-keyword">static</span> <span class="code-object">String</span>
ENDPOINT_ADDRESS = <span class="code-quote">"http:<span class="code-comment">//localhost:8080/<span
class="code-keyword">rest</span>"</span>;
</span><span class="code-keyword">private</span> <span class="code-keyword">final</span>
<span class="code-keyword">static</span> <span class="code-object">String</span>
WADL_ADDRESS = ENDPOINT_ADDRESS + <span class="code-quote">"?_wadl"</span>;
<span class="code-keyword">private</span> <span class="code-keyword">static</span>
Server server;

@BeforeClass
<span class="code-keyword">public</span> <span class="code-keyword">static</span>
void initialize() <span class="code-keyword">throws</span> Exception {
     startServer();
     waitForWADL();
}

<span class="code-keyword">private</span> <span class="code-keyword">static</span>
void startServer() <span class="code-keyword">throws</span> Exception {
     JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
     sf.setResourceClasses(MyJaxrsResource.class);
        
     List&lt;<span class="code-object">Object</span>&gt; providers = <span
class="code-keyword">new</span> ArrayList&lt;<span class="code-object">Object</span>&gt;();
     <span class="code-comment">// add custom providers <span class="code-keyword">if</span>
any
</span>     sf.setProviders(providers);
        
     sf.setResourceProvider(MyJaxrsResource.class.class,
                            <span class="code-keyword">new</span> SingletonResourceProvider(<span
class="code-keyword">new</span> MyJaxrsResource(), <span class="code-keyword">true</span>));
     sf.setAddress(ENDPOINT_ADDRESS);

     server = sf.create();
}

<span class="code-comment">// Optional step - may be needed to ensure that by the time
individual
</span><span class="code-comment">// tests start running the endpoint has been
fully initialized
</span><span class="code-keyword">private</span> <span class="code-keyword">static</span>
void waitForWADL() <span class="code-keyword">throws</span> Exception {
    WebClient client = WebClient.create(WADL_ADDRESS);
    <span class="code-comment">// wait <span class="code-keyword">for</span>
20 secs or so
</span>    <span class="code-keyword">for</span> (<span class="code-object">int</span>
i = 0; i &lt; 20; i++) {
        <span class="code-object">Thread</span>.currentThread().sleep(1000);
        Response response = client.get();
        <span class="code-keyword">if</span> (response.getStatus() == 200) {
            <span class="code-keyword">break</span>;
        }
    }
    <span class="code-comment">// no WADL is available yet - <span class="code-keyword">throw</span>
an exception or give tests a chance to run anyway
</span>}


@AfterClass
<span class="code-keyword">public</span> <span class="code-keyword">static</span>
void destroy() <span class="code-keyword">throws</span> Exception {
   server.stop();
   server.destroy();
}

@Test
<span class="code-keyword">public</span> void testGetBookWithWebClient() {
 WebClient client = WebClient.create(ENDPOINT_ADDRESS);
 client.accept(<span class="code-quote">"text/xml"</span>);
 client.path(<span class="code-quote">"somepath"</span>); 
 Book book = client.get(Book.class);
 assertEquals(123L, book.getId());
}

@Test
<span class="code-keyword">public</span> void testGetBookWithProxy() {
  MyJaxrsResource client = JAXRSClientFactory.create(ENDPOINT_ADDRESS, MyJaxrsResource.class);
  Book book = client.getBook();
  assertEquals(123L, book.getId());
}
}
</pre>
</div></div>

<p>It is quite easy to setup a server and start testing it. The advantage of using the
embedded Jetty is that a complete end-to-end round-trip can be exercised, thus stressing all
the CXF runtime which comes at the cost of some added complexity to do with setting up the
server.</p>

<h1><a name="JAXRSTesting-LocalTransport"></a>Local Transport</h1>

<p>Starting from CXF 2.6.2 it is possible to use CXF Local Transport for testing the
JAX-RS endpoints and clients.<br/>
This avoids the need to start an embedded servlet container and the tests will run faster.</p>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">

<span class="code-keyword">import</span> org.apache.cxf.endpoint.Server;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.client.WebClient;
<span class="code-keyword">import</span> org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;

<span class="code-keyword">import</span> org.apache.cxf.transport.local.LocalConduit;
<span class="code-keyword">import</span> org.apache.cxf.transport.local.LocalTransportFactory;

<span class="code-keyword">import</span> org.junit.Test;
<span class="code-keyword">import</span> org.junit.Assert;
<span class="code-keyword">import</span> org.junit.BeforeClass;
<span class="code-keyword">import</span> org.junit.AfterClass;

<span class="code-keyword">import</span> custom.MyJaxrsResource;
<span class="code-keyword">import</span> custom.Book;

<span class="code-keyword">public</span> class JAXRSTest <span class="code-keyword">extends</span>
Assert {

<span class="code-keyword">private</span> <span class="code-keyword">final</span>
<span class="code-keyword">static</span> <span class="code-object">String</span>
ENDPOINT_ADDRESS = <span class="code-quote">"local:<span class="code-comment">//books"</span>;
</span><span class="code-keyword">private</span> <span class="code-keyword">static</span>
Server server;

@BeforeClass
<span class="code-keyword">public</span> <span class="code-keyword">static</span>
void initialize() <span class="code-keyword">throws</span> Exception {
     startServer();
}

<span class="code-keyword">private</span> <span class="code-keyword">static</span>
void startServer() <span class="code-keyword">throws</span> Exception {
     JAXRSServerFactoryBean sf = <span class="code-keyword">new</span> JAXRSServerFactoryBean();
     sf.setTransportId(LocalTransportFactory.TRANSPORT_ID);
     sf.setResourceClasses(MyJaxrsResource.class);
        
     List&lt;<span class="code-object">Object</span>&gt; providers = <span
class="code-keyword">new</span> ArrayList&lt;<span class="code-object">Object</span>&gt;();
     <span class="code-comment">// add custom providers <span class="code-keyword">if</span>
any
</span>     sf.setProviders(providers);
        
     sf.setResourceProvider(MyJaxrsResource.class.class,
                            <span class="code-keyword">new</span> SingletonResourceProvider(<span
class="code-keyword">new</span> MyJaxrsResource(), <span class="code-keyword">true</span>));
     sf.setAddress(ENDPOINT_ADDRESS);

     server = sf.create();
}

@AfterClass
<span class="code-keyword">public</span> <span class="code-keyword">static</span>
void destroy() <span class="code-keyword">throws</span> Exception {
   server.stop();
   server.destroy();
}

@Test
<span class="code-keyword">public</span> void testGetBookWithWebClientDirectDispatch()
{
 WebClient client = WebClient.create(ENDPOINT_ADDRESS);
 WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, <span
class="code-object">Boolean</span>.TRUE);
 
 client.accept(<span class="code-quote">"text/xml"</span>);
 client.path(<span class="code-quote">"somepath"</span>); 
 Book book = client.get(Book.class);
 assertEquals(123L, book.getId());
}

@Test
<span class="code-keyword">public</span> void testGetBookViaPipe() {
 WebClient client = WebClient.create(ENDPOINT_ADDRESS);
 client.accept(<span class="code-quote">"text/xml"</span>);
 client.path(<span class="code-quote">"somepath"</span>); 
 Book book = client.get(Book.class);
 assertEquals(123L, book.getId());
}



@Test
<span class="code-keyword">public</span> void testAddBookWithProxyDirectDispatch()
{
  MyJaxrsResource client = JAXRSClientFactory.create(ENDPOINT_ADDRESS, MyJaxrsResource.class);
  WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, <span
class="code-object">Boolean</span>.TRUE);
  Book book = client.echoBook(<span class="code-keyword">new</span> Book(123));
  assertEquals(123L, book.getId());
}
}
</pre>
</div></div>

<p>Note that setting a LocalConduit.DIRECT_DISPATCH property to 'true' ensures that
the invocation goes immediately into the service chain after the client out chain has completed.
This works well for all HTTP verbs including GET, POST, etc.</p>

<p>If this property is not set then CXF LocalConduit sets up a pipe which is initiated
via an initial write on the client side. Thus this option is only useful for testing POST
or PUT invocations</p>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing">View
Online</a>
              |
       <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAXRS+Testing?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message