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

boli
21.04.2006 df0db46e678eee3f23ba8c1bbb9150787ee49f37
Added unit tests for some protocol.ldap classes. Removed messageToBindOperation method since it was NYI.
8 files added
4 files modified
3303 ■■■■■ changed files
opends/src/server/org/opends/server/protocols/ldap/AddRequestProtocolOp.java 31 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/CompareRequestProtocolOp.java 32 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/DeleteRequestProtocolOp.java 32 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/ModifyRequestProtocolOp.java 32 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddRequestProtocolOp.java 501 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddResponseProtocolOp.java 424 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestCompareRequestProtocolOp.java 321 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestCompareResponseProtocolOp.java 435 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestDeleteRequestProtocolOp.java 201 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestDeleteResponseProtocolOp.java 434 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyRequestProtocolOp.java 425 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyResponseProtocolOp.java 435 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/AddRequestProtocolOp.java
@@ -32,8 +32,6 @@
import java.util.Iterator;
import java.util.List;
import org.opends.server.core.AddOperation;
import org.opends.server.core.DirectoryException;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Sequence;
@@ -296,35 +294,6 @@
  /**
   * Converts the provided LDAP message containing an add request protocol op to
   * an <CODE>AddOperation</CODE> object that may be processed by the core
   * server.
   *
   * @param  requestMessage    The LDAP message containing the add request
   *                           protocol op.
   * @param  clientConnection  The client connection from which the request was
   *                           read.
   *
   * @return  The add operation created from the provided request message.
   *
   * @throws  DirectoryException  If the provided LDAP message cannot be decoded
   *                              as an add operation.
   */
  public static AddOperation messageToAddOperation(LDAPMessage requestMessage,
                                  LDAPClientConnection clientConnection)
         throws DirectoryException
  {
    assert debugEnter(CLASS_NAME, "messageToAddOperation",
                      String.valueOf(requestMessage),
                      String.valueOf(clientConnection));
    // NYI
    return null;
  }
  /**
   * Appends a string representation of this LDAP protocol op to the provided
   * buffer.
   *
opends/src/server/org/opends/server/protocols/ldap/CompareRequestProtocolOp.java
@@ -30,8 +30,6 @@
import java.util.ArrayList;
import org.opends.server.core.CompareOperation;
import org.opends.server.core.DirectoryException;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Sequence;
@@ -346,36 +344,6 @@
  /**
   * Converts the provided LDAP message containing a compare request protocol
   * op to a <CODE>CompareOperation</CODE> object that may be processed by the
   * core server.
   *
   * @param  requestMessage    The LDAP message containing the compare request
   *                           protocol op.
   * @param  clientConnection  The client connection from which the request was
   *                           read.
   *
   * @return  The compare operation created from the provided request message.
   *
   * @throws  DirectoryException  If the provided LDAP message cannot be decoded
   *                              as a compare operation.
   */
  public static CompareOperation messageToCompareOperation(
                                      LDAPMessage requestMessage,
                                      LDAPClientConnection clientConnection)
         throws DirectoryException
  {
    assert debugEnter(CLASS_NAME, "messageToCompareOperation",
                      String.valueOf(requestMessage),
                      String.valueOf(clientConnection));
    // NYI
    return null;
  }
  /**
   * Appends a string representation of this LDAP protocol op to the provided
   * buffer.
   *
opends/src/server/org/opends/server/protocols/ldap/DeleteRequestProtocolOp.java
@@ -28,8 +28,6 @@
import org.opends.server.core.DeleteOperation;
import org.opends.server.core.DirectoryException;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1OctetString;
@@ -181,36 +179,6 @@
  /**
   * Converts the provided LDAP message containing a delete request protocol op
   * to a <CODE>DeleteOperation</CODE> object that may be processed by the core
   * server.
   *
   * @param  requestMessage    The LDAP message containing the delete request
   *                           protocol op.
   * @param  clientConnection  The client connection from which the request was
   *                           read.
   *
   * @return  The delete operation created from the provided request message.
   *
   * @throws  DirectoryException  If the provided LDAP message cannot be decoded
   *                              as a delete operation.
   */
  public static DeleteOperation messageToDeleteOperation(
                                     LDAPMessage requestMessage,
                                     LDAPClientConnection clientConnection)
         throws DirectoryException
  {
    assert debugEnter(CLASS_NAME, "messageToDeleteOperation",
                      String.valueOf(requestMessage),
                      String.valueOf(clientConnection));
    // NYI
    return null;
  }
  /**
   * Appends a string representation of this LDAP protocol op to the provided
   * buffer.
   *
opends/src/server/org/opends/server/protocols/ldap/ModifyRequestProtocolOp.java
@@ -31,8 +31,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import org.opends.server.core.DirectoryException;
import org.opends.server.core.ModifyOperation;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Sequence;
@@ -296,36 +294,6 @@
  /**
   * Converts the provided LDAP message containing a modify request protocol op
   * to a <CODE>ModifyOperation</CODE> object that may be processed by the core
   * server.
   *
   * @param  requestMessage    The LDAP message containing the modify request
   *                           protocol op.
   * @param  clientConnection  The client connection from which the request was
   *                           read.
   *
   * @return  The modify operation created from the provided request message.
   *
   * @throws  DirectoryException  If the provided LDAP message cannot be decoded
   *                              as a modify operation.
   */
  public static ModifyOperation messageToModifyOperation(
                                     LDAPMessage requestMessage,
                                     LDAPClientConnection clientConnection)
         throws DirectoryException
  {
    assert debugEnter(CLASS_NAME, "messageToModifyOperation",
                      String.valueOf(requestMessage),
                      String.valueOf(clientConnection));
    // NYI
    return null;
  }
  /**
   * Appends a string representation of this LDAP protocol op to the provided
   * buffer.
   *
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddRequestProtocolOp.java
New file
@@ -0,0 +1,501 @@
/*
 * 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.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1Sequence;
import static org.opends.server.util.ServerConstants.EOL;
import org.opends.server.util.Base64;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.StringReader;
import static org.testng.Assert.*;
import org.testng.annotations.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.AddRequestProtocolOp class.
 */
