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