-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add activity to merge AIS metadata files
Fixes #77.
- Loading branch information
Showing
5 changed files
with
180 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ais | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/antchfx/xmlquery" | ||
) | ||
|
||
const MergeMDActivityName = "merge-md-files" | ||
|
||
type ( | ||
MergeMDActivity struct{} | ||
MergeMDActivityParams struct { | ||
METSRelPath string | ||
MetadataRelPath string | ||
WorkingDir string | ||
} | ||
MergeMDActivityResult struct { | ||
Path string | ||
} | ||
) | ||
|
||
func NewMergeMDActivity() *MergeMDActivity { | ||
return &MergeMDActivity{} | ||
} | ||
|
||
func (a *MergeMDActivity) Execute(ctx context.Context, params MergeMDActivityParams) (*MergeMDActivityResult, error) { | ||
aisName, err := aisFilename(filepath.Join(params.WorkingDir, params.MetadataRelPath)) | ||
if err != nil { | ||
return nil, fmt.Errorf("get AIS filename: %v", err) | ||
} | ||
|
||
aisPath := filepath.Join(params.WorkingDir, aisName) | ||
|
||
return &MergeMDActivityResult{Path: aisPath}, nil | ||
} | ||
|
||
func aisFilename(mdpath string) (string, error) { | ||
id, err := parseAccessionID(mdpath) | ||
if err != nil { | ||
return "", fmt.Errorf("get accession number: %v", err) | ||
} | ||
|
||
id = strings.ReplaceAll(id, "/", "_") | ||
|
||
return fmt.Sprintf("AIS_%s", id), nil | ||
} | ||
|
||
func parseAccessionID(path string) (string, error) { | ||
f, err := os.Open(path) // #nosec G304 -- trusted path | ||
if err != nil { | ||
return "", fmt.Errorf("open metadata file: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
sp, err := xmlquery.CreateStreamParser(f, "//paket/ablieferung/ablieferungsnummer") | ||
if err != nil { | ||
return "", fmt.Errorf("create XML parser: %v", err) | ||
} | ||
|
||
n, err := sp.Read() | ||
if err == io.EOF { | ||
return "", fmt.Errorf("can't find ablieferungsnummer in %q", filepath.Base(path)) | ||
} | ||
if err != nil { | ||
return "", fmt.Errorf("read XML stream: %v", err) | ||
} | ||
return n.InnerText(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package ais_test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
temporalsdk_activity "go.temporal.io/sdk/activity" | ||
temporalsdk_testsuite "go.temporal.io/sdk/testsuite" | ||
"gotest.tools/v3/assert" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/ais" | ||
) | ||
|
||
func TestExecute(t *testing.T) { | ||
t.Parallel() | ||
|
||
aipPath := "../testdata/little-Test-AIP-Digitization/" | ||
|
||
tests := []struct { | ||
name string | ||
params ais.MergeMDActivityParams | ||
want ais.MergeMDActivityResult | ||
wantErr string | ||
}{ | ||
{ | ||
name: "Returns an AIS metadata file path", | ||
params: ais.MergeMDActivityParams{ | ||
METSRelPath: "", | ||
MetadataRelPath: "additional/UpdatedAreldaMetadata.xml", | ||
WorkingDir: aipPath, | ||
}, | ||
want: ais.MergeMDActivityResult{ | ||
Path: filepath.Join(aipPath, "AIS_1000_893_3251903"), | ||
}, | ||
}, | ||
{ | ||
name: "Errors if metadata file doesn't exist", | ||
params: ais.MergeMDActivityParams{ | ||
METSRelPath: "", | ||
MetadataRelPath: "additional/missing.xml", | ||
WorkingDir: aipPath, | ||
}, | ||
wantErr: "activity error (type: merge-md-files, scheduledEventID: 0, startedEventID: 0, identity: ): get AIS filename: get accession number: open metadata file: open ../testdata/little-Test-AIP-Digitization/additional/missing.xml: no such file or directory", | ||
}, | ||
{ | ||
name: "Errors if metadata file is invalid", | ||
params: ais.MergeMDActivityParams{ | ||
METSRelPath: "", | ||
MetadataRelPath: "content/content/d_0000001/00000001_PREMIS.xml", | ||
WorkingDir: aipPath, | ||
}, | ||
wantErr: "activity error (type: merge-md-files, scheduledEventID: 0, startedEventID: 0, identity: ): get AIS filename: get accession number: can't find ablieferungsnummer in \"00000001_PREMIS.xml\"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := &temporalsdk_testsuite.WorkflowTestSuite{} | ||
env := ts.NewTestActivityEnvironment() | ||
env.RegisterActivityWithOptions( | ||
ais.NewMergeMDActivity().Execute, | ||
temporalsdk_activity.RegisterOptions{Name: ais.MergeMDActivityName}, | ||
) | ||
|
||
future, err := env.ExecuteActivity(ais.MergeMDActivityName, tt.params) | ||
if tt.wantErr != "" { | ||
if err == nil { | ||
t.Errorf("error is nil, expecting: %q", tt.wantErr) | ||
} else { | ||
assert.ErrorContains(t, err, tt.wantErr) | ||
} | ||
|
||
return | ||
} | ||
assert.NilError(t, err) | ||
|
||
var got ais.MergeMDActivityResult | ||
future.Get(&got) | ||
assert.DeepEqual(t, got, tt.want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters