qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lqu...@apache.org
Subject svn commit: r1751929 [2/3] - in /qpid/java/trunk: broker-core/src/main/java/org/apache/qpid/server/model/ broker-core/src/main/java/org/apache/qpid/server/model/adapter/ broker-core/src/main/java/org/apache/qpid/server/model/preferences/ broker-core/sr...
Date Fri, 08 Jul 2016 15:52:31 GMT
Modified: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java (original)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java Fri Jul  8 15:52:31 2016
@@ -20,6 +20,8 @@
 
 package org.apache.qpid.server.model.testmodels.singleton;
 
+import static org.mockito.Mockito.mock;
+
 import java.security.Principal;
 import java.security.PrivilegedAction;
 import java.util.Collection;
@@ -38,14 +40,17 @@ import com.google.common.collect.Sets;
 import org.apache.qpid.server.model.ConfiguredObject;
 import org.apache.qpid.server.model.Model;
 import org.apache.qpid.server.model.preferences.Preference;
-import org.apache.qpid.server.model.preferences.PreferenceValue;
-import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
+import org.apache.qpid.server.model.preferences.PreferenceFactory;
+import org.apache.qpid.server.model.preferences.PreferenceTestHelper;
+import org.apache.qpid.server.model.preferences.UserPreferencesImpl;
 import org.apache.qpid.server.security.auth.TestPrincipalUtils;
-import org.apache.qpid.server.security.group.GroupPrincipal;
+import org.apache.qpid.server.store.preferences.PreferenceStore;
 import org.apache.qpid.test.utils.QpidTestCase;
 
 public class PreferencesTest extends QpidTestCase
 {
+    public static final String TEST_USERNAME = "testUser";
+    public static final String TEST_USERNAME2 = "testUser2";
     private final Model _model = TestModel.getInstance();
     private ConfiguredObject<?> _testObject;
     private Subject _testSubject;
@@ -58,131 +63,28 @@ public class PreferencesTest extends Qpi
         _testObject = _model.getObjectFactory()
                             .create(TestSingleton.class,
                                     Collections.<String, Object>singletonMap(ConfiguredObject.NAME, objectName));
-        _testSubject = TestPrincipalUtils.createTestSubject("testUser");
-    }
-
-    public void testCreatePreference()
-    {
-        final Map<String, Object> prefValueMap = Collections.<String, Object>singletonMap("myprefkey", "myprefvalue");
-        Preference p = _testObject.getUserPreferences()
-                                  .createPreference(null,
-                                                    "X-PREF1",
-                                                    "myprefname",
-                                                    "myprefdescription",
-                                                    Collections.<Principal>emptySet(), prefValueMap);
-        assertNotNull("Creation failed", p);
-        assertEquals("Unexpected preference name", "myprefname", p.getName());
-        assertEquals("Unexpected preference description", "myprefdescription", p.getDescription());
-        assertEquals("Unexpected preference visibility list", Collections.emptySet(), p.getVisibilityList());
-        assertNotNull("Preference creation date must not be null", p.getLastUpdatedDate());
-        final PreferenceValue preferenceValue = p.getValue();
-        assertNotNull("Preference value is null", preferenceValue);
-        assertEquals("Unexpected preference value", prefValueMap, preferenceValue.getAttributes());
-    }
-
-    public void testPreferenceNameIsMandatory()
-    {
-        final Map<String, Object> prefValueMap = Collections.emptyMap();
-        try
-        {
-            _testObject.getUserPreferences().createPreference(null,
-                                                              "X-PREF1",
-                                                              null,
-                                                              "myprefdescription",
-                                                              Collections.<Principal>emptySet(), prefValueMap);
-            fail("Preference name must not be null");
-        }
-        catch (IllegalArgumentException e)
-        {
-            // pass
-        }
-        try
-        {
-            _testObject.getUserPreferences().createPreference(null,
-                                                              "X-PREF1",
-                                                              "",
-                                                              "myprefdescription",
-                                                              Collections.<Principal>emptySet(), prefValueMap);
-            fail("Preference name must not be empty");
-        }
-        catch (IllegalArgumentException e)
-        {
-            // pass
-        }
-    }
-
-    public void testPreferenceHasUuid()
-    {
-        Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                          "X-TESTPREF",
-                                                                          "testProp1",
-                                                                          "",
-                                                                          null, Collections.<String, Object>emptyMap());
-        Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                          "X-TESTPREF",
-                                                                          "testProp2",
-                                                                          "",
-                                                                          Collections.<Principal>emptySet(),
-                                                                          Collections.<String, Object>emptyMap());
-        UUID id1 = p1.getId();
-        UUID id2 = p2.getId();
-        assertNotNull("preference id must not be null", id1);
-        assertNotNull("preference id must not be null", id2);
-        assertTrue("preference ids must be unique", !id1.equals(id2));
-    }
-
-    public void testPreferenceOwner()
-    {
-        Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                return _testObject.getUserPreferences().createPreference(null,
-                                                                         "X-TESTPREF",
-                                                                         "testProp1",
-                                                                         null,
-                                                                         null, Collections.<String, Object>emptyMap());
-            }
-        });
-        final Principal testPrincipal = _testSubject.getPrincipals(AuthenticatedPrincipal.class).iterator().next();
-        assertEquals("Unexpected preference owner", testPrincipal, p.getOwner());
-    }
-
-    public void testAssociatedObject()
-    {
-        Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                         "X-TESTPREF",
-                                                                         "testProp1",
-                                                                         null,
-                                                                         null, Collections.<String, Object>emptyMap());
-        assertEquals("Unexpected associated object", _testObject, p.getAssociatedObject());
-    }
-
-    public void testType()
-    {
-        final String type = "X-TESTPREF";
-        Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                         type,
-                                                                         "testProp1",
-                                                                         null,
-                                                                         null, Collections.<String, Object>emptyMap());
-        assertEquals("Unexpected type", type, p.getType());
+        _testObject.setUserPreferences(new UserPreferencesImpl(
+                mock(PreferenceStore.class), Collections.<Preference>emptySet()
+        ));
+        _testSubject = TestPrincipalUtils.createTestSubject(TEST_USERNAME);
     }
 
     public void testSimpleRoundTrip()
     {
+        final Preference p = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-TestPropType",
+                "testProp1",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                 "X-TestPropType",
-                                                                                 "testProp1",
-                                                                                 null,
-                                                                                 null,
-                                                                                 Collections.<String, Object>emptyMap());
                 Set<Preference> preferences = Collections.singleton(p);
                 _testObject.getUserPreferences().updateOrAppend(preferences);
                 assertEquals("roundtrip failed", preferences, _testObject.getUserPreferences().getPreferences());
