@@ -17,7 +17,8 @@ public class InMemoryFileDbDownloaderTests
17
17
private readonly FileDownloadProperties _fileDownloadProperties ;
18
18
private readonly Entity _entity ;
19
19
private readonly FileAttachment _file ;
20
-
20
+ private readonly DownloadBlockProperties _downloadBlockProperties ;
21
+
21
22
public InMemoryFileDbDownloaderTests ( )
22
23
{
23
24
_db = new InMemoryDb ( ) ;
@@ -43,6 +44,11 @@ public InMemoryFileDbDownloaderTests()
43
44
AttributeName = "dv_file" ,
44
45
Content = new byte [ ] { 1 , 2 , 3 , 4 }
45
46
} ;
47
+
48
+ _downloadBlockProperties = new DownloadBlockProperties ( )
49
+ {
50
+ FileDownloadSessionId = ""
51
+ } ;
46
52
}
47
53
48
54
[ Fact ]
@@ -93,5 +99,74 @@ public void Should_return_file_download_session_if_properties_are_valid()
93
99
Assert . Equal ( _fileDownloadProperties . Target . LogicalName , _entity . LogicalName ) ;
94
100
Assert . Equal ( _fileDownloadProperties . Target . Id , _entity . Id ) ;
95
101
}
102
+
103
+ [ Fact ]
104
+ public void Should_throw_file_token_continuation_not_found_exception ( )
105
+ {
106
+ _downloadBlockProperties . FileDownloadSessionId = "invalid id" ;
107
+ Assert . Throws < FileTokenContinuationNotFoundException > ( ( ) =>
108
+ _fileDb . DownloadFileBlock ( _downloadBlockProperties ) ) ;
109
+ }
110
+
111
+ [ Theory ]
112
+ [ InlineData ( 0 ) ]
113
+ [ InlineData ( - 23456 ) ]
114
+ public void Should_throw_invalid_length_exception ( long blockLength )
115
+ {
116
+ _fileDb . AddFile ( _file ) ;
117
+
118
+ _entity [ _fileDownloadProperties . FileAttributeName ] = _file . Id ;
119
+ _db . AddEntityRecord ( _entity ) ;
120
+
121
+ var fileContinuationToken = _fileDb . InitFileDownloadSession ( _fileDownloadProperties ) ;
122
+ Assert . NotNull ( fileContinuationToken ) ;
123
+
124
+ _downloadBlockProperties . FileDownloadSessionId = fileContinuationToken ;
125
+ _downloadBlockProperties . BlockLength = blockLength ;
126
+ Assert . Throws < InvalidBlockLengthException > ( ( ) => _fileDb . DownloadFileBlock ( _downloadBlockProperties ) ) ;
127
+ }
128
+
129
+ [ Theory ]
130
+ [ InlineData ( - 1 , 2 ) ]
131
+ [ InlineData ( 0 , 5 ) ]
132
+ [ InlineData ( 2 , 25 ) ]
133
+ [ InlineData ( 4 , 2 ) ]
134
+ public void Should_throw_invalid_offset_exception_if_exceeds_file_length ( long offset , long blockLength )
135
+ {
136
+ _fileDb . AddFile ( _file ) ;
137
+
138
+ _entity [ _fileDownloadProperties . FileAttributeName ] = _file . Id ;
139
+ _db . AddEntityRecord ( _entity ) ;
140
+
141
+ var fileContinuationToken = _fileDb . InitFileDownloadSession ( _fileDownloadProperties ) ;
142
+ Assert . NotNull ( fileContinuationToken ) ;
143
+
144
+ _downloadBlockProperties . FileDownloadSessionId = fileContinuationToken ;
145
+ _downloadBlockProperties . Offset = offset ;
146
+ _downloadBlockProperties . BlockLength = blockLength ;
147
+ Assert . Throws < InvalidOffsetException > ( ( ) => _fileDb . DownloadFileBlock ( _downloadBlockProperties ) ) ;
148
+ }
149
+
150
+ [ Theory ]
151
+ [ InlineData ( 0 , 4 ) ]
152
+ [ InlineData ( 1 , 3 ) ]
153
+ [ InlineData ( 3 , 1 ) ]
154
+ public void Should_download_a_valid_file_block ( long offset , long blockLength )
155
+ {
156
+ _fileDb . AddFile ( _file ) ;
157
+
158
+ _entity [ _fileDownloadProperties . FileAttributeName ] = _file . Id ;
159
+ _db . AddEntityRecord ( _entity ) ;
160
+
161
+ var fileContinuationToken = _fileDb . InitFileDownloadSession ( _fileDownloadProperties ) ;
162
+ Assert . NotNull ( fileContinuationToken ) ;
163
+
164
+ _downloadBlockProperties . FileDownloadSessionId = fileContinuationToken ;
165
+ _downloadBlockProperties . Offset = offset ;
166
+ _downloadBlockProperties . BlockLength = blockLength ;
167
+ var data = _fileDb . DownloadFileBlock ( _downloadBlockProperties ) ;
168
+
169
+ Assert . Equal ( blockLength , data . Length ) ;
170
+ }
96
171
}
97
172
}
0 commit comments