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.opendaylight.protocol.rsvp.parser.spi.EROSubobjectRegistry;
22 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AttributeFilter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
27 public class EROSubobjectListParserTest {
29 private final EROSubobjectRegistry registry = Mockito.mock(EROSubobjectRegistry.class);
30 private final SubobjectContainer subObj = Mockito.mock(SubobjectContainer.class);
31 private final RsvpTeObject rsvpTeObj = Mockito.mock(RsvpTeObject.class);
32 private final EroListParser parser = new EroListParser(this.registry);
33 private final byte[] inputList = new byte[] {1, 3, 1, 2, 4, 1, 2};
34 private final byte[] emptyInput = new byte[] {1, 2};
35 private final byte[] wrongInput = new byte[] {1, 3};
36 private final List<SubobjectContainer> subobjects = Arrays.asList(this.subObj, this.subObj);
39 public void setUp() throws RSVPParsingException {
40 Mockito.doAnswer(invocation -> {
41 if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
44 return EROSubobjectListParserTest.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(invocation -> {
48 ((ByteBuf) invocation.getArguments()[1]).writeByte(1);
50 }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
53 @Test(expected=IllegalArgumentException.class)
54 public void testParsingException() throws RSVPParsingException {
55 this.parser.parseList(null);
58 @Test(expected=RSVPParsingException.class)
59 public void testWrongInput() throws RSVPParsingException {
60 this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
64 public void testParseList() throws RSVPParsingException {
65 assertEquals(0, this.parser.parseList(Unpooled.copiedBuffer(this.emptyInput)).size());
66 final ByteBuf toBeParsed = Unpooled.copiedBuffer(this.inputList);
67 assertEquals(2, this.parser.parseList(toBeParsed).size());
71 public void testSerializeList() {
72 final ByteBuf buffer = Unpooled.buffer(2);
73 assertEquals(0, buffer.readableBytes());
74 this.parser.serializeList(this.subobjects, buffer);
75 assertEquals(2, buffer.readableBytes());
79 public void testAbstractRSVPObjParser() throws RSVPParsingException {
80 final ByteBuf byteAggregator = Unpooled.buffer(4);
81 byte[] output = new byte[] {0, 1, 2, 3};
82 this.parser.serializeAttributeHeader(1, (short) 2, (short) 3, byteAggregator);
83 assertArrayEquals(output, byteAggregator.array());
85 final ByteBuf body = Unpooled.buffer(4);
86 output = new byte[] {0, 0, 0, 1};
87 final AttributeFilter filter = new AttributeFilter(1L);
88 this.parser.writeAttributeFilter(filter, body);
89 assertArrayEquals(output, body.array());
91 final ByteBuf parseTeObj = Unpooled.buffer(1);
92 assertNotNull(this.parser.parseObject(parseTeObj));
93 assertNull(this.parser.parseObject(null));
95 assertEquals(0, parseTeObj.readableBytes());
96 this.parser.serializeObject(null, parseTeObj);
97 assertEquals(0, parseTeObj.readableBytes());
98 this.parser.serializeObject(this.rsvpTeObj, parseTeObj);
99 assertEquals(1, parseTeObj.readableBytes());
100 assertEquals((short) 3, parseTeObj.readUnsignedByte());
103 private class EroListParser extends EROSubobjectListParser {
104 public EroListParser(final EROSubobjectRegistry subobjReg) {
108 protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
112 protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
113 return EROSubobjectListParserTest.this.rsvpTeObj;