Code clean up
[bgpcep.git] / bmp / bmp-spi / src / test / java / org / opendaylight / protocol / bmp / spi / parser / AbstractBmpMessageWithTlvParserTest.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.protocol.bmp.spi.parser;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15
16 import com.google.common.base.Preconditions;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.nio.charset.StandardCharsets;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bmp.spi.registry.SimpleBmpTlvRegistry;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.description.tlv.DescriptionTlv;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.description.tlv.DescriptionTlvBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.initiation.TlvsBuilder;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29
30 public class AbstractBmpMessageWithTlvParserTest {
31
32     private final SimpleBmpTlvRegistry registry = new SimpleBmpTlvRegistry();
33     private final SimpleHandler parser = new SimpleHandler(this.registry);
34     private static final byte[] DATA = {0, 1, 0, 4, 't', 'e', 's', 't'};
35     private static final int TYPE = 1;
36
37     private static final BmpTlvSerializer DESCRIPTION_TLV_SERIALIZER = (tlv, output) -> {
38         Preconditions.checkArgument(tlv instanceof DescriptionTlv, "DescriptionTlv is mandatory.");
39         TlvUtil.formatTlvAscii(TYPE, ((DescriptionTlv) tlv).getDescription(), output);
40     };
41
42     private static final BmpTlvParser DESCRIPTION_TLV_PARSER = buffer -> {
43         if (buffer == null) {
44             return null;
45         }
46         return new DescriptionTlvBuilder().setDescription(buffer.toString(StandardCharsets.US_ASCII)).build();
47     };
48
49     @Before
50     public void setUp() {
51         this.registry.registerBmpTlvParser(TYPE, DESCRIPTION_TLV_PARSER);
52         this.registry.registerBmpTlvSerializer(DescriptionTlv.class, DESCRIPTION_TLV_SERIALIZER);
53     }
54
55     @Test
56     public void testParseTlvs() throws BmpDeserializationException {
57         final ByteBuf buffer = Unpooled.EMPTY_BUFFER;
58         final TlvsBuilder builder = new TlvsBuilder();
59         this.parser.parseTlvs(builder, buffer);
60         assertNull(builder.getDescriptionTlv());
61
62         this.parser.parseTlvs(builder, Unpooled.wrappedBuffer(DATA));
63         assertNotNull(builder.getDescriptionTlv());
64         assertEquals("test", builder.getDescriptionTlv().getDescription());
65     }
66
67     @Test
68     public void testSerializeTlv() {
69         final ByteBuf output = Unpooled.buffer();
70         final DescriptionTlvBuilder builder = new DescriptionTlvBuilder().setDescription("test");
71         this.parser.serializeTlv(builder.build(), output);
72         assertArrayEquals(DATA, ByteArray.getAllBytes(output));
73     }
74
75     @Test(expected = BmpDeserializationException.class)
76     public void testParseCorruptedTlv() throws BmpDeserializationException {
77         final byte[] wrongData = {0, 1, 0, 10, 't', 'e', 's', 't'};
78         this.parser.parseTlvs(new TlvsBuilder(), Unpooled.wrappedBuffer(wrongData));
79     }
80
81     private static final class SimpleHandler extends AbstractBmpMessageWithTlvParser<TlvsBuilder> {
82         SimpleHandler(final BmpTlvRegistry tlvRegistry) {
83             super(tlvRegistry);
84         }
85
86         @Override
87         public void serializeMessageBody(final Notification message, final ByteBuf buffer) {
88         }
89
90         @Override
91         public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
92             return null;
93         }
94
95         @Override
96         public int getBmpMessageType() {
97             return 0;
98         }
99
100         @Override
101         protected void addTlv(final TlvsBuilder builder, final Tlv tlv) {
102             if (tlv instanceof DescriptionTlv) {
103                 builder.setDescriptionTlv((DescriptionTlv) tlv);
104             }
105         }
106     }
107 }