@@ -193,22 +95,17 @@ public class PreferencesTest extends Qpi
 
     public void testOnlyAllowUpdateOwnedPreferences()
     {
-        final Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                return _testObject.getUserPreferences().createPreference(null,
-                                                                         "X-testType",
-                                                                         "prop1",
-                                                                         null,
-                                                                         null,
-                                                                         Collections.<String, Object>emptyMap());
-            }
-        });
+        final Preference p = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "prop1",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        Subject.doAs(TestPrincipalUtils.createTestSubject(TEST_USERNAME2), new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
@@ -230,81 +127,76 @@ public class PreferencesTest extends Qpi
 
     public void testGetOnlyOwnedPreferences()
     {
-        final Set<Preference> p1s = Subject.doAs(_testSubject, new PrivilegedAction<Set<Preference>>()
-        {
-            @Override
-            public Set<Preference> run()
-            {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                 "X-testType",
-                                                                                 "prop1",
-                                                                                 null,
-                                                                                 null,
-                                                                                 Collections.<String, Object>emptyMap());
-                Set<Preference> preferences = Collections.singleton(p);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return _testObject.getUserPreferences().getPreferences();
-            }
-        });
+        final Preference testUserPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        "X-testType",
+                        "prop1",
+                        null,
+                        TEST_USERNAME,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+
+        updateOrAppendAs(_testSubject, testUserPreference);
+
+        final Preference testUser2Preference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        "X-testType",
+                        "prop2",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
 
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        Subject.doAs(TestPrincipalUtils.createTestSubject(TEST_USERNAME2), new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                 "X-testType",
-                                                                                 "prop2",
-                                                                                 null,
-                                                                                 null,
-                                                                                 Collections.<String, Object>emptyMap());
-                Set<Preference> preferences = Collections.singleton(p);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
+                Set<Preference> preferences = Collections.singleton(testUser2Preference);
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(testUser2Preference));
                 Set<Preference> p2s = _testObject.getUserPreferences().getPreferences();
                 assertEquals("Unexpected preferences for subject 2", preferences, p2s);
                 return null;
             }
         });
 
