351093a3c726875d3f4cae78be62b55b234f837a
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / DistinguishedNameSerializerTest.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 import junitx.framework.ArrayAssert;
14 import org.junit.Test;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
16 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
17 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameAfi;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedName;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedNameBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
26
27 public class DistinguishedNameSerializerTest extends BaseTestCase {
28
29     @Test
30     public void deserialize__EmptyString() throws Exception {
31         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("00 11 00"), null);
32
33         assertEquals(DistinguishedNameAfi.VALUE, address.getAddressType());
34         DistinguishedName distinguishedName = (DistinguishedName) address.getAddress();
35
36         assertEquals("", distinguishedName.getDistinguishedName().getValue());
37     }
38
39     @Test
40     public void deserialize__DavidString() throws Exception {
41         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("00 11 64 61 76 69 64 00"),
42                 null);
43
44         assertEquals(DistinguishedNameAfi.VALUE, address.getAddressType());
45         DistinguishedName distinguishedName = (DistinguishedName) address.getAddress();
46
47         assertEquals("david", distinguishedName.getDistinguishedName().getValue());
48
49     }
50
51     @Test
52     public void deserialize__inList() throws Exception {
53         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
54                 + "01 00 00 8 "
55                 + "00 11 64 61 76 69 64 00"), null);
56
57         assertEquals(AfiListLcaf.VALUE, address.getAddressType());
58         assertEquals("david", ((AfiList) address.getAddress()).getAfiList().getAddressList().iterator().next()
59                 .getDistinguishedNameType().getValue());
60
61     }
62
63     @Test(expected = LispSerializationException.class)
64     public void deserialize__ShorterBuffer() throws Exception {
65         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("""
66             40 03 00 00 \
67             01 00 00 10 \
68             00 11 64 61 76 69 64 00"""), null);
69     }
70
71     @Test(expected = LispSerializationException.class)
72     public void deserialize__ShorterBuffer2() throws Exception {
73         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
74                 + "01 00 00 18 "), null);
75     }
76
77     @Test
78     public void deserialize__ReadUntilZero() throws Exception {
79         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("00 11 64 61 76 00 69 64"),
80                 null);
81
82         DistinguishedName distinguishedName = (DistinguishedName) address.getAddress();
83
84         assertEquals("dav", distinguishedName.getDistinguishedName().getValue());
85     }
86
87     @Test
88     public void serialize__Simple() throws Exception {
89         EidBuilder eb = new EidBuilder();
90         eb.setAddressType(DistinguishedNameAfi.VALUE);
91         eb.setVirtualNetworkId(null);
92         eb.setAddress(new DistinguishedNameBuilder().setDistinguishedName(new DistinguishedNameType("david")).build());
93
94         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
95         LispAddressSerializer.getInstance().serialize(buf, eb.build());
96         ByteBuffer expectedBuf = hexToByteBuffer("00 11 64 61 76 69 64 00");
97         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
98     }
99 }