using System.Xml.Linq; using VersOne.Epub.Internal; using VersOne.Epub.Schema; using VersOne.Epub.Test.Comparers; namespace VersOne.Epub.Test.Unit.Readers { public class MetadataReaderTests { private const string MINIMAL_METADATA_XML = ""; private const string FULL_METADATA_XML = $""" Test title 1 Test title 2 John Doe Jane Doe Test subject 1 Test subject 2 Test description 1 Test description 2 Test publisher 1 Test publisher 2 John Editor Jane Editor 2021-12-31T23:59:59.123456Z 2022-01-23 dictionary preview format-1 format-2 https://example.com/books/123 9781234567890 https://example.com/books/123/content-1.html https://example.com/books/123/content-2.html en is https://example.com/books/123/related-1.html https://example.com/books/123/related-2.html New York 1700-1850 Public domain in the USA All rights reserved landscape 123 Brynjólfur Sveinsson """; private const string METADATA_XML_WITHOUT_HREF_IN_METADATA_LINK = $""" """; private const string METADATA_XML_WITHOUT_REL_IN_METADATA_LINK = $""" """; private static EpubMetadata MinimalMetadata => new(); private static EpubMetadata FullMetadata => new ( titles: new List() { new EpubMetadataTitle ( title: "Test title 1", id: "title-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataTitle ( title: "Test title 2", id: "title-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, creators: new List() { new EpubMetadataCreator ( role: "author", id: "creator-1", fileAs: "Doe, John", creator: "John Doe", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataCreator ( role: "author", id: "creator-2", fileAs: "Doe, Jane", creator: "Jane Doe", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, subjects: new List() { new EpubMetadataSubject ( subject: "Test subject 1", id: "subject-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataSubject ( subject: "Test subject 2", id: "subject-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, descriptions: new List() { new EpubMetadataDescription ( description: "Test description 1", id: "description-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataDescription ( description: "Test description 2", id: "description-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, publishers: new List() { new EpubMetadataPublisher ( publisher: "Test publisher 1", id: "publisher-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataPublisher ( publisher: "Test publisher 2", id: "publisher-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, contributors: new List() { new EpubMetadataContributor ( role: "editor", id: "contributor-1", fileAs: "Editor, John", contributor: "John Editor", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataContributor ( role: "editor", id: "contributor-2", fileAs: "Editor, Jane", contributor: "Jane Editor", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, dates: new List() { new EpubMetadataDate ( @event: "creation", id: "date-1", date: "2021-12-31T23:59:59.123456Z" ), new EpubMetadataDate ( @event: "publication", id: "date-2", date: "2022-01-23" ) }, types: new List() { new EpubMetadataType ( type: "dictionary", id: "type-1" ), new EpubMetadataType ( type: "preview", id: "type-2" ) }, formats: new List() { new EpubMetadataFormat ( format: "format-1", id: "format-1" ), new EpubMetadataFormat ( format: "format-2", id: "format-2" ) }, identifiers: new List() { new EpubMetadataIdentifier ( identifier: "https://example.com/books/123", id: "identifier-1", scheme: "URI" ), new EpubMetadataIdentifier ( identifier: "9781234567890", id: "identifier-2", scheme: "ISBN" ) }, sources: new List() { new EpubMetadataSource ( source: "https://example.com/books/123/content-1.html", id: "source-1" ), new EpubMetadataSource ( source: "https://example.com/books/123/content-2.html", id: "source-2" ) }, languages: new List() { new EpubMetadataLanguage ( language: "en", id: "language-1" ), new EpubMetadataLanguage ( language: "is", id: "language-2" ) }, relations: new List() { new EpubMetadataRelation ( relation: "https://example.com/books/123/related-1.html", id: "relation-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataRelation ( relation: "https://example.com/books/123/related-2.html", id: "relation-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, coverages: new List() { new EpubMetadataCoverage ( coverage: "New York", id: "coverage-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataCoverage ( coverage: "1700-1850", id: "coverage-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, rights: new List() { new EpubMetadataRights ( rights: "Public domain in the USA", id: "rights-1", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataRights ( rights: "All rights reserved", id: "rights-2", textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) }, links: new List() { new EpubMetadataLink ( id: "link-1", href: "front.html#meta-json", mediaType: "application/xhtml+xml", properties: null, refines: null, relationships: new List() { EpubMetadataLinkRelationship.RECORD }, hrefLanguage: "en" ), new EpubMetadataLink ( id: "link-2", href: "https://example.com/onix/123", mediaType: "application/xml", properties: new List() { EpubMetadataLinkProperty.ONIX }, refines: null, relationships: new List() { EpubMetadataLinkRelationship.RECORD, EpubMetadataLinkRelationship.ONIX_RECORD }, hrefLanguage: null ), new EpubMetadataLink ( id: "link-3", href: "book.atom", mediaType: "application/atom+xml;type=entry;profile=opds-catalog", properties: null, refines: null, relationships: new List() { EpubMetadataLinkRelationship.RECORD }, hrefLanguage: null ), new EpubMetadataLink ( id: "link-4", href: "title.mp3", mediaType: "audio/mpeg", properties: null, refines: "#title-1", relationships: new List() { EpubMetadataLinkRelationship.VOICING }, hrefLanguage: null ) }, metaItems: new List() { new EpubMetadataMeta ( name: "cover", content: "cover-image" ), new EpubMetadataMeta ( name: null, content: "landscape", id: "meta-1", refines: null, property: "rendition:orientation", scheme: null ), new EpubMetadataMeta ( name: null, content: "123", id: "meta-2", refines: "#identifier-2", property: "identifier-type", scheme: "onix:codelist5", textDirection: EpubTextDirection.LEFT_TO_RIGHT, language: "en" ), new EpubMetadataMeta ( name: null, content: "Brynjólfur Sveinsson", id: "meta-3", refines: "#creator-1", property: "alternate-script", scheme: null, textDirection: EpubTextDirection.RIGHT_TO_LEFT, language: "is" ) } ); [Fact(DisplayName = "Reading a minimal metadata XML should succeed")] public void ReadMinimalMetadataTest() { TestSuccessfulReadOperation(MINIMAL_METADATA_XML, MinimalMetadata); } [Fact(DisplayName = "Reading a full metadata XML should succeed")] public void ReadFullMetadataTest() { TestSuccessfulReadOperation(FULL_METADATA_XML, FullMetadata); } [Fact(DisplayName = "Trying to read metadata XML without 'href' attribute in a metadata link XML node should fail with EpubPackageException")] public void ReadPackageWithoutMetadataLinkHrefTest() { TestFailingReadOperation(METADATA_XML_WITHOUT_HREF_IN_METADATA_LINK); } [Fact(DisplayName = "Trying to read metadata XML without 'rel' attribute in a metadata link XML node should fail with EpubPackageException")] public void ReadPackageWithoutMetadataLinkRelTest() { TestFailingReadOperation(METADATA_XML_WITHOUT_REL_IN_METADATA_LINK); } private static void TestSuccessfulReadOperation(string metadataXml, EpubMetadata expectedEpubMetadata) { XElement metadataNode = XElement.Parse(metadataXml); EpubMetadata actualEpubMetadata = MetadataReader.ReadMetadata(metadataNode); EpubMetadataComparer.CompareEpubMetadatas(expectedEpubMetadata, actualEpubMetadata); } private static void TestFailingReadOperation(string metadataXml) { XElement metadataNode = XElement.Parse(metadataXml); Assert.Throws(() => MetadataReader.ReadMetadata(metadataNode)); } } }