Cleanup eclipse warnings
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / EROSubobjectListParserTest.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.spi.subobjects;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.Arrays;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectRegistry;
23 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AttributeFilter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
27
28 public class EROSubobjectListParserTest {
29
30     private final EROSubobjectRegistry registry = Mockito.mock(EROSubobjectRegistry.class);
31     private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
32     private final RsvpTeObject rsvpTeObj = Mockito.mock(RsvpTeObject.class);
33     private final EroListParser parser = new EroListParser(this.registry);
34     private final byte[] inputList = new byte[] {1, 3, 1, 2, 4, 1, 2};
35     private final byte[] emptyInput = new byte[] {1, 2};
36     private final byte[] wrongInput = new byte[] {1, 3};
37     private final List<SubobjectContainer> subobjects = Arrays.asList(this.subObj, this.subObj);
38
39     @Before
40     public void setUp() throws RSVPParsingException {
41         Mockito.doAnswer(invocation -> {
42             if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
43                 return null;
44             }
45             return EROSubobjectListParserTest.this.subObj;
46         }).when(this.registry).parseSubobject(Mockito.anyInt(), Mockito.any(ByteBuf.class), Mockito.anyBoolean());
47         Mockito.doReturn("lala").when(this.subObj).toString();
48         Mockito.doAnswer(invocation -> {
49             ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
50             return null;
51         }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
52     }
53
54     @Test(expected=IllegalArgumentException.class)
55     public void testParsingException() throws RSVPParsingException {
56         this.parser.parseList(null);
57     }
58
59     @Test(expected=RSVPParsingException.class)
60     public void testWrongInput() throws RSVPParsingException {
61         this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
62     }
63
64     @Test
65     public void testParseList() throws RSVPParsingException {
66         assertEquals(0, this.parser.parseList(Unpooled.copiedBuffer(this.emptyInput)).size());
67         final ByteBuf toBeParsed = Unpooled.copiedBuffer(this.inputList);
68         assertEquals(2, this.parser.parseList(toBeParsed).size());
69     }
70
71     @Test
72     public void testSerializeList() {
73         final ByteBuf buffer = Unpooled.buffer(2);
74         assertEquals(0, buffer.readableBytes());
75         this.parser.serializeList(this.subobjects, buffer);
76         assertEquals(2, buffer.readableBytes());
77     }
78
79     @Test
80     public void testAbstractRSVPObjParser() throws RSVPParsingException {
81         final ByteBuf byteAggregator = Unpooled.buffer(4);
82         byte[] output = new byte[] {0, 1, 2, 3};
83         EroListParser.serializeAttributeHeader(1, (short) 2, (short) 3, byteAggregator);
84         assertArrayEquals(output, byteAggregator.array());
85
86         final ByteBuf body = Unpooled.buffer(4);
87         output = new byte[] {0, 0, 0, 1};
88         final AttributeFilter filter = new AttributeFilter(1L);
89         EroListParser.writeAttributeFilter(filter, body);
90         assertArrayEquals(output, body.array());
91
92         final ByteBuf parseTeObj = Unpooled.buffer(1);
93         assertNotNull(this.parser.parseObject(parseTeObj));
94         assertNull(this.parser.parseObject(null));
95
96         assertEquals(0, parseTeObj.readableBytes());
97         this.parser.serializeObject(null, parseTeObj);
98         assertEquals(0, parseTeObj.readableBytes());
99         this.parser.serializeObject(this.rsvpTeObj, parseTeObj);
100         assertEquals(1, parseTeObj.readableBytes());
101         assertEquals((short) 3, parseTeObj.readUnsignedByte());
102     }
103
104     private class EroListParser extends EROSubobjectListParser {
105         public EroListParser(final EROSubobjectRegistry subobjReg) {
106             super(subobjReg);
107         }
108         @Override
109         protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
110             output.writeByte(3);
111         }
112         @Override
113         protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
114             return EROSubobjectListParserTest.this.rsvpTeObj;
115         }
116     }
117 }