Search in sources :

Example 16 with Preference

use of org.apache.qpid.server.model.preferences.Preference in project qpid-broker-j by apache.

the class RestUserPreferenceHandlerTest method testGetVisiblePreferencesByType.

@Test
public void testGetVisiblePreferencesByType() throws Exception {
    final String prefName = "testpref";
    final String prefType = "X-testtype";
    final RequestInfo rootRequestInfo = RequestInfo.createVisiblePreferencesRequestInfo(Collections.<String>emptyList(), Arrays.asList(prefType), Collections.<String, List<String>>emptyMap());
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            final Set<Preference> preferences = new HashSet<>();
            Map<String, Object> pref1Attributes = createPreferenceAttributes(null, null, prefType, prefName, null, MYUSER_SERIALIZATION, Collections.singleton(MYGROUP_SERIALIZATION), Collections.<String, Object>emptyMap());
            Preference p1 = PreferenceFactory.fromAttributes(_configuredObject, pref1Attributes);
            preferences.add(p1);
            Map<String, Object> pref2Attributes = createPreferenceAttributes(null, null, prefType, "testPref2", null, MYUSER_SERIALIZATION, Collections.<String>emptySet(), Collections.<String, Object>emptyMap());
            Preference p2 = PreferenceFactory.fromAttributes(_configuredObject, pref2Attributes);
            preferences.add(p2);
            awaitPreferenceFuture(_userPreferences.updateOrAppend(preferences));
            return null;
        }
    });
    Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2", MYGROUP);
    Subject.doAs(testSubject2, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            List<Map<String, Object>> preferences = (List<Map<String, Object>>) _handler.handleGET(_userPreferences, rootRequestInfo);
            assertEquals("Unexpected number of preferences", (long) 1, (long) preferences.size());
            assertEquals("Unexpected name of preferences", prefName, preferences.get(0).get(Preference.NAME_ATTRIBUTE));
            Set<Principal> visibilityList = (Set<Principal>) preferences.get(0).get(Preference.VISIBILITY_LIST_ATTRIBUTE);
            assertEquals("Unexpected number of principals in visibility list", (long) 1, (long) visibilityList.size());
            assertTrue("Unexpected principal in visibility list", GenericPrincipal.principalsEqual(_groupPrincipal, visibilityList.iterator().next()));
            assertTrue("Unexpected owner", GenericPrincipal.principalsEqual(_userPrincipal, (Principal) preferences.get(0).get(Preference.OWNER_ATTRIBUTE)));
            return null;
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Subject(javax.security.auth.Subject) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) GenericPrincipal(org.apache.qpid.server.model.preferences.GenericPrincipal) Principal(java.security.Principal) Test(org.junit.Test)

Example 17 with Preference

use of org.apache.qpid.server.model.preferences.Preference in project qpid-broker-j by apache.

the class RestUserPreferenceHandlerTest method testReplaceViaPutByTypeAndName.

@Test
public void testReplaceViaPutByTypeAndName() throws Exception {
    final String prefName = "myprefname";
    final RequestInfo requestInfo = RequestInfo.createPreferencesRequestInfo(Collections.<String>emptyList(), Arrays.asList("X-testtype", prefName));
    final Map<String, Object> pref = new HashMap<>();
    pref.put(Preference.VALUE_ATTRIBUTE, Collections.emptyMap());
    final Preference createdPreference = Subject.doAs(_subject, new PrivilegedAction<Preference>() {

        @Override
        public Preference run() {
            _handler.handlePUT(_configuredObject, requestInfo, pref);
            Set<Preference> preferences = awaitPreferenceFuture(_userPreferences.getPreferences());
            assertEquals("Unexpected number of preferences", (long) 1, (long) preferences.size());
            Preference prefModel = preferences.iterator().next();
            assertEquals("Unexpected preference name", prefName, prefModel.getName());
            return prefModel;
        }
    });
    final Map<String, Object> replacementPref = new HashMap<>();
    replacementPref.put(Preference.ID_ATTRIBUTE, createdPreference.getId().toString());
    replacementPref.put(Preference.VALUE_ATTRIBUTE, Collections.emptyMap());
    final String changedDescription = "Replace that maintains id";
    replacementPref.put(Preference.DESCRIPTION_ATTRIBUTE, changedDescription);
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            _handler.handlePUT(_configuredObject, requestInfo, replacementPref);
            Set<Preference> preferences = awaitPreferenceFuture(_userPreferences.getPreferences());
            assertEquals("Unexpected number of preferences after update", (long) 1, (long) preferences.size());
            Preference updatedPref = preferences.iterator().next();
            assertEquals("Unexpected preference id", createdPreference.getId(), updatedPref.getId());
            assertEquals("Unexpected preference name", prefName, updatedPref.getName());
            assertEquals("Unexpected preference description", changedDescription, updatedPref.getDescription());
            return null;
        }
    });
    replacementPref.remove(Preference.ID_ATTRIBUTE);
    final String changedDescription2 = "Replace that omits id";
    replacementPref.put(Preference.DESCRIPTION_ATTRIBUTE, changedDescription2);
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            _handler.handlePUT(_configuredObject, requestInfo, replacementPref);
            Set<Preference> preferences = awaitPreferenceFuture(_userPreferences.getPreferences());
            assertEquals("Unexpected number of preferences after update", (long) 1, (long) preferences.size());
            Preference updatedPref = preferences.iterator().next();
            assertFalse("Replace without id should create new id", createdPreference.getId().equals(updatedPref.getId()));
            assertEquals("Unexpected preference name", prefName, updatedPref.getName());
            assertEquals("Unexpected preference description", changedDescription2, updatedPref.getDescription());
            return null;
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Test(org.junit.Test)