-        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Set<Preference> preferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected preferences for subject 1", p1s, preferences);
-                return null;
-            }
-        });
+        assertSinglePreference(_testSubject, testUserPreference);
     }
 
     public void testUpdate()
     {
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                p1.getId(),
+                "X-testType",
+                "newPropName",
+                "newDescription",
+                TEST_USERNAME, null,
+                Collections.<String, Object>emptyMap()));
+
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                Preference p2 = _testObject.getUserPreferences().createPreference(p1.getId(),
-                                                                                  "X-testType",
-                                                                                  "newPropName",
-                                                                                  "newDescription",
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p2);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p1));
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p2));
 
-                preferences = _testObject.getUserPreferences().getPreferences();
+                Set<Preference> preferences = _testObject.getUserPreferences().getPreferences();
                 assertEquals("Unexpected number of preferences", 1, preferences.size());
                 Preference newPreference = preferences.iterator().next();
                 assertEquals("Unexpected preference id", p2.getId(), newPreference.getId());
@@ -318,30 +210,33 @@ public class PreferencesTest extends Qpi
 
     public void testProhibitTypeChange()
     {
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                p1.getId(),
+                "X-differentTestType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                Preference p2 = _testObject.getUserPreferences().createPreference(p1.getId(),
-                                                                                  "X-differentTestType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p2);
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p1));
                 try
                 {
-                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p2));
                     fail("Type change should not be allowed");
                 }
                 catch (IllegalArgumentException e)
@@ -355,30 +250,35 @@ public class PreferencesTest extends Qpi
 
     public void testProhibitDuplicateNamesOfSameType()
     {
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p2);
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p1));
+
                 try
                 {
-                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p2));
                     fail("Property with same name and same type should not be allowed");
                 }
                 catch (IllegalArgumentException e)
@@ -392,25 +292,31 @@ public class PreferencesTest extends Qpi
 
     public void testProhibitDuplicateNamesOfSameTypeInSameUpdate()
     {
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
                 Set<Preference> preferences = new HashSet<>();
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p1);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p2);
                 try
                 {
@@ -429,48 +335,46 @@ public class PreferencesTest extends Qpi
     public void testReplace()
     {
         final String preferenceType = "X-testType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        preferenceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "newPropName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "newPropName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p2);
-                _testObject.getUserPreferences().replace(preferences);
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p1));
+                _testObject.getUserPreferences().replace(Collections.singleton(p2));
 
                 Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
                 assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
