Bump upstreams to 2022.09
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / UtilsTest.java
index f570de1a71e6c057153f82d8dd4a5ae26d405c9f..f67bfab2607ea078f282ad8f16878c2aad589a3f 100644 (file)
@@ -8,22 +8,47 @@
 package org.opendaylight.protocol.bgp.parser.spi;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.doReturn;
 
 import com.google.common.primitives.UnsignedBytes;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
+import java.util.Optional;
+import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class UtilsTest {
 
+    @Mock private AddressFamilyRegistry afiReg;
+    @Mock private SubsequentAddressFamilyRegistry safiReg;
+
+    @Before
+    public void setUp() {
+        doReturn(1).when(afiReg).numberForClass(Ipv4AddressFamily.VALUE);
+        doReturn(Ipv4AddressFamily.VALUE).when(afiReg).classForFamily(1);
+        doReturn(null).when(afiReg).classForFamily(2);
+
+        doReturn(1).when(safiReg).numberForClass(UnicastSubsequentAddressFamily.VALUE);
+        doReturn(UnicastSubsequentAddressFamily.VALUE).when(safiReg).classForFamily(1);
+        doReturn(null).when(safiReg).classForFamily(3);
+    }
+
     @Test
     public void testCapabilityUtil() {
         final byte[] result = new byte[] { 1, 2, 4, 8 };
-        ByteBuf aggregator = Unpooled.buffer();
+        final ByteBuf aggregator = Unpooled.buffer();
         CapabilityUtil.formatCapability(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }),aggregator);
         assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
     }
@@ -31,18 +56,19 @@ public class UtilsTest {
     @Test
     public void testMessageUtil() {
         final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
-            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
-            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
-            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
-        ByteBuf formattedMessage = Unpooled.buffer();
+            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+            UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
+            UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
+        final ByteBuf formattedMessage = Unpooled.buffer();
         MessageUtil.formatMessage(3, Unpooled.wrappedBuffer(new byte[] { 32, 5, 14, 21 }), formattedMessage);
         assertArrayEquals(result, ByteArray.getAllBytes(formattedMessage));
     }
 
     @Test
-    public void testParameterUtil() {
+    public void testParameterUtil() throws ParameterLengthOverflowException {
         final byte[] result = new byte[] { 1, 2, 4, 8 };
-        ByteBuf aggregator = Unpooled.buffer();
+        final ByteBuf aggregator = Unpooled.buffer();
         ParameterUtil.formatParameter(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }), aggregator);
         assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
     }
@@ -50,7 +76,7 @@ public class UtilsTest {
     @Test
     public void testAttributeUtil() {
         final byte[] result = new byte[] { 0x40, 03, 04, 10, 00, 00, 02 };
-        ByteBuf aggregator = Unpooled.buffer();
+        final ByteBuf aggregator = Unpooled.buffer();
         AttributeUtil.formatAttribute(64 , 3 , Unpooled.wrappedBuffer(new byte[] { 10, 0, 0, 2 }), aggregator);
         assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
     }
@@ -63,52 +89,66 @@ public class UtilsTest {
         final byte[] result = new byte[262];
         System.arraycopy(header, 0, result, 0, header.length);
         System.arraycopy(value, 0, result, 4, value.length);
-        ByteBuf aggregator = Unpooled.buffer();
+        final ByteBuf aggregator = Unpooled.buffer();
         AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE , 3 , Unpooled.wrappedBuffer(value), aggregator);
         assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
     }
 
-    @Test(expected=UnsupportedOperationException.class)
-    public void testAttributeUtilPrivateConstructor() throws Throwable {
-        final Constructor<AttributeUtil> c = AttributeUtil.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (InvocationTargetException e) {
-            throw e.getCause();
-        }
+    @Test
+    public void testMultiprotocolCapabilitiesUtil() throws BGPParsingException {
+        final byte[] bytes = new byte[] {0, 1, 0, 1};
+        final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
+        final BgpTableType parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg,
+            safiReg).get();
+        assertEquals(Ipv4AddressFamily.VALUE, parsedAfiSafi.getAfi());
+        assertEquals(UnicastSubsequentAddressFamily.VALUE, parsedAfiSafi.getSafi());
+
+        final ByteBuf serializedAfiSafi = Unpooled.buffer(4);
+        MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(afiReg, safiReg, Ipv4AddressFamily.VALUE,
+            UnicastSubsequentAddressFamily.VALUE, serializedAfiSafi);
+        assertArrayEquals(bytes, serializedAfiSafi.array());
+    }
+
+    @Test
+    public void testUnsupportedAfi() {
+        final byte[] bytes = new byte[] {0, 2, 0, 1};
+        final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
+        final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg,
+            safiReg);
+        assertFalse(parsedAfiSafi.isPresent());
+    }
+
+    @Test
+    public void testUnsupportedSafi() {
+        final byte[] bytes = new byte[] {0, 1, 0, 3};
+        final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
+        final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg,
+            safiReg);
+        assertFalse(parsedAfiSafi.isPresent());
     }
 
-    @Test(expected=UnsupportedOperationException.class)
-    public void testCapabilityUtilPrivateConstructor() throws Throwable {
-        final Constructor<CapabilityUtil> c = CapabilityUtil.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (InvocationTargetException e) {
-            throw e.getCause();
-        }
+    @Test(expected = ParameterLengthOverflowException.class)
+    public void testFormatParameterOverflow() throws ParameterLengthOverflowException {
+        ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(256), Unpooled.buffer());
     }
 
-    @Test(expected=UnsupportedOperationException.class)
-    public void testMessageUtilPrivateConstructor() throws Throwable {
-        final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (InvocationTargetException e) {
-            throw e.getCause();
-        }
+    @Test
+    public void testFormatParameter() throws ParameterLengthOverflowException {
+        final ByteBuf output = Unpooled.buffer();
+        ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(255), output);
+
+        assertEquals(257, output.readableBytes());
+        assertEquals(2, output.readUnsignedByte());
+        assertEquals(255, output.readUnsignedByte());
     }
 
-    @Test(expected=UnsupportedOperationException.class)
-    public void testParameterUtilPrivateConstructor() throws Throwable {
-        final Constructor<ParameterUtil> c = ParameterUtil.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (InvocationTargetException e) {
-            throw e.getCause();
-        }
+    @Test
+    public void testFormatExtendedParameter() {
+        final ByteBuf output = Unpooled.buffer();
+        ParameterUtil.formatExtendedParameter(2, Unpooled.buffer().writeZero(256), output);
+
+        assertEquals(259, output.readableBytes());
+        assertEquals(2, output.readUnsignedByte());
+        assertEquals(256, output.readUnsignedShort());
     }
 }