Skip to content

Commit fef4f49

Browse files
committed
add tests
Signed-off-by: Karen X <karenxyr@gmail.com>
1 parent 28b984c commit fef4f49

File tree

3 files changed

+159
-5
lines changed

3 files changed

+159
-5
lines changed

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/request/document/bulk/ActiveShardCountProtoUtilsTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,25 @@ public void testGetActiveShardCountWithWaitForActiveShardsUnspecified() {
5959
assertEquals("Should have DEFAULT active shard count", ActiveShardCount.DEFAULT, result);
6060
}
6161

62-
// TODO: WaitForActiveShards structure changed in protobufs 0.8.0
63-
/*
6462
public void testGetActiveShardCountWithWaitForActiveShardsInt32() {
65-
6663
// Create a protobuf BulkRequest with wait_for_active_shards = 2
67-
WaitForActiveShards waitForActiveShards = WaitForActiveShards.newBuilder().setCount(2).build();
64+
WaitForActiveShards waitForActiveShards = WaitForActiveShards.newBuilder().setInt32(2).build();
6865

6966
ActiveShardCount result = ActiveShardCountProtoUtils.parseProto(waitForActiveShards);
7067

7168
// Verify the result
7269
assertEquals("Should have active shard count of 2", ActiveShardCount.from(2), result);
7370
}
74-
*/
71+
72+
public void testGetActiveShardCountWithWaitForActiveShardsInt32Zero() {
73+
// Create a protobuf BulkRequest with wait_for_active_shards = 0
74+
WaitForActiveShards waitForActiveShards = WaitForActiveShards.newBuilder().setInt32(0).build();
75+
76+
ActiveShardCount result = ActiveShardCountProtoUtils.parseProto(waitForActiveShards);
77+
78+
// Verify the result
79+
assertEquals("Should have active shard count of 0", ActiveShardCount.from(0), result);
80+
}
7581

