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