Skip to content

Commit afea301

Browse files
committed
nha: trim down avlxml only in avl sips
1 parent cbfb731 commit afea301

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

internal/nha/activities/hari.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"encoding/xml"
88
"fmt"
9+
"io"
910
"io/ioutil"
1011
"net"
1112
"net/http"
@@ -71,9 +72,22 @@ func (a UpdateHARIActivity) Execute(ctx context.Context, params *UpdateHARIActiv
7172
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: not found"))
7273
}
7374

74-
blob, err := a.slimDown(path)
75+
f, err := os.Open(path)
7576
if err != nil {
76-
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: %v", err))
77+
return fmt.Errorf("error opening AVLXML file: %v", err)
78+
}
79+
defer f.Close()
80+
81+
var blob []byte
82+
{
83+
if params.NameInfo.Type == nha.TransferTypeAVLXML {
84+
blob, err = a.slimDown(f)
85+
} else {
86+
blob, err = ioutil.ReadAll(f)
87+
}
88+
if err != nil {
89+
return wferrors.NonRetryableError(fmt.Errorf("error reading AVLXML file: %v", err))
90+
}
7791
}
7892

7993
var parentID string
@@ -137,12 +151,7 @@ func (a UpdateHARIActivity) avlxml(path string, kind nha.TransferType) string {
137151
// first go focusing on meeting functional requirements. For large documents,
138152
// we could do much better by implementing a stream decoder that uses the
139153
// token iterator to avoid memory allocation when unnecessary.
140-
func (a UpdateHARIActivity) slimDown(path string) ([]byte, error) {
141-
f, err := os.Open(path)
142-
if err != nil {
143-
return nil, fmt.Errorf("error opening XML: %v", err)
144-
}
145-
154+
func (a UpdateHARIActivity) slimDown(f io.Reader) ([]byte, error) {
146155
doc := avlxml{}
147156
dec := xml.NewDecoder(f)
148157
if err := dec.Decode(&doc); err != nil {

internal/nha/activities/hari_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestHARIActivity(t *testing.T) {
3333
// Tweak the client so we don't have to wait for too long.
3434
hariClient.Timeout = time.Second * 1
3535

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

@@ -71,7 +72,7 @@ func TestHARIActivity(t *testing.T) {
7172
dirOpts: []fs.PathOp{
7273
fs.WithDir("DPJ/journal"),
7374
// XML generated is a trimmed-down version, e.g. `pasientjournal` not included.
74-
fs.WithFile("DPJ/journal/avlxml.xml", "<avlxml><pasientjournal>12345</pasientjournal></avlxml>"),
75+
fs.WithFile("DPJ/journal/avlxml.xml", "<avlxml/>"),
7576
fs.WithDir("metadata"),
7677
fs.WithFile("metadata/identifiers.json", `[{
7778
"file": "objects/DPJ/aFoobar.jpg",
@@ -96,7 +97,7 @@ func TestHARIActivity(t *testing.T) {
9697
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
9798
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
9899
Parent: "12345",
99-
XML: emptyavlxml,
100+
XML: []byte("<avlxml/>"),
100101
},
101102
},
102103
"Receipt is delivered successfully (EPJ)": {
@@ -128,7 +129,7 @@ func TestHARIActivity(t *testing.T) {
128129
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
129130
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
130131
Parent: "12345",
131-
XML: emptyavlxml,
132+
XML: []byte("<avlxml/>"),
132133
},
133134
},
134135
"Receipt is delivered successfully (AVLXML)": {
@@ -144,7 +145,11 @@ func TestHARIActivity(t *testing.T) {
144145
hariConfig: map[string]interface{}{},
145146
dirOpts: []fs.PathOp{
146147
fs.WithDir("AVLXML/objekter"),
147-
fs.WithFile("AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml", "<avlxml/>"),
148+
fs.WithFile(
149+
// Including pasientjournal since we want to test that is removed.
150+
"AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml",
151+
"<avlxml><pasientjournal>12345</pasientjournal></avlxml>",
152+
),
148153
},
149154
wantReceipt: &avlRequest{
150155
Message: "AVLXML was processed by Archivematica pipeline zr-fig-pipe-001",
@@ -167,7 +172,11 @@ func TestHARIActivity(t *testing.T) {
167172
hariConfig: map[string]interface{}{},
168173
dirOpts: []fs.PathOp{
169174
fs.WithDir("AVLXML/objekter"),
170-
fs.WithFile("AVLXML/objekter/avlxml.xml", "<avlxml/>"),
175+
fs.WithFile(
176+
// Including pasientjournal since we want to test that is removed.
177+
"AVLXML/objekter/avlxml-2.16.578.1.39.100.11.9876.4-20191104.xml",
178+
"<avlxml><pasientjournal>12345</pasientjournal></avlxml>",
179+
),
171180
},
172181
wantReceipt: &avlRequest{
173182
Message: "AVLXML was processed by Archivematica pipeline zr-fig-pipe-001",
@@ -206,7 +215,7 @@ func TestHARIActivity(t *testing.T) {
206215
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
207216
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
208217
Parent: "12345",
209-
XML: emptyavlxml,
218+
XML: []byte("<avlxml/>"),
210219
},
211220
},
212221
"Capital letter in journal directory is reached": {
@@ -238,7 +247,7 @@ func TestHARIActivity(t *testing.T) {
238247
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
239248
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
240249
Parent: "12345",
241-
XML: emptyavlxml,
250+
XML: []byte("<avlxml/>"),
242251
},
243252
},
244253
"Lowercase kind attribute is handled successfully": {
@@ -270,7 +279,7 @@ func TestHARIActivity(t *testing.T) {
270279
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
271280
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
272281
Parent: "12345",
273-
XML: emptyavlxml,
282+
XML: []byte("<avlxml/>"),
274283
},
275284
},
276285
"Mock option is honoured": {
@@ -374,7 +383,7 @@ func TestHARIActivity(t *testing.T) {
374383
Timestamp: avlRequestTime{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)},
375384
AIPID: "1db240cc-3cea-4e55-903c-6280562e1866",
376385
Parent: "12345",
377-
XML: emptyavlxml,
386+
XML: []byte("<avlxml/>"),
378387
},
379388
wantErr: testutil.ActivityError{
380389
Message: "error sending request: (unexpected response status: 500 Internal Server Error) - Backend server not available, try again later.\n",

0 commit comments

Comments
 (0)