Skip to content

Commit c8015b9

Browse files
committed
more tests
Signed-off-by: Karen Xu <[email protected]>
1 parent 5ee1000 commit c8015b9

File tree

5 files changed

+489
-0
lines changed

5 files changed

+489
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
package org.opensearch.plugin.transport.grpc.listeners;
9+
10+
import org.opensearch.action.search.SearchResponse;
11+
import org.opensearch.action.search.SearchResponseSections;
12+
import org.opensearch.action.search.ShardSearchFailure;
13+
import org.opensearch.search.SearchHits;
14+
import org.opensearch.test.OpenSearchTestCase;
15+
16+
import java.io.IOException;
17+
18+
import io.grpc.stub.StreamObserver;
19+
import org.mockito.Mock;
20+
import org.mockito.MockitoAnnotations;
21+
22+
import static org.mockito.ArgumentMatchers.any;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.times;
25+
import static org.mockito.Mockito.verify;
26+
27+
public class SearchRequestActionListenerTests extends OpenSearchTestCase {
28+
29+
@Mock
30+
private StreamObserver<org.opensearch.protobufs.SearchResponse> responseObserver;
31+
32+
private SearchRequestActionListener listener;
33+
34+
@Override
35+
public void setUp() throws Exception {
36+
super.setUp();
37+
MockitoAnnotations.openMocks(this);
38+
listener = new SearchRequestActionListener(responseObserver);
39+
}
40+
41+
public void testOnResponse() throws IOException {
42+
43+
// Create a SearchResponse
44+
SearchResponse mockSearchResponse = new SearchResponse(
45+
new SearchResponseSections(SearchHits.empty(), null, null, false, false, null, 1),
46+
randomAlphaOfLengthBetween(5, 10),
47+
5,
48+
5,
49+
0,
50+
100,
51+
ShardSearchFailure.EMPTY_ARRAY,
52+
SearchResponse.Clusters.EMPTY
53+
);
54+
55+
// Call the method under test
56+
listener.onResponse(mockSearchResponse);
57+
58+
// Verify that onNext and onCompleted were called
59+
verify(responseObserver, times(1)).onNext(any(org.opensearch.protobufs.SearchResponse.class));
60+
verify(responseObserver, times(1)).onCompleted();
61+
}
62+
63+
public void testOnFailure() {
64+
// Create a mock StreamObserver
65+
@SuppressWarnings("unchecked")
66+
StreamObserver<org.opensearch.protobufs.SearchResponse> mockResponseObserver = mock(StreamObserver.class);
67+
68+
// Create a SearchRequestActionListener
69+
SearchRequestActionListener listener = new SearchRequestActionListener(mockResponseObserver);
70+
71+
// Create an exception
72+
Exception exception = new Exception("Test exception");
73+
74+
// Call the method under test
75+
listener.onFailure(exception);
76+
77+
// Verify that onError was called with the exception
78+
verify(mockResponseObserver, times(1)).onError(exception);
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
package org.opensearch.plugin.transport.grpc.proto.request.search.sort;
9+
10+
import org.opensearch.protobufs.FieldWithOrderMap;
11+
import org.opensearch.protobufs.ScoreSort;
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.search.sort.SortOrder;
16+
import org.opensearch.test.OpenSearchTestCase;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
public class FieldSortBuilderProtoUtilsTests extends OpenSearchTestCase {
22+
23+
public void testFromProtoWithEmptyMap() {
24+
// Create an empty FieldWithOrderMap
25+
FieldWithOrderMap fieldWithOrderMap = FieldWithOrderMap.newBuilder().build();
26+
27+
// Create a list to populate
28+
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
29+
30+
// Call the method under test
31+
FieldSortBuilderProtoUtils.fromProto(sortBuilders, fieldWithOrderMap);
32+
33+
// Verify the result
34+
assertTrue("SortBuilders list should be empty", sortBuilders.isEmpty());
35+
}
36+
37+
public void testFromProtoWithSingleField() {
38+
// Create a FieldWithOrderMap with a single field
39+
FieldWithOrderMap.Builder builder = FieldWithOrderMap.newBuilder();
40+
builder.putFieldWithOrderMap("field1", ScoreSort.newBuilder().setOrder(ScoreSort.SortOrder.SORT_ORDER_ASC).build());
41+
FieldWithOrderMap fieldWithOrderMap = builder.build();
42+
43+
// Create a list to populate
44+
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
45+
46+
// Call the method under test
47+
FieldSortBuilderProtoUtils.fromProto(sortBuilders, fieldWithOrderMap);
48+
49+
// Verify the result
50+
assertEquals("SortBuilders list should have 1 element", 1, sortBuilders.size());
51+
assertTrue("SortBuilder should be a FieldSortBuilder", sortBuilders.get(0) instanceof FieldSortBuilder);
52+
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilders.get(0);
53+
assertEquals("Field name should match", "field1", fieldSortBuilder.getFieldName());
54+
assertEquals("Sort order should be ASC", SortOrder.ASC, fieldSortBuilder.order());
55+
}
56+
57+
public void testFromProtoWithMultipleFields() {
58+
// Create a FieldWithOrderMap with multiple fields
59+
FieldWithOrderMap.Builder builder = FieldWithOrderMap.newBuilder();
60+
builder.putFieldWithOrderMap("field1", ScoreSort.newBuilder().setOrder(ScoreSort.SortOrder.SORT_ORDER_ASC).build());
61+
builder.putFieldWithOrderMap("field2", ScoreSort.newBuilder().setOrder(ScoreSort.SortOrder.SORT_ORDER_DESC).build());
62+
FieldWithOrderMap fieldWithOrderMap = builder.build();
63+
64+
// Create a list to populate
65+
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
66+
67+
// Call the method under test
68+
FieldSortBuilderProtoUtils.fromProto(sortBuilders, fieldWithOrderMap);
69+
70+
// Verify the result
71+
assertEquals("SortBuilders list should have 2 elements", 2, sortBuilders.size());
72+
73+
// Since the order of entries in a map is not guaranteed, we need to check both fields
74+
boolean foundField1 = false;
75+
boolean foundField2 = false;
76+
77+
for (SortBuilder<?> sortBuilder : sortBuilders) {
78+
assertTrue("SortBuilder should be a FieldSortBuilder", sortBuilder instanceof FieldSortBuilder);
79+
FieldSortBuilder fieldSortBuilder = (FieldSortBuilder) sortBuilder;
80+
81+
if (fieldSortBuilder.getFieldName().equals("field1")) {
82+
foundField1 = true;
83+
assertEquals("Sort order for field1 should be ASC", SortOrder.ASC, fieldSortBuilder.order());
84+
} else if (fieldSortBuilder.getFieldName().equals("field2")) {
85+
foundField2 = true;
86+
assertEquals("Sort order for field2 should be DESC", SortOrder.DESC, fieldSortBuilder.order());
87+
}
88+
}
89+
90+
assertTrue("Should have found field1", foundField1);
91+
assertTrue("Should have found field2", foundField2);
92+
}
93+
94+
public void testFromProtoWithScoreField() {
95+
// Create a FieldWithOrderMap with the special "score" field
96+
FieldWithOrderMap.Builder builder = FieldWithOrderMap.newBuilder();
97+
builder.putFieldWithOrderMap("score", ScoreSort.newBuilder().setOrder(ScoreSort.SortOrder.SORT_ORDER_DESC).build());
98+
FieldWithOrderMap fieldWithOrderMap = builder.build();
99+
100+
// Create a list to populate
101+
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
102+
103+
// Call the method under test
104+
FieldSortBuilderProtoUtils.fromProto(sortBuilders, fieldWithOrderMap);
105+
106+
// Verify the result
107+
assertEquals("SortBuilders list should have 1 element", 1, sortBuilders.size());
108+
assertTrue("SortBuilder should be a ScoreSortBuilder", sortBuilders.get(0) instanceof ScoreSortBuilder);
109+
ScoreSortBuilder scoreSortBuilder = (ScoreSortBuilder) sortBuilders.get(0);
110+
assertEquals("Sort order should be DESC", SortOrder.DESC, scoreSortBuilder.order());
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
package org.opensearch.plugin.transport.grpc.proto.request.search.sort;
9+
10+
import org.opensearch.protobufs.GeoDistanceSort;
11+
import org.opensearch.protobufs.ScoreSort;
12+
import org.opensearch.protobufs.ScriptSort;
13+
import org.opensearch.search.sort.SortOrder;
14+
import org.opensearch.test.OpenSearchTestCase;
15+
16+
public class SortOrderProtoUtilsTests extends OpenSearchTestCase {
17+
18+
public void testFromProtoScoreSortAsc() {
19+
// Test ASC order
20+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(ScoreSort.SortOrder.SORT_ORDER_ASC);
21+
assertEquals("Sort order should be ASC", SortOrder.ASC, sortOrder);
22+
}
23+
24+
public void testFromProtoScoreSortDesc() {
25+
// Test DESC order
26+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(ScoreSort.SortOrder.SORT_ORDER_DESC);
27+
assertEquals("Sort order should be DESC", SortOrder.DESC, sortOrder);
28+
}
29+
30+
public void testFromProtoScoreSortUnspecified() {
31+
// Test UNSPECIFIED order (should throw exception)
32+
IllegalArgumentException exception = expectThrows(
33+
IllegalArgumentException.class,
34+
() -> SortOrderProtoUtils.fromProto(ScoreSort.SortOrder.SORT_ORDER_UNSPECIFIED)
35+
);
36+
assertTrue(
37+
"Exception message should mention 'Must provide oneof sort combinations'",
38+
exception.getMessage().contains("Must provide oneof sort combinations")
39+
);
40+
}
41+
42+
public void testFromProtoGeoDistanceSortAsc() {
43+
// Test ASC order
44+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(GeoDistanceSort.SortOrder.SORT_ORDER_ASC);
45+
assertEquals("Sort order should be ASC", SortOrder.ASC, sortOrder);
46+
}
47+
48+
public void testFromProtoGeoDistanceSortDesc() {
49+
// Test DESC order
50+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(GeoDistanceSort.SortOrder.SORT_ORDER_DESC);
51+
assertEquals("Sort order should be DESC", SortOrder.DESC, sortOrder);
52+
}
53+
54+
public void testFromProtoGeoDistanceSortUnspecified() {
55+
// Test UNSPECIFIED order (should throw exception)
56+
IllegalArgumentException exception = expectThrows(
57+
IllegalArgumentException.class,
58+
() -> SortOrderProtoUtils.fromProto(GeoDistanceSort.SortOrder.SORT_ORDER_UNSPECIFIED)
59+
);
60+
assertTrue(
61+
"Exception message should mention 'Must provide oneof sort combinations'",
62+
exception.getMessage().contains("Must provide oneof sort combinations")
63+
);
64+
}
65+
66+
public void testFromProtoScriptSortAsc() {
67+
// Test ASC order
68+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(ScriptSort.SortOrder.SORT_ORDER_ASC);
69+
assertEquals("Sort order should be ASC", SortOrder.ASC, sortOrder);
70+
}
71+
72+
public void testFromProtoScriptSortDesc() {
73+
// Test DESC order
74+
SortOrder sortOrder = SortOrderProtoUtils.fromProto(ScriptSort.SortOrder.SORT_ORDER_DESC);
75+
assertEquals("Sort order should be DESC", SortOrder.DESC, sortOrder);
76+
}
77+
78+
public void testFromProtoScriptSortUnspecified() {
79+
// Test UNSPECIFIED order (should throw exception)
80+
IllegalArgumentException exception = expectThrows(
81+
IllegalArgumentException.class,
82+
() -> SortOrderProtoUtils.fromProto(ScriptSort.SortOrder.SORT_ORDER_UNSPECIFIED)
83+
);
84+
assertTrue(
85+
"Exception message should mention 'Must provide oneof sort combinations'",
86+
exception.getMessage().contains("Must provide oneof sort combinations")
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)