Test coverage: bmp.spi.registry 80/23680/6
authorIveta Halanova <iveta.halanova@pantheon.sk>
Thu, 2 Jul 2015 09:36:21 +0000 (11:36 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 3 Jul 2015 11:32:02 +0000 (11:32 +0000)
Change-Id: I2165c8e7861aa4667845c8ea8bbba14e2fa99469
Signed-off-by: Iveta Halanova <iveta.halanova@pantheon.sk>
bgp/bmp-spi/pom.xml
bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/AbstractBmpExtensionProviderActivatorTest.java [new file with mode: 0644]
bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpExtensionProviderContextTest.java [new file with mode: 0644]
bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpMessageRegistryTest.java
bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpTlvRegistryTest.java [new file with mode: 0644]

index 50e4585df2f6f617860d5824ccb6d98dfd2a8b4d..4426c51d08da4588a37872f34a1b1d4a8d46744b 100644 (file)
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/AbstractBmpExtensionProviderActivatorTest.java b/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/AbstractBmpExtensionProviderActivatorTest.java
new file mode 100644 (file)
index 0000000..63e55ce
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2015 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.bmp.spi.registry;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
+
+public class AbstractBmpExtensionProviderActivatorTest {
+
+    private final SimpleAbstractBmpExtensionProviderActivator activator = new SimpleAbstractBmpExtensionProviderActivator();
+    private static final SimpleBmpExtensionProviderContext context = new SimpleBmpExtensionProviderContext();
+
+
+    @Test
+    public void testStartActivator() throws BmpDeserializationException {
+        this.activator.start(context);
+        this.activator.close();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testStopActivator() {
+        this.activator.close();
+    }
+
+    private static class SimpleAbstractBmpExtensionProviderActivator extends AbstractBmpExtensionProviderActivator {
+        @Override
+        protected List<AutoCloseable> startImpl(final BmpExtensionProviderContext context) {
+            final List<AutoCloseable> reg = new ArrayList<AutoCloseable>();
+            reg.add(context.registerBmpMessageParser(1, new SimpleBmpMessageRegistry()));
+            return reg;
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpExtensionProviderContextTest.java b/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpExtensionProviderContextTest.java
new file mode 100644 (file)
index 0000000..1d25d7a
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2015 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.bmp.spi.registry;
+
+import static org.junit.Assert.assertNotNull;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
+import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
+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.bmp.message.rev150512.CountTlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.description.tlv.DescriptionTlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.reason.tlv.ReasonTlv;
+
+public class SimpleBmpExtensionProviderContextTest {
+
+    private static final SimpleBmpMessageRegistry MESSAGE_REGISTRY = new SimpleBmpMessageRegistry();
+    private static final SimpleBmpExtensionProviderContext CONTEXT = new SimpleBmpExtensionProviderContext();
+    private static final int TEST_TYPE = 1;
+    @Mock BmpTlvParser tlvParser;
+    @Mock BmpTlvSerializer tlvSerializer;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test
+    public void testRegisterBmpMessageParser() {
+        assertNotNull(CONTEXT.registerBmpMessageParser(TEST_TYPE, MESSAGE_REGISTRY));
+    }
+
+    @Test
+    public void testRegisterBmpMessageSerializer() {
+        assertNotNull(CONTEXT.registerBmpMessageSerializer(Keepalive.class, MESSAGE_REGISTRY));
+    }
+
+    @Test
+    public void testGetBmpMessageRegistry() {
+        assertNotNull(CONTEXT.getBmpMessageRegistry());
+    }
+
+    @Test
+    public void testRegisterBmpStatisticsTlvParser() {
+        assertNotNull(CONTEXT.registerBmpStatisticsTlvParser(TEST_TYPE, this.tlvParser));
+    }
+
+    @Test
+    public void testRegisterBmpStatisticsTlvSerializer() {
+        assertNotNull(CONTEXT.registerBmpStatisticsTlvSerializer(CountTlv.class, this.tlvSerializer));
+    }
+
+    @Test
+    public void testRegisterBmpInitiationTlvParser() {
+        assertNotNull(CONTEXT.registerBmpInitiationTlvParser(TEST_TYPE, this.tlvParser));
+    }
+
+    @Test
+    public void testRegisterBmpInitiationTlvSerializer() {
+        assertNotNull(CONTEXT.registerBmpInitiationTlvSerializer(DescriptionTlv.class, this.tlvSerializer));
+    }
+
+    @Test
+    public void testRegisterBmpTerminationTlvParser() {
+        assertNotNull(CONTEXT.registerBmpTerminationTlvParser(TEST_TYPE, this.tlvParser));
+    }
+
+    @Test
+    public void testRegisterBmpTerminationTlvSerializer() {
+        assertNotNull(CONTEXT.registerBmpTerminationTlvSerializer(ReasonTlv.class, this.tlvSerializer));
+    }
+
+    @Test
+    public void tetsGetBmpStatisticsTlvRegistry() {
+        assertNotNull(CONTEXT.getBmpStatisticsTlvRegistry());
+    }
+
+    @Test
+    public void testGetBmpInitiationTlvRegistry() {
+        assertNotNull(CONTEXT.getBmpInitiationTlvRegistry());
+    }
+
+    @Test
+    public void testGetBmpTerminationTlvRegistry() {
+        assertNotNull(CONTEXT.getBmpTerminationTlvRegistry());
+    }
+}
\ No newline at end of file
index b8f77cd26796b01a5e44154fe07a66bff48484c1..dfb9e0f1721ecb4677e0cb581088c5bee47ac266 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.bmp.spi.registry;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import org.junit.Test;
@@ -24,6 +23,7 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 public class SimpleBmpMessageRegistryTest {
 
     private static final int MSG_TYPE = 15;
+    private final BmpMessageRegistry registry = new SimpleBmpMessageRegistry();
 
     private static final byte[] BMP_TEST_MESSAGE = {
         0x03,//version
@@ -32,21 +32,44 @@ public class SimpleBmpMessageRegistryTest {
         0x00, 0x00, 0x01, 0x01//payload
     };
 
+    private final byte[] exceptionMessage = {
+        0x03,//version
+        0x00, 0x00, 0x00, 0x0A,//message length
+        0x0F,//msg type
+        0x00, 0x00, 0x01, 0x01
+    };
+
     @Test
     public void testBmpMessageRegistry() throws BmpDeserializationException {
-        final BmpMessageRegistry registry = new SimpleBmpMessageRegistry();
         final BmpTestParser bmpTestParser = new BmpTestParser();
-        registry.registerBmpMessageParser(MSG_TYPE, bmpTestParser);
-        registry.registerBmpMessageSerializer(BmpTestMessage.class, bmpTestParser);
+        this.registry.registerBmpMessageParser(MSG_TYPE, bmpTestParser);
+        this.registry.registerBmpMessageSerializer(BmpTestMessage.class, bmpTestParser);
         final BmpTestMessage message = new BmpTestMessage(257);
 
-        assertEquals(message.toString(), ((BmpTestMessage)registry.parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE))).toString());
+        assertEquals(message.toString(), ((BmpTestMessage)this.registry.parseMessage(Unpooled.copiedBuffer(BMP_TEST_MESSAGE))).toString());
 
         final ByteBuf aggregator = Unpooled.buffer(BMP_TEST_MESSAGE.length);
-        registry.serializeMessage(message, aggregator);
+        this.registry.serializeMessage(message, aggregator);
         assertArrayEquals(BMP_TEST_MESSAGE, ByteArray.getAllBytes(aggregator));
     }
 
+    @Test(expected=BmpDeserializationException.class)
+    public void testNotBmpTypeException() throws BmpDeserializationException {
+        this.exceptionMessage[0] = 0x01;
+        this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
+    }
+
+    @Test(expected=BmpDeserializationException.class)
+    public void testLengthException() throws BmpDeserializationException {
+        this.exceptionMessage[4] = 0x01;
+        this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage));
+    }
+
+    @Test(expected=BmpDeserializationException.class)
+    public void testInvalidMessageContextException() throws BmpDeserializationException {
+        this.registry.parseMessage(Unpooled.copiedBuffer(this.exceptionMessage, 0, this.exceptionMessage.length-2));
+    }
+
     private static final class BmpTestParser extends AbstractBmpMessageParser {
 
         @Override
@@ -84,7 +107,7 @@ public class SimpleBmpMessageRegistryTest {
 
         @Override
         public String toString() {
-            return "BmpTestMessage [value=" + value + "]";
+            return "BmpTestMessage [value=" + this.value + "]";
         }
 
     }
diff --git a/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpTlvRegistryTest.java b/bgp/bmp-spi/src/test/java/org/opendaylight/protocol/bmp/spi/registry/SimpleBmpTlvRegistryTest.java
new file mode 100644 (file)
index 0000000..e5dba42
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2015 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.bmp.spi.registry;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
+import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
+import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.Tlv;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
+
+public class SimpleBmpTlvRegistryTest {
+
+    private final SimpleBmpTlvRegistry bmpTlvRegistry = new SimpleBmpTlvRegistry();
+    private final byte[] bytes = new byte[] {
+        1, 2, 3
+    };
+    private final ByteBuf input = Unpooled.wrappedBuffer(this.bytes);
+
+    @Mock private BmpTlvParser descriptionTlvParser;
+    private static final int descriptionTlvType = 1;
+    private static final int otherTlvType = 2;
+    @Mock private BmpTlvSerializer descriptionTlvSerializer;
+
+    @Before
+    public void setUp() throws BmpDeserializationException {
+        MockitoAnnotations.initMocks(this);
+        this.bmpTlvRegistry.registerBmpTlvParser(descriptionTlvType, this.descriptionTlvParser);
+        this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class, this.descriptionTlvSerializer);
+        Mockito.doReturn(new MockDescriptionTlv()).when(this.descriptionTlvParser).parseTlv(this.input);
+    }
+
+    @Test
+    public void testParserRegistration() {
+        assertNotNull(this.bmpTlvRegistry.registerBmpTlvParser(descriptionTlvType, this.descriptionTlvParser));
+    }
+
+    @Test
+    public void testSerializerRegistration() {
+        assertNotNull(this.bmpTlvRegistry.registerBmpTlvSerializer(MockDescriptionTlv.class, this.descriptionTlvSerializer));
+    }
+
+    @Test
+    public void testUnrecognizedType() throws BmpDeserializationException {
+        assertNull(this.bmpTlvRegistry.parseTlv(otherTlvType, this.input));
+        final ByteBuf output = Unpooled.EMPTY_BUFFER;
+        this.bmpTlvRegistry.serializeTlv(new MockTlv(), output);
+        assertEquals(0, output.readableBytes());
+    }
+
+    @Test
+    public void testParseTlv() throws BmpDeserializationException {
+        final Tlv output = this.bmpTlvRegistry.parseTlv(descriptionTlvType, this.input);
+        assertNotNull(output);
+        assertTrue(output instanceof MockDescriptionTlv);
+
+        final ByteBuf aggregator = Unpooled.EMPTY_BUFFER;
+        this.bmpTlvRegistry.serializeTlv(output, aggregator);
+        Mockito.verify(this.descriptionTlvSerializer).serializeTlv(output, aggregator);
+    }
+
+    private final class MockDescriptionTlv implements Tlv {
+        @Override
+        public Class<? extends DataContainer> getImplementedInterface() {
+            return MockDescriptionTlv.class;
+        }
+    }
+
+    private final class MockTlv implements Tlv {
+
+        @Override
+        public Class<? extends DataContainer> getImplementedInterface() {
+            return MockTlv.class;
+        }
+
+    }
+}