public class TestAddRequestProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for add requests.
   */
  private static final byte OP_TYPE_ADD_REQUEST = 0x68;
  /**
   * The protocol op type for add responses.
   */
  private static final byte OP_TYPE_ADD_RESPONSE = 0x69;
  /**
   * The DN for add requests in this test case.
   */
  private static final ASN1OctetString dn =
      new ASN1OctetString("dc=example,dc=com");
  /**
   * The alternative DN for add requests in this test case.
   */
  private static final ASN1OctetString dnAlt =
      new ASN1OctetString("dc=sun,dc=com");
  /**
   * Generate attributes for use in test cases. Attributes will have names
   * like "testAttributeN" where N is the number of the attribute. Values will
   * have the value of "testValueN.K" where N is the attribute number and K
   * is the value number.
   *
   * @param numAttributes Number of attributes to generate. 0 will return
   *                      a empty list.
   * @param numValues     Number of values to assign to each attribute. 0 will
   *                      assign a empty list of values to the attribute.
   * @param prefix        String to prefix the attribute values
   * @return              The generate attributes.
   *
   */
  private ArrayList<LDAPAttribute> generateAttributes(int numAttributes,
                                                      int numValues,
                                                      String prefix)
  {
    ArrayList<LDAPAttribute> attributes = new ArrayList<LDAPAttribute>();
    LDAPAttribute attribute;
    ASN1OctetString value;
    int i, j;
    for(i = 0; i < numAttributes; i++)
    {
      ArrayList<ASN1OctetString> values = new ArrayList<ASN1OctetString>();
      for(j = 0; j < numValues; j++)
      {
        value = new ASN1OctetString(prefix + "Value"+i+"."+j);
        values.add(value);
      }
      attribute = new LDAPAttribute("testAttribute"+i, values);
      attributes.add(attribute);
    }
    return attributes;
  }
  private Boolean attributesEquals(List<LDAPAttribute> attributes1,
                                   List<LDAPAttribute> attributes2)
  {
    if(attributes1.size() != attributes2.size())
    {
      return false;
    }
    int i, j;
    LDAPAttribute attribute1;
    LDAPAttribute attribute2;
    ArrayList<ASN1OctetString> values1;
    ArrayList<ASN1OctetString> values2;
    for(i = 0; i < attributes1.size(); i++)
    {
      attribute1 = attributes1.get(i);
      attribute2 = attributes2.get(i);
      if(!attribute1.getAttributeType().equals(attribute2.getAttributeType()))
      {
        return false;
      }
      if(attribute1.getValues().size() != attribute2.getValues().size())
      {
        return false;
      }
      values1 = attribute1.getValues();
      values2 = attribute2.getValues();
      for(j = 0; j < values1.size(); j++)
      {
        if(!values1.get(j).equals(values2.get(j)))
        {
          return false;
        }
      }
    }
    return true;
  }
  /**
   * 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
  {
    AddRequestProtocolOp addRequest = new AddRequestProtocolOp(dn);
    assertEquals(addRequest.getType(), OP_TYPE_ADD_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
  {
    AddRequestProtocolOp addRequest = new AddRequestProtocolOp(dn);
    assertEquals(addRequest.getProtocolOpName(), "Add 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
  {
    AddRequestProtocolOp addRequest;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with dn param works.
    addRequest = new AddRequestProtocolOp(dn);
    assertEquals(addRequest.getDN(), dn);
    assertNotNull(addRequest.getAttributes());
    assertEquals(addRequest.getAttributes().size(), 0);
    //Test to make sure the constructor with dn and attribute params works.
    attributes = generateAttributes(10, 5, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    assertEquals(addRequest.getDN(), dn);
    assertEquals(addRequest.getAttributes(), attributes);
    //Test to make sure the constructor with dn and attribute params works with
    //null attributes.
    addRequest = new AddRequestProtocolOp(dn, null);
    assertEquals(addRequest.getDN(), dn);
    assertNotNull(addRequest.getAttributes());
    assertEquals(addRequest.getAttributes().size(), 0);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    AddRequestProtocolOp addRequest;
    addRequest = new AddRequestProtocolOp(dn);
    assertEquals(addRequest.getDN(), dn);
    addRequest.setDN(dnAlt);
    assertEquals(addRequest.getDN(), dnAlt);
  }
  /**
   * 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
  {
    AddRequestProtocolOp.decodeAddRequest(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>();
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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>(2);
    ArrayList<ASN1Element> attrElements =
         new ArrayList<ASN1Element>();
    elements.add(new ASN1Sequence(attrElements));
    elements.add(dn);
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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>(2);
    ArrayList<ASN1Element> attrElements =
         new ArrayList<ASN1Element>();
    elements.add(dn);
    elements.add(new ASN1Sequence(attrElements));
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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
  {
    AddRequestProtocolOp addEncoded;
    AddRequestProtocolOp addDecoded;
    ASN1Element element;
    addEncoded = new AddRequestProtocolOp(null, null);
    element = addEncoded.encode();
    addDecoded = (AddRequestProtocolOp)AddRequestProtocolOp.decode(element);
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    AddRequestProtocolOp addEncoded;
    AddRequestProtocolOp addDecoded;
    ASN1Element element;
    ArrayList<LDAPAttribute> attributes;
    //Test case for a full encode decode operation with normal params.
    attributes = generateAttributes(10,5, "test");
    addEncoded = new AddRequestProtocolOp(dn, attributes);
    element = addEncoded.encode();
    addDecoded = (AddRequestProtocolOp)AddRequestProtocolOp.decode(element);
    assertEquals(addEncoded.getType(), OP_TYPE_ADD_REQUEST);
    assertEquals(addEncoded.getDN(), addDecoded.getDN());
    assertTrue(attributesEquals(addEncoded.getAttributes(),
                                addDecoded.getAttributes()));
    //Test case for a full encode decode operation with large attributes.
    attributes = generateAttributes(100,50, "test");
    addEncoded = new AddRequestProtocolOp(dn, attributes);
    element = addEncoded.encode();
    addDecoded = (AddRequestProtocolOp)AddRequestProtocolOp.decode(element);
    assertEquals(addEncoded.getDN(), addDecoded.getDN());
    assertTrue(attributesEquals(addEncoded.getAttributes(),
                                addDecoded.getAttributes()));
    //Test case for a full encode decode operation with no attributes.
    addEncoded = new AddRequestProtocolOp(dn, null);
    element = addEncoded.encode();
    addDecoded = (AddRequestProtocolOp)AddRequestProtocolOp.decode(element);
    assertEquals(addEncoded.getDN(), addDecoded.getDN());
    assertTrue(attributesEquals(addEncoded.getAttributes(),
                                addDecoded.getAttributes()));
  }
  /**
   * Test toLDIF method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void testToLDIF() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<LDAPAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader;
    String line;
    int i, j;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toLDIF(buffer, 80);
    reader = new BufferedReader(new StringReader(buffer.toString()));
    line = reader.readLine();
    assertEquals(line, "dn: "+dn);
    for(i = 0; i < numAttributes; i++)
    {
      for(j = 0; j < numValues; j++)
      {
        line = reader.readLine();
        assertEquals(line, "testAttribute"+i+": "+"testValue"+i+"."+j);
      }
    }
  }
  /**
   * Test toLDIF method with values that need base64 encoding.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void testToLDIFBase64() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<LDAPAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader;
    String line;
    String expectedLine;
    int i, j;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, " test");
    ASN1OctetString dnNeedsBase64 =
      new ASN1OctetString("dc=example,dc=com ");
    addRequest = new AddRequestProtocolOp(dnNeedsBase64, attributes);
    addRequest.toLDIF(buffer, 80);
    reader = new BufferedReader(new StringReader(buffer.toString()));
    line = reader.readLine();
    assertEquals(line, "dn:: "+Base64.encode(dnNeedsBase64.value()));
    for(i = 0; i < numAttributes; i++)
    {
      for(j = 0; j < numValues; j++)
      {
        line = reader.readLine();
        expectedLine =  " testValue"+i+"."+j;
        assertEquals(line, "testAttribute"+i+":: "+Base64.encode(expectedLine.getBytes()));
      }
    }
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<LDAPAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toString(buffer);
    key.append("AddRequest(dn="+dn+", attrs={");
    for(i = 0; i < numAttributes; i++)
    {
      attributes.get(i).toString(key);
      if(i < numAttributes - 1)
      {
        key.append(", ");
      }
    }
    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
  {
    AddRequestProtocolOp addRequest;
    ArrayList<LDAPAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numAttributes, numValues, indent;
    numAttributes = 10;
    numValues = 5;
    indent = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Add Request");
    key.append(EOL);
    key.append(indentBuf);
    key.append("  DN:  ");
    dn.toString(key);
    key.append(EOL);
    key.append("  Attributes:");
    key.append(EOL);
    for (LDAPAttribute attribute : attributes)
    {
      attribute.toString(key, indent+4);
    }
    assertEquals(buffer.toString(), key.toString());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddResponseProtocolOp.java
New file
@@ -0,0 +1,424 @@
/*
 * 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.opends.server.types.DN;
import org.opends.server.types.RDN;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.core.DirectoryServer;
import java.util.ArrayList;
import java.util.Iterator;
import static org.testng.Assert.*;
import org.testng.annotations.*;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.AddResponseProtocolOp class.
 */
