File size: 13,781 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Test.Comparers;
using VersOne.Epub.Test.Unit.Mocks;
using VersOne.Epub.Test.Unit.TestData;
using static VersOne.Epub.Test.Unit.TestData.TestEpubData;

namespace VersOne.Epub.Test.Unit.Readers
{
    public class BookReaderTests
    {
        private readonly TestEnvironmentDependencies environmentDependencies;

        public BookReaderTests()
        {
            environmentDependencies = new TestEnvironmentDependencies();
        }

        [Fact(DisplayName = "Constructing a BookReader instance with non-null constructor parameters should succeed")]
        public void ConstructorWithNonNullParametersTest()
        {
            _ = new BookReader(environmentDependencies, new EpubReaderOptions());
        }

        [Fact(DisplayName = "Constructor should throw ArgumentNullException if environmentDependencies parameter is null")]
        public void ConstructorWithNullEnvironmentDependenciesTest()
        {
            Assert.Throws<ArgumentNullException>(() => new BookReader(null!, new EpubReaderOptions()));
        }

        [Fact(DisplayName = "Constructing a BookReader instance with a null epubReaderOptions parameter should succeed")]
        public void ConstructorWithNullEpubReaderOptionsTest()
        {
            _ = new BookReader(environmentDependencies, null);
        }

