Skip to content

Commit

Permalink
Merge pull request #123 from milosimpson/bill
Browse files Browse the repository at this point in the history
Reusing Chainr instance produces incorrect transform
  • Loading branch information
Milo Simpson committed Apr 27, 2015
2 parents 4672266 + dc2e453 commit df03303
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public String evaluate( WalkedPath walkedPath ) {

@Override
public LiteralPathElement match( String dataKey, WalkedPath walkedPath ) {
return getRawKey().equals( dataKey ) ? this : null ;
if ( getRawKey().equals( dataKey ) ) {
return new LiteralPathElement( getRawKey(), subKeys );
}
return null;
}

@Override
Expand Down
53 changes: 52 additions & 1 deletion jolt-core/src/test/java/com/bazaarvoice/jolt/ChainrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.exception.TransformException;
import com.bazaarvoice.jolt.chainr.transforms.ExplodingTestTransform;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -65,7 +67,7 @@ private Map<String, Object> newCustomJavaActivity( Class cls, Object spec ) {
private List<Map<String,Object>> newCustomJavaChainrSpec( Class cls, Object delegateSpec )
{
List<Map<String,Object>> retvalue = this.newChainrSpec();
retvalue.add( newCustomJavaActivity( cls, delegateSpec ));
retvalue.add( newCustomJavaActivity( cls, delegateSpec ) );
return retvalue;
}

Expand Down Expand Up @@ -241,4 +243,53 @@ public void runTestCases(String testCaseName, boolean sorted ) throws IOExceptio
Assert.assertNull( orderErrorMessage, orderErrorMessage );
}
}




@Test
public void testReuseChainr() {
// Spec which moves "attributeMap"'s keys to a root "attributes" list.
Map<String,Object> specShift = JsonUtils.javason(
"{" +
"'operation':'shift'," +
"'spec' : { 'attributeMap' : { '*' : { '$' : 'attributes[#2]' } } }" +
"}"
);

List<Map<String, Object>> chainrSpec = ImmutableList.of( specShift );

// Create a single Chainr from the spec
Chainr chainr = Chainr.fromSpec(chainrSpec);

// Test input with three attributes
Map<String,Object> content = JsonUtils.javason(
"{ 'attributeMap' : { " +
"'attribute1' : 1, 'attribute2' : 2, 'attribute3' : 3 }" +
"}"
);

Object transformed = chainr.transform(content);

// First time everything checks out
Assert.assertTrue( transformed instanceof Map );
Map transformedMap = (Map) transformed;
Assert.assertEquals( transformedMap.get( "attributes" ), ImmutableList.of( "attribute1", "attribute2", "attribute3" ) );

// Create a new identical input
content = JsonUtils.javason(
"{ 'attributeMap' : { " +
"'attribute1' : 1, 'attribute2' : 2, 'attribute3' : 3 }" +
"}"
);

// Create a new transform from the same Chainr
transformed = chainr.transform(content);

Assert.assertTrue( transformed instanceof Map );
transformedMap = (Map) transformed;
// The following assert fails because attributes will have three leading null values:
// transformedMap["attributes"] == [null, null, null, "attribute1", "attribute2", "attribute3"]
Assert.assertEquals( transformedMap.get( "attributes" ), ImmutableList.of( "attribute1", "attribute2", "attribute3" ) );
}
}

0 comments on commit df03303

Please sign in to comment.