Bump versions to 0.21.8-SNAPSHOT
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / RROSubobjectListParserTest.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.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.Arrays;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectRegistry;
20 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects.list.SubobjectContainer;
23
24 public class RROSubobjectListParserTest {
25
26     private final RROSubobjectRegistry registry = Mockito.mock(RROSubobjectRegistry.class);
27     private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
28     private final RroListParser parser = new RroListParser(this.registry);
29     private final byte[] inputList = new byte[]{1, 3, 1, 2, 4, 1, 2};
30     private final byte[] emptyInput = new byte[]{1, 2};
31     private final byte[] wrongInput = new byte[]{1, 3};
32     private final List<SubobjectContainer> subobjects = Arrays.asList(this.subObj, this.subObj);
33
34     @Before
35     public void setUp() throws RSVPParsingException {
36         Mockito.doAnswer(invocation -> {
37             if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
38                 return null;
39             }
40             return RROSubobjectListParserTest.this.subObj;
41         }).when(this.registry).parseSubobject(Mockito.anyInt(), Mockito.any(ByteBuf.class));
42         Mockito.doReturn("lala").when(this.subObj).toString();
43         Mockito.doAnswer(invocation -> {
44             ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
45             return null;
46         }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
47     }
48
49     @Test(expected = IllegalArgumentException.class)
50     public void testParsingException() throws RSVPParsingException {
51         this.parser.parseList(null);
52     }
53
54     @Test(expected = RSVPParsingException.class)
55     public void testWrongInput() throws RSVPParsingException {
56         this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
57     }
58
59     @Test
60     public void testParseList() throws RSVPParsingException {
61         assertEquals(0, this.parser.parseList(Unpooled.copiedBuffer(this.emptyInput)).size());
62         final ByteBuf toBeParsed = Unpooled.copiedBuffer(this.inputList);
63         assertEquals(2, this.parser.parseList(toBeParsed).size());
64     }
65
66     @Test
67     public void testSerializeList() {
68         final ByteBuf buffer = Unpooled.buffer(2);
69         assertEquals(0, buffer.readableBytes());
70         this.parser.serializeList(this.subobjects, buffer);
71         assertEquals(2, buffer.readableBytes());
72     }
73
74     private class RroListParser extends RROSubobjectListParser {
75         RroListParser(final RROSubobjectRegistry subobjReg) {
76             super(subobjReg);
77         }
78
79         @Override
80         protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
81         }
82
83         @Override
84         protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
85             return null;
86         }
87     }
88 }