Search in sources :

Example 1 with AttributesImpl

use of org.glassfish.security.services.impl.common.AttributesImpl in project Payara by payara.

the class AzResourceImplTest method testAddAttributesFromUriQuery.

@Test
public void testAddAttributesFromUriQuery() throws Exception {
    URI uri = new URI("admin:///tenants/tenant/zirka?locked=true");
    Attributes attributes = new AttributesImpl();
    Attribute attribute;
    Set<String> values;
    Iterator<String> iter;
    BitSet bitset;
    final boolean REPLACE = true;
    // Null
    try {
        addAttributesFromUriQuery(null, attributes, REPLACE);
        fail("Expected IllegalArgumentException from null URI.");
    } catch (IllegalArgumentException e) {
    }
    try {
        addAttributesFromUriQuery(uri, null, REPLACE);
        fail("Expected IllegalArgumentException from null Attributes.");
    } catch (IllegalArgumentException e) {
    }
    assertEquals("Empty attributes", 0, attributes.getAttributeCount());
    // No params
    uri = new URI("admin:///tenants/tenant/zirka");
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Empty attributes", 0, attributes.getAttributeCount());
    // 1 param
    uri = new URI("admin:///tenants/tenant/zirka?name1=value1");
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Attributes count", 1, attributes.getAttributeCount());
    assertNotNull("attribute", attribute = attributes.getAttribute("name1"));
    values = attribute.getValues();
    assertEquals("Values count", 1, values.size());
    iter = values.iterator();
    assertTrue(iter.hasNext());
    assertEquals("Values value", "value1", iter.next());
    assertFalse(iter.hasNext());
    // Repeat, no dup value
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Attributes count", 1, attributes.getAttributeCount());
    assertNotNull("attribute", attribute = attributes.getAttribute("name1"));
    values = attribute.getValues();
    assertEquals("Values count", 1, values.size());
    iter = values.iterator();
    assertTrue("iterator", iter.hasNext());
    assertEquals("Values value", "value1", iter.next());
    assertFalse("iterator", iter.hasNext());
    // New value
    uri = new URI("admin:///tenants/tenant/boris?name1=value2");
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Attributes count", 1, attributes.getAttributeCount());
    assertNotNull("attribute", attribute = attributes.getAttribute("name1"));
    values = attribute.getValues();
    assertEquals("Values count", 2, values.size());
    bitset = new BitSet(2);
    for (String v : values) {
        if ("value1".equals(v) && !bitset.get(0)) {
            bitset.set(0);
        } else if ("value2".equals(v) && !bitset.get(1)) {
            bitset.set(1);
        } else {
            fail("Unexpected attribute value " + v);
        }
    }
    // Replace attribute
    uri = new URI("admin:///tenants/tenant/lucky?name1=value3");
    addAttributesFromUriQuery(uri, attributes, REPLACE);
    assertEquals("Attributes count", 1, attributes.getAttributeCount());
    assertNotNull("attribute", attribute = attributes.getAttribute("name1"));
    values = attribute.getValues();
    assertEquals("Values count", 1, values.size());
    iter = values.iterator();
    assertTrue("iterator", iter.hasNext());
    assertEquals("Values value", "value3", iter.next());
    assertFalse("iterator", iter.hasNext());
    // New attribute
    uri = new URI("admin:///tenants/tenant/lucky?name2=value21&name2=value22");
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Attributes count", 2, attributes.getAttributeCount());
    assertNotNull("attribute", attributes.getAttribute("name1"));
    assertNotNull("attribute", attribute = attributes.getAttribute("name2"));
    values = attribute.getValues();
    assertEquals("Values count", 2, values.size());
    bitset = new BitSet(2);
    for (String v : values) {
        if ("value21".equals(v) && !bitset.get(0)) {
            bitset.set(0);
        } else if ("value22".equals(v) && !bitset.get(1)) {
            bitset.set(1);
        } else {
            fail("Unexpected attribute value " + v);
        }
    }
    // Encoded attribute
    attributes = new AttributesImpl();
    uri = new URI("admin:///tenants/tenant/lucky?na%3Dme2=val%26ue1&na%3Dme2=val%3Due2");
    addAttributesFromUriQuery(uri, attributes, !REPLACE);
    assertEquals("Attributes count", 1, attributes.getAttributeCount());
    assertNotNull("attribute", attribute = attributes.getAttribute("na=me2"));
    values = attribute.getValues();
    assertEquals("Values count", 2, values.size());
    bitset = new BitSet(2);
    for (String v : values) {
        if ("val&ue1".equals(v) && !bitset.get(0)) {
            bitset.set(0);
        } else if ("val=ue2".equals(v) && !bitset.get(1)) {
            bitset.set(1);
        } else {
            fail("Unexpected attribute value " + v);
        }
    }
}
Also used : AttributesImpl(org.glassfish.security.services.impl.common.AttributesImpl) Attribute(org.glassfish.security.services.api.common.Attribute) Attributes(org.glassfish.security.services.api.common.Attributes) BitSet(java.util.BitSet) URI(java.net.URI) AzResourceImpl.decodeURI(org.glassfish.security.services.impl.authorization.AzResourceImpl.decodeURI) Test(org.junit.Test)

Aggregations

URI (java.net.URI)1 BitSet (java.util.BitSet)1 Attribute (org.glassfish.security.services.api.common.Attribute)1 Attributes (org.glassfish.security.services.api.common.Attributes)1 AzResourceImpl.decodeURI (org.glassfish.security.services.impl.authorization.AzResourceImpl.decodeURI)1 AttributesImpl (org.glassfish.security.services.impl.common.AttributesImpl)1 Test (org.junit.Test)1