Fix sonar complains
[bgpcep.git] / rsvp / spi / src / test / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / EROSubobjectListParserTest.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.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;
26
27 public class EROSubobjectListParserTest {
28
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);
37
38     @Before
39     public void setUp() throws RSVPParsingException {
40         Mockito.doAnswer(invocation -> {
41             if (((ByteBuf) invocation.getArguments()[1]).readableBytes() == 0) {
42                 return null;
43             }
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);
49             return null;
50         }).when(this.registry).serializeSubobject(Mockito.any(SubobjectContainer.class), Mockito.any(ByteBuf.class));
51     }
52
53     @Test(expected=IllegalArgumentException.class)
54     public void testParsingException() throws RSVPParsingException {
55         this.parser.parseList(null);
56     }
57
58     @Test(expected=RSVPParsingException.class)
59     public void testWrongInput() throws RSVPParsingException {
60         this.parser.parseList(Unpooled.copiedBuffer(this.wrongInput));
61     }
62
63     @Test
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());
68     }
69
70     @Test
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());
76     }
77
78     @Test
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());
84
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());
90
91         final ByteBuf parseTeObj = Unpooled.buffer(1);
92         assertNotNull(this.parser.parseObject(parseTeObj));
93         assertNull(this.parser.parseObject(null));
94
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());
101     }
102
103     private class EroListParser extends EROSubobjectListParser {
104         public EroListParser(final EROSubobjectRegistry subobjReg) {
105             super(subobjReg);
106         }
107         @Override
108         protected void localSerializeObject(final RsvpTeObject rsvpTeObject, final ByteBuf output) {
109             output.writeByte(3);
110         }
111         @Override
112         protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
113             return EROSubobjectListParserTest.this.rsvpTeObj;
114         }
115     }
116 }