Skip to content

Commit

Permalink
nha: trim down avlxml only in avl sips
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Jun 8, 2020
1 parent cbfb731 commit afea301
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
25 changes: 17 additions & 8 deletions internal/nha/activities/hari.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -71,9 +72,22 @@ func (a UpdateHARIActivity) Execute(ctx context.Context, params *UpdateHARIActiv
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: not found"))
}

blob, err := a.slimDown(path)
f, err := os.Open(path)
if err != nil {
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: %v", err))
return fmt.Errorf("error opening AVLXML file: %v", err)
}
defer f.Close()

var blob []byte
{
if params.NameInfo.Type == nha.TransferTypeAVLXML {
blob, err = a.slimDown(f)
} else {
blob, err = ioutil.ReadAll(f)
}
if err != nil {
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: %v", err))
}
}

var parentID string
Expand Down Expand Up @@ -137,12 +151,7 @@ func (a UpdateHARIActivity) avlxml(path string, kind nha.TransferType) string {
// first go focusing on meeting functional requirements. For large documents,
// we could do much better by implementing a stream decoder that uses the
// token iterator to avoid memory allocation when unnecessary.
func (a UpdateHARIActivity) slimDown(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening XML: %v", err)
}

func (a UpdateHARIActivity) slimDown(f io.Reader) ([]byte, error) {
doc := avlxml{}
dec := xml.NewDecoder(f)
if err := dec.Decode(&doc); err != nil {
Expand Down
27 changes: 18 additions & 9 deletions internal/nha/activities/hari_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestHARIActivity(t *testing.T) {
// Tweak the client so we don't have to wait for too long.
hariClient.Timeout = time.Second * 1

// When slimDown is used.
var emptyavlxml = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<avlxml xmlns:xsi="" xsi:schemaLocation=""><avlxmlversjon></avlxmlversjon><avleveringsidentifikator></avleveringsidentifikator><avleveringsbeskrivelse></avleveringsbeskrivelse><arkivskaper></arkivskaper><avtale></avtale></avlxml>`)

Expand Down Expand Up @@ -71,7 +72,7 @@ func TestHARIActivity(t *testing.T) {
dirOpts: []fs.PathOp{
fs.WithDir("DPJ/journal"),
// XML generated is a trimmed-down version, e.g. `pasientjournal` not included.
fs.WithFile("DPJ/journal/avlxml.xml", "<avlxml><pasientjournal>12345</pasientjournal></avlxml>"),
fs.WithFile("DPJ/journal/avlxml.xml", "<avlxml/>"),
fs.WithDir("metadata"),
fs.WithFile("metadata/identifiers.json", `[{
"file": "objects/DPJ/aFoobar.jpg",
Expand All @@ -96,7 +97,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
},
"Receipt is delivered successfully (EPJ)": {
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
},
"Receipt is delivered successfully (AVLXML)": {
Expand All @@ -144,7 +145,11 @@ func TestHARIActivity(t *testing.T) {
hariConfig: map[string]interface{}{},
dirOpts: []fs.PathOp{
fs.WithDir("AVLXML/objekter"),
fs.WithFile("AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml", "<avlxml/>"),
fs.WithFile(
// Including pasientjournal since we want to test that is removed.
"AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml",
"<avlxml><pasientjournal>12345</pasientjournal></avlxml>",
),
},
wantReceipt: &avlRequest{
Message: "AVLXML was processed by Archivematica pipeline zr-fig-pipe-001",
Expand All @@ -167,7 +172,11 @@ func TestHARIActivity(t *testing.T) {
hariConfig: map[string]interface{}{},
dirOpts: []fs.PathOp{
fs.WithDir("AVLXML/objekter"),
fs.WithFile("AVLXML/objekter/avlxml.xml", "<avlxml/>"),
fs.WithFile(
// Including pasientjournal since we want to test that is removed.
"AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml",
"<avlxml><pasientjournal>12345</pasientjournal></avlxml>",
),
},
wantReceipt: &avlRequest{
Message: "AVLXML was processed by Archivematica pipeline zr-fig-pipe-001",
Expand Down Expand Up @@ -206,7 +215,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
},
"Capital letter in journal directory is reached": {
Expand Down Expand Up @@ -238,7 +247,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
},
"Lowercase kind attribute is handled successfully": {
Expand Down Expand Up @@ -270,7 +279,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
},
"Mock option is honoured": {
Expand Down Expand Up @@ -374,7 +383,7 @@ func TestHARIActivity(t *testing.T) {
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
Parent: "12345",
XML: emptyavlxml,
XML: []byte("<avlxml/>"),
},
wantErr: testutil.ActivityError{
Message: "error sending request: (unexpected response status: 500 Internal Server Error) - Backend server not available, try again later.\n",
Expand Down

0 comments on commit afea301

Please sign in to comment.