1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Reflection ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+ using Azure ;
9+ using Azure . Core . Extensions ;
10+ using Azure . Storage . Files . DataLake ;
11+ using Azure . Storage . Files . DataLake . Models ;
12+ using Microsoft . Extensions . Logging ;
13+ using Moq ;
14+ using NUnit . Framework ;
15+ using SqlCollaborative . Azure . DataPipelineTools . DataLake ;
16+ using SqlCollaborative . Azure . DataPipelineTools . DataLake . Model ;
17+
18+ namespace DataPipelineTools . Tests . DataLake
19+ {
20+ [ TestFixture ]
21+ public class DataLakeServiceTests : TestBase
22+ {
23+ private const string AccountUri = "mydatalake" ;
24+ private const string ContainerName = "mycontainer" ;
25+
26+ private readonly IEnumerable < PathItem > TestData ;
27+ private readonly Mock < DataLakeFileSystemClient > mockFileSystemClient ;
28+
29+ //private readonly Mock<DataLakeDirectoryClient> mockDirectoryClient;
30+ //private readonly Mock<DataLakeFileClient> mockFileClient;
31+
32+ private readonly Mock < ILogger < DataLakeServiceFactory > > mockLogger ;
33+
34+ private readonly DataLakeService Sut ;
35+
36+ public DataLakeServiceTests ( )
37+ {
38+ // Get test data to mock the file system
39+ TestData = GetTestData ( ) ;
40+
41+ // Mock the logger to test where it is called
42+ mockLogger = new Mock < ILogger < DataLakeServiceFactory > > ( ) ;
43+
44+ // Mock the file system client
45+ mockFileSystemClient = BuildMockDataLakeFileSystemClient ( ) ;
46+
47+ // Use the factory to inject the mock logger to get the mock client...
48+ var factory = new DataLakeServiceFactory ( mockLogger . Object ) ;
49+ Sut = factory . CreateDataLakeService ( mockFileSystemClient . Object ) ;
50+ }
51+
52+ private Mock < DataLakeFileSystemClient > BuildMockDataLakeFileSystemClient ( )
53+ {
54+ var mockFileSystemClient = new Mock < DataLakeFileSystemClient > ( ) ;
55+ mockFileSystemClient . SetupGet ( x => x . Name ) . Returns ( ContainerName ) ;
56+ mockFileSystemClient . SetupGet ( x => x . AccountName ) . Returns ( AccountUri ) ;
57+
58+ mockFileSystemClient
59+ . Setup ( x => x . GetDirectoryClient ( It . IsAny < String > ( ) ) )
60+ . Returns < string > ( BuildMockDataLakeDirectoryClient < DataLakeDirectoryClient > ) ;
61+ mockFileSystemClient
62+ . Setup ( x => x . GetFileClient ( It . IsAny < String > ( ) ) )
63+ . Returns < string > ( BuildMockDataLakeDirectoryClient < DataLakeFileClient > ) ;
64+
65+ mockFileSystemClient
66+ . Setup ( x => x . GetPaths ( It . IsAny < string > ( ) , It . IsAny < bool > ( ) , It . IsAny < bool > ( ) , It . IsAny < CancellationToken > ( ) ) )
67+ . Returns ( ( string p , bool r ) =>
68+ {
69+ var items = TestData
70+ // Include all files starting with the test path
71+ . Where ( x => x . Name . StartsWith ( p ) )
72+ // Still include them if the recursive flag is set, otherwise check if the relative path after the search path contains
73+ // directory separator to exclude sub dirs
74+ . Where ( x => r || ! x . Name . Substring ( p . Length ) . Contains ( '/' ) )
75+ . ToList ( )
76+ . AsReadOnly ( ) ;
77+
78+ var page = Page < PathItem > . FromValues ( items , null , Mock . Of < Response > ( ) ) ;
79+ return Pageable < PathItem > . FromPages ( new [ ] { page } ) ;
80+ } ) ;
81+
82+ return mockFileSystemClient ;
83+ }
84+
85+ private T BuildMockDataLakeDirectoryClient < T > ( string directoryName ) where T : DataLakePathClient
86+ {
87+ var mockDirectoryClient = new Mock < T > ( ) ;
88+ mockDirectoryClient . SetupGet ( x => x . FileSystemName ) . Returns ( ContainerName ) ;
89+ mockDirectoryClient . SetupGet ( x => x . AccountName ) . Returns ( AccountUri ) ;
90+ mockDirectoryClient . SetupGet ( x => x . Name ) . Returns ( directoryName ) ;
91+
92+ var directoryNameExists = TestData . Any ( i => i . Name == directoryName ) ;
93+ mockDirectoryClient
94+ . Setup ( x => x . ExistsAsync ( It . IsAny < CancellationToken > ( ) ) )
95+ . ReturnsAsync ( ( ) => Response . FromValue ( directoryNameExists , new Mock < Response > ( ) . Object ) ) ;
96+
97+ mockDirectoryClient
98+ . Setup ( x => x . Exists ( It . IsAny < CancellationToken > ( ) ) )
99+ . Returns ( ( ) => Response . FromValue ( directoryNameExists , new Mock < Response > ( ) . Object ) ) ;
100+
101+ return mockDirectoryClient . Object ;
102+ }
103+
104+ private IEnumerable < PathItem > GetTestData ( )
105+ {
106+ return GetTestData ( "," , properties =>
107+ {
108+ return DataLakeModelFactory . PathItem (
109+ properties [ nameof ( PathItem . Name ) ] ,
110+ Convert . ToBoolean ( properties [ nameof ( PathItem . IsDirectory ) ] ) ,
111+ Convert . ToDateTime ( properties [ nameof ( PathItem . LastModified ) ] ) ,
112+ ETag . All ,
113+ Convert . ToInt32 ( properties [ nameof ( PathItem . ContentLength ) ] ) ,
114+ null ,
115+ null ,
116+ null
117+ ) ;
118+ } ) . ToArray ( ) ;
119+ }
120+
121+ //private IEnumerable<DataLakeItem> GetTestData()
122+ // {
123+ // return GetTestData(",", properties =>
124+ // {
125+ // return new DataLakeItem()
126+ // {
127+ // Directory = properties[nameof(DataLakeItem.Directory)],
128+ // Name = properties[nameof(DataLakeItem.Name)],
129+ // IsDirectory = Convert.ToBoolean(properties[nameof(DataLakeItem.IsDirectory)]),
130+ // ContentLength = Convert.ToInt32(properties[nameof(DataLakeItem.ContentLength)]),
131+ // LastModified = Convert.ToDateTime(properties[nameof(DataLakeItem.LastModified)])
132+ // };
133+ // }).ToArray();
134+ // }
135+
136+ [ SetUp ]
137+ public void Setup ( )
138+ {
139+
140+ }
141+
142+ [ Test ]
143+ public void CheckPathAsync_ShouldReturn_ ( )
144+ {
145+
146+ }
147+
148+
149+ }
150+ }
0 commit comments