Permalink
Browse files

Added tests for AliasSelectorResolvers

  • Loading branch information...
1 parent aa3fd1f commit c7defc4b47bf6b2a2f8390ac97a4c328774da693 @cepage cepage committed May 9, 2012
@@ -4,11 +4,6 @@
{
private static final long serialVersionUID = -5307328628794516457L;
- public ParseSelectorException()
- {
- super();
- }
-
public ParseSelectorException( String s )
{
super( s );
@@ -74,7 +74,7 @@ public void testComplexCoreFields()
Assert.assertEquals( carter.getId(), _objectTree.get( "id" ) );
Assert.assertEquals( carter.getName(), _objectTree.get( "name" ) );
- List<Map<String,Object>> favoriteArtists = getList( "favoriteArtists" );
+ List<Map<String,Object>> favoriteArtists = getList( _objectTree, "favoriteArtists" );
Assert.assertEquals( 2, favoriteArtists.size() );
Map<String,Object> neutralMap = findItem( favoriteArtists, "name", "Neutral Milk Hotel" );
Assert.assertEquals( DataGenerator.neutralMilkHotel().getId(), neutralMap.get( "id" ) );
@@ -1,27 +0,0 @@
-package org.skyscreamer.yoga.selector;
-
-import org.junit.Test;
-
-/**
- * Tests for GDataSelector
- *
- * @author Carter Page <carter@skyscreamer.org>
- */
-public class GDataSelectorTest extends AbstractSelectorTest
-{
- private SelectorParser _selectorParser = new GDataSelectorParser();
-
- @Test
- public void testSimpleSelector() throws Exception
- {
- Selector selector = _selectorParser.parseSelector( "gender,country" );
- testSimpleSelector( selector );
- }
-
- @Test
- public void testNestedSelectors() throws Exception
- {
- Selector selector = _selectorParser.parseSelector( "gender,favoriteArtists(birthday,discography(year,title)),friends" );
- testNestedSelectors( selector );
- }
-}
@@ -1,27 +0,0 @@
-package org.skyscreamer.yoga.selector;
-
-import org.junit.Test;
-
-/**
- * Test for LinkedInSelector
- *
- * @author Carter Page <carter@skyscreamer.org>
- */
-public class LinkedInSelectorTest extends AbstractSelectorTest
-{
- private SelectorParser _selectorParser = new LinkedInSelectorParser();
-
- @Test
- public void testSimpleSelector() throws Exception
- {
- Selector selector = _selectorParser.parseSelector( ":(gender,country)" );
- testSimpleSelector( selector );
- }
-
- @Test
- public void testNestedSelectors() throws Exception
- {
- Selector selector = _selectorParser.parseSelector( ":(gender,favoriteArtists:(birthday,discography:(year,title)),friends)" );
- testNestedSelectors( selector );
- }
-}
@@ -0,0 +1,84 @@
+package org.skyscreamer.yoga.selector;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.skyscreamer.yoga.mapper.ResultTraverser;
+import org.skyscreamer.yoga.test.AbstractTraverserTest;
+import org.skyscreamer.yoga.test.data.DataGenerator;
+import org.skyscreamer.yoga.test.model.Artist;
+import org.skyscreamer.yoga.test.model.User;
+import org.springframework.core.io.ClassPathResource;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * User: corby
+ * Date: 5/8/12
+ */
+public class PropertyResolverTest extends AbstractTraverserTest
+{
+ @Test
+ // Populate a MapSelectorResolver with an aliased selector, and ensure it resolves correctly.
+ public void testMapSelectorResolver()
+ {
+ Map<String,String> definedSelectors = new HashMap<String, String>();
+ definedSelectors.put( "$suggestedAlbums", ":(favoriteArtists:(albums:(title,songs:(id,title))))" );
+ MapSelectorResolver mapSelectorResolver = new MapSelectorResolver();
+ mapSelectorResolver.setDefinedSelectors( definedSelectors );
+ setAliasSelectorResolver( mapSelectorResolver );
+
+ doTestResolver();
+ }
+
+ @Test
+ // Set up a DynamicPropertyResolver with the Properties file selectorAlias.properties, and ensure it
+ // resolves the selector correctly.
+ public void testDynamicPropertyResolver()
+ {
+ DynamicPropertyResolver dynamicPropertyResolver = new DynamicPropertyResolver();
+ ClassPathResource resource = new ClassPathResource( "selectorAlias.properties" );
+ dynamicPropertyResolver.setPropertyFile( resource );
+ setAliasSelectorResolver( dynamicPropertyResolver );
+
+ doTestResolver();
+ }
+
+ @Test (expected=ParseSelectorException.class)
+ public void testBadDynamicPropertyResolver() throws ParseSelectorException
+ {
+ DynamicPropertyResolver dynamicPropertyResolver = new DynamicPropertyResolver();
+ ClassPathResource resource = new ClassPathResource( "garbage" );
+ dynamicPropertyResolver.setPropertyFile( resource );
+ dynamicPropertyResolver.resolveSelector( "$suggestedAlbums" );
+ }
+
+ private void doTestResolver()
+ {
+ User corby = DataGenerator.corby();
+ Artist prince = DataGenerator.prince();
+ corby.getFavoriteArtists().add( prince );
+ prince.getAlbums().add( DataGenerator.nineteen99() );
+ prince.getAlbums().add( DataGenerator.diamondsAndPearls() );
+
+ ResultTraverser traverser = traverserNoEnrichers();
+ doTraverse( corby, "$suggestedAlbums", traverser, _simpleContext );
+ Assert.assertEquals( 3, _objectTree.size() );
+ List<Map<String,Object>> favoriteArtists = getList( _objectTree, "favoriteArtists" );
+ Assert.assertEquals( 1, favoriteArtists.size() );
+ Map<String,Object> princeMap = findItem( favoriteArtists, "name", "Prince" );
+
+ List<Map<String,Object>> albums = getList( princeMap, "albums" );
+ Assert.assertEquals( 2, albums.size() );
+ Map<String,Object> nineteen99Map = findItem( albums, "title", "1999" );
+
+ List<Map<String,Object>> songs = getList( nineteen99Map, "songs" );
+ Assert.assertEquals( 3, songs.size() );
+ Map<String,Object> marriedMap = findItem( songs, "id", 12 );
+
+ Assert.assertEquals( 2, marriedMap.size() );
+ Assert.assertEquals( "Let's Pretend We're Married", marriedMap.get( "title" ) );
+ }
+
+}
@@ -1,15 +1,43 @@
package org.skyscreamer.yoga.selector;
import org.junit.Assert;
+import org.junit.Test;
-/**
- * Shared test logic for selector tests
- *
- * @author Carter Page <carter@skyscreamer.org>
- */
-public abstract class AbstractSelectorTest
+public class SelectorParserTest
{
- protected void testSimpleSelector( Selector selector ) throws Exception
+ @Test
+ public void testGDataSimpleSelector() throws Exception
+ {
+ String selectorExpression = "gender,country";
+ Selector selector = new GDataSelectorParser().parseSelector( selectorExpression );
+ testSimpleSelector( selector );
+ }
+
+ @Test
+ public void testGDataNestedSelectors() throws Exception
+ {
+ String selectorExpression = "gender,favoriteArtists(birthday,discography(year,title)),friends";
+ Selector selector = new GDataSelectorParser().parseSelector( selectorExpression );
+ testNestedSelectors( selector );
+ }
+
+ @Test
+ public void testLinkedInSimpleSelector() throws Exception
+ {
+ String selectorExpression = ":(gender,country)";
+ Selector selector = new LinkedInSelectorParser().parseSelector( selectorExpression );
+ testSimpleSelector( selector );
+ }
+
+ @Test
+ public void testLinkedInNestedSelectors() throws Exception
+ {
+ String selectorExpression = ":(gender,favoriteArtists:(birthday,discography:(year,title)),friends)";
+ Selector selector = new LinkedInSelectorParser().parseSelector( selectorExpression );
+ testNestedSelectors( selector );
+ }
+
+ private void testSimpleSelector( Selector selector ) throws Exception
{
Assert.assertEquals( selector.getFields().size(), 2 );
Selector genderField = selector.getField( "gender" );
@@ -21,7 +49,7 @@ protected void testSimpleSelector( Selector selector ) throws Exception
Assert.assertEquals( countryField.getFields().size(), 0 );
}
- protected void testNestedSelectors( Selector selector ) throws Exception
+ private void testNestedSelectors( Selector selector ) throws Exception
{
Assert.assertEquals( selector.getFields().size(), 3 );
Selector genderField = selector.getField( "gender" );
@@ -8,6 +8,7 @@
import org.skyscreamer.yoga.model.MapHierarchicalModel;
import org.skyscreamer.yoga.populator.DefaultFieldPopulatorRegistry;
import org.skyscreamer.yoga.populator.FieldPopulator;
+import org.skyscreamer.yoga.selector.AliasSelectorResolver;
import org.skyscreamer.yoga.selector.LinkedInSelectorParser;
import org.skyscreamer.yoga.selector.ParseSelectorException;
import org.skyscreamer.yoga.selector.SelectorParser;
@@ -22,11 +23,13 @@
*/
public abstract class AbstractTraverserTest
{
- protected SelectorParser _selectorParser = new LinkedInSelectorParser();
- protected MapHierarchicalModel _model = new MapHierarchicalModel();
protected YogaRequestContext _simpleContext = new YogaRequestContext( "map", new DummyHttpServletResponse() );
+ protected MapHierarchicalModel _model = new MapHierarchicalModel();
protected Map<String, Object> _objectTree;
+ protected Class<? extends SelectorParser> _selectorParserClass = LinkedInSelectorParser.class;
+ protected AliasSelectorResolver _aliasSelectorResolver;
+
protected ResultTraverser traverserNoEnrichers()
{
return traverserWithEnrichers();
@@ -44,13 +47,24 @@ protected void doTraverse( Object instance, String selectorString, ResultTravers
{
try
{
- traverser.traverse( instance, _selectorParser.parseSelector( selectorString ), _model, context );
+ SelectorParser selectorParser = _selectorParserClass.newInstance();
+ selectorParser.setAliasSelectorResolver( _aliasSelectorResolver );
+
+ traverser.traverse( instance, selectorParser.parseSelector( selectorString ), _model, context );
_objectTree = _model.getObjectTree();
}
catch ( ParseSelectorException e )
{
Assert.fail( "Could not parse selector string " + selectorString );
}
+ catch ( InstantiationException e )
+ {
+ Assert.fail( "Could not instantiate " + _selectorParserClass );
+ }
+ catch ( IllegalAccessException e )
+ {
+ Assert.fail( "Could not instantiate " + _selectorParserClass );
+ }
}
protected void addFieldPopulators( ResultTraverser traverser, FieldPopulator... populators )
@@ -61,12 +75,12 @@ protected void addFieldPopulators( ResultTraverser traverser, FieldPopulator...
}
@SuppressWarnings( "unchecked" )
- protected List<Map<String, Object>> getList( String s )
+ protected List<Map<String, Object>> getList( Map<String,Object> map, String s )
{
- return (List<Map<String, Object>>) _objectTree.get( s );
+ return (List<Map<String, Object>>) map.get( s );
}
- protected Map<String, Object> findItem( List<Map<String, Object>> list, String key, String value )
+ protected Map<String, Object> findItem( List<Map<String, Object>> list, String key, Object value )
{
for ( Map<String, Object> item : list )
{
@@ -77,4 +91,14 @@ protected void addFieldPopulators( ResultTraverser traverser, FieldPopulator...
}
return null;
}
+
+ public void setSelectorParserClass( Class<? extends SelectorParser> selectorParserClass )
+ {
+ _selectorParserClass = selectorParserClass;
+ }
+
+ public void setAliasSelectorResolver( AliasSelectorResolver aliasSelectorResolver )
+ {
+ _aliasSelectorResolver = aliasSelectorResolver;
+ }
}
@@ -0,0 +1 @@
+$suggestedAlbums=:(favoriteArtists:(albums:(title,songs:(id,title))))

0 comments on commit c7defc4

Please sign in to comment.