From cbf5e418cc6efa38bc69251bf2f1f95606741b10 Mon Sep 17 00:00:00 2001 From: Paul Merlin Date: Fri, 15 Feb 2013 15:01:19 +0100 Subject: [PATCH] REST-Client Library: unit tests cleanup --- .../library/rest/client/RssReaderTest.java | 61 +++++++------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/libraries/rest-client/src/test/java/org/qi4j/library/rest/client/RssReaderTest.java b/libraries/rest-client/src/test/java/org/qi4j/library/rest/client/RssReaderTest.java index 76eb8a298..52d7e11a5 100644 --- a/libraries/rest-client/src/test/java/org/qi4j/library/rest/client/RssReaderTest.java +++ b/libraries/rest-client/src/test/java/org/qi4j/library/rest/client/RssReaderTest.java @@ -9,7 +9,6 @@ import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.junit.Test; -import org.qi4j.api.value.ValueBuilder; import org.qi4j.bootstrap.AssemblyException; import org.qi4j.bootstrap.ModuleAssembly; import org.qi4j.library.rest.client.api.ContextResourceClient; @@ -20,7 +19,6 @@ import org.qi4j.library.rest.client.spi.ResponseReader; import org.qi4j.library.rest.client.spi.ResultHandler; import org.qi4j.library.rest.common.ValueAssembler; -import org.qi4j.library.rest.common.link.Link; import org.qi4j.test.AbstractQi4jTest; import org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler; import org.restlet.Client; @@ -35,12 +33,14 @@ import static org.qi4j.library.rest.client.api.HandlerCommand.*; /** - * Reads my RSS feed and prints out all comments for each entry. This is an example of how - * to use the RSS client for something more generic that was not produced by Qi4j REST server library. + * Reads Qi4j Github commits on develop ATOM feed and prints out all title and detail url for each entry. + * This is an example of how to use the RSS client for something more generic that was not produced by Qi4j REST server + * library. */ public class RssReaderTest extends AbstractQi4jTest { + private ContextResourceClient crc; @Override @@ -56,7 +56,7 @@ public void assemble( ModuleAssembly module ) @Test public void testReadRssFeed() { - Client client = new Client( Protocol.HTTP ); + Client client = new Client( Protocol.HTTPS ); Reference ref = new Reference( "https://github.com/Qi4j/qi4j-sdk/commits/develop.atom" ); ContextResourceClientFactory contextResourceClientFactory = module.newObject( ContextResourceClientFactory.class, client ); @@ -66,7 +66,7 @@ public void testReadRssFeed() public Object readResponse( Response response, Class resultType ) throws ResourceException { - if (resultType.equals( Document.class )) + if( resultType.equals( Document.class ) ) { try { @@ -80,7 +80,7 @@ public Object readResponse( Response response, Class resultType ) } } - return null; //To change body of implemented methods use File | Settings | File Templates. + return null; } } ); @@ -89,7 +89,7 @@ public Object readResponse( Response response, Class resultType ) @Override public HandlerCommand handleResponse( Response response, ContextResourceClient client ) { - // Try to restart + System.out.println( ">> REFRESH on recoverable error: " + response.getStatus() ); return refresh(); } } ) ); @@ -107,12 +107,14 @@ public HandlerCommand handleResult( Document result, ContextResourceClient clien { final XPath xPath = XPathFactory.newInstance().newXPath(); - System.out.println( "== "+xPath.evaluate( "rss/channel/title", result )+" =="); + System.out.println( "== " + xPath.evaluate( "feed/title", result ) + " ==" ); - final NodeList nodes = (NodeList)xPath.evaluate( "rss/channel/item", result, XPathConstants.NODESET); + final NodeList nodes = (NodeList) xPath.evaluate( "feed/entry", result, XPathConstants.NODESET ); List items = new ArrayList(); - for (int i = 0; i < nodes.getLength(); i++) + for( int i = 0; i < nodes.getLength(); i++ ) + { items.add( nodes.item( i ) ); + } itemNodes = items.iterator(); @@ -127,41 +129,20 @@ public HandlerCommand handleResult( Document result, ContextResourceClient clien private HandlerCommand processEntry( final XPath xPath ) throws XPathExpressionException { - if (!itemNodes.hasNext()) + if( !itemNodes.hasNext() ) + { return null; + } Node item = itemNodes.next(); - System.out.println( "-- "+xPath.evaluate( "title", item )+" --" ); - String commentUrl = xPath.evaluate( "commentRss", item ); + String title = xPath.evaluate( "title", item ); + String detailUrl = xPath.evaluate( "link/@href", item ); - ValueBuilder link = module.newValueBuilder( Link.class ); - link.prototype().rel().set( "item" ); - link.prototype().id().set( "item" ); - link.prototype().href().set( commentUrl ); + System.out.println( "-- " + title + " --" ); + System.out.println( "-- " + detailUrl + " --" ); - return query( link.newInstance() ).onSuccess( new ResultHandler() - { - @Override - public HandlerCommand handleResult( Document result, ContextResourceClient client ) - { - try - { - final NodeList comments = (NodeList)xPath.evaluate( "rss/channel/item", result, XPathConstants.NODESET); - for (int i = 0; i < comments.getLength(); i++) - { - System.out.println( xPath.evaluate( "description", comments.item( i ) )); - System.out.println( "---"); - } - - return processEntry( xPath ); - } - catch( XPathExpressionException e ) - { - throw new ResourceException( e ); - } - } - } ); + return processEntry( xPath ); } } );