Permalink
Browse files

Added account key set with tests in new API.

  • Loading branch information...
srs committed Aug 13, 2012
1 parent e0435aa commit b175ed3e889e7025e5f03c25092828c68f5321fd
@@ -0,0 +1,121 @@
+package com.enonic.wem.api.account;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+
+public final class AccountKeySet
+ implements Iterable<AccountKey>
+{
+ private final ImmutableSet<AccountKey> set;
+
+ private AccountKeySet( final Iterable<AccountKey> refs )
+ {
+ this.set = ImmutableSet.copyOf( refs );
+ }
+
+ public int getSize()
+ {
+ return this.set.size();
+ }
+
+ public boolean isEmpty()
+ {
+ return this.set.isEmpty();
+ }
+
+ public AccountKeySet onlyUsers()
+ {
+ return filterTypes( AccountType.USER );
+ }
+
+ public AccountKeySet onlyGroups()
+ {
+ return filterTypes( AccountType.GROUP );
+ }
+
+ public AccountKeySet onlyRoles()
+ {
+ return filterTypes( AccountType.ROLE );
+ }
+
+ public AccountKeySet filterTypes( final AccountType... types )
+ {
+ final Collection<AccountKey> list = Collections2.filter( this.set, new TypePredicate( types ) );
+ return new AccountKeySet( list );
+ }
+
+ public boolean contains( final AccountKey ref )
+ {
+ return this.set.contains( ref );
+ }
+
+ public Set<AccountKey> getSet()
+ {
+ return this.set;
+ }
+
+ @Override
+ public Iterator<AccountKey> iterator()
+ {
+ return this.set.iterator();
+ }
+
+ public String toString()
+ {
+ return this.set.toString();
+ }
+
+ public static AccountKeySet from( final String... keys )
+ {
+ final Collection<String> list = Lists.newArrayList( keys );
+ final Collection<AccountKey> keyList = Collections2.transform( list, new ParseFunction() );
+ return new AccountKeySet( keyList );
+ }
+
+ public static AccountKeySet from( final AccountKey... keys )
+ {
+ final ArrayList<AccountKey> list = Lists.newArrayList( keys );
+ return new AccountKeySet( list );
+ }
+
+ public static AccountKeySet from( final Iterable<AccountKey> keys )
+ {
+ return new AccountKeySet( keys );
+ }
+
+ private final class TypePredicate
+ implements Predicate<AccountKey>
+ {
+ private final Set<AccountType> types;
+
+ public TypePredicate( final AccountType... types )
+ {
+ this.types = Sets.newHashSet( types );
+ }
+
+ @Override
+ public boolean apply( final AccountKey value )
+ {
+ return this.types.contains( value.getType() );
+ }
+ }
+
+ private final static class ParseFunction
+ implements Function<String, AccountKey>
+ {
+ @Override
+ public AccountKey apply( final String value )
+ {
+ return AccountKey.from( value );
+ }
+ }
+}
@@ -0,0 +1,142 @@
+package com.enonic.wem.api.account;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+import static org.junit.Assert.*;
+
+public class AccountKeySetTest
+{
+ @Test
+ public void testFrom_string()
+ {
+ final AccountKeySet set1 = AccountKeySet.from( "user:other:dummy" );
+ assertFalse( set1.isEmpty() );
+ assertEquals( 1, set1.getSize() );
+ assertFalse( set1.contains( AccountKey.anonymous() ) );
+ assertTrue( set1.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertEquals( "[user:other:dummy]", set1.toString() );
+
+ final AccountKeySet set2 = AccountKeySet.from( "user:other:dummy", "user:other:dummy" );
+ assertEquals( 1, set2.getSize() );
+ assertEquals( "[user:other:dummy]", set2.toString() );
+
+ final AccountKeySet set3 = AccountKeySet.from( "user:other:dummy", "user:other:you" );
+ assertEquals( 2, set3.getSize() );
+ assertEquals( "[user:other:dummy, user:other:you]", set3.toString() );
+ }
+
+ @Test
+ public void testFrom_key()
+ {
+ final AccountKeySet set = AccountKeySet.from( AccountKey.from( "user:other:dummy" ) );
+ assertFalse( set.isEmpty() );
+ assertEquals( 1, set.getSize() );
+ assertTrue( set.contains( AccountKey.from( "user:other:dummy" ) ) );
+ }
+
+ @Test
+ public void testFrom_iterator()
+ {
+ final Collection<AccountKey> list1 = Lists.newArrayList();
+ final AccountKeySet set1 = AccountKeySet.from( list1 );
+ assertTrue( set1.isEmpty() );
+ assertEquals( 0, set1.getSize() );
+
+ final Collection<AccountKey> list2 = Lists.newArrayList();
+ list2.add( AccountKey.from( "user:other:dummy" ) );
+
+ final AccountKeySet set2 = AccountKeySet.from( list2 );
+ assertFalse( set2.isEmpty() );
+ assertEquals( 1, set2.getSize() );
+ }
+
+ @Test
+ public void testIterator()
+ {
+ final AccountKeySet set = AccountKeySet.from( "user:other:dummy", "user:other:you" );
+ final Iterator<AccountKey> it = set.iterator();
+
+ assertNotNull( it );
+ assertTrue( it.hasNext() );
+ assertEquals( "user:other:dummy", it.next().toString() );
+ assertTrue( it.hasNext() );
+ assertEquals( "user:other:you", it.next().toString() );
+ assertFalse( it.hasNext() );
+ }
+
+ @Test
+ public void testGetSet()
+ {
+ final AccountKeySet set = AccountKeySet.from( "user:other:dummy", "user:other:you" );
+ final Set<AccountKey> other = set.getSet();
+
+ assertNotNull( other );
+ assertEquals( 2, other.size() );
+ assertTrue( set.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertTrue( set.contains( AccountKey.from( "user:other:you" ) ) );
+ }
+
+ @Test
+ public void testOnlyUsers()
+ {
+ final AccountKeySet set1 = AccountKeySet.from( "user:other:dummy", "group:other:party", "role:other:admin" );
+ assertEquals( 3, set1.getSize() );
+
+ final AccountKeySet set2 = set1.onlyUsers();
+ assertEquals( 1, set2.getSize() );
+ assertTrue( set2.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "group:other:party" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "role:other:admin" ) ) );
+ }
+
+ @Test
+ public void testOnlyGroups()
+ {
+ final AccountKeySet set1 = AccountKeySet.from( "user:other:dummy", "group:other:party", "role:other:admin" );
+ assertEquals( 3, set1.getSize() );
+
+ final AccountKeySet set2 = set1.onlyGroups();
+ assertEquals( 1, set2.getSize() );
+ assertFalse( set2.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertTrue( set2.contains( AccountKey.from( "group:other:party" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "role:other:admin" ) ) );
+ }
+
+ @Test
+ public void testOnlyRoles()
+ {
+ final AccountKeySet set1 = AccountKeySet.from( "user:other:dummy", "group:other:party", "role:other:admin" );
+ assertEquals( 3, set1.getSize() );
+
+ final AccountKeySet set2 = set1.onlyRoles();
+ assertEquals( 1, set2.getSize() );
+ assertFalse( set2.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "group:other:party" ) ) );
+ assertTrue( set2.contains( AccountKey.from( "role:other:admin" ) ) );
+ }
+
+ @Test
+ public void testFilterTypes()
+ {
+ final AccountKeySet set1 = AccountKeySet.from( "user:other:dummy", "group:other:party", "role:other:admin" );
+ assertEquals( 3, set1.getSize() );
+
+ final AccountKeySet set2 = set1.filterTypes();
+ assertEquals( 0, set2.getSize() );
+ assertFalse( set2.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "group:other:party" ) ) );
+ assertFalse( set2.contains( AccountKey.from( "role:other:admin" ) ) );
+
+ final AccountKeySet set3 = set1.filterTypes(AccountType.GROUP, AccountType.ROLE);
+ assertEquals( 2, set3.getSize() );
+ assertFalse( set3.contains( AccountKey.from( "user:other:dummy" ) ) );
+ assertTrue( set3.contains( AccountKey.from( "group:other:party" ) ) );
+ assertTrue( set3.contains( AccountKey.from( "role:other:admin" ) ) );
+ }
+}

0 comments on commit b175ed3

Please sign in to comment.