Skip to content

Commit edae7f3

Browse files
author
karenx
committed
code cov
Signed-off-by: karenx <karenx@uber.com>
1 parent b13b325 commit edae7f3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/response/search/SearchHitProtoUtilsTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,35 @@ public void testToProtoWithActuallyEmptyBytesArray() throws IOException {
439439
assertNotNull("Hit should not be null", hit);
440440
assertFalse("Source should not be set when explicitly null", hit.hasSource());
441441
}
442+
443+
public void testToProtoWithBytesArrayOffsetConditionCoverage() throws IOException {
444+
// Test the specific condition coverage for BytesArray when offset != 0 OR length != bytes.length
445+
// This covers the else branch in the optimization logic (lines 207-208)
446+
SearchHit searchHit = new SearchHit(1);
447+
448+
// Create a larger byte array with prefix and suffix
449+
String prefix = "PREFIX";
450+
String jsonContent = "{\"field\":\"value\"}";
451+
String suffix = "SUFFIX";
452+
String fullContent = prefix + jsonContent + suffix;
453+
byte[] fullBytes = fullContent.getBytes(StandardCharsets.UTF_8);
454+
455+
// Create BytesArray with offset > 0 to extract just the JSON part
456+
// This will trigger the condition: bytesRef.offset != 0 || bytesRef.length != bytesRef.bytes.length
457+
int offset = prefix.length();
458+
int length = jsonContent.length();
459+
BytesArray slicedBytesArray = new BytesArray(fullBytes, offset, length);
460+
searchHit.sourceRef(slicedBytesArray);
461+
462+
// Call the method under test
463+
Hit hit = SearchHitProtoUtils.toProto(searchHit);
464+
465+
// Verify the result - should use the offset/length version of unsafeWrap
466+
assertNotNull("Hit should not be null", hit);
467+
assertTrue("Source should be set", hit.hasSource());
468+
assertEquals("Source size should match JSON length", length, hit.getSource().size());
469+
470+
byte[] expectedBytes = jsonContent.getBytes(StandardCharsets.UTF_8);
471+
assertArrayEquals("Source bytes should match JSON content", expectedBytes, hit.getSource().toByteArray());
472+
}
442473
}

0 commit comments

Comments
 (0)