|
23 | 23 | import java.net.URI; |
24 | 24 | import java.net.URISyntaxException; |
25 | 25 | import java.net.URL; |
| 26 | +import java.util.ArrayList; |
26 | 27 | import java.util.Arrays; |
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Random; |
|
42 | 43 | import org.apache.hadoop.fs.azurebfs.AbfsCountersImpl; |
43 | 44 | import org.apache.hadoop.fs.azurebfs.AbstractAbfsIntegrationTest; |
44 | 45 | import org.apache.hadoop.fs.azurebfs.AzureBlobFileSystem; |
| 46 | +import org.apache.hadoop.fs.azurebfs.AzureBlobFileSystemStore; |
45 | 47 | import org.apache.hadoop.fs.azurebfs.TestAbfsConfigurationFieldsValidation; |
46 | 48 | import org.apache.hadoop.fs.azurebfs.constants.AbfsServiceType; |
47 | 49 | import org.apache.hadoop.fs.azurebfs.constants.FSOperationType; |
|
58 | 60 | import org.apache.http.HttpResponse; |
59 | 61 |
|
60 | 62 | import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 63 | +import static org.apache.hadoop.fs.azurebfs.ITestAzureBlobFileSystemListStatus.TEST_CONTINUATION_TOKEN; |
61 | 64 | import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.APPEND_ACTION; |
62 | 65 | import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.EXPECT_100_JDK_ERROR; |
63 | 66 | import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.HTTP_METHOD_PATCH; |
|
75 | 78 | import static org.mockito.ArgumentMatchers.any; |
76 | 79 | import static org.mockito.ArgumentMatchers.eq; |
77 | 80 | import static org.mockito.Mockito.mock; |
| 81 | +import static org.mockito.Mockito.times; |
78 | 82 | import static org.mockito.Mockito.when; |
79 | 83 |
|
80 | 84 | import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.APN_VERSION; |
@@ -729,4 +733,81 @@ public void testExpectHundredContinue() throws Exception { |
729 | 733 | .describedAs("The expect header is not false") |
730 | 734 | .isFalse(); |
731 | 735 | } |
| 736 | + |
| 737 | + @Test |
| 738 | + public void testIsNonEmptyDirectory() throws IOException { |
| 739 | + testIsNonEmptyDirectoryInternal(EMPTY_STRING, true, EMPTY_STRING, |
| 740 | + true, 1, false); |
| 741 | + testIsNonEmptyDirectoryInternal(EMPTY_STRING, false, EMPTY_STRING, |
| 742 | + false, 1, true); |
| 743 | + |
| 744 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, true, EMPTY_STRING, |
| 745 | + true, 2, false); |
| 746 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, true, EMPTY_STRING, |
| 747 | + false, 2, true); |
| 748 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, true, TEST_CONTINUATION_TOKEN, |
| 749 | + true, 3, false); |
| 750 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, true, TEST_CONTINUATION_TOKEN, |
| 751 | + false, 2, true); |
| 752 | + |
| 753 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, false, EMPTY_STRING, |
| 754 | + true, 1, true); |
| 755 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, false, EMPTY_STRING, |
| 756 | + false, 1, true); |
| 757 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, false, TEST_CONTINUATION_TOKEN, |
| 758 | + true, 1, true); |
| 759 | + testIsNonEmptyDirectoryInternal(TEST_CONTINUATION_TOKEN, false, TEST_CONTINUATION_TOKEN, |
| 760 | + false, 1, true); |
| 761 | + } |
| 762 | + |
| 763 | + private void testIsNonEmptyDirectoryInternal(String firstCT, |
| 764 | + boolean isfirstEmpty, String secondCT, boolean isSecondEmpty, |
| 765 | + int expectedInvocations, boolean isNonEmpty) throws IOException { |
| 766 | + |
| 767 | + assumeBlobServiceType(); |
| 768 | + AzureBlobFileSystem spiedFs = Mockito.spy(getFileSystem()); |
| 769 | + AzureBlobFileSystemStore spiedStore = Mockito.spy(spiedFs.getAbfsStore()); |
| 770 | + AbfsBlobClient spiedClient = Mockito.spy(spiedStore.getClientHandler().getBlobClient()); |
| 771 | + Mockito.doReturn(spiedStore).when(spiedFs).getAbfsStore(); |
| 772 | + Mockito.doReturn(spiedClient).when(spiedStore).getClient(); |
| 773 | + VersionedFileStatus status1 = new VersionedFileStatus( |
| 774 | + "owner", "group", null, false, 0, false, 0, 0, 0, |
| 775 | + new Path("/testPath/file1"), "version", "encryptionContext"); |
| 776 | + VersionedFileStatus status2 = new VersionedFileStatus( |
| 777 | + "owner", "group", null, false, 0, false, 0, 0, 0, |
| 778 | + new Path("/testPath/file2"), "version", "encryptionContext"); |
| 779 | + |
| 780 | + List<VersionedFileStatus> mockedList1 = new ArrayList<>(); |
| 781 | + mockedList1.add(status1); |
| 782 | + List<VersionedFileStatus> mockedList2 = new ArrayList<>(); |
| 783 | + mockedList2.add(status2); |
| 784 | + |
| 785 | + ListResponseData listResponseData1 = new ListResponseData(); |
| 786 | + listResponseData1.setContinuationToken(firstCT); |
| 787 | + listResponseData1.setFileStatusList(isfirstEmpty ? new ArrayList<>() : mockedList1); |
| 788 | + listResponseData1.setOp(Mockito.mock(AbfsRestOperation.class)); |
| 789 | + |
| 790 | + ListResponseData listResponseData2 = new ListResponseData(); |
| 791 | + listResponseData2.setContinuationToken(secondCT); |
| 792 | + listResponseData2.setFileStatusList(isSecondEmpty ? new ArrayList<>() : mockedList2); |
| 793 | + listResponseData2.setOp(Mockito.mock(AbfsRestOperation.class)); |
| 794 | + |
| 795 | + ListResponseData listResponseData3 = new ListResponseData(); |
| 796 | + listResponseData3.setContinuationToken(EMPTY_STRING); |
| 797 | + listResponseData3.setFileStatusList(new ArrayList<>()); |
| 798 | + listResponseData3.setOp(Mockito.mock(AbfsRestOperation.class)); |
| 799 | + |
| 800 | + Mockito.doReturn(listResponseData1).doReturn(listResponseData2).doReturn(listResponseData3) |
| 801 | + .when(spiedClient).listPath(eq("/testPath"), eq(false), eq(1), |
| 802 | + any(), any(), any()); |
| 803 | + |
| 804 | + Assertions.assertThat(spiedClient.isNonEmptyDirectory("/testPath", |
| 805 | + Mockito.mock(TracingContext.class))) |
| 806 | + .describedAs("isNonEmptyDirectory in client giving unexpected results") |
| 807 | + .isEqualTo(isNonEmpty); |
| 808 | + |
| 809 | + Mockito.verify(spiedClient, times(expectedInvocations)) |
| 810 | + .listPath(eq("/testPath"), eq(false), eq(1), |
| 811 | + any(), any(TracingContext.class), any()); |
| 812 | + } |
732 | 813 | } |
0 commit comments