Skip to content

Commit 56b19aa

Browse files
committed
add Unit Tests for RemoteSegmentMetadataHandler
Signed-off-by: Varun Bansal <[email protected]>
1 parent a204f98 commit 56b19aa

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.index.store.remote.metadata;
10+
11+
import org.apache.lucene.store.OutputStreamIndexOutput;
12+
import org.junit.Before;
13+
import org.opensearch.common.UUIDs;
14+
import org.opensearch.common.bytes.BytesReference;
15+
import org.opensearch.common.io.stream.BytesStreamOutput;
16+
import org.opensearch.common.lucene.store.ByteArrayIndexInput;
17+
import org.opensearch.test.OpenSearchTestCase;
18+
19+
import java.io.IOException;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* Unit Tests for {@link RemoteSegmentMetadataHandler}
25+
*/
26+
public class RemoteSegmentMetadataHandlerTests extends OpenSearchTestCase {
27+
private RemoteSegmentMetadataHandler remoteSegmentMetadataHandler;
28+
29+
@Before
30+
public void setup() throws IOException {
31+
remoteSegmentMetadataHandler = new RemoteSegmentMetadataHandler();
32+
}
33+
34+
public void testReadContent() throws IOException {
35+
BytesStreamOutput output = new BytesStreamOutput();
36+
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput("dummy bytes", "dummy stream", output, 4096);
37+
Map<String, String> expectedOutput = getDummyData();
38+
indexOutput.writeMapOfStrings(expectedOutput);
39+
indexOutput.close();
40+
RemoteSegmentMetadata metadata = remoteSegmentMetadataHandler.readContent(
41+
new ByteArrayIndexInput("dummy bytes", BytesReference.toBytes(output.bytes()))
42+
);
43+
assertEquals(expectedOutput, metadata.toMapOfStrings());
44+
}
45+
46+
public void testWriteContent() throws IOException {
47+
BytesStreamOutput output = new BytesStreamOutput();
48+
OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput("dummy bytes", "dummy stream", output, 4096);
49+
Map<String, String> expectedOutput = getDummyData();
50+
remoteSegmentMetadataHandler.writeContent(indexOutput, RemoteSegmentMetadata.fromMapOfStrings(expectedOutput));
51+
indexOutput.close();
52+
Map<String, String> actualOutput = new ByteArrayIndexInput("dummy bytes", BytesReference.toBytes(output.bytes()))
53+
.readMapOfStrings();
54+
assertEquals(expectedOutput, actualOutput);
55+
}
56+
57+
private Map<String, String> getDummyData() {
58+
Map<String, String> expectedOutput = new HashMap<>();
59+
String prefix = "_0";
60+
expectedOutput.put(
61+
prefix + ".cfe",
62+
prefix + ".cfe::" + prefix + ".cfe__" + UUIDs.base64UUID() + "::" + randomIntBetween(1000, 5000)
63+
);
64+
expectedOutput.put(
65+
prefix + ".cfs",
66+
prefix + ".cfs::" + prefix + ".cfs__" + UUIDs.base64UUID() + "::" + randomIntBetween(1000, 5000)
67+
);
68+
return expectedOutput;
69+
}
70+
}

0 commit comments

Comments
 (0)