github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/portfolio_test.go (about)

     1  /*
     2  Copyright 2020 The pdf Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/pdfcpu/pdfcpu/pkg/api"
    24  )
    25  
    26  func TestPortfolio(t *testing.T) {
    27  	msg := "testPortfolio"
    28  
    29  	if err := prepareForAttachmentTest(t); err != nil {
    30  		t.Fatalf("%s prepare for portfolio: %v\n", msg, err)
    31  	}
    32  
    33  	fileName := filepath.Join(outDir, "go.pdf")
    34  
    35  	// # of portfolio entries must be 0
    36  	listAttachments(t, msg, fileName, 0)
    37  
    38  	// Attach 4 portfolio entries including descriptions.
    39  	files := []string{
    40  		filepath.Join(outDir, "golang.pdf"),
    41  		filepath.Join(outDir, "T4.pdf") + ", CCITT spec",
    42  		filepath.Join(outDir, "go-lecture.pdf"),
    43  		filepath.Join(outDir, "test.wav") + ", test audio file",
    44  	}
    45  	if err := api.AddAttachmentsFile(fileName, "", files, true, nil); err != nil {
    46  		t.Fatalf("%s add portfolio entries: %v\n", msg, err)
    47  	}
    48  
    49  	// List portfolio entries.
    50  	listAttachments(t, msg, fileName, 4)
    51  
    52  	// Extract all portfolio entries.
    53  	if err := api.ExtractAttachmentsFile(fileName, outDir, nil, nil); err != nil {
    54  		t.Fatalf("%s extract all portfolio entries: %v\n", msg, err)
    55  	}
    56  
    57  	// Extract 1 portfolio entry.
    58  	if err := api.ExtractAttachmentsFile(fileName, outDir, []string{"golang.pdf"}, nil); err != nil {
    59  		t.Fatalf("%s extract one portfolio entry: %v\n", msg, err)
    60  	}
    61  
    62  	// Remove 1 portfolio entry.
    63  	if err := api.RemoveAttachmentsFile(fileName, "", []string{"golang.pdf"}, nil); err != nil {
    64  		t.Fatalf("%s remove one portfolio entry: %v\n", msg, err)
    65  	}
    66  	listAttachments(t, msg, fileName, 3)
    67  
    68  	// Remove all portfolio entries.
    69  	if err := api.RemoveAttachmentsFile(fileName, "", nil, nil); err != nil {
    70  		t.Fatalf("%s remove all portfolio entries: %v\n", msg, err)
    71  	}
    72  	listAttachments(t, msg, fileName, 0)
    73  
    74  	// Validate the processed file.
    75  	if err := api.ValidateFile(fileName, nil); err != nil {
    76  		t.Fatalf("%s: validate: %v\n", msg, err)
    77  	}
    78  }