github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/pack/w3cpackage_test.go (about)

     1  // Copyright 2020 Readium Foundation. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license
     3  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     4  
     5  package pack
     6  
     7  import (
     8  	"encoding/json"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/readium/readium-lcp-server/rwpm"
    14  )
    15  
    16  // TestMapW3CPublication tests the mapping of a W3C manifest to a Readium manifest
    17  func TestMapW3CPublication(t *testing.T) {
    18  
    19  	file, err := os.Open("./samples/w3cman1.json")
    20  	if err != nil {
    21  		t.Fatalf("Could not find the sample file, %s", err)
    22  	}
    23  	defer file.Close()
    24  
    25  	var w3cManifest rwpm.W3CPublication
    26  	decoder := json.NewDecoder(file)
    27  	err = decoder.Decode(&w3cManifest)
    28  	if err != nil {
    29  		t.Fatalf("Could not unmarshal sample file, %s", err)
    30  	}
    31  
    32  	rman := generateRWPManifest(w3cManifest)
    33  
    34  	// metadata
    35  	meta := rman.Metadata
    36  
    37  	if meta.Identifier != "id1" {
    38  		t.Fatalf("W3C Identifer badly mapped")
    39  	}
    40  	if meta.Title.Text() != "audiotest" {
    41  		t.Fatalf("W3C Name badly mapped")
    42  	}
    43  	if meta.Publisher.Name() != "Stanford" {
    44  		t.Fatalf("W3C Publisher badly mapped")
    45  	}
    46  	if meta.Author.Name() != "" {
    47  		t.Fatalf("W3C Author badly mapped 1")
    48  	}
    49  
    50  	i := 0
    51  	for _, a := range meta.Author {
    52  		if a.Name.Text() == "Alpha" || a.Name.Text() == "Beta" || a.Name.Text() == "Gamma" {
    53  			i++
    54  		}
    55  	}
    56  	if i != 2 {
    57  		t.Fatalf("W3C Author badly mapped, expected 2 got %d", i)
    58  	}
    59  	if meta.Language[0] != "fr" || meta.Language[1] != "en" {
    60  		t.Fatalf("W3C InLanguage badly mapped")
    61  	}
    62  	if *meta.Published != rwpm.Date(time.Date(2020, 03, 23, 12, 50, 20, 0, time.UTC)) {
    63  		t.Fatalf("W3C DatePublished badly mapped")
    64  	}
    65  	mod := time.Date(2020, 03, 23, 16, 58, 27, 372000000, time.UTC)
    66  	if *meta.Modified != mod {
    67  		t.Fatalf("W3C DateModified badly mapped")
    68  	}
    69  	if meta.Duration != 150 {
    70  		t.Fatalf("W3C Duration badly mapped")
    71  	}
    72  
    73  	// Linked resources
    74  	item0 := rman.ReadingOrder[0]
    75  	if item0.Href != "audio/gtr-jazz.mp3" {
    76  		t.Fatalf("W3C URL badly mapped")
    77  	}
    78  	if item0.Type != "audio/mpeg" {
    79  		t.Fatalf("W3C EncodingFormat badly mapped")
    80  	}
    81  	if item0.Title != "Track 1" {
    82  		t.Fatalf("W3C Name badly mapped")
    83  	}
    84  	if item0.Duration != 10 {
    85  		t.Fatalf("W3C Duration badly mapped")
    86  	}
    87  
    88  	item1 := rman.ReadingOrder[1]
    89  	if item1.Type != "audio/mpeg" {
    90  		t.Fatalf("W3C EncodingFormat badly mapped if missing")
    91  	}
    92  	if item1.Alternate[0].Href != "audio/Latin.mp3" {
    93  		t.Fatalf("W3C Name badly mapped in Alternate")
    94  	}
    95  	if item1.Alternate[0].Type != "audio/mpeg" {
    96  		t.Fatalf("W3C EncodingFormat badly mapped in Alternate")
    97  	}
    98  
    99  }