@@ -480,56 +384,41 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testDelete()
     {
         final String preferenceType = "X-testType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        preferenceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(p1));
                 _testObject.getUserPreferences().replace(Collections.<Preference>emptySet());
 
                 Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
@@ -539,41 +428,45 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testDeleteByType()
     {
         final String preferenceType = "X-testType";
         final String unaffectedPreferenceType = "X-unaffectedType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        preferenceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                unaffectedPreferenceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
@@ -581,19 +474,7 @@ public class PreferencesTest extends Qpi
             public Void run()
             {
                 Set<Preference> preferences = new HashSet<>();
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p1);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  unaffectedPreferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p2);
                 _testObject.getUserPreferences().updateOrAppend(preferences);
 
@@ -606,40 +487,46 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testDeleteByTypeAndName()
     {
         final String preferenceType = "X-testType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        preferenceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                preferenceType,
+                "unaffectedPropName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
@@ -647,19 +534,7 @@ public class PreferencesTest extends Qpi
             public Void run()
             {
                 Set<Preference> preferences = new HashSet<>();
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p1);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  preferenceType,
-                                                                                  "unaffectedPropName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p2);
                 _testObject.getUserPreferences().updateOrAppend(preferences);
 
@@ -673,70 +548,64 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testReplaceByType()
     {
         final String replaceType = "X-replaceType";
         final String unaffectedType = "X-unaffectedType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
-
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        replaceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                replaceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                unaffectedType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p3 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                replaceType,
+                "newPropName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
                 Set<Preference> preferences = new HashSet<>();
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p1);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  unaffectedType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p2);
                 _testObject.getUserPreferences().updateOrAppend(preferences);
 
-                Preference p3 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "newPropName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences = Collections.singleton(p3);
                 _testObject.getUserPreferences().replaceByType(replaceType, preferences);
 
@@ -748,43 +617,65 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Preference of different user was replaced",
-                             unaffectedPreference,
-                             retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testReplaceByTypeAndName()
     {
         final String replaceType = "X-replaceType";
         final String unaffectedType = "X-unaffectedType";
-        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
-        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                Set<Preference> preferences;
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                preferences = Collections.singleton(p1);
-                _testObject.getUserPreferences().updateOrAppend(preferences);
-                return p1;
-            }
-        });
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
+        final Preference unaffectedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        replaceType,
+                        "propName",
+                        null,
+                        TEST_USERNAME2,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+
+        updateOrAppendAs(testSubject2, unaffectedPreference);
+
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                replaceType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+        final Preference p1b = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                replaceType,
+                "unaffectedPropName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
+        final Preference p2 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                unaffectedType,
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
+        final Preference p3 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                replaceType,
+                "propName",
+                "new description",
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
 
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
@@ -792,35 +683,11 @@ public class PreferencesTest extends Qpi
             public Void run()
             {
                 Set<Preference> preferences = new HashSet<>();
-                Preference p1 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p1);
-                Preference p1b = _testObject.getUserPreferences().createPreference(null,
-                                                                                   replaceType,
-                                                                                   "unaffectedPropName",
-                                                                                   null,
-                                                                                   null,
-                                                                                   Collections.<String, Object>emptyMap());
                 preferences.add(p1b);
-                Preference p2 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  unaffectedType,
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 preferences.add(p2);
                 _testObject.getUserPreferences().updateOrAppend(preferences);
 
-                Preference p3 = _testObject.getUserPreferences().createPreference(null,
-                                                                                  replaceType,
-                                                                                  "propName",
-                                                                                  "new description",
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
                 _testObject.getUserPreferences().replaceByTypeAndName(replaceType, "propName", p3);
 
                 Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
@@ -832,19 +699,7 @@ public class PreferencesTest extends Qpi
             }
         });
 
-        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
-        {
-            @Override
-            public Void run()
-            {
-                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
-                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
-                assertEquals("Preference of different user was replaced",
-                             unaffectedPreference,
-                             retrievedPreferences.iterator().next());
-                return null;
-            }
-        });
+        assertSinglePreference(testSubject2, unaffectedPreference);
     }
 
     public void testGetVisiblePreferences()
@@ -852,57 +707,72 @@ public class PreferencesTest extends Qpi
         final Principal testPrincipal = _testSubject.getPrincipals().iterator().next();
 
         Subject peerSubject = TestPrincipalUtils.createTestSubject("peer");
-        final Preference sharedPreference = Subject.doAs(peerSubject, new PrivilegedAction<Preference>()
+        final Preference sharedPreference = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName1",
+                "shared with colleague testUser",
+                "peer",
+                Collections.singleton(testPrincipal.toString()),
+                Collections.<String, Object>emptyMap()));
+
+        Subject.doAs(peerSubject, new PrivilegedAction<Void>()
         {
             @Override
-            public Preference run()
+            public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName1",
-                                                                                  "shared with colleague testUser",
-                                                                                  Collections.singleton(testPrincipal),
-                                                                                  Collections.<String, Object>emptyMap());
-                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(p));
-                return p;
+                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(sharedPreference));
+                return null;
             }
         });
 
         Subject anotherSubject = TestPrincipalUtils.createTestSubject("anotherUser");
