4d1696c47f14e0fe1cdbf8291a053bf2eb47c1a7
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispDistinguishedNameAddressSerializerTest.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.implementation.serializer.address;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13
14 import junitx.framework.ArrayAssert;
15
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
21 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafListAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispDistinguishedNameAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.distinguishedname.DistinguishedNameBuilder;
26
27 public class LispDistinguishedNameAddressSerializerTest extends BaseTestCase {
28
29     @Test
30     public void deserialize__EmptyString() throws Exception {
31         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 00"));
32
33         assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
34         LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
35
36         assertEquals("", distinguishedNameAddress.getDistinguishedName());
37
38     }
39
40     @Test
41     public void deserialize__DavidString() throws Exception {
42         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 64 61 76 69 64 00"));
43
44         assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
45         LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
46
47         assertEquals("david", distinguishedNameAddress.getDistinguishedName());
48
49     }
50
51     @Test
52     public void deserialize__inList() throws Exception {
53         LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
54                 "01 00 00 8 " + //
55                 "00 11 64 61 76 69 64 00"));
56
57         assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
58         LcafListAddress lispLCAFAddress = (LcafListAddress) address;
59
60         assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lispLCAFAddress.getLcafType().byteValue());
61
62         LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) LispAFIConvertor.toAFIfromPrimitive(lispLCAFAddress.getAddresses().get(0)
63                 .getPrimitiveAddress());
64
65         assertEquals("david", distinguishedNameAddress.getDistinguishedName());
66
67     }
68
69     @Test(expected = LispSerializationException.class)
70     public void deserialize__ShorterBuffer() throws Exception {
71         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
72                 "01 00 00 10 " + //
73                 "00 11 64 61 76 69 64 00"));
74     }
75
76     @Test(expected = LispSerializationException.class)
77     public void deserialize__ShorterBuffer2() throws Exception {
78         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
79                 "01 00 00 18 "));
80     }
81
82     @Test
83     public void deserialize__ReadUntilZero() throws Exception {
84         LispDistinguishedNameAddress address = (LispDistinguishedNameAddress) LispAddressSerializer.getInstance().deserialize(
85                 hexToByteBuffer("00 11 64 61 76 00 69 64"));
86         assertEquals("dav", address.getDistinguishedName());
87     }
88
89     @Test
90     public void serialize__Simple() throws Exception {
91         DistinguishedNameBuilder distinguishedName = new DistinguishedNameBuilder().setDistinguishedName("david").setAfi(
92                 AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode());
93
94         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(distinguishedName.build()));
95         LispAddressSerializer.getInstance().serialize(buf, distinguishedName.build());
96         ByteBuffer expectedBuf = hexToByteBuffer("00 11 64 61 76 69 64 00");
97         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
98     }
99
100 }