Add new revision for pcep types model
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPParserTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
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.rev181109.Close;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.KeepaliveBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.keepalive.message.KeepaliveMessageBuilder;
27
28 public class PCEPParserTest {
29
30     private final MessageRegistry registry = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry();
31
32     @Test
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));
38     }
39
40     @Test
41     public void testByteToMessageEncoding() {
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);
50     }
51
52     @Test
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);
60     }
61 }