-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfedora_test.go
More file actions
115 lines (104 loc) · 3.25 KB
/
fedora_test.go
File metadata and controls
115 lines (104 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"os"
"strings"
"sync"
"testing"
"github.com/go-stomp/stomp"
"github.com/go-stomp/stomp/frame"
)
func check(t *testing.T, err error, file string) {
if err != nil {
t.Fatalf("Couldn't open JSON file %s for testing.", file)
}
}
func extractMessages(messageJSON []byte, t *testing.T) []*stomp.Message {
var messageSlice []map[string]interface{}
if err := json.Unmarshal(messageJSON, &messageSlice); err != nil {
t.Fatal(err)
}
messages := make([]*stomp.Message, 0)
for _, m := range messageSlice {
message := &stomp.Message{}
h := m["headers"].(map[string]interface{})
objId := h["org.fcrepo.jms.identifier"].(string)
headers := frame.NewHeader("org.fcrepo.jms.identifier", objId)
bodyBytes := []byte(m["body"].(string))
message.Body = bodyBytes
message.Header = headers
messages = append(messages, message)
}
return messages
}
/* We mock a success handler and a handler that fails on the authentication step to use with testing the ASpace API pipeline. */
func mockFApiOkay(w http.ResponseWriter, req *http.Request) {
// Each condition represents a separate path on the API
w.Header().Set("Content-Type", "text/turtle")
if strings.Contains(req.URL.Path, "/development/70/cc/66/5b/70cc665b-d461-4b96-a3db-019db1b3523f") {
w.Write([]byte("Success"))
} else {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
}
func TestFetchFedoraObjects(t *testing.T) {
tests200 := map[string]string{"/development/70/cc/66/5b/70cc665b-d461-4b96-a3db-019db1b3523f": "Success"}
testsErr := map[string]string{"/development/70/cc/66/5b/70cc665b-d461-4b96-a3db-019db1b3523g": "Not found"}
var wg sync.WaitGroup
// Set up log redirection
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput((os.Stderr))
}()
messageJSON, err := os.ReadFile(testDataPath + "stomp_messages.json")
check(t, err, "stomp_messages.json")
msgChan := make(chan *stomp.Message)
// send messages down channel -- emulating STOMP consumer
go func() {
for _, msg := range extractMessages(messageJSON, t) {
msgChan <- msg
}
close(msgChan)
}()
wg.Add(1)
client, srv := createClientServer("8080", &wg)
http.HandleFunc("/", mockFApiOkay)
// channel to receive errors
errorChan := make(chan error)
defer close(errorChan)
// cancellation channel
done := make(chan struct{})
// launch error handler
wg.Add(1)
logErrors(errorChan, done, &wg)
// kick of process to collect Fedora objects
fedoraObjMap := fetchFedoraObjects(client, msgChan, errorChan)
// create merged results channel
mergedChan := merge(done, fedoraObjMap)
// consume results
// Check for expected successes
for fedoraResult := range mergedChan {
v, ok := tests200[fedoraResult.uri]
if !ok {
t.Errorf("URI %s should not have returned 200", fedoraResult.uri)
} else if v != string(fedoraResult.data) {
t.Errorf("URI %s expected to return %s, got %s instead", fedoraResult.uri, v, string(fedoraResult.data))
}
}
// Shut down server and wait for cleanup
close(done)
srv.Shutdown(context.TODO())
wg.Wait()
// Check for expected errors
for k, _ := range testsErr {
errs := buf.String()
if !strings.Contains(errs, k) {
t.Errorf("URI %s expected error but error not logged.", k)
}
}
}