2 * Copyright (c) 2013 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.bgp.rib.impl;
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;
17 import java.io.IOException;
18 import java.io.PipedInputStream;
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;
29 import org.opendaylight.protocol.framework.DeserializerException;
30 import org.opendaylight.protocol.framework.DocumentedException;
32 public class InputStreamTest {
46 public void setUp() throws IOException {
47 MockitoAnnotations.initMocks(this);
48 this.is = (BGPInputStream) BGPInputStream.FACTORY.getProtocolInputStream(this.pis, this.mf);
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());
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());
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);