Activate code generation
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / InputStreamTest.java
1 /*
2  * Copyright (c) 2013 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.bgp.rib.impl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import java.io.IOException;
18 import java.io.PipedInputStream;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.bgp.parser.BGPMessage;
25 import org.opendaylight.protocol.bgp.parser.BGPMessageHeader;
26 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactory;
27 import org.opendaylight.protocol.bgp.rib.impl.BGPInputStream;
28
29 import org.opendaylight.protocol.framework.DeserializerException;
30 import org.opendaylight.protocol.framework.DocumentedException;
31
32 public class InputStreamTest {
33
34         @Mock
35         PipedInputStream pis;
36
37         @Mock
38         BGPMessageFactory mf;
39
40         @Mock
41         BGPMessageHeader h;
42
43         BGPInputStream is;
44
45         @Before
46         public void setUp() throws IOException {
47                 MockitoAnnotations.initMocks(this);
48                 this.is = (BGPInputStream) BGPInputStream.FACTORY.getProtocolInputStream(this.pis, this.mf);
49         }
50
51         @Test
52         public void testHeaderNotAvailable() throws IOException {
53                 doReturn(BGPMessageHeader.COMMON_HEADER_LENGTH - 4).when(this.pis).available();
54                 doReturn(-1).when(this.pis).read((byte[]) any());
55                 assertFalse(this.is.isMessageAvailable());
56                 assertFalse(this.is.header.isParsed());
57         }
58
59         @Test
60         public void testHeaderAvailable() throws IOException {
61                 doReturn(BGPMessageHeader.COMMON_HEADER_LENGTH).when(this.pis).available();
62                 doReturn(5).when(this.pis).read((byte[]) any());
63                 assertTrue(this.is.isMessageAvailable());
64                 assertTrue(this.is.header.isParsed());
65         }
66
67         @Test
68         public void testGetMessage() throws IOException, DeserializerException, DocumentedException {
69                 doReturn(BGPMessageHeader.COMMON_HEADER_LENGTH).when(this.pis).available();
70                 doReturn(5).when(this.pis).read((byte[]) any());
71                 doReturn("").when(this.h).toString();
72                 this.is.header = this.h;
73                 doNothing().when(this.h).setParsed();
74                 doReturn(true).when(this.h).isParsed();
75                 doReturn(100).when(this.h).getLength();
76                 doReturn(mock(BGPMessage.class)).when(this.mf).parse((byte[]) any(), any(BGPMessageHeader.class));
77                 assertTrue(this.is.getMessage() instanceof BGPMessage);
78         }
79 }