1057N/A/*
1057N/A * CDDL HEADER START
1057N/A *
1057N/A * The contents of this file are subject to the terms of the
1057N/A * Common Development and Distribution License, Version 1.0 only
1057N/A * (the "License"). You may not use this file except in compliance
1057N/A * with the License.
1057N/A *
1057N/A * You can obtain a copy of the license at
1057N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
1057N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
1057N/A * See the License for the specific language governing permissions
1057N/A * and limitations under the License.
1057N/A *
1057N/A * When distributing Covered Code, include this CDDL HEADER in each
1057N/A * file and include the License file at
1057N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
1057N/A * add the following below this CDDL HEADER, with the fields enclosed
1057N/A * by brackets "[]" replaced with your own identifying information:
1057N/A * Portions Copyright [yyyy] [name of copyright owner]
1057N/A *
1057N/A * CDDL HEADER END
1057N/A *
1057N/A *
4114N/A * Copyright 2008-2009 Sun Microsystems, Inc.
5357N/A * Portions Copyright 2011 ForgeRock AS
1057N/A */
1057N/Apackage org.opends.server.types;
1057N/A
1057N/A
1057N/A
3853N/Aimport static org.testng.Assert.*;
3853N/A
1057N/Aimport java.util.Collections;
1057N/Aimport java.util.LinkedHashSet;
1057N/A
3853N/Aimport org.opends.server.TestCaseUtils;
3853N/Aimport org.opends.server.admin.std.meta.VirtualAttributeCfgDefn.ConflictBehavior;
3853N/Aimport org.opends.server.extensions.EntryDNVirtualAttributeProvider;
1057N/Aimport org.testng.annotations.BeforeClass;
1057N/Aimport org.testng.annotations.Test;
1057N/A
1057N/A
1057N/A
1057N/A/**
1057N/A * This class provides a set of test cases for virtual attributes.
1057N/A */
1057N/Apublic class VirtualAttributeTestCase
1057N/A extends TypesTestCase
1057N/A{
1057N/A // The attribute type for the entryDN attribute.
1057N/A private AttributeType entryDNType;
1057N/A
1057N/A // The virtual attribute instance that will be used for all the testing.
1057N/A private VirtualAttribute virtualAttribute;
1057N/A
1057N/A // The virutal attribute rule that will be used for the testing.
1057N/A private VirtualAttributeRule virtualAttributeRule;
1057N/A
1057N/A
1057N/A
1057N/A /**
1057N/A * Ensures that the Directory Server is running.
1057N/A *
1057N/A * @throws Exception If an unexpected problem occurs.
1057N/A */
1057N/A @BeforeClass()
1057N/A public void startServer()
1057N/A throws Exception
1057N/A {
1057N/A TestCaseUtils.startServer();
1057N/A
1057N/A entryDNType = DirectoryConfig.getAttributeType("entrydn", false);
1057N/A assertNotNull(entryDNType);
1057N/A
1057N/A EntryDNVirtualAttributeProvider provider =
1057N/A new EntryDNVirtualAttributeProvider();
1057N/A
1057N/A virtualAttributeRule = new VirtualAttributeRule(entryDNType, provider,
1057N/A Collections.<DN>emptySet(),
5357N/A SearchScope.WHOLE_SUBTREE,
1057N/A Collections.<DN>emptySet(),
1057N/A Collections.<SearchFilter>emptySet(),
1057N/A ConflictBehavior.VIRTUAL_OVERRIDES_REAL);
1057N/A
1057N/A Entry entry = TestCaseUtils.makeEntry(
1057N/A "dn: o=test",
1057N/A "objectClass: top",
1057N/A "objectClass: organization",
1057N/A "o: test");
1057N/A
1057N/A virtualAttribute = new VirtualAttribute(entryDNType, entry,
1057N/A virtualAttributeRule);
1057N/A }
1057N/A
1057N/A
1057N/A
1057N/A /**
1057N/A * Tests the various getter methods for virtual attributes.
1057N/A *
1057N/A * @throws Exception If an unexpected problem occurs.
1057N/A */
1057N/A @Test()
1057N/A public void testGetters()
1057N/A throws Exception
1057N/A {
1057N/A assertEquals(virtualAttribute.getVirtualAttributeRule(),
1057N/A virtualAttributeRule);
1057N/A
1057N/A assertTrue(virtualAttribute.isVirtual());
1057N/A }
1057N/A
1057N/A
1057N/A
1057N/A /**
1057N/A * Tests the various methods that interact with the virtual values.
1057N/A *
1057N/A * @throws Exception If an unexpected problem occurs.
1057N/A */
1057N/A @Test()
1057N/A public void testValues()
1057N/A throws Exception
1057N/A {
3853N/A assertEquals(virtualAttribute.size(), 1);
4134N/A assertTrue(virtualAttribute.contains(AttributeValues.create(entryDNType, "o=test")));
1057N/A
3853N/A assertTrue(!virtualAttribute.isEmpty());
1057N/A
4134N/A assertTrue(virtualAttribute.contains(AttributeValues.create(entryDNType,
1057N/A "o=test")));
4134N/A assertFalse(virtualAttribute.contains(AttributeValues.create(entryDNType,
1057N/A "o=not test")));
1057N/A
1057N/A LinkedHashSet<AttributeValue> testValues =
1057N/A new LinkedHashSet<AttributeValue>();
4134N/A testValues.add(AttributeValues.create(entryDNType, "o=test"));
3853N/A assertTrue(virtualAttribute.containsAll(testValues));
1057N/A
4134N/A testValues.add(AttributeValues.create(entryDNType, "o=not test"));
3853N/A assertFalse(virtualAttribute.containsAll(testValues));
1057N/A }
1057N/A
1057N/A
1057N/A
1057N/A /**
1057N/A * Tests the various methods that apply to different kinds of matching.
1057N/A *
1057N/A * @throws Exception If an unexpected problem occurs.
1057N/A */
1057N/A @Test()
1057N/A public void testMatching()
1057N/A throws Exception
1057N/A {
1057N/A assertEquals(virtualAttribute.matchesSubstring(
4134N/A ByteString.valueOf("o="), null,
4134N/A ByteString.valueOf("test")),
1057N/A ConditionResult.UNDEFINED);
1057N/A
4134N/A AttributeValue assertionValue = AttributeValues.create(entryDNType, "o=test");
1057N/A assertEquals(virtualAttribute.greaterThanOrEqualTo(assertionValue),
1057N/A ConditionResult.UNDEFINED);
1057N/A assertEquals(virtualAttribute.lessThanOrEqualTo(assertionValue),
1057N/A ConditionResult.UNDEFINED);
1057N/A assertEquals(virtualAttribute.approximatelyEqualTo(assertionValue),
1057N/A ConditionResult.UNDEFINED);
1057N/A }
1057N/A
1057N/A
1057N/A
1057N/A /**
1057N/A * Tests the {@code toString} method.
1057N/A */
1057N/A @Test()
1057N/A public void testToString()
1057N/A {
1057N/A String vattrString = virtualAttribute.toString();
1057N/A assertNotNull(vattrString);
1057N/A assertTrue(vattrString.length() > 0);
1057N/A }
1057N/A}
1057N/A