Example 18 with Preference

use of org.apache.qpid.server.model.preferences.Preference in project qpid-broker-j by apache.

the class PreferencesTest method testSimpleRoundTrip.

@Test
public void testSimpleRoundTrip() {
    final Preference p = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, "X-TestPropType", "testProp1", null, TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    updateOrAppendAs(_testSubject, p);
    assertPreferences(_testSubject, p);
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Test(org.junit.Test)

Example 19 with Preference

use of org.apache.qpid.server.model.preferences.Preference in project qpid-broker-j by apache.

the class PreferencesTest method testUpdate.

@Test
public void testUpdate() {
    final Preference p1 = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, "X-testType", "propName", null, TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    final Preference p2 = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, p1.getId(), "X-testType", "newPropName", "newDescription", TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    updateOrAppendAs(_testSubject, p1);
    updateOrAppendAs(_testSubject, p2);
    assertPreferences(_testSubject, p2);
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Test(org.junit.Test)

Example 20 with Preference

use of org.apache.qpid.server.model.preferences.Preference in project qpid-broker-j by apache.

the class PreferencesTest method testReplaceByTypeAndName.

@Test
public void testReplaceByTypeAndName() {
    final String replaceType = "X-replaceType";
    final String unaffectedType = "X-unaffectedType";
    Subject testSubject2 = TestPrincipalUtils.createTestSubject(TEST_USERNAME2);
    final Preference unaffectedPreference = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, replaceType, "propName", null, TEST_PRINCIPAL2_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    updateOrAppendAs(testSubject2, unaffectedPreference);
    final Preference p1 = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, replaceType, "propName", null, TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    final Preference p1b = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, replaceType, "unaffectedPropName", null, TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    final Preference p2 = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, unaffectedType, "propName", null, TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    final Preference p3 = PreferenceFactory.fromAttributes(_testObject, PreferenceTestHelper.createPreferenceAttributes(null, null, replaceType, "propName", "new description", TEST_PRINCIPAL_SERIALIZATION, null, Collections.<String, Object>emptyMap()));
    updateOrAppendAs(_testSubject, p1, p1b, p2);
    Subject.doAs(_testSubject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            awaitPreferenceFuture(_testObject.getUserPreferences().replaceByTypeAndName(replaceType, "propName", p3));
            return null;
        }
    });
    assertPreferences(_testSubject, p1b, p2, p3);
    assertPreferences(testSubject2, unaffectedPreference);
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Aggregations

Preference (org.apache.qpid.server.model.preferences.Preference)40 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)39 Test (org.junit.Test)31 Subject (javax.security.auth.Subject)16 HashMap (java.util.HashMap)11 Map (java.util.Map)9 Set (java.util.Set)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)5 Date (java.util.Date)4 GroupPrincipal (org.apache.qpid.server.security.group.GroupPrincipal)4 Principal (java.security.Principal)3 UUID (java.util.UUID)3 GenericPrincipal (org.apache.qpid.server.model.preferences.GenericPrincipal)3 UserPreferencesImpl (org.apache.qpid.server.model.preferences.UserPreferencesImpl)3 LinkedHashSet (java.util.LinkedHashSet)2 CurrentThreadTaskExecutor (org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor)2 UserPreferences (org.apache.qpid.server.model.preferences.UserPreferences)2