### Question:
BasicObjectPermissionArgumentVoter implements AccessDecisionVoter<Object>, AfterInvocationProvider { protected Collection<ObjectPermission> getObjectPermissions(Object object) { List<ObjectPermission> permissions = new ArrayList<ObjectPermission>(); if (object instanceof ObjectPermission) { permissions.add((ObjectPermission) object); } else if (object instanceof Collection) { Collection collection = (Collection) object; if (!collection.isEmpty() && collection.iterator().next() instanceof ObjectPermission) { permissions.addAll(collection); } } else if (object instanceof MethodInvocation) { Object[] arguments = ((MethodInvocation) object).getArguments(); if (arguments != null) { for (Object argument : arguments) { permissions.addAll(getObjectPermissions(argument)); } } } return permissions; }  @Override boolean supports(Class clazz); @Override int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes); @Override Object decide(Authentication authentication, Object object, Collection<ConfigAttribute> attributes,
            Object returnedObject);  }### Answer:
@Test public void getObjectPermissions_singlePermission(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); final ObjectPermissionImpl objectPermission = new ObjectPermissionImpl(); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions(objectPermission); assertNotNull(objectPermissions); assertEquals(objectPermissions.size(), 1); assertSame(objectPermissions.iterator().next(), objectPermission); }
@Test public void getObjectPermissions_anotherType(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions("testString"); assertNotNull(objectPermissions); assertTrue(objectPermissions.isEmpty()); }
@Test public void getObjectPermissions_PermissionsCollection(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); List<ObjectPermission> permissionsCollection = new ArrayList<ObjectPermission>(); permissionsCollection.add(new ObjectPermissionImpl()); permissionsCollection.add(new ObjectPermissionImpl()); permissionsCollection.add(new ObjectPermissionImpl()); permissionsCollection.add(new ObjectPermissionImpl()); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions(permissionsCollection); assertNotNull(objectPermissions); assertEquals(objectPermissions.size(), permissionsCollection.size()); assertTrue(objectPermissions.containsAll(permissionsCollection)); }
@Test public void getObjectPermissions_PermissionsCollection_anotherType(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); List<String> permissionsCollection = new ArrayList<String>(); permissionsCollection.add("test1"); permissionsCollection.add("test2"); permissionsCollection.add("test3"); permissionsCollection.add("test4"); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions(permissionsCollection); assertNotNull(objectPermissions); assertTrue(objectPermissions.isEmpty()); }
@Test public void getObjectPermissions_methodInvocation(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); MethodInvocation methodInvocation = mock(MethodInvocation.class); final ObjectPermissionImpl expectedObject = new ObjectPermissionImpl(); when(methodInvocation.getArguments()).thenReturn(new Object[]{expectedObject}); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions(methodInvocation); assertNotNull(objectPermissions); assertEquals(objectPermissions.size(), 1); assertSame(objectPermissions.iterator().next(), expectedObject); }
@Test public void getObjectPermissions_methodInvocation_multipleArguments(){ when(voter.getObjectPermissions(any(Object.class))).thenCallRealMethod(); MethodInvocation methodInvocation = mock(MethodInvocation.class); final ObjectPermissionImpl expectedObject1 = new ObjectPermissionImpl(); final ObjectPermissionImpl expectedObject2 = new ObjectPermissionImpl(); final ObjectPermissionImpl expectedObject3 = new ObjectPermissionImpl(); when(methodInvocation.getArguments()).thenReturn(new Object[]{expectedObject1, expectedObject2, expectedObject3}); final Collection<ObjectPermission> objectPermissions = voter.getObjectPermissions(methodInvocation); assertNotNull(objectPermissions); assertEquals(objectPermissions.size(), 3); List<ObjectPermission> inputPermissions = new ArrayList<ObjectPermission>(); inputPermissions.add(expectedObject1); inputPermissions.add(expectedObject2); inputPermissions.add(expectedObject3); assertTrue(objectPermissions.containsAll(inputPermissions)); }