Skip to content

Commit 21068a3

Browse files
authored
HDDS-10213. Improve assertTrue assertions in remaining integration-test (#6101)
1 parent 11bc604 commit 21068a3

31 files changed

+242
-237
lines changed

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
118118
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.KEY_NOT_FOUND;
119119
import static org.apache.hadoop.ozone.om.helpers.BucketLayout.FILE_SYSTEM_OPTIMIZED;
120+
import static org.assertj.core.api.Assertions.assertThat;
120121
import static org.junit.jupiter.api.Assertions.assertEquals;
121122
import static org.junit.jupiter.api.Assertions.assertFalse;
122123
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -392,7 +393,7 @@ private void checkInvalidPath(Path path) {
392393
InvalidPathException pathException = assertThrows(
393394
InvalidPathException.class, () -> fs.create(path, false)
394395
);
395-
assertTrue(pathException.getMessage().contains("Invalid path Name"));
396+
assertThat(pathException.getMessage()).contains("Invalid path Name");
396397
}
397398

398399
@Test
@@ -466,7 +467,7 @@ public void testRecursiveDelete() throws Exception {
466467
// delete a dir with sub-file
467468
try {
468469
FileStatus[] parents = fs.listStatus(grandparent);
469-
assertTrue(parents.length > 0);
470+
assertThat(parents.length).isGreaterThan(0);
470471
fs.delete(parents[0].getPath(), false);
471472
fail("Must throw exception as dir is not empty!");
472473
} catch (PathIsNotEmptyDirectoryException pde) {
@@ -537,8 +538,8 @@ private void checkPath(Path path) {
537538
fs.getFileStatus(path);
538539
fail("testRecursiveDelete failed");
539540
} catch (IOException ex) {
540-
assertTrue(ex instanceof FileNotFoundException);
541-
assertTrue(ex.getMessage().contains("No such file or directory"));
541+
assertInstanceOf(FileNotFoundException.class, ex);
542+
assertThat(ex.getMessage()).contains("No such file or directory");
542543
}
543544
}
544545

@@ -748,7 +749,7 @@ public void testListStatusOnLargeDirectory() throws Exception {
748749
assertEquals(numDirs, fileStatuses.length, "Total directories listed do not match the existing directories");
749750

750751
for (int i = 0; i < numDirs; i++) {
751-
assertTrue(paths.contains(fileStatuses[i].getPath().getName()));
752+
assertThat(paths).contains(fileStatuses[i].getPath().getName());
752753
}
753754
}
754755

@@ -1003,7 +1004,7 @@ public void testSeekOnFileLength() throws IOException {
10031004
fs.open(fileNotExists);
10041005
fail("Should throw FileNotFoundException as file doesn't exist!");
10051006
} catch (FileNotFoundException fnfe) {
1006-
assertTrue(fnfe.getMessage().contains("KEY_NOT_FOUND"), "Expected KEY_NOT_FOUND error");
1007+
assertThat(fnfe.getMessage()).contains("KEY_NOT_FOUND");
10071008
}
10081009
}
10091010

@@ -1026,12 +1027,16 @@ public void testAllocateMoreThanOneBlock() throws IOException {
10261027
FileStatus fileStatus = fs.getFileStatus(file);
10271028
long blkSize = fileStatus.getBlockSize();
10281029
long fileLength = fileStatus.getLen();
1029-
assertTrue(fileLength > blkSize, "Block allocation should happen");
1030+
assertThat(fileLength)
1031+
.withFailMessage("Block allocation should happen")
1032+
.isGreaterThan(blkSize);
10301033

10311034
long newNumBlockAllocations =
10321035
cluster.getOzoneManager().getMetrics().getNumBlockAllocates();
10331036

1034-
assertTrue((newNumBlockAllocations > numBlockAllocationsOrg), "Block allocation should happen");
1037+
assertThat(newNumBlockAllocations)
1038+
.withFailMessage("Block allocation should happen")
1039+
.isGreaterThan(numBlockAllocationsOrg);
10351040

10361041
stream.seek(fileLength);
10371042
assertEquals(-1, stream.read());
@@ -1366,7 +1371,7 @@ public void testRenameDir() throws Exception {
13661371
IllegalArgumentException exception = assertThrows(
13671372
IllegalArgumentException.class,
13681373
() -> fs.rename(new Path(fs.getUri().toString() + "fake" + dir), dest));
1369-
assertTrue(exception.getMessage().contains("Wrong FS"));
1374+
assertThat(exception.getMessage()).contains("Wrong FS");
13701375
}
13711376

13721377
private OzoneKeyDetails getKey(Path keyPath, boolean isDirectory)
@@ -1419,7 +1424,7 @@ public void testGetDirectoryModificationTime()
14191424
for (int i = 0; i < 5; i++) {
14201425
Thread.sleep(10);
14211426
fileStatuses = o3fs.listStatus(mdir1);
1422-
assertTrue(modificationTime <= fileStatuses[0].getModificationTime());
1427+
assertThat(modificationTime).isLessThanOrEqualTo(fileStatuses[0].getModificationTime());
14231428
}
14241429
}
14251430

@@ -1817,7 +1822,7 @@ public void testOzFsReadWrite() throws IOException {
18171822

18181823
// The timestamp of the newly created file should always be greater than
18191824
// the time when the test was started
1820-
assertTrue(status.getModificationTime() > currentTime);
1825+
assertThat(status.getModificationTime()).isGreaterThan(currentTime);
18211826

18221827
assertFalse(status.isDirectory());
18231828
assertEquals(FsPermission.getFileDefault(), status.getPermission());
@@ -1968,7 +1973,7 @@ void testListStatus2() throws IOException {
19681973
assertChange(initialStats, statistics, Statistic.OBJECTS_LIST.getSymbol(), 2);
19691974
assertEquals(initialListStatusCount + 2, omMetrics.getNumListStatus());
19701975
for (Path p : paths) {
1971-
assertTrue(Arrays.asList(statusList).contains(fs.getFileStatus(p)));
1976+
assertThat(Arrays.asList(statusList)).contains(fs.getFileStatus(p));
19721977
}
19731978
}
19741979

@@ -2006,7 +2011,7 @@ void testOzoneManagerFileSystemInterface() throws IOException {
20062011
// doesn't actually exist on server; if it exists, it will be a fixed value.
20072012
// In this case, the dir key exists.
20082013
assertEquals(0, omStatus.getKeyInfo().getDataSize());
2009-
assertTrue(omStatus.getKeyInfo().getModificationTime() <= currentTime);
2014+
assertThat(omStatus.getKeyInfo().getModificationTime()).isLessThanOrEqualTo(currentTime);
20102015
assertEquals(new Path(omStatus.getPath()).getName(),
20112016
o3fs.pathToKey(path));
20122017
}
@@ -2020,13 +2025,12 @@ public void testOzoneManagerLocatedFileStatus() throws IOException {
20202025
stream.writeBytes(data);
20212026
}
20222027
FileStatus status = fs.getFileStatus(path);
2023-
assertTrue(status instanceof LocatedFileStatus);
2024-
LocatedFileStatus locatedFileStatus = (LocatedFileStatus) status;
2025-
assertTrue(locatedFileStatus.getBlockLocations().length >= 1);
2028+
LocatedFileStatus locatedFileStatus = assertInstanceOf(LocatedFileStatus.class, status);
2029+
assertThat(locatedFileStatus.getBlockLocations().length).isGreaterThanOrEqualTo(1);
20262030

20272031
for (BlockLocation blockLocation : locatedFileStatus.getBlockLocations()) {
2028-
assertTrue(blockLocation.getNames().length >= 1);
2029-
assertTrue(blockLocation.getHosts().length >= 1);
2032+
assertThat(blockLocation.getNames().length).isGreaterThanOrEqualTo(1);
2033+
assertThat(blockLocation.getHosts().length).isGreaterThanOrEqualTo(1);
20302034
}
20312035
}
20322036

@@ -2046,8 +2050,7 @@ void testBlockOffsetsWithMultiBlockFile() throws Exception {
20462050
stream.writeBytes(data);
20472051
}
20482052
FileStatus status = fs.getFileStatus(path);
2049-
assertTrue(status instanceof LocatedFileStatus);
2050-
LocatedFileStatus locatedFileStatus = (LocatedFileStatus) status;
2053+
LocatedFileStatus locatedFileStatus = assertInstanceOf(LocatedFileStatus.class, status);
20512054
BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
20522055

20532056
assertEquals(0, blockLocations[0].getOffset());
@@ -2099,7 +2102,7 @@ void testFileSystemWithObjectStoreLayout() throws IOException {
20992102
config.set(FS_DEFAULT_NAME_KEY, obsRootPath);
21002103

21012104
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> FileSystem.get(config));
2102-
assertTrue(e.getMessage().contains("OBJECT_STORE, which does not support file system semantics"));
2105+
assertThat(e.getMessage()).contains("OBJECT_STORE, which does not support file system semantics");
21032106
}
21042107
}
21052108

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTestWithFSO.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
import java.io.IOException;
4646
import java.util.ArrayList;
4747

48+
import static org.assertj.core.api.Assertions.assertThat;
4849
import static org.junit.jupiter.api.Assertions.assertEquals;
4950
import static org.junit.jupiter.api.Assertions.assertFalse;
51+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5052
import static org.junit.jupiter.api.Assertions.assertNotNull;
5153
import static org.junit.jupiter.api.Assertions.assertThrows;
5254
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -305,8 +307,8 @@ public void testRenameParentDirModificationTime() throws IOException {
305307
.getModificationTime();
306308
// rename should change the parent directory of source and object files
307309
// modification time but not change modification time of the renamed file
308-
assertTrue(dir1BeforeMTime < dir1AfterMTime);
309-
assertTrue(dir2BeforeMTime < dir2AfterMTime);
310+
assertThat(dir1BeforeMTime).isLessThan(dir1AfterMTime);
311+
assertThat(dir2BeforeMTime).isLessThan(dir2AfterMTime);
310312
assertEquals(file1BeforeMTime, file1AfterMTime);
311313

312314
// mv "/dir1/subdir1/" to "/dir2/subdir1/"
@@ -323,8 +325,8 @@ public void testRenameParentDirModificationTime() throws IOException {
323325
dir2AfterMTime = getFs().getFileStatus(dir2).getModificationTime();
324326
long subdir1AfterMTime = getFs().getFileStatus(renamedSubdir1)
325327
.getModificationTime();
326-
assertTrue(dir1BeforeMTime < dir1AfterMTime);
327-
assertTrue(dir2BeforeMTime < dir2AfterMTime);
328+
assertThat(dir1BeforeMTime).isLessThan(dir1AfterMTime);
329+
assertThat(dir2BeforeMTime).isLessThan(dir2AfterMTime);
328330
assertEquals(subdir1BeforeMTime, subdir1AfterMTime);
329331
}
330332

@@ -379,7 +381,7 @@ private void renameAndAssert(OMMetadataManager omMgr,
379381
long bucketAfterMTime = omBucketInfo.getModificationTime();
380382
long fileAfterMTime = getFs().getFileStatus(to).getModificationTime();
381383
if (exceptChangeMtime) {
382-
assertTrue(bucketBeforeMTime < bucketAfterMTime);
384+
assertThat(bucketBeforeMTime).isLessThan(bucketAfterMTime);
383385
} else {
384386
assertEquals(bucketBeforeMTime, bucketAfterMTime);
385387
}
@@ -434,7 +436,7 @@ public void testMultiLevelDirs() throws Exception {
434436
long d6ObjectID =
435437
verifyDirKey(volumeId, bucketId, d4ObjectID,
436438
"d6", "/d1/d2/d3/d4/d6", dirKeys, omMgr);
437-
assertTrue(d5ObjectID != d6ObjectID, "Wrong objectIds for sub-dirs[" + d5ObjectID + "/d5, " + d6ObjectID
439+
assertNotEquals(d5ObjectID, d6ObjectID, "Wrong objectIds for sub-dirs[" + d5ObjectID + "/d5, " + d6ObjectID
438440
+ "/d6] of same parent!");
439441

440442
assertEquals(6, getCluster().getOzoneManager().getMetrics().getNumKeys(), "Wrong OM numKeys metrics");
@@ -520,10 +522,10 @@ public void testFSDeleteLogWarnNoExist() throws Exception {
520522
GenericTestUtils.LogCapturer logCapture = GenericTestUtils.LogCapturer
521523
.captureLogs(BasicOzoneClientAdapterImpl.LOG);
522524
getFs().delete(new Path("/d1/d3/noexist/"), true);
523-
assertTrue(logCapture.getOutput().contains(
524-
"delete key failed Unable to get file status"));
525-
assertTrue(logCapture.getOutput().contains(
526-
"WARN ozone.BasicOzoneClientAdapterImpl"));
525+
assertThat(logCapture.getOutput()).contains(
526+
"delete key failed Unable to get file status");
527+
assertThat(logCapture.getOutput()).contains(
528+
"WARN ozone.BasicOzoneClientAdapterImpl");
527529
}
528530

529531
private void verifyOMFileInfoFormat(OmKeyInfo omKeyInfo, String fileName,
@@ -546,7 +548,7 @@ long verifyDirKey(long volumeId, long bucketId, long parentId,
546548
" using dbKey: " + dbKey);
547549
assertEquals(parentId, dirInfo.getParentObjectID(), "Parent Id mismatches");
548550
assertEquals(dirKey, dirInfo.getName(), "Mismatches directory name");
549-
assertTrue(dirInfo.getCreationTime() > 0, "Mismatches directory creation time param");
551+
assertThat(dirInfo.getCreationTime()).isGreaterThan(0);
550552
assertEquals(dirInfo.getCreationTime(), dirInfo.getModificationTime());
551553
return dirInfo.getObjectID();
552554
}

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import static org.apache.hadoop.ozone.security.acl.IAccessAuthorizer.ACLType.WRITE;
125125
import static org.apache.hadoop.ozone.security.acl.IAccessAuthorizer.ACLType.DELETE;
126126
import static org.apache.hadoop.ozone.security.acl.IAccessAuthorizer.ACLType.LIST;
127+
import static org.assertj.core.api.Assertions.assertThat;
127128
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
128129
import static org.junit.jupiter.api.Assertions.assertEquals;
129130
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -774,7 +775,7 @@ void testListStatusOnLargeDirectory() throws Exception {
774775
assertEquals(numDirs, fileStatuses.length, "Total directories listed do not match the existing directories");
775776

776777
for (int i = 0; i < numDirs; i++) {
777-
assertTrue(paths.contains(fileStatuses[i].getPath().getName()));
778+
assertThat(paths).contains(fileStatuses[i].getPath().getName());
778779
}
779780
} finally {
780781
// Cleanup
@@ -1107,7 +1108,7 @@ void testListStatusRootAndVolumeRecursive() throws IOException {
11071108
*/
11081109
private FileStatus[] customListStatus(Path f, boolean recursive,
11091110
String startPath, int numEntries) throws IOException {
1110-
assertTrue(numEntries > 0);
1111+
assertThat(numEntries).isGreaterThan(0);
11111112
LinkedList<FileStatus> statuses = new LinkedList<>();
11121113
List<FileStatus> tmpStatusList;
11131114
do {
@@ -1484,9 +1485,9 @@ void testSymlinkList() throws Exception {
14841485
new GenericTestUtils.SystemOutCapturer()) {
14851486
String linkPathStr = rootPath + destVolume;
14861487
ToolRunner.run(shell, new String[]{"-ls", linkPathStr});
1487-
assertTrue(capture.getOutput().contains("drwxrwxrwx"));
1488-
assertTrue(capture.getOutput().contains(linkPathStr +
1489-
OZONE_URI_DELIMITER + srcBucket));
1488+
assertThat(capture.getOutput()).contains("drwxrwxrwx");
1489+
assertThat(capture.getOutput()).contains(linkPathStr +
1490+
OZONE_URI_DELIMITER + srcBucket);
14901491
} finally {
14911492
shell.close();
14921493
}
@@ -1507,12 +1508,12 @@ void testSymlinkList() throws Exception {
15071508
String linkPathStr = rootPath + destVolume;
15081509
ToolRunner.run(shell, new String[]{"-ls", "-R",
15091510
linkPathStr + OZONE_URI_DELIMITER + srcBucket});
1510-
assertTrue(capture.getOutput().contains("drwxrwxrwx"));
1511-
assertTrue(capture.getOutput().contains(linkPathStr +
1512-
OZONE_URI_DELIMITER + srcBucket));
1513-
assertTrue(capture.getOutput().contains("-rw-rw-rw-"));
1514-
assertTrue(capture.getOutput().contains(linkPathStr +
1515-
OZONE_URI_DELIMITER + srcBucket + OZONE_URI_DELIMITER + key));
1511+
assertThat(capture.getOutput()).contains("drwxrwxrwx");
1512+
assertThat(capture.getOutput()).contains(linkPathStr +
1513+
OZONE_URI_DELIMITER + srcBucket);
1514+
assertThat(capture.getOutput()).contains("-rw-rw-rw-");
1515+
assertThat(capture.getOutput()).contains(linkPathStr +
1516+
OZONE_URI_DELIMITER + srcBucket + OZONE_URI_DELIMITER + key);
15161517
} finally {
15171518
shell.close();
15181519
}
@@ -1676,7 +1677,7 @@ void testDeleteBucketLink() throws Exception {
16761677
// confirm link is gone
16771678
FileNotFoundException exception = assertThrows(FileNotFoundException.class,
16781679
() -> fs.getFileStatus(dirPathLink));
1679-
assertTrue(exception.getMessage().contains("File not found."));
1680+
assertThat(exception.getMessage()).contains("File not found.");
16801681

16811682
// Cleanup
16821683
fs.delete(bucketPath1, true);
@@ -1926,15 +1927,15 @@ void testTrash() throws Exception {
19261927
}, 1000, 180000);
19271928

19281929
if (isBucketFSOptimized) {
1929-
assertTrue(getOMMetrics()
1930-
.getNumTrashAtomicDirRenames() > prevNumTrashAtomicDirRenames);
1930+
assertThat(getOMMetrics().getNumTrashAtomicDirRenames())
1931+
.isGreaterThan(prevNumTrashAtomicDirRenames);
19311932
} else {
19321933
// This condition should pass after the checkpoint
1933-
assertTrue(getOMMetrics()
1934-
.getNumTrashRenames() > prevNumTrashRenames);
1934+
assertThat(getOMMetrics().getNumTrashRenames())
1935+
.isGreaterThan(prevNumTrashRenames);
19351936
// With new layout version, file renames wouldn't be counted
1936-
assertTrue(getOMMetrics()
1937-
.getNumTrashFilesRenames() > prevNumTrashFileRenames);
1937+
assertThat(getOMMetrics().getNumTrashFilesRenames())
1938+
.isGreaterThan(prevNumTrashFileRenames);
19381939
}
19391940

19401941
// wait for deletion of checkpoint dir
@@ -1993,7 +1994,7 @@ void testCreateWithInvalidPaths() {
19931994
private void checkInvalidPath(Path path) {
19941995
InvalidPathException exception = assertThrows(InvalidPathException.class,
19951996
() -> fs.create(path, false));
1996-
assertTrue(exception.getMessage().contains("Invalid path Name"));
1997+
assertThat(exception.getMessage()).contains("Invalid path Name");
19971998
}
19981999

19992000

@@ -2445,7 +2446,7 @@ void testSnapshotDiff() throws Exception {
24452446
IllegalArgumentException.class,
24462447
() -> ofs.getSnapshotDiffReport(volumePath1, finalFromSnap,
24472448
finalToSnap));
2448-
assertTrue(exception.getMessage().contains(errorMsg));
2449+
assertThat(exception.getMessage()).contains(errorMsg);
24492450
}
24502451

24512452
@Test

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestDirectoryDeletingServiceWithFSO.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
import java.util.concurrent.TimeoutException;
5858
import java.util.function.LongSupplier;
5959

60+
import static org.assertj.core.api.Assertions.assertThat;
6061
import static org.junit.jupiter.api.Assertions.assertEquals;
62+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
6163
import static org.junit.jupiter.api.Assertions.assertTrue;
6264
import static org.junit.jupiter.api.Assertions.fail;
6365
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ACL_ENABLED;
@@ -177,7 +179,7 @@ public void testDeleteEmptyDirectory() throws Exception {
177179
assertEquals(root.getName(), iterator.next().getValue().getName());
178180
}
179181

180-
assertTrue(dirDeletingService.getRunCount().get() > 1);
182+
assertThat(dirDeletingService.getRunCount().get()).isGreaterThan(1);
181183
}
182184

183185
/**
@@ -244,9 +246,9 @@ public void testDeleteWithLargeSubPathsThanBatchSize() throws Exception {
244246
assertSubPathsCount(dirDeletingService::getDeletedDirsCount, 19);
245247

246248
long elapsedRunCount = dirDeletingService.getRunCount().get() - preRunCount;
247-
assertTrue(dirDeletingService.getRunCount().get() > 1);
249+
assertThat(dirDeletingService.getRunCount().get()).isGreaterThan(1);
248250
// Ensure dir deleting speed, here provide a backup value for safe CI
249-
assertTrue(elapsedRunCount >= 7);
251+
assertThat(elapsedRunCount).isGreaterThanOrEqualTo(7);
250252
}
251253

252254
@Test
@@ -295,7 +297,7 @@ public void testDeleteWithMultiLevels() throws Exception {
295297
assertSubPathsCount(dirDeletingService::getMovedDirsCount, 2);
296298
assertSubPathsCount(dirDeletingService::getDeletedDirsCount, 5);
297299

298-
assertTrue(dirDeletingService.getRunCount().get() > 1);
300+
assertThat(dirDeletingService.getRunCount().get()).isGreaterThan(1);
299301
}
300302

301303
@Test
@@ -549,8 +551,8 @@ private void checkPath(Path path) {
549551
fs.getFileStatus(path);
550552
fail("testRecursiveDelete failed");
551553
} catch (IOException ex) {
552-
assertTrue(ex instanceof FileNotFoundException);
553-
assertTrue(ex.getMessage().contains("No such file or directory"));
554+
assertInstanceOf(FileNotFoundException.class, ex);
555+
assertThat(ex.getMessage()).contains("No such file or directory");
554556
}
555557
}
556558

0 commit comments

Comments
 (0)