Promote MessageRegistry to pcep-api
[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.MessageRegistry;
20 import org.opendaylight.protocol.pcep.spi.pojo.DefaultPCEPExtensionConsumerContext;
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 = new DefaultPCEPExtensionConsumerContext().getMessageHandlerRegistry();
31
32     @Test
33     public void testMessageToByteEncoding() {
34         PCEPMessageToByteEncoder encoder = new PCEPMessageToByteEncoder(this.registry);
35         ByteBuf out = Unpooled.buffer();
36         encoder.encode(
37             null, new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build(), out);
38         assertArrayEquals(new byte[] { 32, 2, 0, 4 }, ByteArray.readAllBytes(out));
39     }
40
41     @Test
42     public void testByteToMessageEncoding() {
43         PCEPByteToMessageDecoder decoder = new PCEPByteToMessageDecoder(this.registry);
44         List<Object> out = new ArrayList<>();
45         decoder.decode(null,
46             Unpooled.wrappedBuffer(new byte[] {
47                 32, 2, 0, 4
48             }),
49             out);
50         assertTrue(out.get(0) instanceof Keepalive);
51         decoder.decode(null,
52             Unpooled.wrappedBuffer(new byte[] {
53                 0x20, 0x07, 0, 0x0C, 0x0F, 0x10, 0, 8,
54                 0, 0, 0, 5
55             }),
56             out);
57         assertTrue(out.get(1) instanceof Close);
58         decoder.decode(null,
59             Unpooled.wrappedBuffer(new byte[] {
60                 0x20, 06, 00, 0x18, 0x21, 0x10, 00, 0x0c,
61                 00, 00, 00, 00, 00, 00, 00, 01,
62                 0x0d, 0x10, 00, 0x08, 00, 00, 0x18, 02
63             }),
64             out);
65         assertTrue(out.get(2) instanceof Pcerr);
66     }
67
68     @Test
69     public void testHandlerFactory() {
70         PCEPHandlerFactory handlers = new PCEPHandlerFactory(this.registry);
71         assertEquals(1, handlers.getEncoders().length);
72         assertTrue(handlers.getEncoders()[0] instanceof PCEPMessageToByteEncoder);
73         assertEquals(2, handlers.getDecoders().length);
74         assertTrue(handlers.getDecoders()[0] instanceof PCEPMessageHeaderDecoder);
75         assertTrue(handlers.getDecoders()[1] instanceof PCEPByteToMessageDecoder);
76     }
77 }