2 * Copyright (c) 2014 Cisco Systems, 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.protocol.pcep.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
20 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Close;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
28 public class PCEPParserTest {
30 private final MessageRegistry registry = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry();
33 public void testMessageToByteEncoding() {
34 PCEPMessageToByteEncoder encoder = new PCEPMessageToByteEncoder(this.registry);
35 ByteBuf out = Unpooled.buffer();
36 encoder.encode(null, new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build(), out);
37 assertArrayEquals(new byte[] { 32, 2, 0, 4 }, ByteArray.readAllBytes(out));
41 public void testByteToMessageEncoding() throws Exception {
42 PCEPByteToMessageDecoder decoder = new PCEPByteToMessageDecoder(this.registry);
43 List<Object> out = new ArrayList<>();
44 decoder.decode(null, Unpooled.wrappedBuffer(new byte[] { 32, 2, 0, 4 }), out);
45 assertTrue(out.get(0) instanceof Keepalive);
46 decoder.decode(null, Unpooled.wrappedBuffer(new byte[] { 0x20, 0x07, 0, 0x0C, 0x0F, 0x10, 0, 8, 0, 0, 0, 5 }), out);
47 assertTrue(out.get(1) instanceof Close);
48 decoder.decode(null, Unpooled.wrappedBuffer(new byte[] { 0x20, 06, 00, 0x18, 0x21,0x10, 00,0x0c, 00,00,00,00,00,00,00,01,0x0d,0x10,00,0x08,00,00,0x18,02 }), out);
49 assertTrue(out.get(2) instanceof Pcerr);
53 public void testHandlerFactory() {
54 PCEPHandlerFactory handlers = new PCEPHandlerFactory(this.registry);
55 assertEquals(1, handlers.getEncoders().length);
56 assertTrue(handlers.getEncoders()[0] instanceof PCEPMessageToByteEncoder);
57 assertEquals(2, handlers.getDecoders().length);
58 assertTrue(handlers.getDecoders()[0] instanceof PCEPMessageHeaderDecoder);
59 assertTrue(handlers.getDecoders()[1] instanceof PCEPByteToMessageDecoder);