-        final Preference notSharedPreference = Subject.doAs(anotherSubject, new PrivilegedAction<Preference>()
+        final Preference notSharedPreference = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName2",
+                null,
+                "anotherUser",
+                null,
+                Collections.<String, Object>emptyMap()));
+
+        Subject.doAs(anotherSubject, new PrivilegedAction<Void>()
         {
             @Override
-            public Preference run()
+            public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName2",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(p));
-                return p;
+                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(notSharedPreference));
+                return null;
             }
         });
 
+        final Preference p = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
+
         Subject.doAs(_testSubject, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
+
                 _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(p));
 
                 Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getVisiblePreferences();
                 assertEquals("Unexpected number of preferences", 2, retrievedPreferences.size());
-                assertTrue("Preference of my peer did not exist in visible set", retrievedPreferences.contains(sharedPreference));
+                assertTrue("Preference of my peer did not exist in visible set",
+                           retrievedPreferences.contains(sharedPreference));
                 assertTrue("My preference did not exist in visible set", retrievedPreferences.contains(p));
-                assertFalse("Preference of the other user unexpectedly exists in visible set", retrievedPreferences.contains(notSharedPreference));
+                assertFalse("Preference of the other user unexpectedly exists in visible set",
+                            retrievedPreferences.contains(notSharedPreference));
                 return null;
             }
         });
@@ -910,53 +780,55 @@ public class PreferencesTest extends Qpi
 
     public void testGetVisiblePreferencesSharedByGroup()
     {
-        Subject testSubjectWithGroup = TestPrincipalUtils.createTestSubject("testUser", "testGroup");
+        final String testGroupName = "testGroup";
+        Subject testSubjectWithGroup = TestPrincipalUtils.createTestSubject(TEST_USERNAME, testGroupName);
 
-        Principal tempGroupPrincipal = null;
-        for (Principal principal : testSubjectWithGroup.getPrincipals())
-        {
-            if (principal instanceof GroupPrincipal)
-            {
-                tempGroupPrincipal = principal;
-                break;
-            }
-        }
-        final Principal groupPrincipal = tempGroupPrincipal;
+        final Preference sharedPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        "X-testType",
+                        "propName1",
+                        null,
+                        "peer",
+                        Collections.singleton(testGroupName),
+                        Collections.<String, Object>emptyMap()));
 
         Subject peerSubject = TestPrincipalUtils.createTestSubject("peer");
-        final Preference sharedPreference = Subject.doAs(peerSubject, new PrivilegedAction<Preference>()
+        Subject.doAs(peerSubject, new PrivilegedAction<Void>()
         {
             @Override
-            public Preference run()
+            public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName1",
-                                                                                  null,
-                                                                                  Collections.singleton(groupPrincipal),
-                                                                                  Collections.<String, Object>emptyMap());
-                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(p));
-                return p;
+                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(sharedPreference));
+                return null;
             }
         });
 
+        final Preference testUserPreference =
+                PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                        null,
+                        null,
+                        "X-testType",
+                        "propName",
+                        null,
+                        TEST_USERNAME,
+                        null,
+                        Collections.<String, Object>emptyMap()));
+
         Subject.doAs(testSubjectWithGroup, new PrivilegedAction<Void>()
         {
             @Override
             public Void run()
             {
-                Preference p = _testObject.getUserPreferences().createPreference(null,
-                                                                                  "X-testType",
-                                                                                  "propName",
-                                                                                  null,
-                                                                                  null,
-                                                                                  Collections.<String, Object>emptyMap());
-                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(p));
+                _testObject.getUserPreferences().updateOrAppend(Sets.newHashSet(testUserPreference));
 
                 Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getVisiblePreferences();
                 assertEquals("Unexpected number of preferences", 2, retrievedPreferences.size());
-                assertTrue("Preference of my peer did not exist in visible set", retrievedPreferences.contains(sharedPreference));
-                assertTrue("My preference did not exist in visible set", retrievedPreferences.contains(p));
+                assertTrue("Preference of my peer did not exist in visible set",
+                           retrievedPreferences.contains(sharedPreference));
+                assertTrue("My preference did not exist in visible set",
+                           retrievedPreferences.contains(testUserPreference));
                 return null;
             }
         });
