Migrate away from legacy setters
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / LabelSubobjectParserTest.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.protocol.rsvp.parser.impl.subobject.label.GeneralizedLabelParser;
18 import org.opendaylight.protocol.rsvp.parser.impl.subobject.label.Type1LabelParser;
19 import org.opendaylight.protocol.rsvp.parser.impl.subobject.label.WavebandSwitchingLabelParser;
20 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint32;
29
30 public class LabelSubobjectParserTest {
31     private static final byte[] GENERALIZED_LABEL_BYTES = {(byte) 0x80, 0x02, 0x00, 0x04, 0x12, 0x00, 0x25,
32         (byte) 0xFF};
33
34     private static final byte[] TYPE_ONE_LABEL_BYTES = {(byte) 0x81, 0x01, 0x12, 0x00, 0x25, (byte) 0xFF};
35
36     private static final byte[] WAVEBAND_LABEL_BYTES = {0x01, 0x03, 0x00, 0x00, 0x12, 0x34, 0x00, 0x00, (byte) 0x99,
37         (byte) 0x99, 0x00, 0x00, 0x11, 0x11};
38
39     @Test
40     public void testGeneralizedLabel() throws RSVPParsingException {
41         final GeneralizedLabelParser parser = new GeneralizedLabelParser();
42         final GeneralizedLabelBuilder iBuilder = new GeneralizedLabelBuilder();
43         iBuilder.setGeneralizedLabel(ByteArray.cutBytes(GENERALIZED_LABEL_BYTES, 2));
44         final GeneralizedLabelCaseBuilder builder = new GeneralizedLabelCaseBuilder()
45             .setGeneralizedLabel(iBuilder.build());
46         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(
47             ByteArray.cutBytes(GENERALIZED_LABEL_BYTES, 2))));
48         final ByteBuf buff = Unpooled.buffer();
49         parser.serializeLabel(true, false, builder.build(), buff);
50         assertArrayEquals(GENERALIZED_LABEL_BYTES, ByteArray.getAllBytes(buff));
51
52         try {
53             parser.parseLabel(null);
54             fail();
55         } catch (final IllegalArgumentException e) {
56             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
57         }
58
59         try {
60             parser.parseLabel(Unpooled.EMPTY_BUFFER);
61             fail();
62         } catch (final IllegalArgumentException e) {
63             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
64         }
65     }
66
67     @Test
68     public void testWavebandLabel() throws RSVPParsingException {
69         final WavebandSwitchingLabelParser parser = new WavebandSwitchingLabelParser();
70         final WavebandSwitchingLabelBuilder iBuilder = new WavebandSwitchingLabelBuilder()
71                 .setWavebandId(Uint32.valueOf(0x1234))
72                 .setStartLabel(Uint32.valueOf(0x9999))
73                 .setEndLabel(Uint32.valueOf(0x1111));
74         final WavebandSwitchingLabelCaseBuilder builder = new WavebandSwitchingLabelCaseBuilder()
75             .setWavebandSwitchingLabel(iBuilder.build());
76         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(
77             ByteArray.cutBytes(WAVEBAND_LABEL_BYTES, 2))));
78         final ByteBuf buff = Unpooled.buffer();
79         parser.serializeLabel(false, true, builder.build(), buff);
80         assertArrayEquals(WAVEBAND_LABEL_BYTES, ByteArray.getAllBytes(buff));
81
82         try {
83             parser.parseLabel(null);
84             fail();
85         } catch (final IllegalArgumentException e) {
86             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
87         }
88         try {
89             parser.parseLabel(Unpooled.EMPTY_BUFFER);
90             fail();
91         } catch (final IllegalArgumentException e) {
92             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
93         }
94     }
95
96     @Test
97     public void testTypeOneLabel() throws RSVPParsingException {
98         final Type1LabelParser parser = new Type1LabelParser();
99         final Type1LabelCaseBuilder builder = new Type1LabelCaseBuilder()
100                 .setType1Label(new Type1LabelBuilder().setType1Label(Uint32.valueOf(0x120025ffL)).build());
101         assertEquals(builder.build(), parser.parseLabel(Unpooled.wrappedBuffer(
102             ByteArray.cutBytes(TYPE_ONE_LABEL_BYTES, 2))));
103         final ByteBuf buff = Unpooled.buffer();
104         parser.serializeLabel(true, true, builder.build(), buff);
105         assertArrayEquals(TYPE_ONE_LABEL_BYTES, ByteArray.getAllBytes(buff));
106
107         try {
108             parser.parseLabel(null);
109             fail();
110         } catch (final IllegalArgumentException e) {
111             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
112         }
113         try {
114             parser.parseLabel(Unpooled.EMPTY_BUFFER);
115             fail();
116         } catch (final IllegalArgumentException e) {
117             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
118         }
119     }
120 }