DefaultFieldFilterService implements FieldFilterService { @Override public ComplexNode toComplexNode( FieldFilterParams params ) { if ( params.getObjects().isEmpty() ) { return null; } Object object = params.getObjects().get( 0 ); CollectionNode collectionNode = toCollectionNode( object.getClass(), params ); if ( !collectionNode.getChildren().isEmpty() ) { return (ComplexNode) collectionNode.getChildren().get( 0 ); } return null; } DefaultFieldFilterService(
        FieldParser fieldParser,
        SchemaService schemaService,
        AclService aclService,
        CurrentUserService currentUserService,
        AttributeService attributeService,
        @Autowired( required = false ) Set<NodeTransformer> nodeTransformers ); @PostConstruct void init(); @Override ComplexNode toComplexNode( FieldFilterParams params ); @Override CollectionNode toCollectionNode( Class<?> wrapper, FieldFilterParams params );  }
@Test public void baseIdentifiableIdOnly() { final OrganisationUnit ou1 = new OrganisationUnit(); ou1.setUid( "abc1" ); final OrganisationUnit ou2 = new OrganisationUnit(); ou2.setUid( "abc2" ); final CategoryOption option = new CategoryOption(); option.setUid( "def1" ); option.getOrganisationUnits().add( ou1 ); option.getOrganisationUnits().add( ou2 ); final FieldFilterParams params = new FieldFilterParams( Collections.singletonList( option ), Arrays.asList( "id", "organisationUnits" ) ); final ComplexNode node = service.toComplexNode( params ); Assert.assertEquals( "categoryOption", node.getName() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "id" ) instanceof SimpleNode ); Assert.assertEquals( "def1", ( (SimpleNode) getNamedNode( node.getUnorderedChildren(), "id" ) ).getValue() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "organisationUnits" ) instanceof CollectionNode ); final CollectionNode collectionNode = (CollectionNode) getNamedNode( node.getUnorderedChildren(), "organisationUnits" ); Assert.assertEquals( 2, collectionNode.getUnorderedChildren().size() ); final List<String> ouIds = new ArrayList<>(); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 0 ) instanceof ComplexNode ); ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "organisationUnit", complexNode.getName() ); Assert.assertEquals( 1, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( complexNode.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); SimpleNode simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "id", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 1 ) instanceof ComplexNode ); complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 1 ); Assert.assertEquals( "organisationUnit", complexNode.getName() ); Assert.assertEquals( 1, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( complexNode.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "id", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertThat( ouIds, Matchers.containsInAnyOrder( "abc1", "abc2" ) ); }
@Test public void baseIdentifiableName() { final OrganisationUnit ou1 = new OrganisationUnit(); ou1.setUid( "abc1" ); ou1.setName( "OU 1" ); final OrganisationUnit ou2 = new OrganisationUnit(); ou2.setUid( "abc2" ); ou2.setName( "OU 2" ); final CategoryOption option = new CategoryOption(); option.setUid( "def1" ); option.getOrganisationUnits().add( ou1 ); option.getOrganisationUnits().add( ou2 ); final FieldFilterParams params = new FieldFilterParams( Collections.singletonList( option ), Arrays.asList( "id", "organisationUnits~pluck(name)[id,name]" ) ); final ComplexNode node = service.toComplexNode( params ); Assert.assertEquals( "categoryOption", node.getName() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "id" ) instanceof SimpleNode ); Assert.assertEquals( "def1", ( (SimpleNode) getNamedNode( node.getUnorderedChildren(), "id" ) ).getValue() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "organisationUnits" ) instanceof CollectionNode ); final CollectionNode collectionNode = (CollectionNode) getNamedNode( node.getUnorderedChildren(), "organisationUnits" ); Assert.assertEquals( 2, collectionNode.getUnorderedChildren().size() ); final List<String> ouIds = new ArrayList<>(); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); SimpleNode simpleNode = (SimpleNode) collectionNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "name", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 1 ) instanceof SimpleNode ); simpleNode = (SimpleNode) collectionNode.getUnorderedChildren().get( 1 ); Assert.assertEquals( "name", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertThat( ouIds, Matchers.containsInAnyOrder( "OU 1", "OU 2" ) ); }
@Test public void defaultClass() { final CategoryOption co1 = new CategoryOption(); co1.setUid( "abc1" ); final CategoryOption co2 = new CategoryOption(); co2.setUid( "abc2" ); co2.setName( "default" ); final CategoryOption co3 = new CategoryOption(); co3.setUid( "abc3" ); final Category category = new Category(); category.setUid( "def1" ); category.getCategoryOptions().add( co1 ); category.getCategoryOptions().add( co2 ); category.getCategoryOptions().add( co3 ); final FieldFilterParams params = new FieldFilterParams( Collections.singletonList( category ), Arrays.asList( "id", "categoryOptions" ) ); params.setDefaults( Defaults.EXCLUDE ); final ComplexNode node = service.toComplexNode( params ); Assert.assertEquals( "category", node.getName() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "id" ) instanceof SimpleNode ); Assert.assertEquals( "def1", ( (SimpleNode) getNamedNode( node.getUnorderedChildren(), "id" ) ).getValue() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "categoryOptions" ) instanceof CollectionNode ); final CollectionNode collectionNode = (CollectionNode) getNamedNode( node.getUnorderedChildren(), "categoryOptions" ); Assert.assertEquals( 2, collectionNode.getUnorderedChildren().size() ); final List<String> coIds = new ArrayList<>(); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 0 ) instanceof ComplexNode ); ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "categoryOption", complexNode.getName() ); Assert.assertEquals( 1, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( complexNode.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); SimpleNode simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "id", simpleNode.getName() ); coIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 1 ) instanceof ComplexNode ); complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 1 ); Assert.assertEquals( "categoryOption", complexNode.getName() ); Assert.assertEquals( 1, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( complexNode.getUnorderedChildren().get( 0 ) instanceof SimpleNode ); simpleNode = (SimpleNode) complexNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "id", simpleNode.getName() ); coIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertThat( coIds, Matchers.containsInAnyOrder( "abc1", "abc3" ) ); }
@Test public void baseIdentifiable() { final OrganisationUnit ou1 = new OrganisationUnit(); ou1.setUid( "abc1" ); ou1.setName( "Test 1" ); final OrganisationUnit ou2 = new OrganisationUnit(); ou2.setUid( "abc2" ); ou2.setName( "Test 2" ); final CategoryOption option = new CategoryOption(); option.setUid( "def1" ); option.getOrganisationUnits().add( ou1 ); option.getOrganisationUnits().add( ou2 ); final FieldFilterParams params = new FieldFilterParams( Collections.singletonList( option ), Arrays.asList( "id", "organisationUnits[id,name]" ) ); final ComplexNode node = service.toComplexNode( params ); Assert.assertEquals( "categoryOption", node.getName() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "id" ) instanceof SimpleNode ); Assert.assertEquals( "def1", ( (SimpleNode) getNamedNode( node.getUnorderedChildren(), "id" ) ).getValue() ); Assert.assertTrue( getNamedNode( node.getUnorderedChildren(), "organisationUnits" ) instanceof CollectionNode ); final CollectionNode collectionNode = (CollectionNode) getNamedNode( node.getUnorderedChildren(), "organisationUnits" ); Assert.assertEquals( 2, collectionNode.getUnorderedChildren().size() ); final List<String> ouIds = new ArrayList<>(); final List<String> ouNames = new ArrayList<>(); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 0 ) instanceof ComplexNode ); ComplexNode complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 0 ); Assert.assertEquals( "organisationUnit", complexNode.getName() ); Assert.assertEquals( 2, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( getNamedNode( complexNode.getUnorderedChildren(), "id" ) instanceof SimpleNode ); SimpleNode simpleNode = (SimpleNode) getNamedNode( complexNode.getUnorderedChildren(), "id" ); Assert.assertEquals( "id", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( getNamedNode( complexNode.getUnorderedChildren(), "name" ) instanceof SimpleNode ); simpleNode = (SimpleNode) getNamedNode( complexNode.getUnorderedChildren(), "name" ); Assert.assertEquals( "name", simpleNode.getName() ); ouNames.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( collectionNode.getUnorderedChildren().get( 1 ) instanceof ComplexNode ); complexNode = (ComplexNode) collectionNode.getUnorderedChildren().get( 1 ); Assert.assertEquals( "organisationUnit", complexNode.getName() ); Assert.assertEquals( 2, complexNode.getUnorderedChildren().size() ); Assert.assertTrue( getNamedNode( complexNode.getUnorderedChildren(), "id" ) instanceof SimpleNode ); simpleNode = (SimpleNode) getNamedNode( complexNode.getUnorderedChildren(), "id" ); Assert.assertEquals( "id", simpleNode.getName() ); ouIds.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertTrue( getNamedNode( complexNode.getUnorderedChildren(), "name" ) instanceof SimpleNode ); simpleNode = (SimpleNode) getNamedNode( complexNode.getUnorderedChildren(), "name" ); Assert.assertEquals( "name", simpleNode.getName() ); ouNames.add( String.valueOf( simpleNode.getValue() ) ); Assert.assertThat( ouIds, Matchers.containsInAnyOrder( "abc1", "abc2" ) ); Assert.assertThat( ouNames, Matchers.containsInAnyOrder( "Test 1", "Test 2" ) ); }