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