7682
public void testGetActiveShardCountWithWaitForActiveShardsNoCase() {
7783
// Create a protobuf BulkRequest with wait_for_active_shards but no case set

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/request/search/InnerHitsBuilderProtoUtilsTests.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,71 @@ public void testFromProtoWithEmptyList() throws IOException {
246246
assertNotNull("InnerHitBuilder list should not be null", innerHitBuilders);
247247
assertEquals("Should have 0 InnerHitBuilders", 0, innerHitBuilders.size());
248248
}
249+
250+
public void testFromProtoWithNullInnerHits() {
251+
// Test null input validation for single InnerHits
252+
IllegalArgumentException exception = expectThrows(
253+
IllegalArgumentException.class,
254+
() -> InnerHitsBuilderProtoUtils.fromProto((InnerHits) null)
255+
);
256+
257+
assertEquals("InnerHits cannot be null", exception.getMessage());
258+
}
259+
260+
public void testFromProtoWithNullList() {
261+
// Test null input validation for InnerHits list
262+
IllegalArgumentException exception = expectThrows(
263+
IllegalArgumentException.class,
264+
() -> InnerHitsBuilderProtoUtils.fromProto((List<InnerHits>) null)
265+
);
266+
267+
assertEquals("InnerHits list cannot be null", exception.getMessage());
268+
}
269+
270+
public void testFromProtoWithSort() throws IOException {
271+
// Create a protobuf InnerHits with sort (this will throw UnsupportedOperationException due to SortBuilderProtoUtils)
272+
InnerHits innerHits = InnerHits.newBuilder()
273+
.setName("test_inner_hits")
274+
.addSort(org.opensearch.protobufs.SortCombinations.newBuilder().build())
275+
.build();
276+
277+
// This should throw UnsupportedOperationException from SortBuilderProtoUtils.fromProto
278+
UnsupportedOperationException exception = expectThrows(
279+
UnsupportedOperationException.class,
280+
() -> InnerHitsBuilderProtoUtils.fromProto(innerHits)
281+
);
282+
283+
assertEquals("sort not supported yet", exception.getMessage());
284+
}
285+
286+
public void testFromProtoWithHighlight() throws IOException {
287+
// Create a protobuf InnerHits with highlight
288+
org.opensearch.protobufs.Highlight highlightProto = org.opensearch.protobufs.Highlight.newBuilder().build();
289+
290+
InnerHits innerHits = InnerHits.newBuilder().setName("test_inner_hits").setHighlight(highlightProto).build();
291+
292+
// This should throw UnsupportedOperationException from HighlightBuilderProtoUtils.fromProto
293+
UnsupportedOperationException exception = expectThrows(
294+
UnsupportedOperationException.class,
295+
() -> InnerHitsBuilderProtoUtils.fromProto(innerHits)
296+
);
297+
298+
assertEquals("highlight not supported yet", exception.getMessage());
299+
}
300+
301+
public void testFromProtoWithCollapse() throws IOException {
302+
// Create a protobuf InnerHits with collapse
303+
org.opensearch.protobufs.FieldCollapse collapseProto = org.opensearch.protobufs.FieldCollapse.newBuilder()
304+
.setField("category")
305+
.build();
306+
307+
InnerHits innerHits = InnerHits.newBuilder().setName("test_inner_hits").setCollapse(collapseProto).build();
308+
309+
// This should work and create the InnerHitBuilder with collapse
310+
InnerHitBuilder innerHitBuilder = InnerHitsBuilderProtoUtils.fromProto(innerHits);
311+
312+
// Verify the result
313+
assertNotNull("InnerHitBuilder should not be null", innerHitBuilder);
314+
assertNotNull("InnerCollapseBuilder should not be null", innerHitBuilder.getInnerCollapseBuilder());
315+
}
249316
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.transport.grpc.proto.request.search.sort;
10+
11+
import org.opensearch.protobufs.SortCombinations;
12+
import org.opensearch.search.sort.FieldSortBuilder;
13+
import org.opensearch.search.sort.ScoreSortBuilder;
14+
import org.opensearch.search.sort.SortBuilder;
15+
import org.opensearch.test.OpenSearchTestCase;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public class SortBuilderProtoUtilsTests extends OpenSearchTestCase {
21+
22+
public void testFromProtoThrowsUnsupportedOperation() {
23+
// Create an empty list of SortCombinations
24+
List<SortCombinations> sortProto = new ArrayList<>();
25+
26+
// This should throw UnsupportedOperationException
27+
UnsupportedOperationException exception = expectThrows(
28+
UnsupportedOperationException.class,
29+
() -> SortBuilderProtoUtils.fromProto(sortProto)
30+
);
31+
32+
assertEquals("sort not supported yet", exception.getMessage());
33+
}
34+
35+
public void testFromProtoWithSortCombinationsThrowsUnsupportedOperation() {
36+
// Create a list with a SortCombination
37+
List<SortCombinations> sortProto = new ArrayList<>();
38+
sortProto.add(SortCombinations.newBuilder().build());
39+
40+
// This should throw UnsupportedOperationException
41+
UnsupportedOperationException exception = expectThrows(
42+
UnsupportedOperationException.class,
43+
() -> SortBuilderProtoUtils.fromProto(sortProto)
44+
);
45+
46+
assertEquals("sort not supported yet", exception.getMessage());
47+
}
48+
49+
public void testFieldOrScoreSortWithScoreField() {
50+
// Test with "score" field
51+
SortBuilder<?> sortBuilder = SortBuilderProtoUtils.fieldOrScoreSort("score");
52+
53+
assertTrue("Should return ScoreSortBuilder for score field", sortBuilder instanceof ScoreSortBuilder);
54+
}
55+
56+
public void testFieldOrScoreSortWithRegularField() {
57+
// Test with regular field name
58+
SortBuilder<?> sortBuilder = SortBuilderProtoUtils.fieldOrScoreSort("username");
59+
60+
assertTrue("Should return FieldSortBuilder for regular field", sortBuilder instanceof FieldSortBuilder);
61+
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder;
62+
assertEquals("Field name should match", "username", fieldSortBuilder.getFieldName());
63+
}
64+
65+
public void testFieldOrScoreSortWithEmptyField() {
66+
// Test with empty field name
67+
SortBuilder<?> sortBuilder = SortBuilderProtoUtils.fieldOrScoreSort("");
68+
69+
assertTrue("Should return FieldSortBuilder for empty field", sortBuilder instanceof FieldSortBuilder);
70+
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder;
71+
assertEquals("Field name should be empty", "", fieldSortBuilder.getFieldName());
72+
}
73+
74+
public void testFieldOrScoreSortWithNullField() {
75+
// Test with null field name - should throw NullPointerException
76+
NullPointerException exception = expectThrows(NullPointerException.class, () -> SortBuilderProtoUtils.fieldOrScoreSort(null));
77+
78+
// Verify the exception is thrown (the method doesn't handle null gracefully)
79+
assertNotNull("Should throw NullPointerException for null field", exception);
80+
}
81+
}

0 commit comments

Comments
 (0)