PluckNodeTransformer implements NodeTransformer { @Override public Node transform( Node node, List<String> args ) { checkNotNull( node ); checkNotNull( node.getProperty() ); Property property = node.getProperty(); if ( property.isCollection() ) { final String fieldName = ( args == null || args.isEmpty() ) ? null : StringUtils.defaultIfEmpty( args.get( 0 ), null ); final CollectionNode collectionNode = new CollectionNode( node.getName(), node.getUnorderedChildren().size() ); collectionNode.setNamespace( node.getNamespace() ); for ( final Node objectNode : node.getUnorderedChildren() ) { for ( final Node fieldNode : objectNode.getUnorderedChildren() ) { if ( fieldNode instanceof SimpleNode && ( fieldName == null || fieldName.equals( fieldNode.getName() ) ) ) { final SimpleNode childNode = new SimpleNode( fieldNode.getName(), ( (SimpleNode) fieldNode ).getValue() ); childNode.setProperty( collectionNode.getProperty() ); collectionNode.addChild( childNode ); break; } } } return collectionNode; } return node; }  @Override String name(); @Override Node transform( Node node, List<String> args );  }
@Test public void withoutArg() { Node result = transformer.transform( collectionNode, null ); Assert.assertTrue( result instanceof CollectionNode ); CollectionNode collection = (CollectionNode) result; Assert.assertEquals( "organisationUnits", collection.getName() ); Assert.assertEquals( "testUrn", collection.getNamespace() ); Assert.assertEquals( 2, collection.getUnorderedChildren().size() ); Assert.assertEquals( "id", collection.getUnorderedChildren().get( 0 ).getName() ); Assert.assertTrue( collection.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); Assert.assertEquals( "abc1", ( (SimpleNode) collection.getUnorderedChildren().get( 0 ) ).getValue() ); Assert.assertEquals( "id", collection.getUnorderedChildren().get( 1 ).getName() ); Assert.assertTrue( collection.getUnorderedChildren().get( 1 ) instanceof SimpleNode ); Assert.assertEquals( "abc2", ( (SimpleNode) collection.getUnorderedChildren().get( 1 ) ).getValue() ); }
@Test public void withArg() { Node result = transformer.transform( collectionNode, Collections.singletonList( "name" ) ); Assert.assertTrue( result instanceof CollectionNode ); CollectionNode collection = (CollectionNode) result; Assert.assertEquals( "organisationUnits", collection.getName() ); Assert.assertEquals( "testUrn", collection.getNamespace() ); Assert.assertEquals( 2, collection.getUnorderedChildren().size() ); Assert.assertEquals( "name", collection.getUnorderedChildren().get( 0 ).getName() ); Assert.assertTrue( collection.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); Assert.assertEquals( "OU 1", ( (SimpleNode) collection.getUnorderedChildren().get( 0 ) ).getValue() ); Assert.assertEquals( "name", collection.getUnorderedChildren().get( 1 ).getName() ); Assert.assertTrue( collection.getUnorderedChildren().get( 1 ) instanceof SimpleNode ); Assert.assertEquals( "OU 2", ( (SimpleNode) collection.getUnorderedChildren().get( 1 ) ).getValue() ); }