public class TestAddResponseProtocolOp
{
  /**
   * The protocol op type for add requests.
   */
  private static final byte OP_TYPE_ADD_REQUEST = 0x68;
  /**
   * The protocol op type for add responses.
   */
  private static final byte OP_TYPE_ADD_RESPONSE = 0x69;
  /**
   * The result code for add result operations.
   */
  private static final int resultCode = 10;
  /**
   * The error message to use for add result operations.
   */
  private static final String resultMsg = "Test Successful";
  /**
   * The DN to use for add 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
  {
    AddResponseProtocolOp addResponse = new AddResponseProtocolOp(resultCode);
    assertEquals(addResponse.getType(), OP_TYPE_ADD_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
  {
    AddResponseProtocolOp addResponse = new AddResponseProtocolOp(resultCode);
    assertEquals(addResponse.getProtocolOpName(), "Add 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
  {
    AddResponseProtocolOp addResponse;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with result code param works.
    addResponse = new AddResponseProtocolOp(resultCode);
    assertEquals(addResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code and error message
    //params works.
    addResponse = new AddResponseProtocolOp(resultCode, resultMsg);
    assertEquals(addResponse.getErrorMessage(), resultMsg);
    assertEquals(addResponse.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");
    addResponse = new AddResponseProtocolOp(resultCode, resultMsg, dn,
                                           referralURLs);
    assertEquals(addResponse.getErrorMessage(), resultMsg);
    assertEquals(addResponse.getResultCode(), resultCode);
    assertEquals(addResponse.getMatchedDN(), dn);
    assertEquals(addResponse.getReferralURLs(), referralURLs);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    AddResponseProtocolOp addResponse;
    addResponse = new AddResponseProtocolOp(resultCode);
    addResponse.setResultCode(resultCode + 1);
    assertEquals(addResponse.getResultCode(), resultCode + 1);
    addResponse.setErrorMessage(resultMsg);
    assertEquals(addResponse.getErrorMessage(), resultMsg);
    addResponse.setMatchedDN(dn);
    assertEquals(addResponse.getMatchedDN(), dn);
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    addResponse.setReferralURLs(referralURLs);
    assertEquals(addResponse.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>();
    AddResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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());
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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());
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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());
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_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());
    AddRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_ADD_RESPONSE,
                                                 elements));
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    AddResponseProtocolOp addEncoded;
    AddResponseProtocolOp addDecoded;
    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.
    addEncoded = new AddResponseProtocolOp(resultCode, resultMsg, dn,
                                           referralURLs);
    element = addEncoded.encode();
    addDecoded = (AddResponseProtocolOp)AddResponseProtocolOp.decode(element);
    assertEquals(addEncoded.getType(), OP_TYPE_ADD_RESPONSE);
    assertEquals(addEncoded.getMatchedDN().compareTo(addDecoded.getMatchedDN()),
                 0);
    assertEquals(addEncoded.getErrorMessage(), addDecoded.getErrorMessage());
    assertEquals(addEncoded.getResultCode(), addDecoded.getResultCode());
    assertEquals(addEncoded.getReferralURLs(), addDecoded.getReferralURLs());
    //Test case for a full encode decode operation with an empty DN params.
    addEncoded = new AddResponseProtocolOp(resultCode, resultMsg, new DN(),
                                           referralURLs);
    element = addEncoded.encode();
    addDecoded = (AddResponseProtocolOp)AddResponseProtocolOp.decode(element);
    assertEquals(addDecoded.getMatchedDN(), null);
    //Test case for a full empty referral url param.
    ArrayList<String> emptyReferralURLs = new ArrayList<String>();
    addEncoded = new AddResponseProtocolOp(resultCode, resultMsg, dn,
                                           emptyReferralURLs);
    element = addEncoded.encode();
    addDecoded = (AddResponseProtocolOp)AddResponseProtocolOp.decode(element);
    assertTrue(addDecoded.getReferralURLs() == null);
    //Test case for a full encode decode operation with resultCode param only.
    addEncoded = new AddResponseProtocolOp(resultCode);
    element = addEncoded.encode();
    addDecoded = (AddResponseProtocolOp)AddResponseProtocolOp.decode(element);
    assertEquals(addDecoded.getMatchedDN(), null);
    assertEquals(addDecoded.getErrorMessage(), null);
    assertEquals(addEncoded.getResultCode(), addDecoded.getResultCode());
    assertTrue(addDecoded.getReferralURLs() == null);
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    AddResponseProtocolOp addResponse;
    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");
    addResponse = new AddResponseProtocolOp(resultCode, resultMsg, dn,
                                           referralURLs);
    addResponse.toString(buffer);
    key.append("AddResponse(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
  {
    AddResponseProtocolOp addResponse;
    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;
    addResponse = new AddResponseProtocolOp(resultCode, resultMsg, dn,
                                           referralURLs);
    addResponse.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Add 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());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestCompareRequestProtocolOp.java
New file
@@ -0,0 +1,321 @@
/*
 * 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.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1Sequence;
import org.opends.server.protocols.asn1.ASN1Null;
import static org.opends.server.util.ServerConstants.EOL;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import java.util.ArrayList;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.CompareRequestProtocolOp class.
 */
public class TestCompareRequestProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for compare requests.
   */
  public static final byte OP_TYPE_COMPARE_REQUEST = 0x6E;
  /**
   * The protocol op type for compare responses.
   */
  public static final byte OP_TYPE_COMPARE_RESPONSE = 0x6F;
  /**
   * The DN for compare requests in this test case.
   */
  private static final ASN1OctetString dn =
      new ASN1OctetString("dc=example,dc=com");
  /**
   * The alternative DN for compare requests in this test case.
   */
  private static final ASN1OctetString dnAlt =
      new ASN1OctetString("dc=sun,dc=com");
  // The assertion value for this compare request.
  private ASN1OctetString assertionValue =
      new ASN1OctetString("=test");
  // The assertion value for this compare request.
  private ASN1OctetString assertionValueAlt =
      new ASN1OctetString("=testAlt");
  // The attribute type for this compare request.
  private String attributeType = "testAttribute";
  // The alternate attribute type for this compare request.
  private String attributeTypeAlt = "testAttributeAlt";
  /**
   * 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
  {
    CompareRequestProtocolOp compareRequest = new CompareRequestProtocolOp(dn,
      attributeType, assertionValue);
    assertEquals(compareRequest.getType(), OP_TYPE_COMPARE_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
  {
    CompareRequestProtocolOp compareRequest = new CompareRequestProtocolOp(dn,
      attributeType, assertionValue);
    assertEquals(compareRequest.getProtocolOpName(), "Compare 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
  {
    CompareRequestProtocolOp compareRequest;
    //Test to make sure the constructor with dn param works.
    compareRequest = new CompareRequestProtocolOp(dn, attributeType,
                                                  assertionValue);
    assertEquals(compareRequest.getDN(), dn);
    assertEquals(compareRequest.getAssertionValue(), assertionValue);
    assertEquals(compareRequest.getAttributeType(), attributeType);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    CompareRequestProtocolOp compareRequest;
    compareRequest = new CompareRequestProtocolOp(dn, attributeType,
                                                  assertionValue);
    assertEquals(compareRequest.getDN(), dn);
    compareRequest.setDN(dnAlt);
    assertEquals(compareRequest.getDN(), dnAlt);
    assertEquals(compareRequest.getAttributeType(), attributeType);
    compareRequest.setAttributeType(attributeTypeAlt);
    assertEquals(compareRequest.getAttributeType(), attributeTypeAlt);
    assertEquals(compareRequest.getAssertionValue(), assertionValue);
    compareRequest.setAssertionValue(assertionValueAlt);
    assertEquals(compareRequest.getAssertionValue(), assertionValueAlt);
  }
  /**
   * 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
  {
    CompareRequestProtocolOp.decodeCompareRequest(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>();
    CompareRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_COMPARE_REQUEST,
                                                 elements));
  }
  /**
   * Test the decode method when invalid elements is passed.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(expectedExceptions = LDAPException.class)
  public void testDecodeInvalidElement() throws Exception
  {
    ArrayList<ASN1Element> elements = new ArrayList<ASN1Element>(2);
    elements.add(new ASN1Null());
    elements.add(new ASN1Null());
    CompareRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_COMPARE_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>(2);
    elements.add(dn);
    elements.add(new ASN1Null());
    CompareRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_COMPARE_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
  {
    CompareRequestProtocolOp compareEncoded;
    CompareRequestProtocolOp compareDecoded;
    ASN1Element element;
    compareEncoded = new CompareRequestProtocolOp(null, null, null);
    element = compareEncoded.encode();
    compareDecoded = (CompareRequestProtocolOp)CompareRequestProtocolOp.decode(
        element);
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    CompareRequestProtocolOp compareEncoded;
    CompareRequestProtocolOp compareDecoded;
    ASN1Element element;
    ArrayList<LDAPAttribute> attributes;
    //Test case for a full encode decode operation with normal params.
    compareEncoded = new CompareRequestProtocolOp(dn, attributeType,
                                                  assertionValue);
    element = compareEncoded.encode();
    compareDecoded = (CompareRequestProtocolOp)CompareRequestProtocolOp.decode(
        element);
    assertEquals(compareEncoded.getType(), OP_TYPE_COMPARE_REQUEST);
    assertEquals(compareEncoded.getDN(), compareDecoded.getDN());
    assertEquals(compareEncoded.getAttributeType(),
                 compareDecoded.getAttributeType());
    assertEquals(compareEncoded.getAssertionValue(),
                 compareDecoded.getAssertionValue());
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    CompareRequestProtocolOp compareRequest;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    compareRequest = new CompareRequestProtocolOp(dn, attributeType,
                                                  assertionValue);
    compareRequest.toString(buffer);
    key.append("CompareRequest(dn="+dn+", attribute="+attributeType+", " +
        "value="+assertionValue+")");
    assertEquals(buffer.toString(), key.toString());
  }
  /**
   * Test the toString (multi line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringMultiLine() throws Exception
  {
    CompareRequestProtocolOp compareRequest;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int indent;
    indent = 5;
    compareRequest = new CompareRequestProtocolOp(dn, attributeType,
                                                  assertionValue);
    compareRequest.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Compare Request");
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Target DN:  ");
    dn.toString(key);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Attribute Type:  ");
    key.append(attributeType);
    key.append(EOL);
    key.append(indentBuf);
    key.append("  Assertion Value:");
    key.append(EOL);
    assertionValue.toString(key, indent+4);
    assertEquals(buffer.toString(), key.toString());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestCompareResponseProtocolOp.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.CompareRequestProtocolOp class.
 */
public class TestCompareResponseProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for compare requests.
   */
  public static final byte OP_TYPE_COMPARE_REQUEST = 0x6E;
  /**
   * The protocol op type for compare responses.
   */
  public static final byte OP_TYPE_COMPARE_RESPONSE = 0x6F;
  /**
   * 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
  {
    CompareResponseProtocolOp compareResponse = new CompareResponseProtocolOp(
        resultCode);
    assertEquals(compareResponse.getType(), OP_TYPE_COMPARE_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
  {
    CompareResponseProtocolOp compareResponse = new CompareResponseProtocolOp(
        resultCode);
    assertEquals(compareResponse.getProtocolOpName(), "Compare 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
  {
    CompareResponseProtocolOp compareResponse;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with result code param works.
    compareResponse = new CompareResponseProtocolOp(resultCode);
    assertEquals(compareResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code and error message
    //params works.
    compareResponse = new CompareResponseProtocolOp(resultCode, resultMsg);
    assertEquals(compareResponse.getErrorMessage(), resultMsg);
    assertEquals(compareResponse.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");
    compareResponse = new CompareResponseProtocolOp(resultCode, resultMsg, dn,
                                                  referralURLs);
    assertEquals(compareResponse.getErrorMessage(), resultMsg);
    assertEquals(compareResponse.getResultCode(), resultCode);
    assertEquals(compareResponse.getMatchedDN(), dn);
    assertEquals(compareResponse.getReferralURLs(), referralURLs);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    CompareResponseProtocolOp compareResponse;
    compareResponse = new CompareResponseProtocolOp(resultCode);
    compareResponse.setResultCode(resultCode + 1);
    assertEquals(compareResponse.getResultCode(), resultCode + 1);
    compareResponse.setErrorMessage(resultMsg);
    assertEquals(compareResponse.getErrorMessage(), resultMsg);
    compareResponse.setMatchedDN(dn);
    assertEquals(compareResponse.getMatchedDN(), dn);
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    compareResponse.setReferralURLs(referralURLs);
    assertEquals(compareResponse.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>();
    CompareResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_COMPARE_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_COMPARE_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_COMPARE_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_COMPARE_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_COMPARE_RESPONSE,
                                                    elements));
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    CompareResponseProtocolOp deleteEncoded;
    CompareResponseProtocolOp 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 CompareResponseProtocolOp(resultCode, resultMsg, dn,
                                                 referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (CompareResponseProtocolOp)CompareResponseProtocolOp.decode(
        element);
    assertEquals(deleteEncoded.getType(), OP_TYPE_COMPARE_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 CompareResponseProtocolOp(resultCode, resultMsg, new DN(),
                                                 referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (CompareResponseProtocolOp)CompareResponseProtocolOp.decode(
        element);
    assertEquals(deleteDecoded.getMatchedDN(), null);
    //Test case for a full empty referral url param.
    ArrayList<String> emptyReferralURLs = new ArrayList<String>();
    deleteEncoded = new CompareResponseProtocolOp(resultCode, resultMsg, dn,
                                                 emptyReferralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (CompareResponseProtocolOp)CompareResponseProtocolOp.decode(
        element);
    assertTrue(deleteDecoded.getReferralURLs() == null);
    //Test case for a full encode decode operation with resultCode param only.
    deleteEncoded = new CompareResponseProtocolOp(resultCode);
    element = deleteEncoded.encode();
    deleteDecoded = (CompareResponseProtocolOp)CompareResponseProtocolOp.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
  {
    CompareResponseProtocolOp compareResponse;
    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");
    compareResponse = new CompareResponseProtocolOp(resultCode, resultMsg, dn,
                                                  referralURLs);
    compareResponse.toString(buffer);
    key.append("CompareResponse(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
  {
    CompareResponseProtocolOp compareResponse;
    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;
    compareResponse = new CompareResponseProtocolOp(resultCode, resultMsg, dn,
                                                  referralURLs);
    compareResponse.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Compare 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());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestDeleteRequestProtocolOp.java
New file
@@ -0,0 +1,201 @@
/*
 * 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 static org.testng.Assert.*;
import org.testng.annotations.*;
import org.opends.server.protocols.asn1.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Element;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.DeleteRequestProtocolOp class.
 */
public class TestDeleteRequestProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for delete requests.
   */
  public static final byte OP_TYPE_DELETE_REQUEST = 0x4A;
  /**
   * The protocol op type for delete responses.
   */
  public static final byte OP_TYPE_DELETE_RESPONSE = 0x6B;
  /**
   * The DN for delete requests in this test case.
   */
  private static final ASN1OctetString dn =
      new ASN1OctetString("dc=example,dc=com");
  /**
   * The alternative DN for delete requests in this test case.
   */
  private static final ASN1OctetString dnAlt =
      new ASN1OctetString("dc=sun,dc=com");
  /**
   * Test the constructors to make sure the right objects are constructed.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testConstructors() throws Exception
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    assertEquals(deleteRequest.getDN(), dn);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    deleteRequest.setDN(dnAlt);;
    assertEquals(deleteRequest.getDN(), dnAlt);
  }
    /**
   * 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
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    assertEquals(deleteRequest.getType(), OP_TYPE_DELETE_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
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    assertEquals(deleteRequest.getProtocolOpName(), "Delete Request");
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    DeleteRequestProtocolOp deleteEncoded;
    DeleteRequestProtocolOp deleteDecoded;
    ASN1Element element;
    deleteEncoded = new DeleteRequestProtocolOp(dn);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteRequestProtocolOp)DeleteRequestProtocolOp.decode(
        element);
    assertEquals(element.getType(), OP_TYPE_DELETE_REQUEST);
    assertEquals(deleteDecoded.getDN(), deleteEncoded.getDN());
  }
  /**
   * 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
  {
    DeleteRequestProtocolOp deleteEncoded;
    DeleteRequestProtocolOp deleteDecoded;
    ASN1Element element;
    deleteEncoded = new DeleteRequestProtocolOp(null);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteRequestProtocolOp)DeleteRequestProtocolOp.decode(
        element);
  }
  /**
   * 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
  {
    DeleteRequestProtocolOp.decode(null);
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    StringBuilder buffer = new StringBuilder();
    String expectedStr = "DeleteRequest(dn=" + dn.toString() + ")";
    deleteRequest.toString(buffer);
    assertEquals(buffer.toString(), expectedStr);
  }
  /**
   * Test the toString (multi line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringMultiLine() throws Exception
  {
    DeleteRequestProtocolOp deleteRequest = new DeleteRequestProtocolOp(dn);
    StringBuilder buffer = new StringBuilder();
    String expectedStr = "   Delete Request\n     Entry DN:  " + dn.toString() +
        "\n";
    deleteRequest.toString(buffer, 3);
    assertEquals(buffer.toString(), expectedStr);
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestDeleteResponseProtocolOp.java
New file
@@ -0,0 +1,434 @@
/*
 * 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 static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.*;
import org.opends.server.protocols.asn1.*;
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 static org.opends.server.util.ServerConstants.EOL;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.DeleteResponseProtocolOp class.
 */
public class TestDeleteResponseProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for delete response.
   */
  public static final byte OP_TYPE_DELETE_REQUEST = 0x4A;
  /**
   * The protocol op type for delete responses.
   */
  public static final byte OP_TYPE_DELETE_RESPONSE = 0x6B;
  /**
   * The result code for delete result operations.
   */
  private static final int resultCode = 10;
  /**
   * The error message to use for delete result operations.
   */
  private static final String resultMsg = "Test Successful";
/**
   * The DN to use for delete 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
  {
    DeleteResponseProtocolOp deleteResponse = new DeleteResponseProtocolOp(
        resultCode);
    assertEquals(deleteResponse.getType(), OP_TYPE_DELETE_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
  {
    DeleteResponseProtocolOp deleteResponse = new DeleteResponseProtocolOp(
        resultCode);
    assertEquals(deleteResponse.getProtocolOpName(), "Delete 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
  {
    DeleteResponseProtocolOp deleteResponse;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with result code param works.
    deleteResponse = new DeleteResponseProtocolOp(resultCode);
    assertEquals(deleteResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code and error message
    //params works.
    deleteResponse = new DeleteResponseProtocolOp(resultCode, resultMsg);
    assertEquals(deleteResponse.getErrorMessage(), resultMsg);
    assertEquals(deleteResponse.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");
    deleteResponse = new DeleteResponseProtocolOp(resultCode, resultMsg, dn,
                                            referralURLs);
    assertEquals(deleteResponse.getErrorMessage(), resultMsg);
    assertEquals(deleteResponse.getResultCode(), resultCode);
    assertEquals(deleteResponse.getMatchedDN(), dn);
    assertEquals(deleteResponse.getReferralURLs(), referralURLs);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    DeleteResponseProtocolOp deleteResponse;
    deleteResponse = new DeleteResponseProtocolOp(resultCode);
    deleteResponse.setResultCode(resultCode + 1);
    assertEquals(deleteResponse.getResultCode(), resultCode + 1);
    deleteResponse.setErrorMessage(resultMsg);
    assertEquals(deleteResponse.getErrorMessage(), resultMsg);
    deleteResponse.setMatchedDN(dn);
    assertEquals(deleteResponse.getMatchedDN(), dn);
    ArrayList<String> referralURLs = new ArrayList<String>();
    referralURLs.add("ds1.example.com");
    referralURLs.add("ds2.example.com");
    referralURLs.add("ds3.example.com");
    deleteResponse.setReferralURLs(referralURLs);
    assertEquals(deleteResponse.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>();
    DeleteResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_DELETE_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_DELETE_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_DELETE_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_DELETE_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_DELETE_RESPONSE,
                                                 elements));
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    DeleteResponseProtocolOp deleteEncoded;
    DeleteResponseProtocolOp 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 DeleteResponseProtocolOp(resultCode, resultMsg, dn,
                                           referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteResponseProtocolOp)DeleteResponseProtocolOp.decode(
        element);
    assertEquals(deleteEncoded.getType(), OP_TYPE_DELETE_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 DeleteResponseProtocolOp(resultCode, resultMsg, new DN(),
                                           referralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteResponseProtocolOp)DeleteResponseProtocolOp.decode(
        element);
    assertEquals(deleteDecoded.getMatchedDN(), null);
    //Test case for a full empty referral url param.
    ArrayList<String> emptyReferralURLs = new ArrayList<String>();
    deleteEncoded = new DeleteResponseProtocolOp(resultCode, resultMsg, dn,
                                           emptyReferralURLs);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteResponseProtocolOp)DeleteResponseProtocolOp.decode(
        element);
    assertTrue(deleteDecoded.getReferralURLs() == null);
    //Test case for a full encode decode operation with resultCode param only.
    deleteEncoded = new DeleteResponseProtocolOp(resultCode);
    element = deleteEncoded.encode();
    deleteDecoded = (DeleteResponseProtocolOp)DeleteResponseProtocolOp.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
  {
    DeleteResponseProtocolOp deleteResponse;
    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");
    deleteResponse = new DeleteResponseProtocolOp(resultCode, resultMsg, dn,
                                            referralURLs);
    deleteResponse.toString(buffer);
    key.append("DeleteResponse(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
  {
    DeleteResponseProtocolOp deleteResponse;
    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;
    deleteResponse = new DeleteResponseProtocolOp(resultCode, resultMsg, dn,
                                            referralURLs);
    deleteResponse.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Delete 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());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyRequestProtocolOp.java
New file
@@ -0,0 +1,425 @@
/*
 * 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.ASN1OctetString;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.asn1.ASN1Sequence;
import static org.opends.server.util.ServerConstants.EOL;
import org.opends.server.types.ModificationType;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Random;
/**
 * This class defines a set of tests for the
 * org.opends.server.protocol.ldap.ModifyRequestProtocolOp class.
 */
public class TestModifyRequestProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for modify requests.
   */
  public static final byte OP_TYPE_MODIFY_REQUEST = 0x66;
  /**
   * The protocol op type for modify responses.
   */
  public static final byte OP_TYPE_MODIFY_RESPONSE = 0x67;
  /**
   * The DN for modify requests in this test case.
   */
  private static final ASN1OctetString dn =
      new ASN1OctetString("dc=example,dc=com");
  /**
   * The alternative DN for add requests in this test case.
   */
  private static final ASN1OctetString dnAlt =
      new ASN1OctetString("dc=sun,dc=com");
  /**
   * Generate modifications for use in test cases. Attributes will have names
   * like "testAttributeN" where N is the number of the attribute. Modification
   * types will be random.
   *
   * @param numAttributes Number of attributes to generate. 0 will return
   *                      a empty list.
   * @param prefix        String to prefix the attribute values
   * @return              The generate attributes.
   *
   */
  private ArrayList<LDAPModification> generateModifications(int numAttributes,
                                                            String prefix)
  {
    ArrayList<LDAPModification> modifies = new ArrayList<LDAPModification>();
    LDAPAttribute attribute;
    ModificationType modificationType;
    Random randomGenerator = new Random(0);
    int i, j;
    for(i = 0; i < numAttributes; i++)
    {
      attribute = new LDAPAttribute("testAttribute"+i);
      switch(i % 4)
      {
        case 0 : modificationType = ModificationType.ADD;
          break;
        case 1 : modificationType = ModificationType.DELETE;
          break;
        case 2 : modificationType = ModificationType.REPLACE;
          break;
        case 3 : modificationType = ModificationType.INCREMENT;
          break;
        default : modificationType = ModificationType.ADD;
      }
      modifies.add(new LDAPModification(modificationType, attribute));
    }
    return modifies;
  }
  private Boolean modificationsEquals(ArrayList<LDAPModification> modifies1,
                                      ArrayList<LDAPModification> modifies2)
  {
    if(modifies1.size() != modifies2.size())
    {
      return false;
    }
    int i, j;
    LDAPModification modify1;
    LDAPModification modify2;
    for(i = 0; i < modifies1.size(); i++)
    {
      modify1 = modifies1.get(i);
      modify2 = modifies2.get(i);
      if(!modify1.getAttribute().getAttributeType().equals(
          modify2.getAttribute().getAttributeType()))
      {
        System.out.println(modify1.getAttribute());
        System.out.println(modify2.getAttribute());
        System.out.println("attribute !=");
        return false;
      }
      if(!modify1.getModificationType().equals(modify2.getModificationType()))
      {
        System.out.println("mod type !=");
        return false;
      }
    }
    return true;
  }
  /**
   * 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
  {
    ModifyRequestProtocolOp modifyRequest = new ModifyRequestProtocolOp(dn);
    assertEquals(modifyRequest.getType(), OP_TYPE_MODIFY_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
  {
    ModifyRequestProtocolOp modifyRequest = new ModifyRequestProtocolOp(dn);
    assertEquals(modifyRequest.getProtocolOpName(), "Modify 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
  {
    ModifyRequestProtocolOp modifyRequest;
    ArrayList<LDAPModification> modifications;
    //Test to make sure the constructor with dn param works.
    modifyRequest = new ModifyRequestProtocolOp(dn);
    assertEquals(modifyRequest.getDN(), dn);
    assertNotNull(modifyRequest.getModifications());
    assertEquals(modifyRequest.getModifications().size(), 0);
    //Test to make sure the constructor with dn and attribute params works.
    modifications = generateModifications(10, "test");
    modifyRequest = new ModifyRequestProtocolOp(dn, modifications);
    assertEquals(modifyRequest.getDN(), dn);
    assertEquals(modifyRequest.getModifications(), modifications);
    //Test to make sure the constructor with dn and attribute params works with
    //null attributes.
    modifyRequest = new ModifyRequestProtocolOp(dn, null);
    assertEquals(modifyRequest.getDN(), dn);
    assertNotNull(modifyRequest.getModifications());
    assertEquals(modifyRequest.getModifications().size(), 0);
  }
  /**
   * Test to make sure that setter methods work.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testSetMethods() throws Exception
  {
    ModifyRequestProtocolOp modifyRequest;
    modifyRequest = new ModifyRequestProtocolOp(dn);
    assertEquals(modifyRequest.getDN(), dn);
    modifyRequest.setDN(dnAlt);
    assertEquals(modifyRequest.getDN(), dnAlt);
  }
  /**
   * 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
  {
    ModifyRequestProtocolOp.decodeModifyRequest(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>();
    ModifyRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_REQUEST,
                                                    elements));
  }
  /**
   * Test the decode method when invalid modifies 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>(2);
    ArrayList<ASN1Element> attrElements =
         new ArrayList<ASN1Element>();
    elements.add(new ASN1Sequence(attrElements));
    elements.add(dn);
    ModifyRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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>(2);
    ArrayList<ASN1Element> attrElements =
         new ArrayList<ASN1Element>();
    elements.add(dn);
    elements.add(new ASN1Sequence(attrElements));
    ModifyRequestProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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
  {
    ModifyRequestProtocolOp modifyEncoded;
    ModifyRequestProtocolOp modifyDecoded;
    ASN1Element element;
    modifyEncoded = new ModifyRequestProtocolOp(null, null);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyRequestProtocolOp)ModifyRequestProtocolOp.decode(
        element);
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    ModifyRequestProtocolOp modifyEncoded;
    ModifyRequestProtocolOp modifyDecoded;
    ASN1Element element;
    ArrayList<LDAPModification> modifies;
    //Test case for a full encode decode operation with normal params.
    modifies = generateModifications(10,"test");
    modifyEncoded = new ModifyRequestProtocolOp(dn, modifies);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyRequestProtocolOp)ModifyRequestProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getType(), OP_TYPE_MODIFY_REQUEST);
    assertEquals(modifyEncoded.getDN(), modifyDecoded.getDN());
    assertTrue(modificationsEquals(modifyEncoded.getModifications(),
                                   modifyDecoded.getModifications()));
    //Test case for a full encode decode operation with large modifications.
    modifies = generateModifications(100,"test");
    modifyEncoded = new ModifyRequestProtocolOp(dn, modifies);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyRequestProtocolOp)ModifyRequestProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getDN(), modifyDecoded.getDN());
    assertTrue(modificationsEquals(modifyEncoded.getModifications(),
                                   modifyDecoded.getModifications()));
    //Test case for a full encode decode operation with no attributes.
    modifyEncoded = new ModifyRequestProtocolOp(dn, null);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyRequestProtocolOp)ModifyRequestProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getDN(), modifyDecoded.getDN());
    assertTrue(modificationsEquals(modifyEncoded.getModifications(),
                                   modifyDecoded.getModifications()));
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    ModifyRequestProtocolOp modifyRequest;
    ArrayList<LDAPModification> modifications;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numModifications;
    numModifications = 10;
    modifications = generateModifications(numModifications, "test");
    modifyRequest = new ModifyRequestProtocolOp(dn, modifications);
    modifyRequest.toString(buffer);
    key.append("ModifyRequest(dn="+dn+", mods={");
    for(i = 0; i < numModifications; i++)
    {
      modifications.get(i).toString(key);
      if(i < numModifications - 1)
      {
        key.append(", ");
      }
    }
    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
  {
    ModifyRequestProtocolOp modifyRequest;
    ArrayList<LDAPModification> modifications;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numModifications,  indent;
    numModifications = 10;
    indent = 5;
    modifications = generateModifications(numModifications, "test");
    modifyRequest = new ModifyRequestProtocolOp(dn, modifications);
    modifyRequest.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    key.append(indentBuf);
    key.append("Modify Request");
    key.append(EOL);
    key.append(indentBuf);
    key.append("  DN:  ");
    dn.toString(key);
    key.append(EOL);
    key.append("  Modifications:");
    key.append(EOL);
    for (LDAPModification modify : modifications)
    {
      modify.toString(key, indent+4);
    }
    assertEquals(buffer.toString(), key.toString());
  }
}
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestModifyResponseProtocolOp.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.ModifyResponseProtocolOp class.
 */
public class TestModifyResponseProtocolOp extends LdapTestCase
{
  /**
   * The protocol op type for modify requests.
   */
  public static final byte OP_TYPE_MODIFY_REQUEST = 0x66;
  /**
   * The protocol op type for modify responses.
   */
  public static final byte OP_TYPE_MODIFY_RESPONSE = 0x67;
  /**
   * The result code for add result operations.
   */
  private static final int resultCode = 10;
  /**
   * The error message to use for add result operations.
   */
  private static final String resultMsg = "Test Successful";
/**
   * The DN to use for add 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
  {
    ModifyResponseProtocolOp modifyResponse = new ModifyResponseProtocolOp(
        resultCode);
    assertEquals(modifyResponse.getType(), OP_TYPE_MODIFY_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
  {
    ModifyResponseProtocolOp modifyResponse = new ModifyResponseProtocolOp(
        resultCode);
    assertEquals(modifyResponse.getProtocolOpName(), "Modify 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
  {
    ModifyResponseProtocolOp modifyResponse;
    ArrayList<LDAPAttribute> attributes;
    //Test to make sure the constructor with result code param works.
    modifyResponse = new ModifyResponseProtocolOp(resultCode);
    assertEquals(modifyResponse.getResultCode(), resultCode);
    //Test to make sure the constructor with result code and error message
    //params works.
    modifyResponse = new ModifyResponseProtocolOp(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 ModifyResponseProtocolOp(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
  {
    ModifyResponseProtocolOp modifyResponse;
    modifyResponse = new ModifyResponseProtocolOp(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>();
    ModifyResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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());
    ModifyResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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());
    ModifyResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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());
    ModifyResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_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());
    ModifyResponseProtocolOp.decode(new ASN1Sequence(OP_TYPE_MODIFY_RESPONSE,
                                                    elements));
  }
  /**
   * Test the encode and decode methods and corner cases.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test
  public void testEncodeDecode() throws Exception
  {
    ModifyResponseProtocolOp modifyEncoded;
    ModifyResponseProtocolOp modifyDecoded;
    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.
    modifyEncoded = new ModifyResponseProtocolOp(resultCode, resultMsg, dn,
                                                 referralURLs);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyResponseProtocolOp)ModifyResponseProtocolOp.decode(
        element);
    assertEquals(modifyEncoded.getType(), OP_TYPE_MODIFY_RESPONSE);
    assertEquals(modifyEncoded.getMatchedDN().compareTo(
        modifyDecoded.getMatchedDN()),
                 0);
    assertEquals(modifyEncoded.getErrorMessage(),
                 modifyDecoded.getErrorMessage());
    assertEquals(modifyEncoded.getResultCode(), modifyDecoded.getResultCode());
    assertEquals(modifyEncoded.getReferralURLs(),
                 modifyDecoded.getReferralURLs());
    //Test case for a full encode decode operation with an empty DN params.
    modifyEncoded = new ModifyResponseProtocolOp(resultCode, resultMsg, new DN(),
                                                 referralURLs);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyResponseProtocolOp)ModifyResponseProtocolOp.decode(
        element);
    assertEquals(modifyDecoded.getMatchedDN(), null);
    //Test case for a full empty referral url param.
    ArrayList<String> emptyReferralURLs = new ArrayList<String>();
    modifyEncoded = new ModifyResponseProtocolOp(resultCode, resultMsg, dn,
                                                 emptyReferralURLs);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyResponseProtocolOp)ModifyResponseProtocolOp.decode(
        element);
    assertTrue(modifyDecoded.getReferralURLs() == null);
    //Test case for a full encode decode operation with resultCode param only.
    modifyEncoded = new ModifyResponseProtocolOp(resultCode);
    element = modifyEncoded.encode();
    modifyDecoded = (ModifyResponseProtocolOp)ModifyResponseProtocolOp.decode(
        element);
    assertEquals(modifyDecoded.getMatchedDN(), null);
    assertEquals(modifyDecoded.getErrorMessage(), null);
    assertEquals(modifyEncoded.getResultCode(), modifyDecoded.getResultCode());
    assertTrue(modifyDecoded.getReferralURLs() == null);
  }
  /**
   * Test the toString (single line) method.
   *
   * @throws Exception If the test fails unexpectedly.
   */
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    ModifyResponseProtocolOp 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 ModifyResponseProtocolOp(resultCode, resultMsg, dn,
                                                  referralURLs);
    modifyResponse.toString(buffer);
    key.append("ModifyResponse(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
  {
    ModifyResponseProtocolOp 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 ModifyResponseProtocolOp(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 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());
  }
}