Remove use of junit-addons
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / AfiListSerializerTest.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.serializer.address;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13
14 import com.google.common.collect.ImmutableSet;
15 import java.nio.ByteBuffer;
16 import java.util.Iterator;
17 import java.util.Set;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
20 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddressBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
29
30 public class AfiListSerializerTest extends BaseTestCase {
31
32     @Test
33     public void deserialize__Simple() throws Exception {
34         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
35                 + "01 00 00 18 "
36                 + "00 01 AA BB CC DD " // IPv4
37                 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
38
39         assertEquals(AfiListLcaf.VALUE, address.getAddressType());
40         AfiList afiList = (AfiList) address.getAddress();
41
42         Set<SimpleAddress> addressList = afiList.getAfiList().getAddressList();
43         assertEquals(2, addressList.size());
44
45         Iterator<SimpleAddress> it = addressList.iterator();
46         assertEquals("170.187.204.221", it.next().stringValue());
47         assertEquals("1122:3344:1122:3344:1122:3344:1122:3344", it.next().stringValue());
48     }
49
50     @Test
51     public void deserialize__NoAddresses() throws Exception {
52         Rloc address = LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
53                 + "01 00 00 00 "));
54
55         assertEquals(AfiListLcaf.VALUE, address.getAddressType());
56         AfiList afiList = (AfiList) address.getAddress();
57
58         assertEquals(Set.of(), afiList.getAfiList().getAddressList());
59     }
60
61     @Test(expected = LispSerializationException.class)
62     public void deserialize__ShorterBuffer() throws Exception {
63         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("""
64             40 03 00 00 \
65             01 00 00 18 \
66             00 01 AA BB CC DD \
67             00 02 11 22 33 44 11 22 33 44 11 22 33 44"""));
68     }
69
70     @Test(expected = LispSerializationException.class)
71     public void deserialize__ShorterBuffer2() throws Exception {
72         LispAddressSerializer.getInstance().deserializeRloc(hexToByteBuffer("40 03 00 00 "
73                 + "01 00 00 18 "));
74     }
75
76     @Test
77     public void serialize__Simple() throws Exception {
78         AfiListBuilder listBuilder = new AfiListBuilder();
79         listBuilder.setAddressList(ImmutableSet.of(
80             SimpleAddressBuilder.getDefaultInstance("170.187.204.221"),
81             SimpleAddressBuilder.getDefaultInstance("1122:3344:1122:3344:1122:3344:1122:3344")));
82
83         RlocBuilder rb = new RlocBuilder();
84         rb.setAddressType(AfiListLcaf.VALUE);
85         rb.setVirtualNetworkId(null);
86         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
87             .lisp.address.address.AfiListBuilder()
88             .setAfiList(listBuilder.build()).build());
89
90         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
91         LispAddressSerializer.getInstance().serialize(buf, rb.build());
92         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
93                 + "01 00 00 18 "
94                 + "00 01 AA BB CC DD " // IPv4
95                 + "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
96         assertArrayEquals(expectedBuf.array(), buf.array());
97     }
98
99     @Test
100     public void serialize__NoAddresses() throws Exception {
101         AfiListBuilder listBuilder = new AfiListBuilder();
102         listBuilder.setAddressList(Set.of());
103
104         RlocBuilder rb = new RlocBuilder();
105         rb.setAddressType(AfiListLcaf.VALUE);
106         rb.setVirtualNetworkId(null);
107         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
108             .lisp.address.address.AfiListBuilder()
109             .setAfiList(listBuilder.build()).build());
110
111         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(rb.build()));
112         LispAddressSerializer.getInstance().serialize(buf, rb.build());
113         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
114                 + "01 00 00 00");
115         assertArrayEquals(expectedBuf.array(), buf.array());
116     }
117
118     @Test
119     public void equals__Simple() throws Exception {
120         final SimpleAddress ip1 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:1");
121         final SimpleAddress ip2 = SimpleAddressBuilder.getDefaultInstance("0:0:0:0:0:0:0:2");
122
123         AfiListBuilder listBuilder = new AfiListBuilder().setAddressList(Set.of(ip1));
124
125         final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
126                 .lisp.address.address.afi.list.AfiList address1 = listBuilder.build();
127         listBuilder.setAddressList(Set.of(ip1));
128         final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
129                 .lisp.address.address.afi.list.AfiList address2 = listBuilder.build();
130         listBuilder.setAddressList(Set.of(ip2));
131         final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
132                 .lisp.address.address.afi.list.AfiList address3 = listBuilder.build();
133
134         assertEquals(address1, address2);
135         assertNotEquals(address2, address3);
136         assertNotEquals(address1, address3);
137     }
138 }