16f7bd830b46e5907dc2ea0a51dbdebc485b68fc
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / persisted / ChunkedOutputStreamTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.controller.cluster.datastore.persisted;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import java.io.IOException;
14 import java.util.List;
15 import org.junit.Test;
16
17 public class ChunkedOutputStreamTest {
18     private static final int INITIAL_SIZE = 256;
19
20     private final ChunkedOutputStream stream = new ChunkedOutputStream(INITIAL_SIZE);
21
22     @Test
23     public void testBasicWrite() throws IOException {
24         for (int i = 0; i < INITIAL_SIZE; ++i) {
25             stream.write(i);
26         }
27
28         final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
29         assertEquals(INITIAL_SIZE, chunk.length);
30         for (int i = 0; i < INITIAL_SIZE; ++i) {
31             assertEquals((byte) i, chunk[i]);
32         }
33     }
34
35     @Test
36     public void testBasicLargeWrite() throws IOException {
37         final byte[] array = createArray(INITIAL_SIZE);
38         stream.write(array);
39         final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
40         assertArrayEquals(array, chunk);
41     }
42
43     @Test
44     public void testGrowWrite() throws IOException {
45         for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
46             stream.write(i);
47         }
48
49         final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
50         assertEquals(INITIAL_SIZE * 2, chunk.length);
51         for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
52             assertEquals((byte) i, chunk[i]);
53         }
54     }
55
56     @Test
57     public void testGrowLargeWrite() throws IOException {
58         final byte[] array = createArray(INITIAL_SIZE * 2);
59         stream.write(array);
60         final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
61         assertArrayEquals(array, chunk);
62     }
63
64     @Test
65     public void testTwoChunksWrite() throws IOException {
66         int size = ChunkedOutputStream.MAX_ARRAY_SIZE + 1;
67         for (int i = 0; i < size; ++i) {
68             stream.write(i);
69         }
70
71         int counter = 0;
72         for (byte[] chunk: assertFinishedStream(size, 2)) {
73             for (byte actual: chunk) {
74                 assertEquals((byte) counter++, actual);
75             }
76         }
77     }
78
79     private List<byte[]> assertFinishedStream(final int expectedSize, final int expectedChunks) {
80         stream.close();
81         final ChunkedByteArray array = stream.toChunkedByteArray();
82         assertEquals(expectedSize, array.size());
83
84         final List<byte[]> chunks = array.getChunks();
85         assertEquals(expectedChunks, chunks.size());
86         return chunks;
87     }
88
89     private static byte[] createArray(final int size) {
90         final byte[] array = new byte[size];
91         for (int i = 0; i < size; ++i) {
92             array[i] = (byte) i;
93         }
94         return array;
95     }
96 }