        [Fact(DisplayName = "Reading a minimal EPUB book from a file should succeed")]
        public void ReadMinimalBookFromFileTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateMinimalTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateMinimalTestEpubBook(EPUB_FILE_PATH);
            BookReader bookReader = new(environmentDependencies, null);
            EpubBook actualEpubBook = bookReader.ReadBook(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a minimal EPUB 2 book without navigation from a file with IgnoreMissingToc = true should succeed")]
        public void ReadMinimalEpub2BookWithoutNavigationFromFileTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateMinimalTestEpub2FileWithoutNavigation();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateMinimalTestEpub2BookWithoutNavigation(EPUB_FILE_PATH);
            EpubReaderOptions epubReaderOptions = new()
            {
                PackageReaderOptions = new PackageReaderOptions()
                {
                    IgnoreMissingToc = true
                }
            };
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = bookReader.ReadBook(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a file synchronously without downloading remote files should succeed")]
        public void ReadBookFromFileWithoutDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(EPUB_FILE_PATH, populateRemoteFilesContents: false);
            BookReader bookReader = new(environmentDependencies, null);
            EpubBook actualEpubBook = bookReader.ReadBook(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a file asynchronously without downloading remote files should succeed")]
        public async Task ReadBookFromFileAsyncWithoutDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(EPUB_FILE_PATH, populateRemoteFilesContents: false);
            BookReader bookReader = new(environmentDependencies, null);
            EpubBook actualEpubBook = await bookReader.ReadBookAsync(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a stream synchronously without downloading remote files should succeed")]
        public void ReadBookFromStreamWithoutDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            MemoryStream epubFileStream = new();
            environmentDependencies.FileSystem = new TestFileSystem(epubFileStream, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(null, populateRemoteFilesContents: false);
            BookReader bookReader = new(environmentDependencies, null);
            EpubBook actualEpubBook = bookReader.ReadBook(epubFileStream);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a stream asynchronously without downloading remote files should succeed")]
        public async Task ReadBookFromStreamAsyncWithoutDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            MemoryStream epubFileStream = new();
            environmentDependencies.FileSystem = new TestFileSystem(epubFileStream, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(null, populateRemoteFilesContents: false);
            BookReader bookReader = new(environmentDependencies, null);
            EpubBook actualEpubBook = await bookReader.ReadBookAsync(epubFileStream);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a file synchronously with downloading remote files should succeed")]
        public void ReadBookFromFileWithDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(EPUB_FILE_PATH, populateRemoteFilesContents: true);
            EpubReaderOptions epubReaderOptions = CreateEpubReaderOptionsToDownloadRemoteFiles();
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = bookReader.ReadBook(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a file asynchronously with downloading remote files should succeed")]
        public async Task ReadBookFromFileAsyncWithDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(EPUB_FILE_PATH, populateRemoteFilesContents: true);
            EpubReaderOptions epubReaderOptions = CreateEpubReaderOptionsToDownloadRemoteFiles();
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = await bookReader.ReadBookAsync(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a stream synchronously with downloading remote files should succeed")]
        public void ReadBookFromStreamWithDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            MemoryStream epubFileStream = new();
            environmentDependencies.FileSystem = new TestFileSystem(epubFileStream, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(null, populateRemoteFilesContents: true);
            EpubReaderOptions epubReaderOptions = CreateEpubReaderOptionsToDownloadRemoteFiles();
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = bookReader.ReadBook(epubFileStream);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book from a stream asynchronously with downloading remote files should succeed")]
        public async Task ReadBookFromStreamAsyncWithDownloadingRemoteFilesTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            MemoryStream epubFileStream = new();
            environmentDependencies.FileSystem = new TestFileSystem(epubFileStream, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(null, populateRemoteFilesContents: true);
            EpubReaderOptions epubReaderOptions = CreateEpubReaderOptionsToDownloadRemoteFiles();
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = await bookReader.ReadBookAsync(epubFileStream);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "Reading a full EPUB book with null ContentDownloaderOptions should succeed")]
        public void ReadBookFromFileWithNullContentDownloaderOptionsTest()
        {
            TestZipFile testEpubFile = TestEpubFiles.CreateFullTestEpubFile();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, testEpubFile);
            EpubBook expectedEpubBook = TestEpubBooks.CreateFullTestEpubBook(EPUB_FILE_PATH, populateRemoteFilesContents: false);
            EpubReaderOptions epubReaderOptions = new()
            {
                ContentDownloaderOptions = null!
            };
            BookReader bookReader = new(environmentDependencies, epubReaderOptions);
            EpubBook actualEpubBook = bookReader.ReadBook(EPUB_FILE_PATH);
            EpubBookComparer.CompareEpubBooks(expectedEpubBook, actualEpubBook);
        }

        [Fact(DisplayName = "ReadBook should throw FileNotFoundException if the specified file does not exist")]
        public void ReadBookFromFileWithMissingFileTest()
        {
            BookReader bookReader = new(environmentDependencies, null);
            Assert.Throws<FileNotFoundException>(() => bookReader.ReadBook(EPUB_FILE_PATH));
        }

        [Fact(DisplayName = "ReadBookAsync should throw FileNotFoundException if the specified file does not exist")]
        public async Task ReadBookFromFileAsyncWithMissingFileTest()
        {
            BookReader bookReader = new(environmentDependencies, null);
            await Assert.ThrowsAsync<FileNotFoundException>(() => bookReader.ReadBookAsync(EPUB_FILE_PATH));
        }

        [Fact(DisplayName = "ReadBook should rethrow EPUB parsing exceptions")]
        public void ReadBookFromFileWithIncorrectEpubTest()
        {
            TestZipFile incorrectEpubFile = new();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, incorrectEpubFile);
            BookReader bookReader = new(environmentDependencies, null);
            Assert.Throws<EpubContainerException>(() => bookReader.ReadBook(EPUB_FILE_PATH));
        }

        [Fact(DisplayName = "ReadBookAsync should rethrow EPUB parsing exceptions")]
        public async Task ReadBookFromFileAsyncWithIncorrectEpubTest()
        {
            TestZipFile incorrectEpubFile = new();
            environmentDependencies.FileSystem = new TestFileSystem(EPUB_FILE_PATH, incorrectEpubFile);
            BookReader bookReader = new(environmentDependencies, null);
            await Assert.ThrowsAsync<EpubContainerException>(() => bookReader.ReadBookAsync(EPUB_FILE_PATH));
        }

        private static EpubReaderOptions CreateEpubReaderOptionsToDownloadRemoteFiles()
        {
            TestContentDownloader testContentDownloader = new();
            testContentDownloader.AddTextRemoteFile(REMOTE_HTML_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_HTML_FILE_CONTENT);
            testContentDownloader.AddTextRemoteFile(REMOTE_CSS_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_CSS_FILE_CONTENT);
            testContentDownloader.AddByteRemoteFile(REMOTE_IMAGE_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_IMAGE_FILE_CONTENT);
            testContentDownloader.AddByteRemoteFile(REMOTE_FONT_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_FONT_FILE_CONTENT);
            testContentDownloader.AddTextRemoteFile(REMOTE_XML_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_XML_FILE_CONTENT);
            testContentDownloader.AddByteRemoteFile(REMOTE_AUDIO_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_AUDIO_FILE_CONTENT);
            testContentDownloader.AddByteRemoteFile(REMOTE_VIDEO_CONTENT_FILE_HREF, TestEpubFiles.REMOTE_VIDEO_FILE_CONTENT);
            return new()
            {
                ContentDownloaderOptions = new ContentDownloaderOptions()
                {
                    DownloadContent = true,
                    CustomContentDownloader = testContentDownloader
                }
            };
        }
    }
}