File size: 20,024 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
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 = "<metadata />";
private const string FULL_METADATA_XML = $"""
<metadata xmlns="http://www.idpf.org/2007/opf" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title-1" dir="ltr" lang="en">Test title 1</dc:title>
<dc:title id="title-2" dir="rtl" lang="is">Test title 2</dc:title>
<dc:creator id="creator-1" opf:role="author" opf:file-as="Doe, John" dir="ltr" lang="en">John Doe</dc:creator>
<dc:creator id="creator-2" opf:role="author" opf:file-as="Doe, Jane" dir="rtl" lang="is">Jane Doe</dc:creator>
<dc:subject id="subject-1" dir="ltr" lang="en">Test subject 1</dc:subject>
<dc:subject id="subject-2" dir="rtl" lang="is">Test subject 2</dc:subject>
<dc:description id="description-1" dir="ltr" lang="en">Test description 1</dc:description>
<dc:description id="description-2" dir="rtl" lang="is">Test description 2</dc:description>
<dc:publisher id="publisher-1" dir="ltr" lang="en">Test publisher 1</dc:publisher>
<dc:publisher id="publisher-2" dir="rtl" lang="is">Test publisher 2</dc:publisher>
<dc:contributor id="contributor-1" opf:role="editor" opf:file-as="Editor, John" dir="ltr" lang="en">John Editor</dc:contributor>
<dc:contributor id="contributor-2" opf:role="editor" opf:file-as="Editor, Jane" dir="rtl" lang="is">Jane Editor</dc:contributor>
<dc:date id="date-1" opf:event="creation">2021-12-31T23:59:59.123456Z</dc:date>
<dc:date id="date-2" opf:event="publication">2022-01-23</dc:date>
<dc:type id="type-1">dictionary</dc:type>
<dc:type id="type-2">preview</dc:type>
<dc:format id="format-1">format-1</dc:format>
<dc:format id="format-2">format-2</dc:format>
<dc:identifier id="identifier-1" opf:scheme="URI">https://example.com/books/123</dc:identifier>
<dc:identifier id="identifier-2" opf:scheme="ISBN">9781234567890</dc:identifier>
<dc:source id="source-1">https://example.com/books/123/content-1.html</dc:source>
<dc:source id="source-2">https://example.com/books/123/content-2.html</dc:source>
<dc:language id="language-1">en</dc:language>
<dc:language id="language-2">is</dc:language>
<dc:relation id="relation-1" dir="ltr" lang="en">https://example.com/books/123/related-1.html</dc:relation>
<dc:relation id="relation-2" dir="rtl" lang="is">https://example.com/books/123/related-2.html</dc:relation>
<dc:coverage id="coverage-1" dir="ltr" lang="en">New York</dc:coverage>
<dc:coverage id="coverage-2" dir="rtl" lang="is">1700-1850</dc:coverage>
<dc:rights id="rights-1" dir="ltr" lang="en">Public domain in the USA</dc:rights>
<dc:rights id="rights-2" dir="rtl" lang="is">All rights reserved</dc:rights>
<link id="link-1" rel="record" href="front.html#meta-json" media-type="application/xhtml+xml" hreflang="en" />
<link id="link-2" rel="record onix-record" href="https://example.com/onix/123" media-type="application/xml" properties="onix" />
<link id="link-3" rel="record" href="book.atom" media-type="application/atom+xml;type=entry;profile=opds-catalog" />
<link id="link-4" rel="voicing" refines="#title-1" href="title.mp3" media-type="audio/mpeg" />
<meta name="cover" content="cover-image" />
<meta id="meta-1" property="rendition:orientation">landscape</meta>
<meta id="meta-2" property="identifier-type" refines="#identifier-2" scheme="onix:codelist5" dir="ltr" lang="en">123</meta>
<meta id="meta-3" property="alternate-script" refines="#creator-1" dir="rtl" lang="is">Brynjólfur Sveinsson</meta>
</metadata>
""";
private const string METADATA_XML_WITHOUT_HREF_IN_METADATA_LINK = $"""
<metadata>
<link rel="record" />
</metadata>
""";
private const string METADATA_XML_WITHOUT_REL_IN_METADATA_LINK = $"""
<metadata>
<link href="chapter.html" />
</metadata>
""";
private static EpubMetadata MinimalMetadata => new();
private static EpubMetadata FullMetadata =>
new
(
titles: new List<EpubMetadataTitle>()
{
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<EpubMetadataCreator>()
{
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<EpubMetadataSubject>()
{
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<EpubMetadataDescription>()
{
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<EpubMetadataPublisher>()
{
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<EpubMetadataContributor>()
{
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<EpubMetadataDate>()
{
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<EpubMetadataType>()
{
new EpubMetadataType
(
type: "dictionary",
id: "type-1"
),
new EpubMetadataType
(
type: "preview",
id: "type-2"
)
},
formats: new List<EpubMetadataFormat>()
{
new EpubMetadataFormat
(
format: "format-1",
id: "format-1"
),
new EpubMetadataFormat
(
format: "format-2",
id: "format-2"
)
},
identifiers: new List<EpubMetadataIdentifier>()
{
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<EpubMetadataSource>()
{
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<EpubMetadataLanguage>()
{
new EpubMetadataLanguage
(
language: "en",
id: "language-1"
),
new EpubMetadataLanguage
(
language: "is",
id: "language-2"
)
},
relations: new List<EpubMetadataRelation>()
{
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<EpubMetadataCoverage>()
{
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<EpubMetadataRights>()
{
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<EpubMetadataLink>()
{
new EpubMetadataLink
(
id: "link-1",
href: "front.html#meta-json",
mediaType: "application/xhtml+xml",
properties: null,
refines: null,
relationships: new List<EpubMetadataLinkRelationship>()
{
EpubMetadataLinkRelationship.RECORD
},
hrefLanguage: "en"
),
new EpubMetadataLink
(
id: "link-2",
href: "https://example.com/onix/123",
mediaType: "application/xml",
properties: new List<EpubMetadataLinkProperty>()
{
EpubMetadataLinkProperty.ONIX
},
refines: null,
relationships: new List<EpubMetadataLinkRelationship>()
{
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>()
{
EpubMetadataLinkRelationship.RECORD
},
hrefLanguage: null
),
new EpubMetadataLink
(
id: "link-4",
href: "title.mp3",
mediaType: "audio/mpeg",
properties: null,
refines: "#title-1",
relationships: new List<EpubMetadataLinkRelationship>()
{
EpubMetadataLinkRelationship.VOICING
},
hrefLanguage: null
)
},
metaItems: new List<EpubMetadataMeta>()
{
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<EpubPackageException>(() => MetadataReader.ReadMetadata(metadataNode));
}
}
}
|