@@ -966,23 +838,21 @@ public class PreferencesTest extends Qpi
     {
         Date before = new Date();
         Thread.sleep(1);
-        final Preference p1 = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                return _testObject.getUserPreferences().createPreference(null,
-                                                                         "X-testType",
-                                                                         "propName1",
-                                                                         null,
-                                                                         null,
-                                                                         Collections.<String, Object>emptyMap());
-            }
-        });
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName1",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Thread.sleep(1);
         Date after = new Date();
         Date lastUpdatedDate = p1.getLastUpdatedDate();
-        assertTrue(String.format("Creation date is too early. Expected : after %s  Found : %s", before, lastUpdatedDate),
+        assertTrue(String.format("Creation date is too early. Expected : after %s  Found : %s",
+                                 before,
+                                 lastUpdatedDate),
                    before.before(lastUpdatedDate));
         assertTrue(String.format("Creation date is too late. Expected : after %s  Found : %s", after, lastUpdatedDate),
                    after.after(lastUpdatedDate));
@@ -990,19 +860,15 @@ public class PreferencesTest extends Qpi
 
     public void testLastUpdatedDateIsImmutable() throws Exception
     {
-        final Preference p1 = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
-        {
-            @Override
-            public Preference run()
-            {
-                return _testObject.getUserPreferences().createPreference(null,
-                                                                         "X-testType",
-                                                                         "propName1",
-                                                                         null,
-                                                                         null,
-                                                                         Collections.<String, Object>emptyMap());
-            }
-        });
+        final Preference p1 = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                null,
+                "X-testType",
+                "propName1",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap()));
         Date lastUpdatedDate = p1.getLastUpdatedDate();
         lastUpdatedDate.setTime(0);
         Date lastUpdatedDate2 = p1.getLastUpdatedDate();
@@ -1016,14 +882,16 @@ public class PreferencesTest extends Qpi
         final String type = "X-PREF1";
         final String name = "myprefname";
         final String description = "myprefdescription";
-        final Set<Principal> visibilitySet = Collections.<Principal>emptySet();
-        Preference p = _testObject.getUserPreferences()
-                                  .createPreference(uuid,
-                                                    type,
-                                                    name,
-                                                    description,
-                                                    visibilitySet,
-                                                    prefValueMap);
+        final Set<String> visibilitySet = Collections.emptySet();
+        Preference p = PreferenceFactory.recover(_testObject, PreferenceTestHelper.createPreferenceAttributes(
+                null,
+                uuid,
+                type,
+                name,
+                description,
+                TEST_USERNAME,
+                visibilitySet,
+                prefValueMap));
         assertNotNull("Creation failed", p);
         Date lastUpdatedDate = p.getLastUpdatedDate();
 
@@ -1032,11 +900,39 @@ public class PreferencesTest extends Qpi
         expectedAttributes.put("type", type);
         expectedAttributes.put("name", name);
         expectedAttributes.put("description", description);
-        expectedAttributes.put("owner", "");
+        expectedAttributes.put("owner", TEST_USERNAME);
         expectedAttributes.put("associatedObject", _testObject.getId());
         expectedAttributes.put("visibilityList", visibilitySet);
         expectedAttributes.put("lastUpdatedDate", lastUpdatedDate);
         expectedAttributes.put("value", prefValueMap);
         assertEquals("Unexpected preference attributes", expectedAttributes, p.getAttributes());
     }
+
+    private void updateOrAppendAs(final Subject testSubject, final Preference testUserPreference)
+    {
+        Subject.doAs(testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                _testObject.getUserPreferences().updateOrAppend(Collections.singleton(testUserPreference));
+                return null;
+            }
+        });
+    }
+
+    private void assertSinglePreference(final Subject subject, final Preference preference)
+    {
+        Subject.doAs(subject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", preference, retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
 }

Modified: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/TestSingletonImpl.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/TestSingletonImpl.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/TestSingletonImpl.java (original)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/TestSingletonImpl.java Fri Jul  8 15:52:31 2016
@@ -227,10 +227,4 @@ public class TestSingletonImpl extends A
     {
         return _attrWithDefaultFromContextMaterializeInit;
     }
-
-    @Override
-    protected PreferenceStore getPreferencesStore()
-    {
-        return _preferenceStore;
-    }
 }

