Add unit tests for LISP control message serializer and deserializer

Change-Id: Id517db99635ad8e055d6581e5c0f3ac9f45f2869
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecordTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecordTest.java
index 7b99612..99dcfcf 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecordTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispLocatorRecordTest.java
@@ -16,11 +16,19 @@
 package org.onosproject.lisp.msg.protocols;
 
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.types.LispIpv4Address;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.*;
 
 /**
  * Unit tests for DefaultLispLocatorRecord class.
@@ -35,7 +43,9 @@
     public void setup() {
 
         LispLocatorRecord.LocatorRecordBuilder builder1 =
-                    new DefaultLispLocatorRecord.DefaultLocatorRecordBuilder();
+                    new DefaultLocatorRecordBuilder();
+
+        LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
 
         record1 = builder1
                         .withPriority((byte) 0x01)
@@ -45,10 +55,11 @@
                         .withLocalLocator(true)
                         .withRlocProbed(false)
                         .withRouted(true)
+                        .withLocatorAfi(ipv4Locator1)
                         .build();
 
         LispLocatorRecord.LocatorRecordBuilder builder2 =
-                    new DefaultLispLocatorRecord.DefaultLocatorRecordBuilder();
+                    new DefaultLocatorRecordBuilder();
 
         sameAsRecord1 = builder2
                         .withPriority((byte) 0x01)
@@ -58,10 +69,13 @@
                         .withLocalLocator(true)
                         .withRlocProbed(false)
                         .withRouted(true)
+                        .withLocatorAfi(ipv4Locator1)
                         .build();
 
         LispLocatorRecord.LocatorRecordBuilder builder3 =
-                    new DefaultLispLocatorRecord.DefaultLocatorRecordBuilder();
+                    new DefaultLocatorRecordBuilder();
+
+        LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
 
         record2 = builder3
                         .withPriority((byte) 0x02)
@@ -71,6 +85,7 @@
                         .withLocalLocator(false)
                         .withRlocProbed(true)
                         .withRouted(false)
+                        .withLocatorAfi(ipv4Locator2)
                         .build();
     }
 
@@ -85,6 +100,8 @@
     public void testConstruction() {
         DefaultLispLocatorRecord record = (DefaultLispLocatorRecord) record1;
 
+        LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
+
         assertThat(record.getPriority(), is((byte) 0x01));
         assertThat(record.getWeight(), is((byte) 0x01));
         assertThat(record.getMulticastPriority(), is((byte) 0x01));
@@ -92,5 +109,20 @@
         assertThat(record.isLocalLocator(), is(true));
         assertThat(record.isRlocProbed(), is(false));
         assertThat(record.isRouted(), is(true));
+        assertThat(record.getLocatorAfi(), is(ipv4Locator));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+
+        LocatorRecordWriter writer = new LocatorRecordWriter();
+        writer.writeTo(byteBuf, record1);
+
+        LocatorRecordReader reader = new LocatorRecordReader();
+        LispLocatorRecord deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(record1, deserialized).testEquals();
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotifyTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotifyTest.java
index 26d8ff0..819da3d 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotifyTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapNotifyTest.java
@@ -16,11 +16,17 @@
 package org.onosproject.lisp.msg.protocols;
 
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.*;
 
 /**
  * Unit tests for DefaultLispMapNotify class.
@@ -35,25 +41,25 @@
     public void setup() {
 
         LispMapNotify.NotifyBuilder builder1 =
-                        new DefaultLispMapNotify.DefaultNotifyBuilder();
+                        new DefaultNotifyBuilder();
 
         notify1 = builder1
                         .withKeyId((short) 1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapNotify.NotifyBuilder builder2 =
-                        new DefaultLispMapNotify.DefaultNotifyBuilder();
+                        new DefaultNotifyBuilder();
 
         sameAsNotify1 = builder2
                         .withKeyId((short) 1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapNotify.NotifyBuilder builder3 =
-                        new DefaultLispMapNotify.DefaultNotifyBuilder();
+                        new DefaultNotifyBuilder();
 
         notify2 = builder3
                         .withKeyId((short) 2)
@@ -75,6 +81,20 @@
 
         assertThat(notify.getKeyId(), is((short) 1));
         assertThat(notify.getNonce(), is(1L));
-        assertThat(notify.getRecordCount(), is((byte) 0x01));
+        assertThat(notify.getRecordCount(), is((byte) 0));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+
+        NotifyWriter writer = new NotifyWriter();
+        writer.writeTo(byteBuf, notify1);
+
+        NotifyReader reader = new NotifyReader();
+        LispMapNotify deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(notify1, deserialized).testEquals();
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecordTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecordTest.java
index 1c85b11..25ce27f 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecordTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRecordTest.java
@@ -16,11 +16,19 @@
 package org.onosproject.lisp.msg.protocols;
 
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.types.LispIpv4Address;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.*;
 
 /**
  * Unit tests for DefaultLispMapRecord class.
@@ -35,29 +43,37 @@
     public void setup() {
 
         LispMapRecord.MapRecordBuilder builder1 =
-                        new DefaultLispMapRecord.DefaultMapRecordBuilder();
+                        new DefaultMapRecordBuilder();
+
+        LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
 
         record1 = builder1
                         .withRecordTtl(100)
                         .withAuthoritative(true)
-                        .withLocatorCount(100)
+                        .withLocatorCount(0)
                         .withMapVersionNumber((short) 1)
                         .withMaskLength((byte) 0x01)
+                        .withAction(LispMapReplyAction.NativelyForward)
+                        .withEidPrefixAfi(ipv4Locator1)
                         .build();
 
         LispMapRecord.MapRecordBuilder builder2 =
-                        new DefaultLispMapRecord.DefaultMapRecordBuilder();
+                        new DefaultMapRecordBuilder();
 
         sameAsRecord1 = builder2
                         .withRecordTtl(100)
                         .withAuthoritative(true)
-                        .withLocatorCount(100)
+                        .withLocatorCount(0)
                         .withMapVersionNumber((short) 1)
                         .withMaskLength((byte) 0x01)
+                        .withAction(LispMapReplyAction.NativelyForward)
+                        .withEidPrefixAfi(ipv4Locator1)
                         .build();
 
         LispMapRecord.MapRecordBuilder builder3 =
-                        new DefaultLispMapRecord.DefaultMapRecordBuilder();
+                        new DefaultMapRecordBuilder();
+
+        LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
 
         record2 = builder3
                         .withRecordTtl(200)
@@ -65,6 +81,8 @@
                         .withLocatorCount(200)
                         .withMapVersionNumber((short) 2)
                         .withMaskLength((byte) 0x02)
+                        .withAction(LispMapReplyAction.Drop)
+                        .withEidPrefixAfi(ipv4Locator2)
                         .build();
     }
 
@@ -79,10 +97,28 @@
     public void testConstruction() {
         DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
 
+        LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
+
         assertThat(record.getRecordTtl(), is(100));
         assertThat(record.isAuthoritative(), is(true));
-        assertThat(record.getLocatorCount(), is(100));
+        assertThat(record.getLocatorCount(), is(0));
         assertThat(record.getMapVersionNumber(), is((short) 1));
         assertThat(record.getMaskLength(), is((byte) 0x01));
+        assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward));
+        assertThat(record.getEidPrefixAfi(), is(ipv4Locator));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+
+        MapRecordWriter writer = new MapRecordWriter();
+        writer.writeTo(byteBuf, record1);
+
+        MapRecordReader reader = new MapRecordReader();
+        LispMapRecord deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(record1, deserialized).testEquals();
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegisterTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegisterTest.java
index 1b0a5a4..f7c5e49 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegisterTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRegisterTest.java
@@ -16,8 +16,15 @@
 package org.onosproject.lisp.msg.protocols;
 
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterReader;
+import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -42,7 +49,7 @@
                         .withIsWantMapNotify(false)
                         .withKeyId((short) 1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapRegister.RegisterBuilder builder2 =
@@ -53,7 +60,7 @@
                         .withIsWantMapNotify(false)
                         .withKeyId((short) 1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapRegister.RegisterBuilder builder3 =
@@ -83,6 +90,20 @@
         assertThat(register.isWantMapNotify(), is(false));
         assertThat(register.getKeyId(), is((short) 1));
         assertThat(register.getNonce(), is(1L));
-        assertThat(register.getRecordCount(), is((byte) 0x01));
+        assertThat(register.getRecordCount(), is((byte) 0));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+
+        RegisterWriter writer = new RegisterWriter();
+        writer.writeTo(byteBuf, register1);
+
+        RegisterReader reader = new RegisterReader();
+        LispMapRegister deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(register1, deserialized).testEquals();
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReplyTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReplyTest.java
index 7b530d2..5b3c899 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReplyTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapReplyTest.java
@@ -16,8 +16,15 @@
 package org.onosproject.lisp.msg.protocols;
 
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader;
+import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyWriter;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -42,7 +49,7 @@
                         .withIsProbe(false)
                         .withIsSecurity(true)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapReply.ReplyBuilder builder2 =
@@ -53,7 +60,7 @@
                         .withIsProbe(false)
                         .withIsSecurity(true)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
         LispMapReply.ReplyBuilder builder3 =
@@ -82,6 +89,19 @@
         assertThat(reply.isProbe(), is(false));
         assertThat(reply.isSecurity(), is(true));
         assertThat(reply.getNonce(), is(1L));
-        assertThat(reply.getRecordCount(), is((byte) 0x01));
+        assertThat(reply.getRecordCount(), is((byte) 0));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+        ReplyWriter writer = new ReplyWriter();
+        writer.writeTo(byteBuf, reply1);
+
+        ReplyReader reader = new ReplyReader();
+        LispMapReply deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(reply1, deserialized).testEquals();
     }
 }
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequestTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequestTest.java
index 155c6a6..088c4f8 100644
--- a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequestTest.java
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/protocols/DefaultLispMapRequestTest.java
@@ -15,12 +15,24 @@
  */
 package org.onosproject.lisp.msg.protocols;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.types.LispAfiAddress;
