2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.serializer.address;
10 import static org.junit.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
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.lisp.proto.rev151105.LcafListAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispAFIAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispDistinguishedNameAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.lispaddresscontainer.address.distinguishedname.DistinguishedNameBuilder;
28 public class LispDistinguishedNameAddressSerializerTest extends BaseTestCase {
31 public void deserialize__EmptyString() throws Exception {
32 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 00"));
34 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
35 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
37 assertEquals("", distinguishedNameAddress.getDistinguishedName());
42 public void deserialize__DavidString() throws Exception {
43 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 64 61 76 69 64 00"));
45 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
46 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
48 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
53 public void deserialize__inList() throws Exception {
54 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
56 "00 11 64 61 76 69 64 00"));
58 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
59 LcafListAddress lispLCAFAddress = (LcafListAddress) address;
61 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lispLCAFAddress.getLcafType().byteValue());
63 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) LispAFIConvertor.toAFIfromPrimitive(lispLCAFAddress.getAddresses().get(0)
64 .getPrimitiveAddress());
66 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
70 @Test(expected = LispSerializationException.class)
71 public void deserialize__ShorterBuffer() throws Exception {
72 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
74 "00 11 64 61 76 69 64 00"));
77 @Test(expected = LispSerializationException.class)
78 public void deserialize__ShorterBuffer2() throws Exception {
79 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
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());
91 public void serialize__Simple() throws Exception {
92 DistinguishedNameBuilder distinguishedName = new DistinguishedNameBuilder().setDistinguishedName("david").setAfi(
93 AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode());
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());