File size: 4,407 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
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Test.Unit.Mocks;

namespace VersOne.Epub.Test.Unit.Readers
{
    public class RootFilePathReaderTests
    {
        private const string EPUB_CONTAINER_FILE_PATH = "META-INF/container.xml";
        private const string ROOT_FILE_PATH_FOR_CORRECT_CONTAINER_FILE = "OEBPS/content.opf";

        private const string CORRECT_CONTAINER_FILE = $"""
            <?xml version='1.0' encoding='utf-8'?>
            <container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
              <rootfiles>
                <rootfile media-type="application/oebps-package+xml" full-path="{ROOT_FILE_PATH_FOR_CORRECT_CONTAINER_FILE}" />
              </rootfiles>
            </container>
            """;

        private const string INCORRECT_CONTAINER_FILE_NO_FULL_PATH_ATTRIBUTE = $"""
            <?xml version='1.0' encoding='utf-8'?>
            <container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
              <rootfiles>
                <rootfile media-type="application/oebps-package+xml" />
              </rootfiles>
            </container>
            """;

        private const string INCORRECT_CONTAINER_FILE_NO_ROOTFILE_ELEMENT = $"""
            <?xml version='1.0' encoding='utf-8'?>
            <container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
              <rootfiles>
              </rootfiles>
            </container>
            """;

        private const string INCORRECT_CONTAINER_FILE_NO_ROOTFILES_ELEMENT = $"""
            <?xml version='1.0' encoding='utf-8'?>
            <container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
            </container>
            """;

        private const string INCORRECT_CONTAINER_FILE_NO_CONTAINER_ELEMENT = $"""
            <?xml version='1.0' encoding='utf-8'?>
            <test />
            """;

        [Fact(DisplayName = "Constructing a RootFilePathReader instance with a non-null epubReaderOptions parameter should succeed")]
        public void ConstructorWithNonNullEpubReaderOptionsTest()
        {
            _ = new RootFilePathReader(new EpubReaderOptions());
        }

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

        [Fact(DisplayName = "Getting root file path from a ZIP archive with a correct container file should succeed")]
        public async Task TestGetRootFilePathAsyncWithCorrectContainerFile()
        {
            TestZipFile testZipFile = new();
            testZipFile.AddEntry(EPUB_CONTAINER_FILE_PATH, CORRECT_CONTAINER_FILE);
            RootFilePathReader rootFilePathReader = new();
            string actualRootFilePath = await rootFilePathReader.GetRootFilePathAsync(testZipFile);
            Assert.Equal(ROOT_FILE_PATH_FOR_CORRECT_CONTAINER_FILE, actualRootFilePath);
        }

        [Fact(DisplayName = "Getting root file path from a ZIP archive without a container file should throw EpubContainerException")]
        public async Task TestGetRootFilePathAsyncWithNoContainerFile()
        {
            TestZipFile testZipFile = new();
            RootFilePathReader rootFilePathReader = new();
            await Assert.ThrowsAsync<EpubContainerException>(() => rootFilePathReader.GetRootFilePathAsync(testZipFile));
        }

        [Theory(DisplayName = "Getting root file path from a ZIP archive with an incorrect container file should throw EpubContainerException")]
        [InlineData(INCORRECT_CONTAINER_FILE_NO_FULL_PATH_ATTRIBUTE)]
        [InlineData(INCORRECT_CONTAINER_FILE_NO_ROOTFILE_ELEMENT)]
        [InlineData(INCORRECT_CONTAINER_FILE_NO_ROOTFILES_ELEMENT)]
        [InlineData(INCORRECT_CONTAINER_FILE_NO_CONTAINER_ELEMENT)]
        public async Task TestGetRootFilePathAsyncWithIncorrectContainerFile(string containerFileContent)
        {
            TestZipFile testZipFile = new();
            testZipFile.AddEntry(EPUB_CONTAINER_FILE_PATH, containerFileContent);
            RootFilePathReader rootFilePathReader = new();
            await Assert.ThrowsAsync<EpubContainerException>(() => rootFilePathReader.GetRootFilePathAsync(testZipFile));
        }
    }
}