diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java index 73face6fc9350..efd31dda92d09 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestRecoveryActionTests.java @@ -92,21 +92,7 @@ public void testRestRecoveryAction() { when(targetNode.getHostName()).thenReturn(randomAlphaOfLength(8)); when(state.getTargetNode()).thenReturn(targetNode); - ReplicationLuceneIndex index = mock(ReplicationLuceneIndex.class); - - final int totalRecoveredFiles = randomIntBetween(1, 64); - when(index.totalRecoverFiles()).thenReturn(totalRecoveredFiles); - final int recoveredFileCount = randomIntBetween(0, totalRecoveredFiles); - when(index.recoveredFileCount()).thenReturn(recoveredFileCount); - when(index.recoveredFilesPercent()).thenReturn((100f * recoveredFileCount) / totalRecoveredFiles); - when(index.totalFileCount()).thenReturn(randomIntBetween(totalRecoveredFiles, 2 * totalRecoveredFiles)); - - final int totalRecoveredBytes = randomIntBetween(1, 1 << 24); - when(index.totalRecoverBytes()).thenReturn((long) totalRecoveredBytes); - final int recoveredBytes = randomIntBetween(0, totalRecoveredBytes); - when(index.recoveredBytes()).thenReturn((long) recoveredBytes); - when(index.recoveredBytesPercent()).thenReturn((100f * recoveredBytes) / totalRecoveredBytes); - when(index.totalRecoverBytes()).thenReturn((long) randomIntBetween(totalRecoveredBytes, 2 * totalRecoveredBytes)); + ReplicationLuceneIndex index = createTestIndex(); when(state.getIndex()).thenReturn(index); final RecoveryState.Translog translog = mock(RecoveryState.Translog.class); @@ -215,6 +201,36 @@ public void testRestRecoveryAction() { } } + private ReplicationLuceneIndex createTestIndex() { + ReplicationLuceneIndex index = new ReplicationLuceneIndex(); + final int filesToRecoverCount = randomIntBetween(1, 64); + final int recoveredFilesCount = randomIntBetween(0, filesToRecoverCount); + addTestFileMetadata(index, 0, recoveredFilesCount, false, true); + addTestFileMetadata(index, recoveredFilesCount, filesToRecoverCount, false, false); + + final int totalFilesCount = randomIntBetween(filesToRecoverCount, 2 * filesToRecoverCount); + addTestFileMetadata(index, filesToRecoverCount, totalFilesCount, true, false); + return index; + } + + private void addTestFileMetadata(ReplicationLuceneIndex index, int startIndex, int endIndex, boolean reused, boolean isFullyRecovered) { + for (int i = startIndex; i < endIndex; i++) { + final int completeFileSize = randomIntBetween(1, 1024); + index.addFileDetail(String.valueOf(i), completeFileSize, reused); + + if (!reused) { + final int recoveredFileSize; + if (isFullyRecovered) { + recoveredFileSize = completeFileSize; + + } else { + recoveredFileSize = randomIntBetween(0, completeFileSize); + } + index.addRecoveredBytesToFile(String.valueOf(i), recoveredFileSize); + } + } + } + private static String percent(float percent) { return String.format(Locale.ROOT, "%1.1f%%", percent); }