mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

boli
27.47.2006 919631050d15772fc49dbe3608e4086de7a8382e
Add test cases for the ModifyDN operation along with test cases for the LDAPFilter class.
3 files added
1094 ■■■■■ changed files
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestLDAPFilter.java 298 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyDNRequestProtocolOp.java 361 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyDNResponseProtocolOp.java 435 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestLDAPFilter.java
New file
@@ -0,0 +1,298 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.ldap;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertSame;
import org.opends.server.types.SearchFilter;
import org.opends.server.types.FilterType;
import org.opends.server.types.AttributeType;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.TestCaseUtils;
import java.util.ArrayList;
public class TestLDAPFilter extends LdapTestCase
{
  @BeforeTest
  public void setup() throws Exception
  {
    TestCaseUtils.startServer();
  }
  @DataProvider(name="badfilterstrings")
  public Object[][] getBadFilterStrings() throws Exception
  {
    return new Object[][]
    {
      { null, null },
      { "", null },
      { "=", null },
      { "()", null },
      { "(&(objectClass=*)(sn=s*s)", null },
      { "(dob>12221)", null },
      { "(cn=bob\\2 doe)", null },
      { "(cn=\\4j\\w2\\yu)", null },
      { "(cn=ds\\2)", null },
      { "(&(givenname=bob)|(sn=pep)dob=12))", null },
      { "(:=bob)", null },
      { "(=sally)", null },
      { "(cn=billy bob", null }
    };
  }
  @DataProvider(name="filterstrings")
  public Object[][] getFilterStrings() throws Exception
  {
    LDAPFilter equal = LDAPFilter.createEqualityFilter("objectClass",
                                        new ASN1OctetString("\\test*(Value)"));
    LDAPFilter equal2 = LDAPFilter.createEqualityFilter("objectClass",
                                                      new ASN1OctetString(""));
    LDAPFilter approx = LDAPFilter.createApproximateFilter("sn",
                                        new ASN1OctetString("\\test*(Value)"));
    LDAPFilter greater = LDAPFilter.createGreaterOrEqualFilter("employeeNumber",
                                        new ASN1OctetString("\\test*(Value)"));
    LDAPFilter less = LDAPFilter.createLessOrEqualFilter("dob",
                                        new ASN1OctetString("\\test*(Value)"));
    LDAPFilter presense = LDAPFilter.createPresenceFilter("login");
    ArrayList<ASN1OctetString> any = new ArrayList<ASN1OctetString>(0);
    ArrayList<ASN1OctetString> multiAny = new ArrayList<ASN1OctetString>(1);
    multiAny.add(new ASN1OctetString("\\wid*(get)"));
    multiAny.add(new ASN1OctetString("*"));
    LDAPFilter substring1 = LDAPFilter.createSubstringFilter("givenName",
                                                 new ASN1OctetString("\\Jo*()"),
                                                      any,
                                                 new ASN1OctetString("\\n*()"));
    LDAPFilter substring2 = LDAPFilter.createSubstringFilter("givenName",
                                                 new ASN1OctetString("\\Jo*()"),
                                                      multiAny,
                                                 new ASN1OctetString("\\n*()"));
    LDAPFilter substring3 = LDAPFilter.createSubstringFilter("givenName",
                                                      new ASN1OctetString(""),
                                                      any,
                                                 new ASN1OctetString("\\n*()"));
    LDAPFilter substring4 = LDAPFilter.createSubstringFilter("givenName",
                                                 new ASN1OctetString("\\Jo*()"),
                                                      any,
                                                      new ASN1OctetString(""));
    LDAPFilter substring5 = LDAPFilter.createSubstringFilter("givenName",
                                                      new ASN1OctetString(""),
                                                      multiAny,
                                                      new ASN1OctetString(""));
    LDAPFilter extensible1 = LDAPFilter.createExtensibleFilter("2.4.6.8.19",
                                                "cn",
                                           new ASN1OctetString("\\John* (Doe)"),
                                                false);
    LDAPFilter extensible2 = LDAPFilter.createExtensibleFilter("2.4.6.8.19",
                                                "cn",
                                           new ASN1OctetString("\\John* (Doe)"),
                                                true);
    LDAPFilter extensible3 = LDAPFilter.createExtensibleFilter("2.4.6.8.19",
                                                null,
                                           new ASN1OctetString("\\John* (Doe)"),
                                                true);
    LDAPFilter extensible4 = LDAPFilter.createExtensibleFilter(null,
                                                "cn",
                                           new ASN1OctetString("\\John* (Doe)"),
                                                true);
    LDAPFilter extensible5 = LDAPFilter.createExtensibleFilter("2.4.6.8.19",
                                                null,
                                           new ASN1OctetString("\\John* (Doe)"),
                                                false);
    LDAPFilter extensible6 = LDAPFilter.createExtensibleFilter(null,
                                                null,
                                           new ASN1OctetString(""),
                                                true);
    ArrayList<LDAPFilter> list1 = new ArrayList<LDAPFilter>();
    list1.add(equal);
    list1.add(approx);
    LDAPFilter and = LDAPFilter.createANDFilter(list1);
    ArrayList<LDAPFilter> list2 = new ArrayList<LDAPFilter>();
    list2.add(substring1);
    list2.add(extensible1);
    list2.add(and);
    return new Object[][]
    {
        { "(objectClass=\\5ctest\\2a\\28Value\\29)", equal },
        { "(objectClass=)", equal2 },
        { "(sn~=\\5ctest\\2a\\28Value\\29)", approx },
        { "(employeeNumber>=\\5ctest\\2a\\28Value\\29)", greater },
        { "(dob<=\\5ctest\\2a\\28Value\\29)", less },
        { "(login=*)", presense },
        { "(givenName=\\5cJo\\2a\\28\\29*\\5cn\\2a\\28\\29)", substring1 },
        { "(givenName=\\5cJo\\2a\\28\\29*\\5cwid\\2a\\28get\\29*\\2a*\\5cn\\2a\\28\\29)", substring2 },
        { "(givenName=*\\5cn\\2a\\28\\29)", substring3 },
        { "(givenName=\\5cJo\\2a\\28\\29*)", substring4 },
        { "(givenName=*\\5cwid\\2a\\28get\\29*\\2a*)", substring5 },
        { "(cn:2.4.6.8.19:=\\5cJohn\\2a \\28Doe\\29)", extensible1 },
        { "(cn:dn:2.4.6.8.19:=\\5cJohn\\2a \\28Doe\\29)", extensible2 },
        { "(:dn:2.4.6.8.19:=\\5cJohn\\2a \\28Doe\\29)", extensible3 },
        { "(cn:dn:=\\5cJohn\\2a \\28Doe\\29)", extensible4 },
        { "(:2.4.6.8.19:=\\5cJohn\\2a \\28Doe\\29)", extensible5 },
        { "(:dn:=)", extensible6 },
        { "(&(objectClass=\\5ctest\\2a\\28Value\\29)(sn~=\\5ctest\\2a\\28Value\\29))",
            LDAPFilter.createANDFilter(list1) },
        { "(|(objectClass=\\5ctest\\2a\\28Value\\29)(sn~=\\5ctest\\2a\\28Value\\29))",
            LDAPFilter.createORFilter(list1) },
        { "(!(objectClass=\\5ctest\\2a\\28Value\\29))", LDAPFilter.createNOTFilter(equal) },
        { "(|(givenName=\\5cJo\\2a\\28\\29*\\5cn\\2a\\28\\29)(cn:2.4.6.8.19:=\\5cJohn\\2a \\28Doe\\29)" +
            "(&(objectClass=\\5ctest\\2a\\28Value\\29)(sn~=\\5ctest\\2a\\28Value\\29)))",
            LDAPFilter.createORFilter(list2) }
    };
  }
  @Test(dataProvider = "filterstrings")
  public void testDecode(String filterStr, LDAPFilter filter) throws Exception
  {
    //LDAPFilter decodedFilter = LDAPFilter.decode(filterStr);
    //System.out.println(decodedFilter.);
    //System.out.println(filter.toString());
    LDAPFilter decoded = LDAPFilter.decode(filterStr);
    assertEquals(decoded.toString(), filter.toString());
    assertEquals(decoded.getAssertionValue(), filter.getAssertionValue());
    assertEquals(decoded.getAttributeType(), filter.getAttributeType());
    assertEquals(decoded.getDNAttributes(), filter.getDNAttributes());
    if(decoded.getFilterComponents() != null || filter.getFilterComponents() != null)
    {
      assertEquals(decoded.getFilterComponents().toString(), filter.getFilterComponents().toString());
    }
    assertEquals(decoded.getFilterType(), filter.getFilterType());
    assertEquals(decoded.getMatchingRuleID(), filter.getMatchingRuleID());
    if(decoded.getNOTComponent() != null || filter.getNOTComponent() != null)
    {
      assertEquals(decoded.getNOTComponent().toString(), filter.getNOTComponent().toString());
    }
    if(decoded.getSubAnyElements() != null && decoded.getSubAnyElements().size() > 0 ||
        filter.getSubAnyElements() != null && filter.getSubAnyElements().size() > 0)
    {
      assertEquals(decoded.getSubAnyElements(), filter.getSubAnyElements());
    }
    if(decoded.getSubFinalElement() != null && decoded.getSubFinalElement().stringValue() != "" ||
      filter.getSubFinalElement() != null && filter.getSubFinalElement().stringValue() != "")
    {
      assertEquals(decoded.getSubFinalElement(), filter.getSubFinalElement());
    }
    if(decoded.getSubInitialElement() != null && decoded.getSubInitialElement().stringValue() != "" ||
        filter.getSubInitialElement() != null && filter.getSubInitialElement().stringValue() != "")
    {
      assertEquals(decoded.getSubInitialElement(), filter.getSubInitialElement());
    }
  }
  @Test(dataProvider = "badfilterstrings", expectedExceptions = LDAPException.class)
  public void testDecodeException (String filterStr, LDAPFilter filter) throws Exception
  {
    LDAPFilter.decode(filterStr);
  }
  @Test
  public void testToSearchFilter() throws Exception
  {
    LDAPFilter filter = LDAPFilter.decode(
        "(&" +
          "(cn>=*)" +
          "(:1.2.3.4:=Bob)" +
          "(cn:=Jane)" +
          "(|" +
            "(sn<=gh*sh*sl)" +
            "(!(cn:dn:2.4.6.8.19:=Sally))" +
            "(cn~=blvd)" +
            "(cn=*)" +
          ")" +
          "(cn=*n)" +
          "(cn=n*)" +
          "(cn=n*n)" +
          "(:dn:=Sally)" +
          "(:dn:1.2.3.4:=Doe)" +
          "(cn:2.4.6.8.10:=)" +
        ")");
    SearchFilter searchFilter = filter.toSearchFilter();
    LDAPFilter newFilter = new LDAPFilter(searchFilter);
    assertEquals(filter.toString(), newFilter.toString());
  }
  @Test(dataProvider = "filterstrings")
  public void testEncodeDecode(String filterStr, LDAPFilter filter) throws Exception
  {
    assertEquals(LDAPFilter.decode(filter.encode()).toString(), filter.toString());
  }
  @Test
  public void testEncodeDecodeComplex() throws Exception
  {
    LDAPFilter filter = LDAPFilter.decode(
        "(&" +
          "(cn>=*)" +
          "(:1.2.3.4:=Bob)" +
          "(cn:=Jane)" +
          "(|" +
            "(sn<=gh*sh*sl)" +
            "(!(cn:dn:2.4.6.8.19:=Sally))" +
            "(cn~=blvd)" +
            "(cn=*)" +
          ")" +
          "(cn=*n)" +
          "(cn=n*)" +
          "(cn=n*n)" +
          "(:dn:=Sally)" +
          "(:dn:1.2.3.4:=Doe)" +
          "(cn:2.4.6.8.10:=)" +
        ")");
    assertEquals(LDAPFilter.decode(filter.encode()).toString(), filter.toString());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyDNRequestProtocolOp.java
New file
@@ -0,0 +1,361 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.ldap;
import org.opends.server.protocols.asn1.*;
import static org.opends.server.util.ServerConstants.EOL;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import java.util.ArrayList;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.ModifyDNRequestProtocolOp class.
 */
public class TestModifyDNRequestProtocolOp
{
  /**
   * The protocol op type for modify DN requests.
   */
  public static final byte OP_TYPE_MODIFY_DN_REQUEST = 0x6C;
  /**
   * The protocol op type for modify DN responses.
   */
  public static final byte OP_TYPE_MODIFY_DN_RESPONSE = 0x6D;
  /**
   * The DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString dn =
      new ASN1OctetString("dc=example,dc=com");
  /**
   * The alt DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString altDn =
      new ASN1OctetString("dc=alt,dc=example,dc=com");
  /**
   * The new DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString newRdn =
      new ASN1OctetString("dc=example-new");
  /**
   * The alt new DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString altNewRdn =
      new ASN1OctetString("ou=alt,dc=example-new");
  /**
   * The new superiour DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString newSuperiorDn =
      new ASN1OctetString("dc=widget,dc=com");
  /**
   * The alt new superiour DN for modify DN requests in this test case.
   */
  private static final ASN1OctetString altNewSuperiorDn =
      new ASN1OctetString("dc=alt,dc=widget,dc=com");
  /**
   * Test to make sure the class processes the right LDAP op type.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testOpType() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest = new ModifyDNRequestProtocolOp(dn,
                                                                            newRdn, true);
    assertEquals(modifyRequest.getType(), OP_TYPE_MODIFY_DN_REQUEST);
  }
  /**
   * Test to make sure the class returns the correct protocol name.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testProtocolOpName() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest = new ModifyDNRequestProtocolOp(dn,
                                                                            newRdn, true);
    assertEquals(modifyRequest.getProtocolOpName(), "Modify DN Request");
  }
  /**
   * Test the constructors to make sure the right objects are constructed.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testConstructors() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest;
    modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true);
    assertEquals(modifyRequest.getEntryDN(), dn);
    assertEquals(modifyRequest.getNewRDN(), newRdn);
    assertEquals(modifyRequest.deleteOldRDN(), true);
    assertNull(modifyRequest.getNewSuperior());
    modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, false,
                                                  newSuperiorDn);
    assertEquals(modifyRequest.getEntryDN(), dn);
    assertEquals(modifyRequest.getNewRDN(), newRdn);
    assertEquals(modifyRequest.getNewSuperior(), newSuperiorDn);
    assertEquals(modifyRequest.deleteOldRDN(), false);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest;
    modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true,
                                                  newSuperiorDn);
    modifyRequest.setEntryDN(altDn);
    assertEquals(modifyRequest.getEntryDN(), altDn);
    modifyRequest.setNewRDN(altNewRdn);
    assertEquals(modifyRequest.getNewRDN(), altNewRdn);
    modifyRequest.setNewSuperior(altNewSuperiorDn);
    assertEquals(modifyRequest.getNewSuperior(), altNewSuperiorDn);
    modifyRequest.setDeleteOldRDN(false);
    assertEquals(modifyRequest.deleteOldRDN(), false);
  }
  /**
   * Test the decode method when an null element is passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeNullElement() throws Exception
  {
    ModifyDNRequestProtocolOp.decodeModifyDNRequest(null);
  }
  /**
   * Test the decode method when an empty element is passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeEmptyElement() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>();
    ModifyDNRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_REQUEST,
                                                 elements));
  }
  /**
   * Test the decode method when the wrong number of elements is passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidElementNum() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    ModifyDNRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_REQUEST,
                                                 elements));
  }
  /**
   * Test the decode method when invalid attributes in element is passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidElement() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(3);
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    ModifyDNRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_REQUEST,
                                                 elements));
  }
  /**
   * Test the decode method when an element w/ wrong op type is passed.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeWrongElementType() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(3);
    elements.add(dn);
    elements.add(newRdn);
    elements.add(new ASN1Boolean(true));
    ModifyDNRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                 elements));
  }
  /**
   * Test the encode and decode methods with null params
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = Exception.class)
  public void testNullEncodeDecode() throws Exception
  {
    ModifyDNRequestProtocolOp modifyEncoded;
    ModifyDNRequestProtocolOp modifyDecoded;
    ASN1Element element;
    modifyEncoded = new ModifyDNRequestProtocolOp(null, null, true);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyDNRequestProtocolOp)ModifyDNRequestProtocolOp.decode(
        element);
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    ModifyDNRequestProtocolOp modifyEncoded;
    ModifyDNRequestProtocolOp modifyDecoded;
    ASN1Element element;
    modifyEncoded = new ModifyDNRequestProtocolOp(dn, newRdn, true,
                                                  newSuperiorDn);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyDNRequestProtocolOp)ModifyDNRequestProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getEntryDN(), modifyDecoded.getEntryDN());
    assertEquals(modifyEncoded.getNewRDN(), modifyDecoded.getNewRDN());
    assertEquals(modifyEncoded.getNewSuperior(), modifyDecoded.getNewSuperior());
    assertEquals(modifyEncoded.deleteOldRDN(), modifyDecoded.deleteOldRDN());
    modifyEncoded = new ModifyDNRequestProtocolOp(dn, newRdn, true);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyDNRequestProtocolOp)ModifyDNRequestProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getEntryDN(), modifyDecoded.getEntryDN());
    assertEquals(modifyEncoded.getNewRDN(), modifyDecoded.getNewRDN());
    assertEquals(modifyEncoded.getNewSuperior(), modifyDecoded.getNewSuperior());
    assertEquals(modifyEncoded.deleteOldRDN(), modifyDecoded.deleteOldRDN());
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true,
                                                  newSuperiorDn);
    modifyRequest.toString(buffer);
    key.append("ModifyDNRequest(dn="+dn+", newRDN="+newRdn+", " +
        "deleteOldRDN="+true+", newSuperior="+newSuperiorDn+")");
    assertEquals(buffer.toString(), key.toString());
  }
  /**
   * Test the toString (multi line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringMultiLine() throws Exception
  {
    ModifyDNRequestProtocolOp modifyRequest;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int indent;
    indent = 5;
    modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true,
                                                  newSuperiorDn);
    modifyRequest.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Modify DN Request");
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Entry DN:  ");
    dn.toString(key);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  New RDN:  ");
    key.append(newRdn);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Delete Old RDN:  ");
    key.append(true);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  New Superior:  ");
    key.append(newSuperiorDn);
    key.append(EOL);
    assertEquals(buffer.toString(), key.toString());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyDNResponseProtocolOp.java
New file
@@ -0,0 +1,435 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.ldap;
import org.opends.server.types.DN;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.RDN;
import org.opends.server.core.DirectoryServer;
import org.opends.server.protocols.asn1.*;
import static org.opends.server.util.ServerConstants.EOL;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.ModifyDNResponseProtocolOp class.
 */
public class TestModifyDNResponseProtocolOp
{
  /**
   * The protocol op type for modify DN requests.
   */
  public static final byte OP_TYPE_MODIFY_DN_REQUEST = 0x6C;
  /**
   * The protocol op type for modify DN responses.
   */
  public static final byte OP_TYPE_MODIFY_DN_RESPONSE = 0x6D;
  /**
   * The result code for compare result operations.
   */
  private static final int resultCode = 10;
  /**
   * The error message to use for compare result operations.
   */
  private static final String resultMsg = "Test Successful";
/**
   * The DN to use for compare result operations
   */
  private DN dn;
  @BeforeClass
  public void setupDN()
  {
    //Setup the DN to use in the response tests.
    AttributeType attribute =
        DirectoryServer.getDefaultAttributeType("testAttribute");
    AttributeValue attributeValue = new AttributeValue(attribute, "testValue");
    RDN rdn = new RDN(attribute, attributeValue);
    RDN[] rdns = {rdn};
    dn = new DN(rdns);
  }
  /**
   * Test to make sure the class processes the right LDAP op type.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testOpType() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse = new ModifyDNResponseProtocolOp(
        resultCode);
    assertEquals(modifyResponse.getType(), OP_TYPE_MODIFY_DN_RESPONSE);
  }
  /**
   * Test to make sure the class returns the correct protocol name.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testProtocolOpName() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse = new ModifyDNResponseProtocolOp(
        resultCode);
    assertEquals(modifyResponse.getProtocolOpName(), "Modify DN Response");
  }
  /**
   * Test the constructors to make sure the right objects are constructed.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testConstructors() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with result code param works.
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode);
    assertEquals(modifyResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code and error message
    //params works.
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode, resultMsg);
    assertEquals(modifyResponse.getErrorMessage(), resultMsg);
    assertEquals(modifyResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code, message, dn, and
    //referal params works.
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode, resultMsg, dn,
                                                    referralURLs);
    assertEquals(modifyResponse.getErrorMessage(), resultMsg);
    assertEquals(modifyResponse.getResultCode(), resultCode);
    assertEquals(modifyResponse.getMatchedDN(), dn);
    assertEquals(modifyResponse.getReferralURLs(), referralURLs);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse;
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode);
    modifyResponse.setResultCode(resultCode + 1);
    assertEquals(modifyResponse.getResultCode(), resultCode + 1);
    modifyResponse.setErrorMessage(resultMsg);
    assertEquals(modifyResponse.getErrorMessage(), resultMsg);
    modifyResponse.setMatchedDN(dn);
    assertEquals(modifyResponse.getMatchedDN(), dn);
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    modifyResponse.setReferralURLs(referralURLs);
    assertEquals(modifyResponse.getReferralURLs(), referralURLs);
  }
  /**
   * Test the decode method when an empty element is passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeEmptyElement() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>();
    ModifyDNResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                      elements));
  }
  /**
   * Test the decode method when an element with a invalid result code is
   * passed
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidResultCode() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1OctetString("Invalid Data"));
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    DeleteResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                     elements));
  }
  /**
   * Test the decode method when an element with a invalid dn is
   * passed. Never throws an exception as long as the element is not null.
   * This is the expected behavior.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  //@Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidDN() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1Enumerated(resultCode));
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    DeleteResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                     elements));
  }
  /**
   * Test the decode method when an element with a invalid result message is
   * passed. Never throws an exception as long as the element is not null.
   * This is the expected behavior.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  //@Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidResultMsg() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1Enumerated(resultCode));
    elements.add(new ASN1OctetString(dn.toString()));
    elements.add(new ASN1Null());
    DeleteResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                     elements));
  }
  /**
   * Test the decode method when an element with a invalid referral URL is
   * passed. Never throws an exception as long as the element is not null.
   * This is the expected behavior.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  //@Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidReferralURLs() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1Enumerated(resultCode));
    elements.add(new ASN1OctetString(dn.toString()));
    elements.add(new ASN1OctetString(resultMsg));
    elements.add(new ASN1Null());
    DeleteResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_DN_RESPONSE,
                                                     elements));
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    ModifyDNResponseProtocolOp deleteEncoded;
    ModifyDNResponseProtocolOp deleteDecoded;
    ASN1Element element;
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    //Test case for a full encode decode operation with normal params.
    deleteEncoded = new ModifyDNResponseProtocolOp(resultCode, resultMsg, dn,
                                                  referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (ModifyDNResponseProtocolOp)ModifyDNResponseProtocolOp.decode(
        element);
    assertEquals(deleteEncoded.getType(), OP_TYPE_MODIFY_DN_RESPONSE);
    assertEquals(deleteEncoded.getMatchedDN().compareTo(
        deleteDecoded.getMatchedDN()),
                 0);
    assertEquals(deleteEncoded.getErrorMessage(),
                 deleteDecoded.getErrorMessage());
    assertEquals(deleteEncoded.getResultCode(), deleteDecoded.getResultCode());
    assertEquals(deleteEncoded.getReferralURLs(),
                 deleteDecoded.getReferralURLs());
    //Test case for a full encode decode operation with an empty DN params.
    deleteEncoded = new ModifyDNResponseProtocolOp(resultCode, resultMsg, new DN(),
                                                  referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (ModifyDNResponseProtocolOp)ModifyDNResponseProtocolOp.decode(
        element);
    assertEquals(deleteDecoded.getMatchedDN(), null);
    //Test case for a full empty referral url param.
    ArrayList<String> emptyReferralURLs = new ArrayList<String>();
    deleteEncoded = new ModifyDNResponseProtocolOp(resultCode, resultMsg, dn,
                                                  emptyReferralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (ModifyDNResponseProtocolOp)ModifyDNResponseProtocolOp.decode(
        element);
    assertTrue(deleteDecoded.getReferralURLs() == null);
    //Test case for a full encode decode operation with resultCode param only.
    deleteEncoded = new ModifyDNResponseProtocolOp(resultCode);
    element = deleteEncoded.encode();
    deleteDecoded = (ModifyDNResponseProtocolOp)ModifyDNResponseProtocolOp.decode(
        element);
    assertEquals(deleteDecoded.getMatchedDN(), null);
    assertEquals(deleteDecoded.getErrorMessage(), null);
    assertEquals(deleteEncoded.getResultCode(), deleteDecoded.getResultCode());
    assertTrue(deleteDecoded.getReferralURLs() == null);
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode, resultMsg, dn,
                                                    referralURLs);
    modifyResponse.toString(buffer);
    key.append("ModifyDNResponse(resultCode="+resultCode+", " +
        "errorMessage="+resultMsg+", matchedDN="+dn.toString()+", " +
        "referralURLs={");
    Iterator<String> iterator = referralURLs.iterator();
      key.append(iterator.next());
    while (iterator.hasNext())
    {
      key.append(", ");
      key.append(iterator.next());
    }
    key.append("})");
    assertEquals(buffer.toString(), key.toString());
  }
  /**
   * Test the toString (multi line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringMultiLine() throws Exception
  {
    ModifyDNResponseProtocolOp modifyResponse;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    int indent = 5;
    int i;
    modifyResponse = new ModifyDNResponseProtocolOp(resultCode, resultMsg, dn,
                                                    referralURLs);
    modifyResponse.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Modify DN Response");
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Result Code:  ");
    key.append(resultCode);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Error Message:  ");
    key.append(resultMsg);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Matched DN:  ");
    key.append(dn.toString());
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Referral URLs:  ");
    key.append(EOL);
    for (String url : referralURLs)
    {
      key.append(indentBuf);
      key.append("  ");
      key.append(url);
      key.append(EOL);
    }
    assertEquals(buffer.toString(), key.toString());
  }
}