|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.io; |
| 10 | + |
| 11 | +import org.apache.lucene.codecs.CodecUtil; |
| 12 | +import org.apache.lucene.index.CorruptIndexException; |
| 13 | +import org.apache.lucene.index.IndexFormatTooNewException; |
| 14 | +import org.apache.lucene.index.IndexFormatTooOldException; |
| 15 | +import org.apache.lucene.store.BufferedChecksumIndexInput; |
| 16 | +import org.apache.lucene.store.IndexInput; |
| 17 | +import org.apache.lucene.store.IndexOutput; |
| 18 | +import org.apache.lucene.store.OutputStreamIndexOutput; |
| 19 | +import org.junit.Before; |
| 20 | +import org.opensearch.common.bytes.BytesReference; |
| 21 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 22 | +import org.opensearch.common.lucene.store.ByteArrayIndexInput; |
| 23 | +import org.opensearch.test.OpenSearchTestCase; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.Mockito.doAnswer; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link org.opensearch.common.io.VersionedCodecStreamWrapper}. |
| 34 | + */ |
| 35 | +public class VersionedCodecStreamWrapperTests extends OpenSearchTestCase { |
| 36 | + |
| 37 | + private static final String CODEC = "dummycodec"; |
| 38 | + private static final int VERSION = 1; |
| 39 | + |
| 40 | + IndexIOStreamHandler<DummyObject> ioStreamHandler; |
| 41 | + VersionedCodecStreamWrapper<DummyObject> versionedCodecStreamWrapper; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() throws IOException { |
| 45 | + ioStreamHandler = mock(IndexIOStreamHandler.class); |
| 46 | + versionedCodecStreamWrapper = new VersionedCodecStreamWrapper(ioStreamHandler, VERSION, CODEC); |
| 47 | + } |
| 48 | + |
| 49 | + public void testReadStream() throws IOException { |
| 50 | + DummyObject expectedObject = new DummyObject("test read"); |
| 51 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 52 | + DummyObject readData = versionedCodecStreamWrapper.readStream(createHeaderFooterBytes(CODEC, VERSION, true, true)); |
| 53 | + assertEquals(readData, expectedObject); |
| 54 | + } |
| 55 | + |
| 56 | + public void testReadWithOldVersionThrowsException() throws IOException { |
| 57 | + DummyObject expectedObject = new DummyObject("test read"); |
| 58 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 59 | + assertThrows( |
| 60 | + IndexFormatTooOldException.class, |
| 61 | + () -> versionedCodecStreamWrapper.readStream(createHeaderFooterBytes(CODEC, 0, true, true)) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public void testReadWithNewVersionThrowsException() throws IOException { |
| 66 | + DummyObject expectedObject = new DummyObject("test read"); |
| 67 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 68 | + assertThrows( |
| 69 | + IndexFormatTooNewException.class, |
| 70 | + () -> versionedCodecStreamWrapper.readStream(createHeaderFooterBytes(CODEC, 2, true, true)) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public void testReadWithUnexpectedCodecThrowsException() throws IOException { |
| 75 | + DummyObject expectedObject = new DummyObject("test read"); |
| 76 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 77 | + assertThrows( |
| 78 | + CorruptIndexException.class, |
| 79 | + () -> versionedCodecStreamWrapper.readStream(createHeaderFooterBytes("wrong codec", VERSION, true, true)) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public void testReadWithNoHeaderThrowsException() throws IOException { |
| 84 | + DummyObject expectedObject = new DummyObject("test read"); |
| 85 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 86 | + assertThrows( |
| 87 | + CorruptIndexException.class, |
| 88 | + () -> versionedCodecStreamWrapper.readStream(createHeaderFooterBytes("wrong codec", VERSION, false, true)) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + public void testReadWithNoFooterThrowsException() throws IOException { |
| 93 | + DummyObject expectedObject = new DummyObject("test read"); |
| 94 | + when(ioStreamHandler.readContent(any())).thenReturn(expectedObject); |
| 95 | + assertThrows( |
| 96 | + CorruptIndexException.class, |
| 97 | + () -> versionedCodecStreamWrapper.readStream(createHeaderFooterBytes("wrong codec", VERSION, true, false)) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + public void testWriteStream() throws IOException { |
| 102 | + DummyObject expectedObject = new DummyObject("test read"); |
| 103 | + BytesStreamOutput output = new BytesStreamOutput(); |
| 104 | + OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput("dummy bytes", "dummy stream", output, 4096); |
| 105 | + doAnswer(invocation -> { |
| 106 | + IndexOutput io = invocation.getArgument(0); |
| 107 | + io.writeString("test write"); |
| 108 | + return null; |
| 109 | + }).when(ioStreamHandler).writeContent(indexOutput, expectedObject); |
| 110 | + versionedCodecStreamWrapper.writeStream(indexOutput, expectedObject); |
| 111 | + indexOutput.close(); |
| 112 | + IndexInput indexInput = new ByteArrayIndexInput("dummy bytes", BytesReference.toBytes(output.bytes())); |
| 113 | + BufferedChecksumIndexInput bii = new BufferedChecksumIndexInput(indexInput); |
| 114 | + |
| 115 | + CodecUtil.checkHeader(bii, CODEC, VERSION, VERSION); |
| 116 | + assertEquals(bii.readString(), "test write"); |
| 117 | + CodecUtil.checkFooter(bii); |
| 118 | + } |
| 119 | + |
| 120 | + private ByteArrayIndexInput createHeaderFooterBytes(String codec, int version, boolean writeHeader, boolean writeFooter) |
| 121 | + throws IOException { |
| 122 | + BytesStreamOutput output = new BytesStreamOutput(); |
| 123 | + OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput("dummy bytes", "dummy stream", output, 4096); |
| 124 | + if (writeHeader) { |
| 125 | + CodecUtil.writeHeader(indexOutput, codec, version); |
| 126 | + } |
| 127 | + if (writeFooter) { |
| 128 | + CodecUtil.writeFooter(indexOutput); |
| 129 | + } |
| 130 | + indexOutput.close(); |
| 131 | + return new ByteArrayIndexInput("dummy bytes", BytesReference.toBytes(output.bytes())); |
| 132 | + } |
| 133 | + |
| 134 | + private static class DummyObject { |
| 135 | + private static String dummyString; |
| 136 | + |
| 137 | + public DummyObject(String dummy) { |
| 138 | + dummyString = dummy; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments