2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.rsvp.parser.spi.subobjects;
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;
25 public class XROSubobjectListParserTest {
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);
36 public void setUp() throws RSVPParsingException {
37 Mockito.doAnswer(new Answer<Object>() {
39 public Object answer(final InvocationOnMock invocation) throws Throwable {
40 if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
43 return XROSubobjectListParserTest.this.subObj;
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>() {
49 public Object answer(final InvocationOnMock invocation) throws Throwable {
50 ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
53 }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
56 @Test(expected=RSVPParsingException.class)
57 public void testWrongInput() throws RSVPParsingException {
58 this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
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());
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());
76 private class XroListParser extends XROSubobjectListParser {
77 public XroListParser(final XROSubobjectRegistry subobjReg) {
81 protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
84 protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {