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