Migrate deprecated assertThat()
[bgpcep.git] / bgp / util / src / test / java / org / opendaylight / protocol / bgp / util / BGPBinaryFileParserTest.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.util;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.hamcrest.core.Is.is;
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.List;
18 import org.junit.Test;
19
20 public class BGPBinaryFileParserTest {
21
22     private static final byte FF = (byte) 255;
23
24     private static List<byte[]> extractFromFile(final String fileName) throws IOException {
25         final InputStream is = BGPBinaryFileParserTest.class.getResourceAsStream(fileName);
26         if (is == null) {
27             throw new IOException("Failed to get resource " + fileName);
28         }
29
30         final ByteArrayOutputStream bis = new ByteArrayOutputStream();
31         final byte[] data = new byte[1000];
32         int nread;
33         while ((nread = is.read(data, 0, data.length)) != -1) {
34             bis.write(data, 0, nread);
35         }
36         bis.flush();
37         return BinaryBGPDumpFileParser.parseMessages(bis.toByteArray());
38     }
39
40     @Test
41     public void testCorrectExtraction() throws IOException {
42         final List<byte[]> parsedMessages = extractFromFile("/BgpMessages.bin");
43
44         assertThat(parsedMessages.size(), is(43));
45
46         // 1st message
47         assertThat(parsedMessages.get(0).length, is(19));
48         checkMarker(parsedMessages);
49         assertThat(parsedMessages.get(0)[16], is((byte) 0));
50         assertThat(parsedMessages.get(0)[17], is((byte) 19));
51         assertThat(parsedMessages.get(0)[18], is((byte) 4));
52
53         // 39th message
54         assertThat(parsedMessages.get(38).length, is(91));
55         checkMarker(parsedMessages);
56         assertThat(parsedMessages.get(38)[16], is((byte) 0));
57         assertThat(parsedMessages.get(38)[17], is((byte) 91));
58         assertThat(parsedMessages.get(38)[18], is((byte) 2));
59         assertThat(parsedMessages.get(38)[90], is((byte) 236));
60
61     }
62
63     private void checkMarker(final List<byte[]> parsedMessages) {
64         for (int i = 0; i < 16; i++) {
65             assertThat(parsedMessages.get(0)[i], is(FF));
66         }
67     }
68
69     /**
70      * In BgpMessages_wrong_header file, first FF sequence is corrupted.
71      */
72     @Test
73     public void testCorruptedHeader() throws IOException {
74         final List<byte[]> parsedMessages = extractFromFile("/BgpMessages_wrong_header.bin");
75         assertEquals(42, parsedMessages.size());
76     }
77 }