Test coverage: rsvp-spi/subobjects
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / XROSubobjectListParserTest.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.RSVPParsingException;
21 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectRegistry;
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.exclude.route.object.exclude.route.object.SubobjectContainer;
24
25 public class XROSubobjectListParserTest {
26
27     private final XROSubobjectRegistry registry = Mockito.mock(XROSubobjectRegistry.class);
28     private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
29     private final XroListParser parser = new XroListParser(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(new Answer<Object>() {
38             @Override
39             public Object answer(final InvocationOnMock invocation) throws Throwable {
40                 if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
41                     return null;
42                 }
43                 return XROSubobjectListParserTest.this.subObj;
44             }
45         }).when(this.registry).parseSubobject(Mockito.anyInt(), Mockito.any(ByteBuf.class), Mockito.anyBoolean());
46         Mockito.doReturn("lala").when(this.subObj).toString();
47         Mockito.doAnswer(new Answer<Object>() {
48             @Override
49             public Object answer(final InvocationOnMock invocation) throws Throwable {
50                 ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
51                 return null;
52             }
53         }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
54     }
55
56     @Test(expected=RSVPParsingException.class)
57     public void testWrongInput() throws RSVPParsingException {
58         this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
59     }
60
61     @Test
62     public void testParseList() throws RSVPParsingException {
63         assertEquals(0, this.parser.parseList(Unpooled.copiedBuffer(this.emptyInput)).size());
64         final ByteBuf toBeParsed = Unpooled.copiedBuffer(this.inputList);
65         assertEquals(2, this.parser.parseList(toBeParsed).size());
66     }
67
68     @Test
69     public void testSerializeList() {
70         final ByteBuf buffer = Unpooled.buffer(2);
71         assertEquals(0, buffer.readableBytes());
72         this.parser.serializeList(this.subobjects, buffer);
73         assertEquals(2, buffer.readableBytes());
74     }
75
76     private class XroListParser extends XROSubobjectListParser {
77         public XroListParser(final XROSubobjectRegistry subobjReg) {
78             super(subobjReg);
79         }
80         @Override
81         protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
82         }
83         @Override
84         protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
85             return null;
86         }
87     }
88 }