-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
178 lines (165 loc) · 4.59 KB
/
main.go
File metadata and controls
178 lines (165 loc) · 4.59 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/go-stomp/stomp"
)
const TEST_RESOURCE = "http://localhost:8080/fcrepo/rest/test"
const TEST_TRIPLES = "./test-data/simple-container.nt"
// data structure for STOMP messages
type stompMessage struct {
MsgId string `json:"id"`
Type []string `json:"type"`
}
func createConn() *stomp.Conn {
conn, err := stomp.Dial("tcp", "localhost:61613", stomp.ConnOpt.AcceptVersion(stomp.V12),
stomp.ConnOpt.HeartBeat(10*time.Second, 5*time.Second))
if err != nil {
log.Fatalf("unable to connect to Fedora queue: %w", err)
}
log.Println("Connected to Fedora queue.")
return conn
}
func loadTestTriples(path string) []byte {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("Test file not found at %s", path)
}
return data
}
func createSub(conn *stomp.Conn, done chan struct{}) chan *stomp.Message {
messages := make(chan *stomp.Message)
sub, err := conn.Subscribe("/queue/fedora", stomp.AckClient)
if err != nil {
log.Fatalf("Unable to subscibe to queue: %w", err)
}
log.Println("Subscribed to queue.")
go func() {
defer sub.Unsubscribe()
defer close(messages)
for {
select {
case msg := <-sub.C:
if (msg != nil) && (msg.Body != nil) {
messages <- msg
if msg.ShouldAck() {
err := conn.Ack(msg)
if err != nil {
log.Printf("Ack error, %+v\n", msg.Err.Error())
}
}
}
case <-done:
return
}
}
}()
return messages
}
func makeRequest(client *http.Client, url, _type string, data []byte) *http.Response {
auth := "fedoraAdmin:fedoraAdmin"
auth = base64.StdEncoding.EncodeToString([]byte(auth))
headers := map[string]string{"Authorization": "Basic " + auth, "Overwrite-Tombstone": "true"}
if _type == "PUT" {
headers["Content-Type"] = "text/turtle"
}
var req *http.Request
if data != nil {
req, _ = http.NewRequest(_type, url, bytes.NewBuffer(data))
} else {
req, _ = http.NewRequest(_type, url, nil)
}
for k, v := range headers {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Could not do %s request on Fedora API, %w", _type, err)
}
return resp
}
func deleteAndPurge(client *http.Client, url string) {
log.Println("Deleting resource %s", url)
resp := makeRequest(client, url, "DELETE", nil)
if resp.StatusCode > 299 {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
log.Fatalf("Unable to delete test resource %s: %s", url, string(body))
}
// purge resource
resp = makeRequest(client, url, "GET", nil)
if resp.StatusCode == 410 {
log.Println("Purging tombstone.")
links := resp.Header["Link"]
var tombstoneURL string
for _, l := range links {
if strings.Contains(l, "hasTombstone") {
linkSlice := strings.Split(l, ";")
tombstoneURL = strings.Trim(linkSlice[0], "<>")
break
}
}
resp = makeRequest(client, tombstoneURL, "DELETE", nil)
if resp.StatusCode != 204 {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
log.Fatalf("Unable to delete tombstone %s: %s", url, string(body))
}
} else {
log.Fatal(resp.Status)
}
}
func createTestResource(client *http.Client) {
// GET the resource: if it exists, we want to delete it first.
resp := makeRequest(client, TEST_RESOURCE, "GET", nil)
if resp.StatusCode < 300 { // exists -- delete first
deleteAndPurge(client, TEST_RESOURCE)
}
testData := loadTestTriples(TEST_TRIPLES)
resp = makeRequest(client, TEST_RESOURCE, "PUT", testData)
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode < 300 {
// create successful
log.Println("Created a resource!")
} else {
log.Fatalf("Unable to create the test resource %s: %s", TEST_RESOURCE, string(body))
}
}
func main() {
client := http.Client{Timeout: time.Duration(5) * time.Second}
conn := createConn()
done := make(chan struct{})
messages := createSub(conn, done)
// loop until we successfully create it or fail entirely
createTestResource(&client)
parts := strings.Split(TEST_RESOURCE, "/")
resourceID := parts[len(parts)-1]
// expect one message to show the resource has been created
// should be the last message
loop:
for {
select {
case msg := <-messages:
if msg.Header.Get("org.fcrepo.jms.identifier") == "/"+resourceID {
var parsedMessage stompMessage
json.Unmarshal(msg.Body, &parsedMessage)
if parsedMessage.Type[0] == "Create" {
log.Println("Message received: object created.")
break loop
}
}
case <-time.After(10 * time.Second):
log.Fatalf("Test failed. Message in queue not found for created object.")
}
}
close(done)
deleteAndPurge(&client, TEST_RESOURCE)
}