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.tools.junit.BaseTestCase;
19 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
20 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
22 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispDistinguishedNameAddress;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.DistinguishedNameBuilder;
26 public class LispDistinguishedNameAddressSerializerTest extends BaseTestCase {
29 public void deserialize__EmptyString() throws Exception {
30 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 00"));
32 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
33 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
35 assertEquals("", distinguishedNameAddress.getDistinguishedName());
40 public void deserialize__DavidString() throws Exception {
41 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("00 11 64 61 76 69 64 00"));
43 assertEquals(AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode(), address.getAfi().shortValue());
44 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) address;
46 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
51 public void deserialize__inList() throws Exception {
52 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
54 "00 11 64 61 76 69 64 00"));
56 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
57 LcafListAddress lispLCAFAddress = (LcafListAddress) address;
59 assertEquals(LispCanonicalAddressFormatEnum.LIST.getLispCode(), lispLCAFAddress.getLcafType().byteValue());
61 LispDistinguishedNameAddress distinguishedNameAddress = (LispDistinguishedNameAddress) lispLCAFAddress.getAddresses().get(0)
62 .getPrimitiveAddress();
64 assertEquals("david", distinguishedNameAddress.getDistinguishedName());
68 @Test(expected = LispSerializationException.class)
69 public void deserialize__ShorterBuffer() throws Exception {
70 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
72 "00 11 64 61 76 69 64 00"));
75 @Test(expected = LispSerializationException.class)
76 public void deserialize__ShorterBuffer2() throws Exception {
77 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
82 public void deserialize__ReadUntilZero() throws Exception {
83 LispDistinguishedNameAddress address = (LispDistinguishedNameAddress) LispAddressSerializer.getInstance().deserialize(
84 hexToByteBuffer("00 11 64 61 76 00 69 64"));
85 assertEquals("dav", address.getDistinguishedName());
89 public void serialize__Simple() throws Exception {
90 DistinguishedNameBuilder distinguishedName = new DistinguishedNameBuilder().setDistinguishedName("david").setAfi(
91 AddressFamilyNumberEnum.DISTINGUISHED_NAME.getIanaCode());
93 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(distinguishedName.build()));
94 LispAddressSerializer.getInstance().serialize(buf, distinguishedName.build());
95 ByteBuffer expectedBuf = hexToByteBuffer("00 11 64 61 76 69 64 00");
96 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());