Added: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/store/preferences/PreferencesRecovererTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/store/preferences/PreferencesRecovererTest.java?rev=1751929&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/store/preferences/PreferencesRecovererTest.java (added)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/store/preferences/PreferencesRecovererTest.java Fri Jul  8 15:52:31 2016
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.store.preferences;
+
+import static org.mockito.Mockito.mock;
+
+import java.security.PrivilegedAction;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.security.auth.Subject;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.server.model.Model;
+import org.apache.qpid.server.model.preferences.Preference;
+import org.apache.qpid.server.model.preferences.PreferenceTestHelper;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestCar;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestEngine;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestModel;
+import org.apache.qpid.server.security.auth.TestPrincipalUtils;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class PreferencesRecovererTest extends QpidTestCase
+{
+    public static final String TEST_USERNAME = "testUser";
+    private final Model _model = TestModel.getInstance();
+    private PreferenceStore _store;
+    private TestCar _testObject;
+    private ConfiguredObject<?> _testChildObject;
+    private Subject _testSubject;
+
+    @Override
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        _store = mock(PreferenceStore.class);
+        _testObject = _model.getObjectFactory()
+                            .create(TestCar.class,
+                                    Collections.<String, Object>singletonMap(ConfiguredObject.NAME, getTestName()));
+        _testChildObject = _testObject.createChild(TestEngine.class,
+                                                   Collections.<String, Object>singletonMap(ConfiguredObject.NAME, getTestName()));
+        _testSubject = TestPrincipalUtils.createTestSubject(TEST_USERNAME);
+    }
+
+    public void testRecoverEmptyPreferences() throws Exception
+    {
+        PreferencesRecoverer recoverer = new PreferencesRecoverer();
+        recoverer.recoverPreferences(_testObject, Collections.<PreferenceRecord>emptyList(), _store);
+        assertNotNull("Object should have UserPreferences", _testObject.getUserPreferences());
+        assertNotNull("Child object should have UserPreferences", _testChildObject.getUserPreferences());
+    }
+
+    public void testRecoverPreferences() throws Exception
+    {
+        PreferencesRecoverer recoverer = new PreferencesRecoverer();
+
+        final UUID p1Id = UUID.randomUUID();
+        Map<String, Object> pref1Attributes = PreferenceTestHelper.createPreferenceAttributes(
+                _testObject.getId(),
+                p1Id,
+                "X-testType",
+                "testPref1",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap());
+        PreferenceRecord record1 = new PreferenceRecordImpl(p1Id, pref1Attributes);
+        final UUID p2Id = UUID.randomUUID();
+        Map<String, Object> pref2Attributes = PreferenceTestHelper.createPreferenceAttributes(
+                _testChildObject.getId(),
+                p2Id,
+                "X-testType",
+                "testPref2",
+                null,
+                TEST_USERNAME,
+                null,
+                Collections.<String, Object>emptyMap());
+        PreferenceRecord record2 = new PreferenceRecordImpl(p2Id, pref2Attributes);
+        recoverer.recoverPreferences(_testObject, Arrays.asList(record1, record2), _store);
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, preferences.size());
+
+                Set<Preference> childPreferences = _testChildObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, childPreferences.size());
+                return null;
+            }
+        });
+    }
+
+}

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java Fri Jul  8 15:52:31 2016
@@ -701,13 +701,13 @@ public class RestServlet extends Abstrac
         if ("POST".equals(request.getMethod()))
         {
             Object providedObject = getRequestProvidedObject(request, requestInfo, Object.class);
-            restUserPreferenceHandler.handlePOST(userPreferences, requestInfo, providedObject);
+            restUserPreferenceHandler.handlePOST(target, requestInfo, providedObject);
         }
         else if ("PUT".equals(request.getMethod()))
         {
             Map<String, Object> providedObject = getRequestProvidedObject(request, requestInfo);
             final RestUserPreferenceHandler.ActionTaken actionTaken =
-                    restUserPreferenceHandler.handlePUT(userPreferences, requestInfo, providedObject);
+                    restUserPreferenceHandler.handlePUT(target, requestInfo, providedObject);
 
             switch(actionTaken)
             {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message