+import org.onosproject.lisp.msg.types.LispIpv4Address;
+
+import java.util.List;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.onosproject.lisp.msg.protocols.DefaultLispMapRequest.*;
 
 /**
  * Unit tests for DefaultLispMapRequest class.
@@ -34,8 +46,14 @@
     @Before
     public void setup() {
 
-        LispMapRequest.RequestBuilder builder1 =
-                        new DefaultLispMapRequest.DefaultRequestBuilder();
+        RequestBuilder builder1 = new DefaultRequestBuilder();
+
+        LispIpv4Address ipv4Eid1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
+
+        LispIpv4Address ipv4Rloc1 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
+        LispIpv4Address ipv4Rloc2 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
+
+        List<LispAfiAddress> rlocs1 = ImmutableList.of(ipv4Rloc1, ipv4Rloc2);
 
         request1 = builder1
                         .withIsAuthoritative(true)
@@ -44,12 +62,13 @@
                         .withIsProbe(false)
                         .withIsSmr(true)
                         .withIsSmrInvoked(false)
+                        .withSourceEid(ipv4Eid1)
+                        .withItrRlocs(rlocs1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
-        LispMapRequest.RequestBuilder builder2 =
-                        new DefaultLispMapRequest.DefaultRequestBuilder();
+        RequestBuilder builder2 = new DefaultRequestBuilder();
 
         sameAsRequest1 = builder2
                         .withIsAuthoritative(true)
@@ -58,12 +77,20 @@
                         .withIsProbe(false)
                         .withIsSmr(true)
                         .withIsSmrInvoked(false)
+                        .withSourceEid(ipv4Eid1)
+                        .withItrRlocs(rlocs1)
                         .withNonce(1L)
-                        .withRecordCount((byte) 0x01)
+                        .withRecordCount((byte) 0)
                         .build();
 
-        LispMapRequest.RequestBuilder builder3 =
-                        new DefaultLispMapRequest.DefaultRequestBuilder();
+        RequestBuilder builder3 = new DefaultRequestBuilder();
+
+        LispIpv4Address ipv4Eid2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
+
+        LispIpv4Address ipv4Rloc3 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
+        LispIpv4Address ipv4Rloc4 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
+
+        List<LispAfiAddress> rlocs2 = ImmutableList.of(ipv4Rloc3, ipv4Rloc4);
 
         request2 = builder3
                         .withIsAuthoritative(false)
@@ -72,6 +99,8 @@
                         .withIsProbe(true)
                         .withIsSmr(false)
                         .withIsSmrInvoked(true)
+                        .withSourceEid(ipv4Eid2)
+                        .withItrRlocs(rlocs2)
                         .withNonce(2L)
                         .withRecordCount((byte) 0x02)
                         .build();
@@ -95,6 +124,19 @@
         assertThat(request.isSmr(), is(true));
         assertThat(request.isSmrInvoked(), is(false));
         assertThat(request.getNonce(), is(1L));
-        assertThat(request.getRecordCount(), is((byte) 0x01));
+        assertThat(request.getRecordCount(), is((byte) 0));
+    }
+
+    @Test
+    public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
+        ByteBuf byteBuf = Unpooled.buffer();
+        RequestWriter writer = new RequestWriter();
+        writer.writeTo(byteBuf, request1);
+
+        RequestReader reader = new RequestReader();
+        LispMapRequest deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester()
+                .addEqualityGroup(request1, deserialized).testEquals();
     }
 }