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