diff --git a/src/main/java/org/sakaiproject/nakamura/api/lite/authorizable/Authorizable.java b/src/main/java/org/sakaiproject/nakamura/api/lite/authorizable/Authorizable.java index 8330eb5b..81fc71c7 100644 --- a/src/main/java/org/sakaiproject/nakamura/api/lite/authorizable/Authorizable.java +++ b/src/main/java/org/sakaiproject/nakamura/api/lite/authorizable/Authorizable.java @@ -272,9 +272,9 @@ public Object getProperty(String name) { * remove the property. * @param name */ - public void removeProperty(String name) { - if (!readOnly && authorizableMap.containsKey(name)) { - modifiedMap.put(name, new RemoveProperty()); + public void removeProperty(String key) { + if (!readOnly && (authorizableMap.containsKey(key) || modifiedMap.containsKey(key))) { + modifiedMap.put(key, new RemoveProperty()); } } diff --git a/src/test/java/org/sakaiproject/nakamura/api/lite/authorizable/AuthorizableTest.java b/src/test/java/org/sakaiproject/nakamura/api/lite/authorizable/AuthorizableTest.java new file mode 100644 index 00000000..d86a4c58 --- /dev/null +++ b/src/test/java/org/sakaiproject/nakamura/api/lite/authorizable/AuthorizableTest.java @@ -0,0 +1,92 @@ +package org.sakaiproject.nakamura.api.lite.authorizable; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class AuthorizableTest { + + protected Authorizable u; + + @Before + public void setup(){ + u = new User(new HashMap()); + } + + /** + * A non-anonymous user has the EVERYONE principal. + */ + @Test + public void testInitEmpty(){ + Assert.assertEquals(1, u.getPrincipals().length); + Assert.assertEquals(Group.EVERYONE, u.getPrincipals()[0]); + } + + /** + * The anonymous user has no principals. + */ + @Test + public void testInitAnonymous(){ + Map props = new HashMap(); + props.put(Authorizable.ID_FIELD, User.ANON_USER); + Authorizable a = new User(props); + Assert.assertEquals(0, a.getPrincipals().length); + } + + @Test + public void testInitPrincipals(){ + Map props = new HashMap(); + props.put(Authorizable.PRINCIPALS_FIELD, "principal1-managers;principal2"); + Authorizable a = new User(props); + // principal1-managers, principal2, Group.EVERYONE + Assert.assertEquals(3, a.getPrincipals().length); + } + + @Test + public void testHasProperty(){ + Assert.assertFalse(u.hasProperty("anykey")); + u.setProperty("anykey", "where's the any key?"); + Assert.assertTrue(u.hasProperty("anykey")); + } + + /** + * Reset the {@link Authorizable} properties with new ones. + * Old properties should no longer be present. + */ + @Test + public void testReset(){ + u.setProperty("anykey", "where's the any key?"); + + Map newProps = new HashMap(); + newProps.put("tab", "No time for that the computer's starting!"); + u.reset(newProps); + + Assert.assertFalse(u.hasProperty("anykey")); + Assert.assertTrue(u.hasProperty("tab")); + Assert.assertEquals(0, u.modifiedMap.size()); + } + + /** + * Reset the {@link Authorizable} properties with an empty {@link HashMap}. + * No properties should be present. + */ + @Test + public void testResetEmpty(){ + u.setProperty("anykey", "where's the any key?"); + u.reset(new HashMap()); + Assert.assertFalse(u.hasProperty("anykey")); + Assert.assertEquals(0, u.modifiedMap.size()); + } + + @Test + public void testRemoveProperty(){ + Authorizable u = new User(new HashMap()); + u.setProperty("anykey", "where's the any key?"); + Assert.assertTrue(u.hasProperty("anykey")); + u.removeProperty("anykey"); + Assert.assertFalse(u.hasProperty("anykey")); + } +} diff --git a/src/test/java/org/sakaiproject/nakamura/lite/authorizable/AuthorizableTest.java b/src/test/java/org/sakaiproject/nakamura/lite/authorizable/AuthorizableTest.java deleted file mode 100644 index 3990531b..00000000 --- a/src/test/java/org/sakaiproject/nakamura/lite/authorizable/AuthorizableTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.sakaiproject.nakamura.lite.authorizable; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; -import org.sakaiproject.nakamura.api.lite.authorizable.Authorizable; -import org.sakaiproject.nakamura.api.lite.authorizable.User; - -public class AuthorizableTest { - - @Test - public void testHasProperty(){ - Authorizable a = new User(new HashMap()); - Assert.assertFalse(a.hasProperty("anykey")); - - a.setProperty("anykey", "where's the any key?"); - Assert.assertTrue(a.hasProperty("anykey")); - } - - @Test - public void testReset(){ - Authorizable a = new User(new HashMap()); - a.setProperty("anykey", "where's the any key?"); - - Map newProps = new HashMap(); - newProps.put("tab", "No time for that the computer's starting!"); - a.reset(newProps); - - Assert.assertFalse(a.hasProperty("anykey")); - Assert.assertTrue(a.hasProperty("tab")); - } - - @Test - public void testResetEmpty(){ - Authorizable a = new User(new HashMap()); - a.setProperty("anykey", "where's the any key?"); - a.reset(new HashMap()); - Assert.assertFalse(a.hasProperty("anykey")); - } -}