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