Added unit tests to MesaageRegistry. 21/5921/1
authorDana Kutenicsova <dkutenic@cisco.com>
Sat, 5 Apr 2014 20:27:27 +0000 (22:27 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Sat, 5 Apr 2014 20:27:27 +0000 (22:27 +0200)
Change-Id: I8f7b9072ef9305cf8cfb30c420f3d8e95876a8c3
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ComplementaryTest.java
bgp/parser-spi/pom.xml
bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java
bgp/parser-spi/src/test/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistryTest.java [new file with mode: 0644]

index 18ca225bae7b20654da27222151a5591e5497cbd..7a0d7e544c1e306d088adf1aa15592d9433b9e6f 100644 (file)
@@ -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);
index 6650332302ef7b7d7a301df27ac3ef895b84f107..d18c5f3394a4689648dbabe4f99752fb6d4f6f30 100644 (file)
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+               <dependency>
+                       <groupId>org.mockito</groupId>
+                       <artifactId>mockito-core</artifactId>
+               </dependency>
     </dependencies>
 
     <build>
index 2b482dd7a09a8c6f092c6f6e4ef08f8ebf58b55c..55ddddf988e364c126b09f2d5faa331f1ebddb58 100644 (file)
@@ -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 (file)
index 0000000..9e5b910
--- /dev/null
@@ -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);
+       }
+}