Skip to content

Commit

Permalink
REST-Client Library: unit tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eskatos committed Feb 15, 2013
1 parent 1166673 commit cbf5e41
Showing 1 changed file with 21 additions and 40 deletions.
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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 );

Expand All @@ -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
{
Expand All @@ -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;
}
} );

Expand All @@ -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();
}
} ) );
Expand All @@ -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<Node> items = new ArrayList<Node>();
for (int i = 0; i < nodes.getLength(); i++)
for( int i = 0; i < nodes.getLength(); i++ )
{
items.add( nodes.item( i ) );
}

itemNodes = items.iterator();

Expand All @@ -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> 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<Document>()
{
@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 );
}
} );

Expand Down

0 comments on commit cbf5e41

Please sign in to comment.