|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.transport.memorycontainer.memory; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertFalse; |
| 10 | +import static org.junit.Assert.assertNotNull; |
| 11 | +import static org.junit.Assert.assertTrue; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Collections; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 21 | +import org.opensearch.common.unit.TimeValue; |
| 22 | +import org.opensearch.common.xcontent.XContentFactory; |
| 23 | +import org.opensearch.core.common.bytes.BytesReference; |
| 24 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 25 | +import org.opensearch.core.xcontent.ToXContent; |
| 26 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 27 | +import org.opensearch.index.reindex.BulkByScrollResponse; |
| 28 | +import org.opensearch.index.reindex.BulkByScrollTask; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for MLDeleteMemoriesByQueryResponse |
| 32 | + */ |
| 33 | +public class MLDeleteMemoriesByQueryResponseTest { |
| 34 | + |
| 35 | + private BulkByScrollResponse mockBulkResponse; |
| 36 | + private BulkByScrollTask.Status mockStatus; |
| 37 | + private MLDeleteMemoriesByQueryResponse response; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() { |
| 41 | + mockBulkResponse = mock(BulkByScrollResponse.class); |
| 42 | + mockStatus = mock(BulkByScrollTask.Status.class); |
| 43 | + |
| 44 | + // Setup basic mock behavior |
| 45 | + when(mockBulkResponse.getTook()).thenReturn(TimeValue.timeValueMillis(1000)); |
| 46 | + when(mockBulkResponse.isTimedOut()).thenReturn(false); |
| 47 | + when(mockBulkResponse.getDeleted()).thenReturn(10L); |
| 48 | + when(mockBulkResponse.getBatches()).thenReturn(1); |
| 49 | + when(mockBulkResponse.getVersionConflicts()).thenReturn(0L); |
| 50 | + when(mockBulkResponse.getNoops()).thenReturn(0L); |
| 51 | + when(mockBulkResponse.getBulkRetries()).thenReturn(0L); |
| 52 | + when(mockBulkResponse.getSearchRetries()).thenReturn(0L); |
| 53 | + when(mockBulkResponse.getStatus()).thenReturn(mockStatus); |
| 54 | + when(mockBulkResponse.getBulkFailures()).thenReturn(Collections.emptyList()); |
| 55 | + when(mockBulkResponse.getSearchFailures()).thenReturn(Collections.emptyList()); |
| 56 | + |
| 57 | + // Setup status mock |
| 58 | + when(mockStatus.getThrottled()).thenReturn(TimeValue.timeValueMillis(0)); |
| 59 | + when(mockStatus.getThrottledUntil()).thenReturn(TimeValue.timeValueMillis(0)); |
| 60 | + when(mockStatus.getRequestsPerSecond()).thenReturn(-1.0f); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testConstructorWithBulkResponse() { |
| 65 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 66 | + assertNotNull(response); |
| 67 | + assertEquals(mockBulkResponse, response.getBulkResponse()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testToXContentBasic() throws IOException { |
| 72 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 73 | + |
| 74 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 75 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 76 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 77 | + |
| 78 | + assertNotNull(json); |
| 79 | + assertTrue(json.contains("\"took\":1000")); |
| 80 | + assertTrue(json.contains("\"timed_out\":false")); |
| 81 | + assertTrue(json.contains("\"deleted\":10")); |
| 82 | + assertTrue(json.contains("\"batches\":1")); |
| 83 | + assertTrue(json.contains("\"version_conflicts\":0")); |
| 84 | + assertTrue(json.contains("\"noops\":0")); |
| 85 | + assertTrue(json.contains("\"retries\"")); |
| 86 | + assertTrue(json.contains("\"bulk\":0")); |
| 87 | + assertTrue(json.contains("\"search\":0")); |
| 88 | + assertTrue(json.contains("\"throttled_millis\":0")); |
| 89 | + assertTrue(json.contains("\"requests_per_second\":-1.0")); |
| 90 | + assertFalse(json.contains("\"bulk_failures\"")); |
| 91 | + assertFalse(json.contains("\"search_failures\"")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testToXContentWithTimedOut() throws IOException { |
| 96 | + when(mockBulkResponse.isTimedOut()).thenReturn(true); |
| 97 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 98 | + |
| 99 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 100 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 101 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 102 | + |
| 103 | + assertTrue(json.contains("\"timed_out\":true")); |
| 104 | + } |
| 105 | + |
| 106 | + // Skip tests for bulk/search failures since inner classes don't exist in this version |
| 107 | + // The code handles them properly but we can't create mock instances for testing |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testToXContentWithRetries() throws IOException { |
| 111 | + when(mockBulkResponse.getBulkRetries()).thenReturn(3L); |
| 112 | + when(mockBulkResponse.getSearchRetries()).thenReturn(2L); |
| 113 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 114 | + |
| 115 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 116 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 117 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 118 | + |
| 119 | + assertTrue(json.contains("\"bulk\":3")); |
| 120 | + assertTrue(json.contains("\"search\":2")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testToXContentWithThrottling() throws IOException { |
| 125 | + when(mockStatus.getThrottled()).thenReturn(TimeValue.timeValueMillis(5000)); |
| 126 | + when(mockStatus.getThrottledUntil()).thenReturn(TimeValue.timeValueMillis(10000)); |
| 127 | + when(mockStatus.getRequestsPerSecond()).thenReturn(100.0f); |
| 128 | + |
| 129 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 130 | + |
| 131 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 132 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 133 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 134 | + |
| 135 | + assertTrue(json.contains("\"throttled_millis\":5000")); |
| 136 | + assertTrue(json.contains("\"throttled_until_millis\":10000")); |
| 137 | + assertTrue(json.contains("\"requests_per_second\":100.0")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testToXContentWithVersionConflicts() throws IOException { |
| 142 | + when(mockBulkResponse.getVersionConflicts()).thenReturn(5L); |
| 143 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 144 | + |
| 145 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 146 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 147 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 148 | + |
| 149 | + assertTrue(json.contains("\"version_conflicts\":5")); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testToXContentWithNoops() throws IOException { |
| 154 | + when(mockBulkResponse.getNoops()).thenReturn(3L); |
| 155 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 156 | + |
| 157 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 158 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 159 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 160 | + |
| 161 | + assertTrue(json.contains("\"noops\":3")); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testToXContentWithLargeDeleteCount() throws IOException { |
| 166 | + when(mockBulkResponse.getDeleted()).thenReturn(1000000L); |
| 167 | + when(mockBulkResponse.getBatches()).thenReturn(100); |
| 168 | + when(mockBulkResponse.getTook()).thenReturn(TimeValue.timeValueMillis(60000)); |
| 169 | + |
| 170 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 171 | + |
| 172 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 173 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 174 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 175 | + |
| 176 | + assertTrue(json.contains("\"deleted\":1000000")); |
| 177 | + assertTrue(json.contains("\"batches\":100")); |
| 178 | + assertTrue(json.contains("\"took\":60000")); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testSerializationDeserialization() throws IOException { |
| 183 | + // Create a real BulkByScrollResponse for proper serialization |
| 184 | + BulkByScrollResponse realResponse = new BulkByScrollResponse(Collections.emptyList(), null); |
| 185 | + |
| 186 | + MLDeleteMemoriesByQueryResponse originalResponse = new MLDeleteMemoriesByQueryResponse(realResponse); |
| 187 | + |
| 188 | + // Serialize |
| 189 | + BytesStreamOutput output = new BytesStreamOutput(); |
| 190 | + originalResponse.writeTo(output); |
| 191 | + |
| 192 | + // Deserialize |
| 193 | + StreamInput input = output.bytes().streamInput(); |
| 194 | + MLDeleteMemoriesByQueryResponse deserializedResponse = new MLDeleteMemoriesByQueryResponse(input); |
| 195 | + |
| 196 | + assertNotNull(deserializedResponse); |
| 197 | + assertNotNull(deserializedResponse.getBulkResponse()); |
| 198 | + assertEquals(originalResponse.getBulkResponse().getDeleted(), deserializedResponse.getBulkResponse().getDeleted()); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testToXContentWithNullFailureLists() throws IOException { |
| 203 | + when(mockBulkResponse.getBulkFailures()).thenReturn(null); |
| 204 | + when(mockBulkResponse.getSearchFailures()).thenReturn(null); |
| 205 | + |
| 206 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 207 | + |
| 208 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 209 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 210 | + String json = BytesReference.bytes(builder).utf8ToString(); |
| 211 | + |
| 212 | + assertFalse(json.contains("\"bulk_failures\"")); |
| 213 | + assertFalse(json.contains("\"search_failures\"")); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testGetBulkResponse() { |
| 218 | + response = new MLDeleteMemoriesByQueryResponse(mockBulkResponse); |
| 219 | + assertEquals(mockBulkResponse, response.getBulkResponse()); |
| 220 | + } |
| 221 | +} |
0 commit comments