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.implementation.serializer.address;
10 import static junit.framework.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
21 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LcafListAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispDistinguishedNameAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.distinguishedname.DistinguishedNameBuilder;
27 public class LispDistinguishedNameAddressSerializerTest extends BaseTestCase {
30 public void deserialize__EmptyString() throws Exception {
31 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 00"));
33 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
34 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
36 assertEquals("", distinguishedNameAddress.getDistinguishedName());
41 public void deserialize__DavidString() throws Exception {
42 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 64 61 76 69 64 00"));
44 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
45 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
47 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
52 public void deserialize__inList() throws Exception {
53 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
55 "00 11 64 61 76 69 64 00"));
57 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
58 LcafListAddress lispLCAFAddress = (LcafListAddress) address;
60 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lispLCAFAddress.getLcafType().byteValue());
62 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) LispAFIConvertor.toAFIfromPrimitive(lispLCAFAddress.getAddresses().get(0)
63 .getPrimitiveAddress());
65 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
69 @Test(expected = LispSerializationException.class)
70 public void deserialize__ShorterBuffer() throws Exception {
71 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
73 "00 11 64 61 76 69 64 00"));
76 @Test(expected = LispSerializationException.class)
77 public void deserialize__ShorterBuffer2() throws Exception {
78 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
83 public void deserialize__ReadUntilZero() throws Exception {
84 LispDistinguishedNameAddress address = (LispDistinguishedNameAddress) LispAddressSerializer.getInstance().deserialize(
85 hexToByteBuffer("00 11 64 61 76 00 69 64"));
86 assertEquals("dav", address.getDistinguishedName());
90 public void serialize__Simple() throws Exception {
91 DistinguishedNameBuilder distinguishedName = new DistinguishedNameBuilder().setDistinguishedName("david").setAfi(
92 AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode());
94 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(distinguishedName.build()));
95 LispAddressSerializer.getInstance().serialize(buf, distinguishedName.build());
96 ByteBuffer expectedBuf = hexToByteBuffer("00 11 64 61 76 69 64 00");
97 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());