using System.Collections.ObjectModel; 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.Content.Collections { public class EpubContentCollectionRefTests { private static ReadOnlyCollection Local { get { List list = new() { TestEpubContentRef.Chapter1FileRef, TestEpubContentRef.Chapter2FileRef }; return list.AsReadOnly(); } } private static ReadOnlyCollection Remote { get { List list = new() { TestEpubContentRef.RemoteHtmlContentFileRef, TestEpubContentRef.RemoteCssContentFileRef }; return list.AsReadOnly(); } } private static IEpubContentLoader ContentLoader => new TestEpubContentLoader(); [Fact(DisplayName = "Constructing a EpubContentCollectionRef instance with default parameters should succeed")] public void ConstructorWithNonNullParametersTest() { EpubContentCollectionRef epubContentCollectionRef = new(); Assert.NotNull(epubContentCollectionRef.Local); Assert.Empty(epubContentCollectionRef.Local); Assert.NotNull(epubContentCollectionRef.Remote); Assert.Empty(epubContentCollectionRef.Remote); } [Fact(DisplayName = "Constructing a EpubContentCollectionRef instance with null local parameter should succeed")] public void ConstructorWithNullLocalParameterTest() { EpubContentCollectionRef epubContentCollectionRef = new(null, Remote); Assert.NotNull(epubContentCollectionRef.Local); Assert.Empty(epubContentCollectionRef.Local); CompareRemoteTextRefReadOnlyCollections(Remote, epubContentCollectionRef.Remote); } [Fact(DisplayName = "Constructing a EpubContentCollectionRef instance with null remote parameter should succeed")] public void ConstructorWithNullRemoteParameterTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, null); CompareLocalTextRefReadOnlyCollections(Local, epubContentCollectionRef.Local); Assert.NotNull(epubContentCollectionRef.Remote); Assert.Empty(epubContentCollectionRef.Remote); } [Fact(DisplayName = "Constructor should throw EpubPackageException if local parameter contains content files with duplicate keys")] public void ConstructorWithLocalDuplicateKeysTest() { string duplicateKey = CHAPTER1_FILE_NAME; ReadOnlyCollection localWithDuplicateKeys = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: duplicateKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ), new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: duplicateKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER2_FILE_PATH, epubContentLoader: ContentLoader ) } ); Assert.Throws(() => new EpubContentCollectionRef(localWithDuplicateKeys, Remote)); } [Fact(DisplayName = "Constructor should throw EpubPackageException if local parameter contains content files with duplicate file paths")] public void ConstructorWithLocalDuplicateFilePathsTest() { string duplicateFilePath = CHAPTER1_FILE_PATH; ReadOnlyCollection localWithDuplicateFilePaths = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: duplicateFilePath, epubContentLoader: ContentLoader ), new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER2_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: duplicateFilePath, epubContentLoader: ContentLoader ) } ); Assert.Throws(() => new EpubContentCollectionRef(localWithDuplicateFilePaths, Remote)); } [Fact(DisplayName = "Constructor should throw EpubPackageException if remote parameter contains content files with duplicate URLs")] public void ConstructorWithRemoteDuplicateUrlsTest() { string duplicateKey = REMOTE_HTML_CONTENT_FILE_HREF; ReadOnlyCollection remoteWithDuplicateKeys = new ( new List() { new EpubRemoteTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: duplicateKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ), new EpubRemoteTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: duplicateKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ) } ); Assert.Throws(() => new EpubContentCollectionRef(Local, remoteWithDuplicateKeys)); } [Fact(DisplayName = "ContainsLocalFileRefWithKey should return true if the local file reference with the given key exists and false otherwise")] public void ContainsLocalFileWithKeyWithNonNullKeyTest() { string existingKey = CHAPTER1_FILE_NAME; string nonExistingKey = CHAPTER2_FILE_NAME; ReadOnlyCollection local = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: existingKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.True(epubContentCollectionRef.ContainsLocalFileRefWithKey(existingKey)); Assert.False(epubContentCollectionRef.ContainsLocalFileRefWithKey(nonExistingKey)); } [Fact(DisplayName = "ContainsLocalFileRefWithKey should throw ArgumentNullException if key argument is null")] public void ContainsLocalFileWithKeyWithNullKeyTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.ContainsLocalFileRefWithKey(null!)); } [Fact(DisplayName = "GetLocalFileRefByKey should return the local file reference with the given key if it exists")] public void GetLocalFileByKeyWithExistingKeyTest() { string existingKey = CHAPTER1_FILE_NAME; EpubLocalTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: existingKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ); ReadOnlyCollection local = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); EpubLocalTextContentFileRef actualFileRef = epubContentCollectionRef.GetLocalFileRefByKey(existingKey); EpubContentRefComparer.CompareEpubLocalContentFileRefs(expectedFileRef, actualFileRef); } [Fact(DisplayName = "GetLocalFileRefByKey should throw EpubContentCollectionRefException if the local file reference with the given key doesn't exist")] public void GetLocalFileByKeyWithNonExistingKeyTest() { string nonExistingKey = CHAPTER2_FILE_NAME; ReadOnlyCollection local = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.Throws(() => epubContentCollectionRef.GetLocalFileRefByKey(nonExistingKey)); } [Fact(DisplayName = "GetLocalFileRefByKey should throw ArgumentNullException if key argument is null")] public void GetLocalFileByKeyWithNullKeyTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.GetLocalFileRefByKey(null!)); } [Fact(DisplayName = "TryGetLocalFileRefByKey should return true with the local file reference with the given key if it exists and false with null reference otherwise")] public void TryGetLocalFileByKeyWithNonNullKeyTest() { string existingKey = CHAPTER1_FILE_NAME; string nonExistingKey = CHAPTER2_FILE_NAME; EpubLocalTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: existingKey, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ); ReadOnlyCollection local = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.True(epubContentCollectionRef.TryGetLocalFileRefByKey(existingKey, out EpubLocalTextContentFileRef actualFileRefForExistingKey)); EpubContentRefComparer.CompareEpubLocalContentFileRefs(expectedFileRef, actualFileRefForExistingKey); Assert.False(epubContentCollectionRef.TryGetLocalFileRefByKey(nonExistingKey, out EpubLocalTextContentFileRef actualFileRefForNonExistingKey)); Assert.Null(actualFileRefForNonExistingKey); } [Fact(DisplayName = "TryGetLocalFileRefByKey should throw ArgumentNullException if key argument is null")] public void TryGetLocalFileByKeyWithNullKeyTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.TryGetLocalFileRefByKey(null!, out _)); } [Fact(DisplayName = "ContainsLocalFileRefWithFilePath should return true if the local file reference with the given file path exists and false otherwise")] public void ContainsLocalFileWithFilePathWithNonNullFilePathTest() { string existingFilePath = CHAPTER1_FILE_PATH; string nonExistingFilePath = CHAPTER2_FILE_PATH; ReadOnlyCollection local = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: existingFilePath, epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.True(epubContentCollectionRef.ContainsLocalFileRefWithFilePath(existingFilePath)); Assert.False(epubContentCollectionRef.ContainsLocalFileRefWithFilePath(nonExistingFilePath)); } [Fact(DisplayName = "ContainsLocalFileRefWithFilePath should throw ArgumentNullException if filePath argument is null")] public void ContainsLocalFileWithFilePathWithNullFilePathTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.ContainsLocalFileRefWithFilePath(null!)); } [Fact(DisplayName = "GetLocalFileRefByFilePath should return the local file reference with the given file path if it exists")] public void GetLocalFileByFilePathWithExistingFilePathTest() { string existingFilePath = CHAPTER1_FILE_PATH; EpubLocalTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: existingFilePath, epubContentLoader: ContentLoader ); ReadOnlyCollection local = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); EpubLocalTextContentFileRef actualFileRef = epubContentCollectionRef.GetLocalFileRefByFilePath(existingFilePath); EpubContentRefComparer.CompareEpubLocalContentFileRefs(expectedFileRef, actualFileRef); } [Fact(DisplayName = "GetLocalFileRefByFilePath should throw EpubContentCollectionRefException if the local file reference with the given file path doesn't exist")] public void GetLocalFileByFilePathWithNonExistingFilePathTest() { string nonExistingFilePath = CHAPTER2_FILE_PATH; ReadOnlyCollection local = new ( new List() { new EpubLocalTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: CHAPTER1_FILE_PATH, epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.Throws(() => epubContentCollectionRef.GetLocalFileRefByFilePath(nonExistingFilePath)); } [Fact(DisplayName = "GetLocalFileRefByFilePath should throw ArgumentNullException if filePath argument is null")] public void GetLocalFileByFilePathWithNullFilePathTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.GetLocalFileRefByFilePath(null!)); } [Fact(DisplayName = "TryGetLocalFileRefByFilePath should return true with the local file reference with the given file path if it exists and false with null reference otherwise")] public void TryGetLocalFileByFilePathWithNonNullFilePathTest() { string existingFilePath = CHAPTER1_FILE_PATH; string nonExistingFilePath = CHAPTER2_FILE_PATH; EpubLocalTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: CHAPTER1_FILE_NAME, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), filePath: existingFilePath, epubContentLoader: ContentLoader ); ReadOnlyCollection local = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(local, Remote); Assert.True(epubContentCollectionRef.TryGetLocalFileRefByFilePath(existingFilePath, out EpubLocalTextContentFileRef actualFileRefForExistingFilePath)); EpubContentRefComparer.CompareEpubLocalContentFileRefs(expectedFileRef, actualFileRefForExistingFilePath); Assert.False(epubContentCollectionRef.TryGetLocalFileRefByFilePath(nonExistingFilePath, out EpubLocalTextContentFileRef actualFileRefForNonExistingFilePath)); Assert.Null(actualFileRefForNonExistingFilePath); } [Fact(DisplayName = "TryGetLocalFileRefByFilePath should throw ArgumentNullException if filePath argument is null")] public void TryGetLocalFileByFilePathWithNullFilePathTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.TryGetLocalFileRefByFilePath(null!, out _)); } [Fact(DisplayName = "ContainsRemoteFileRefWithUrl should return true if the remote file reference with the given URL exists and false otherwise")] public void ContainsRemoteFileWithUrlWithNonNullUrlTest() { string existingUrl = REMOTE_HTML_CONTENT_FILE_HREF; string nonExistingUrl = REMOTE_CSS_CONTENT_FILE_HREF; ReadOnlyCollection remote = new ( new List() { new EpubRemoteTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: existingUrl, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(Local, remote); Assert.True(epubContentCollectionRef.ContainsRemoteFileRefWithUrl(existingUrl)); Assert.False(epubContentCollectionRef.ContainsRemoteFileRefWithUrl(nonExistingUrl)); } [Fact(DisplayName = "ContainsRemoteFileRefWithUrl should throw ArgumentNullException if url argument is null")] public void ContainsRemoteFileWithUrlWithNullUrlTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.ContainsRemoteFileRefWithUrl(null!)); } [Fact(DisplayName = "GetRemoteFileRefByUrl should return the remote file reference with the given URL if it exists")] public void GetRemoteFileByUrlWithExistingUrlTest() { string existingUrl = REMOTE_HTML_CONTENT_FILE_HREF; EpubRemoteTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: existingUrl, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ); ReadOnlyCollection remote = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(Local, remote); EpubRemoteTextContentFileRef actualFileRef = epubContentCollectionRef.GetRemoteFileRefByUrl(existingUrl); EpubContentRefComparer.CompareEpubRemoteContentFileRefs(expectedFileRef, actualFileRef); } [Fact(DisplayName = "GetRemoteFileRefByUrl should throw EpubContentCollectionRefException if the remote file reference with the given URL doesn't exist")] public void GetRemoteFileByUrlWithNonExistingUrlTest() { string nonExistingUrl = REMOTE_CSS_CONTENT_FILE_HREF; ReadOnlyCollection remote = new ( new List() { new EpubRemoteTextContentFileRef ( metadata: new EpubContentFileRefMetadata ( key: REMOTE_HTML_CONTENT_FILE_HREF, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ) } ); EpubContentCollectionRef epubContentCollectionRef = new(Local, remote); Assert.Throws(() => epubContentCollectionRef.GetRemoteFileRefByUrl(nonExistingUrl)); } [Fact(DisplayName = "GetRemoteFileRefByUrl should throw ArgumentNullException if url argument is null")] public void GetRemoteFileByUrlWithNullUrlTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.GetRemoteFileRefByUrl(null!)); } [Fact(DisplayName = "TryGetRemoteFileRefByUrl should return true with the remote file reference with the given URL if it exists and false with null reference otherwise")] public void TryGetRemoteFileByUrlWithNonNullUrlTest() { string existingUrl = REMOTE_HTML_CONTENT_FILE_HREF; string nonExistingUrl = REMOTE_CSS_CONTENT_FILE_HREF; EpubRemoteTextContentFileRef expectedFileRef = new ( metadata: new EpubContentFileRefMetadata ( key: existingUrl, contentType: HTML_CONTENT_TYPE, contentMimeType: HTML_CONTENT_MIME_TYPE ), epubContentLoader: ContentLoader ); ReadOnlyCollection remote = new ( new List() { expectedFileRef } ); EpubContentCollectionRef epubContentCollectionRef = new(Local, remote); Assert.True(epubContentCollectionRef.TryGetRemoteFileRefByUrl(existingUrl, out EpubRemoteTextContentFileRef actualFileRefForExistingUrl)); EpubContentRefComparer.CompareEpubRemoteContentFileRefs(expectedFileRef, actualFileRefForExistingUrl); Assert.False(epubContentCollectionRef.TryGetRemoteFileRefByUrl(nonExistingUrl, out EpubRemoteTextContentFileRef actualFileRefForNonExistingUrl)); Assert.Null(actualFileRefForNonExistingUrl); } [Fact(DisplayName = "TryGetRemoteFileRefByUrl should throw ArgumentNullException if url argument is null")] public void TryGetRemoteFileByUrlWithNullUrlTest() { EpubContentCollectionRef epubContentCollectionRef = new(Local, Remote); Assert.Throws(() => epubContentCollectionRef.TryGetRemoteFileRefByUrl(null!, out _)); } private static void CompareLocalTextRefReadOnlyCollections(ReadOnlyCollection expected, ReadOnlyCollection actual) { CollectionComparer.CompareCollections(expected, actual, EpubContentRefComparer.CompareEpubLocalContentFileRefs); } private static void CompareRemoteTextRefReadOnlyCollections(ReadOnlyCollection expected, ReadOnlyCollection actual) { CollectionComparer.CompareCollections(expected, actual, EpubContentRefComparer.CompareEpubRemoteContentFileRefs); } } }