325N/A/*
325N/A * CDDL HEADER START
325N/A *
325N/A * The contents of this file are subject to the terms of the
325N/A * Common Development and Distribution License, Version 1.0 only
325N/A * (the "License"). You may not use this file except in compliance
325N/A * with the License.
325N/A *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
325N/A * See the License for the specific language governing permissions
325N/A * and limitations under the License.
325N/A *
325N/A * When distributing Covered Code, include this CDDL HEADER in each
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * information:
325N/A * Portions Copyright [yyyy] [name of copyright owner]
325N/A *
325N/A * CDDL HEADER END
325N/A *
325N/A *
3232N/A * Copyright 2006-2008 Sun Microsystems, Inc.
325N/A */
325N/Apackage org.opends.server.protocols.ldap;
325N/A
325N/Aimport org.opends.server.protocols.asn1.*;
1177N/Aimport org.opends.server.types.LDAPException;
4134N/Aimport org.opends.server.types.ByteString;
4134N/Aimport org.opends.server.types.ByteStringBuilder;
325N/Aimport static org.opends.server.util.ServerConstants.EOL;
2342N/Aimport org.opends.server.DirectoryServerTestCase;
325N/Aimport org.testng.annotations.Test;
325N/Aimport static org.testng.Assert.*;
325N/A
325N/A/**
325N/A * This class defines a set of tests for the
325N/A * org.opends.server.protocol.ldap.ModifyDNRequestProtocolOp class.
325N/A */
2342N/Apublic class TestModifyDNRequestProtocolOp extends DirectoryServerTestCase {
325N/A /**
325N/A * The protocol op type for modify DN requests.
325N/A */
325N/A public static final byte OP_TYPE_MODIFY_DN_REQUEST = 0x6C;
325N/A
325N/A
325N/A
325N/A /**
325N/A * The protocol op type for modify DN responses.
325N/A */
325N/A public static final byte OP_TYPE_MODIFY_DN_RESPONSE = 0x6D;
325N/A
325N/A /**
325N/A * The DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString dn =
4134N/A ByteString.valueOf("dc=example,dc=com");
325N/A
325N/A /**
325N/A * The alt DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString altDn =
4134N/A ByteString.valueOf("dc=alt,dc=example,dc=com");
325N/A
325N/A /**
325N/A * The new DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString newRdn =
4134N/A ByteString.valueOf("dc=example-new");
325N/A
325N/A /**
325N/A * The alt new DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString altNewRdn =
4134N/A ByteString.valueOf("ou=alt,dc=example-new");
325N/A
325N/A /**
325N/A * The new superiour DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString newSuperiorDn =
4134N/A ByteString.valueOf("dc=widget,dc=com");
325N/A
325N/A /**
325N/A * The alt new superiour DN for modify DN requests in this test case.
325N/A */
4134N/A private static final ByteString altNewSuperiorDn =
4134N/A ByteString.valueOf("dc=alt,dc=widget,dc=com");
325N/A
325N/A /**
325N/A * Test to make sure the class processes the right LDAP op type.
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test
325N/A public void testOpType() throws Exception
325N/A {
325N/A ModifyDNRequestProtocolOp modifyRequest = new ModifyDNRequestProtocolOp(dn,
325N/A newRdn, true);
325N/A assertEquals(modifyRequest.getType(), OP_TYPE_MODIFY_DN_REQUEST);
325N/A }
325N/A
325N/A /**
325N/A * Test to make sure the class returns the correct protocol name.
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test
325N/A public void testProtocolOpName() throws Exception
325N/A {
325N/A ModifyDNRequestProtocolOp modifyRequest = new ModifyDNRequestProtocolOp(dn,
325N/A newRdn, true);
325N/A assertEquals(modifyRequest.getProtocolOpName(), "Modify DN Request");
325N/A }
325N/A
325N/A /**
325N/A * Test the constructors to make sure the right objects are constructed.
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test
325N/A public void testConstructors() throws Exception
325N/A {
325N/A ModifyDNRequestProtocolOp modifyRequest;
325N/A
325N/A modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true);
325N/A assertEquals(modifyRequest.getEntryDN(), dn);
325N/A assertEquals(modifyRequest.getNewRDN(), newRdn);
325N/A assertEquals(modifyRequest.deleteOldRDN(), true);
325N/A assertNull(modifyRequest.getNewSuperior());
325N/A
325N/A modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, false,
325N/A newSuperiorDn);
325N/A assertEquals(modifyRequest.getEntryDN(), dn);
325N/A assertEquals(modifyRequest.getNewRDN(), newRdn);
325N/A assertEquals(modifyRequest.getNewSuperior(), newSuperiorDn);
325N/A assertEquals(modifyRequest.deleteOldRDN(), false);
325N/A }
325N/A
325N/A /**
325N/A * Test the decode method when an null element is passed
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = LDAPException.class)
325N/A public void testDecodeNullElement() throws Exception
325N/A {
4134N/A LDAPReader.readProtocolOp(null);
325N/A }
325N/A
325N/A /**
325N/A * Test the decode method when an empty element is passed
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = LDAPException.class)
325N/A public void testDecodeEmptyElement() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
4134N/A writer.writeStartSequence(OP_TYPE_MODIFY_DN_REQUEST);
4134N/A writer.writeEndSequence();
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A LDAPReader.readProtocolOp(reader);
325N/A }
325N/A
325N/A /**
325N/A * Test the decode method when the wrong number of elements is passed
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = LDAPException.class)
325N/A public void testDecodeInvalidElementNum() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
4134N/A writer.writeStartSequence(OP_TYPE_MODIFY_DN_REQUEST);
4134N/A writer.writeNull();
4134N/A writer.writeNull();
4134N/A writer.writeEndSequence();
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A LDAPReader.readProtocolOp(reader);
325N/A }
325N/A
325N/A /**
325N/A * Test the decode method when invalid attributes in element is passed
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = LDAPException.class)
325N/A public void testDecodeInvalidElement() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
4134N/A writer.writeStartSequence(OP_TYPE_MODIFY_DN_REQUEST);
4134N/A writer.writeNull();
4134N/A writer.writeNull();
4134N/A writer.writeNull();
4134N/A writer.writeEndSequence();
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A LDAPReader.readProtocolOp(reader);
325N/A }
325N/A
325N/A /**
325N/A * Test the decode method when an element w/ wrong op type is passed.
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = LDAPException.class)
325N/A public void testDecodeWrongElementType() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
4134N/A writer.writeStartSequence(OP_TYPE_MODIFY_DN_RESPONSE);
4134N/A writer.writeOctetString(dn);
4134N/A writer.writeOctetString(newRdn);
4134N/A writer.writeBoolean(true);
4134N/A writer.writeEndSequence();
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A LDAPReader.readProtocolOp(reader);
325N/A }
325N/A
325N/A /**
325N/A * Test the encode and decode methods with null params
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test(expectedExceptions = Exception.class)
325N/A public void testNullEncodeDecode() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
325N/A ModifyDNRequestProtocolOp modifyEncoded;
325N/A ModifyDNRequestProtocolOp modifyDecoded;
325N/A
325N/A modifyEncoded = new ModifyDNRequestProtocolOp(null, null, true);
4134N/A modifyEncoded.write(writer);
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A modifyDecoded = (ModifyDNRequestProtocolOp)LDAPReader.readProtocolOp(reader);
325N/A }
325N/A
325N/A /**
325N/A * Test the encode and decode methods and corner cases.
325N/A *
325N/A * @throws Exception If the test failed unexpectedly.
325N/A */
325N/A @Test
325N/A public void testEncodeDecode() throws Exception
325N/A {
4134N/A ByteStringBuilder builder = new ByteStringBuilder();
4134N/A ASN1Writer writer = ASN1.getWriter(builder);
325N/A ModifyDNRequestProtocolOp modifyEncoded;
325N/A ModifyDNRequestProtocolOp modifyDecoded;
325N/A
325N/A modifyEncoded = new ModifyDNRequestProtocolOp(dn, newRdn, true,
325N/A newSuperiorDn);
4134N/A modifyEncoded.write(writer);
4134N/A
4134N/A ASN1Reader reader = ASN1.getReader(builder.toByteString());
4134N/A modifyDecoded = (ModifyDNRequestProtocolOp)LDAPReader.readProtocolOp(reader);
325N/A
325N/A assertEquals(modifyEncoded.getEntryDN(), modifyDecoded.getEntryDN());
325N/A assertEquals(modifyEncoded.getNewRDN(), modifyDecoded.getNewRDN());
325N/A assertEquals(modifyEncoded.getNewSuperior(), modifyDecoded.getNewSuperior());
325N/A assertEquals(modifyEncoded.deleteOldRDN(), modifyDecoded.deleteOldRDN());
325N/A
4134N/A builder.clear();
325N/A modifyEncoded = new ModifyDNRequestProtocolOp(dn, newRdn, true);
4134N/A modifyEncoded.write(writer);
4134N/A
4134N/A reader = ASN1.getReader(builder.toByteString());
4134N/A modifyDecoded = (ModifyDNRequestProtocolOp)LDAPReader.readProtocolOp(reader);
325N/A
325N/A assertEquals(modifyEncoded.getEntryDN(), modifyDecoded.getEntryDN());
325N/A assertEquals(modifyEncoded.getNewRDN(), modifyDecoded.getNewRDN());
325N/A assertEquals(modifyEncoded.getNewSuperior(), modifyDecoded.getNewSuperior());
325N/A assertEquals(modifyEncoded.deleteOldRDN(), modifyDecoded.deleteOldRDN());
325N/A }
325N/A
325N/A /**
325N/A * Test the toString (single line) method.
325N/A *
325N/A * @throws Exception If the test fails unexpectedly.
325N/A */
325N/A @Test
325N/A public void TestToStringSingleLine() throws Exception
325N/A {
325N/A ModifyDNRequestProtocolOp modifyRequest;
325N/A StringBuilder buffer = new StringBuilder();
325N/A StringBuilder key = new StringBuilder();
325N/A
325N/A modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true,
325N/A newSuperiorDn);
325N/A modifyRequest.toString(buffer);
325N/A
325N/A key.append("ModifyDNRequest(dn="+dn+", newRDN="+newRdn+", " +
325N/A "deleteOldRDN="+true+", newSuperior="+newSuperiorDn+")");
325N/A
325N/A assertEquals(buffer.toString(), key.toString());
325N/A }
325N/A
325N/A /**
325N/A * Test the toString (multi line) method.
325N/A *
325N/A * @throws Exception If the test fails unexpectedly.
325N/A */
325N/A @Test
325N/A public void TestToStringMultiLine() throws Exception
325N/A {
325N/A ModifyDNRequestProtocolOp modifyRequest;
325N/A StringBuilder buffer = new StringBuilder();
325N/A StringBuilder key = new StringBuilder();
325N/A int i;
325N/A int indent;
325N/A
325N/A indent = 5;
325N/A modifyRequest = new ModifyDNRequestProtocolOp(dn, newRdn, true,
325N/A newSuperiorDn);
325N/A modifyRequest.toString(buffer, indent);
325N/A
325N/A StringBuilder indentBuf = new StringBuilder(indent);
325N/A for (i=0 ; i < indent; i++)
325N/A {
325N/A indentBuf.append(' ');
325N/A }
325N/A
325N/A key.append(indentBuf);
325N/A key.append("Modify DN Request");
325N/A key.append(EOL);
325N/A
325N/A key.append(indentBuf);
325N/A key.append(" Entry DN: ");
4134N/A key.append(dn);
325N/A key.append(EOL);
325N/A
325N/A key.append(indentBuf);
325N/A key.append(" New RDN: ");
325N/A key.append(newRdn);
325N/A key.append(EOL);
325N/A
325N/A key.append(indentBuf);
325N/A key.append(" Delete Old RDN: ");
325N/A key.append(true);
325N/A key.append(EOL);
325N/A
325N/A key.append(indentBuf);
325N/A key.append(" New Superior: ");
325N/A key.append(newSuperiorDn);
325N/A key.append(EOL);
325N/A
325N/A assertEquals(buffer.toString(), key.toString());
325N/A }
325N/A}