<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ </dependency>
</dependencies>
<build>
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
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;
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
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
@Override
public String toString() {
- return "BmpTestMessage [value=" + value + "]";
+ return "BmpTestMessage [value=" + this.value + "]";
}
}
--- /dev/null
+/*
+ * 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;
+ }
+
+ }
+}