From d23ce7c3206dc6dfcd5f8a47b5d12a09515a3499 Mon Sep 17 00:00:00 2001 From: Dana Kutenicsova Date: Sat, 5 Apr 2014 22:27:27 +0200 Subject: [PATCH] Added unit tests to MesaageRegistry. Change-Id: I8f7b9072ef9305cf8cfb30c420f3d8e95876a8c3 Signed-off-by: Dana Kutenicsova --- .../bgp/parser/impl/ComplementaryTest.java | 2 +- bgp/parser-spi/pom.xml | 4 ++ .../parser/spi/AbstractMessageRegistry.java | 17 +++---- .../spi/AbstractMessageRegistryTest.java | 50 +++++++++++++++++++ 4 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistryTest.java diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java index 18ca225bae..7a0d7e544c 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java @@ -272,7 +272,7 @@ public class ComplementaryTest { String ex = ""; try { msgReg.serializeMessage(null); - } catch (final IllegalArgumentException e) { + } catch (final NullPointerException e) { ex = e.getMessage(); } assertEquals("BGPMessage is mandatory.", ex); diff --git a/bgp/parser-spi/pom.xml b/bgp/parser-spi/pom.xml index 6650332302..d18c5f3394 100644 --- a/bgp/parser-spi/pom.xml +++ b/bgp/parser-spi/pom.xml @@ -67,6 +67,10 @@ junit junit + + org.mockito + mockito-core + diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java index 2b482dd7a0..55ddddf988 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java @@ -35,8 +35,9 @@ public abstract class AbstractMessageRegistry implements MessageRegistry { @Override public final Notification parseMessage(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException { - Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Array of bytes cannot be null or empty."); - Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH); + Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty."); + Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, + "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH); final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH); if (!Arrays.equals(marker, MARKER)) { @@ -53,8 +54,8 @@ public abstract class AbstractMessageRegistry implements MessageRegistry { throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength); } if (msgBody.readableBytes() != messageLength - MessageUtil.COMMON_HEADER_LENGTH) { - throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + "; Expected: " - + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); + throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); } final Notification msg = parseBody(messageType, msgBody, messageLength); if (msg == null) { @@ -66,13 +67,9 @@ public abstract class AbstractMessageRegistry implements MessageRegistry { @Override public final byte[] serializeMessage(final Notification message) { - if (message == null) { - throw new IllegalArgumentException("BGPMessage is mandatory."); - } + Preconditions.checkNotNull(message, "BGPMessage is mandatory."); final byte[] ret = serializeMessageImpl(message); - if (ret == null) { - throw new IllegalArgumentException("Unknown instance of BGPMessage. Passed " + message.getClass()); - } + Preconditions.checkNotNull(ret, "Unknown instance of BGPMessage. Passed ", message.getClass()); return ret; } } diff --git a/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistryTest.java b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistryTest.java new file mode 100644 index 0000000000..9e5b9109be --- /dev/null +++ b/bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistryTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.protocol.bgp.parser.spi; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; + +import org.junit.Test; +import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; +import org.opendaylight.protocol.bgp.parser.BGPParsingException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder; +import org.opendaylight.yangtools.yang.binding.Notification; + +public class AbstractMessageRegistryTest { + + public static final byte[] keepAliveBMsg = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 }; + + private final AbstractMessageRegistry registry = new AbstractMessageRegistry() { + + @Override + protected byte[] serializeMessageImpl(Notification message) { + return keepAliveBMsg; + } + + @Override + protected Notification parseBody(int type, ByteBuf body, int messageLength) throws BGPDocumentedException { + return new KeepaliveBuilder().build(); + } + }; + + @Test + public void testRegistry() throws BGPDocumentedException, BGPParsingException { + final Notification keepAlive = new KeepaliveBuilder().build(); + final byte[] serialized = this.registry.serializeMessage(keepAlive); + assertArrayEquals(keepAliveBMsg, serialized); + + final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg)); + assertTrue(not instanceof Keepalive); + } +} -- 2.36.6