|
| 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