Skip to content

Commit 958ef45

Browse files
committed
CROSSLINK-190 WIP
1 parent ced77cc commit 958ef45

File tree

9 files changed

+211
-66
lines changed

9 files changed

+211
-66
lines changed

broker/adapter/api_directory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (a *ApiDirectory) GetDirectory(symbols []string, durl string) ([]DirectoryE
7979
}
8080
}
8181
}
82+
// TODO: if NCIP entry does not have ISO18626 endpoint, we ignore NCIP here
8283
if apiUrl != "" && len(symbols) > 0 {
8384
vendor := GetVendorFromUrl(apiUrl)
8485
name, ok := d["name"].(string)

broker/app/app.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
"context"
55
_ "embed"
66
"fmt"
7-
prapi "github.com/indexdata/crosslink/broker/patron_request/api"
8-
pr_db "github.com/indexdata/crosslink/broker/patron_request/db"
9-
proapi "github.com/indexdata/crosslink/broker/patron_request/oapi"
10-
prservice "github.com/indexdata/crosslink/broker/patron_request/service"
117
"log/slog"
128
"math"
139
"net/http"
@@ -18,6 +14,11 @@ import (
1814
"syscall"
1915
"time"
2016

17+
prapi "github.com/indexdata/crosslink/broker/patron_request/api"
18+
pr_db "github.com/indexdata/crosslink/broker/patron_request/db"
19+
proapi "github.com/indexdata/crosslink/broker/patron_request/oapi"
20+
prservice "github.com/indexdata/crosslink/broker/patron_request/service"
21+
2122
"github.com/dustin/go-humanize"
2223
"github.com/indexdata/crosslink/broker/adapter"
2324
"github.com/indexdata/crosslink/broker/api"
@@ -35,6 +36,8 @@ import (
3536
"github.com/indexdata/crosslink/broker/ill_db"
3637
"github.com/indexdata/go-utils/utils"
3738
"github.com/jackc/pgx/v5/pgxpool"
39+
40+
"github.com/indexdata/crosslink/broker/ncipclient"
3841
)
3942

4043
var HTTP_PORT = utils.Must(utils.GetEnvInt("HTTP_PORT", 8081))
@@ -156,7 +159,8 @@ func Init(ctx context.Context) (Context, error) {
156159
iso18626Handler := handler.CreateIso18626Handler(eventBus, eventRepo, illRepo, dirAdapter)
157160
supplierLocator := service.CreateSupplierLocator(eventBus, illRepo, dirAdapter, holdingsAdapter)
158161
workflowManager := service.CreateWorkflowManager(eventBus, illRepo, service.WorkflowConfig{})
159-
prActionService := prservice.CreatePatronRequestActionService(prRepo, illRepo, eventBus, &iso18626Handler)
162+
ncipAdapter := prservice.CreateNcipAdapter(ncipclient.CreateNcipClient(http.DefaultClient), dirAdapter)
163+
prActionService := prservice.CreatePatronRequestActionService(prRepo, illRepo, eventBus, &iso18626Handler, ncipAdapter)
160164
prApiHandler := prapi.NewApiHandler(prRepo, eventBus)
161165

162166
AddDefaultHandlers(eventBus, iso18626Client, supplierLocator, workflowManager, iso18626Handler, prActionService, prApiHandler, prMessageHandler)

broker/ncipclient/ncipclient_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"os"
8+
"strconv"
89
"testing"
910

1011
"github.com/indexdata/go-utils/utils"
1112
"github.com/stretchr/testify/assert"
1213

14+
mockapp "github.com/indexdata/crosslink/illmock/app"
1315
"github.com/indexdata/crosslink/illmock/netutil"
1416
"github.com/indexdata/crosslink/ncip"
1517

16-
"github.com/indexdata/crosslink/broker/test/apputils"
1718
test "github.com/indexdata/crosslink/broker/test/utils"
1819
)
1920

2021
func TestMain(m *testing.M) {
2122
mockPort := utils.Must(test.GetFreePort())
2223

23-
apputils.StartMockApp(mockPort)
24+
// same as apputils.StartMockApp
25+
// but not used here as this results in cyclic dependencies
26+
test.Expect(os.Setenv("HTTP_PORT", strconv.Itoa(mockPort)), "failed to set mock server port")
27+
go func() {
28+
var mockApp mockapp.MockApp
29+
test.Expect(mockApp.Run(), "failed to start illmock server")
30+
}()
31+
test.WaitForServiceUp(mockPort)
2432

25-
m.Run()
33+
os.Exit(m.Run())
2634
}
2735

2836
func TestPrepareHeaderEmpty(t *testing.T) {

broker/patron_request/api/api-handler.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"net/http"
8+
"sync"
9+
710
"github.com/google/uuid"
811
"github.com/indexdata/crosslink/broker/common"
912
"github.com/indexdata/crosslink/broker/events"
@@ -12,8 +15,6 @@ import (
1215
prservice "github.com/indexdata/crosslink/broker/patron_request/service"
1316
"github.com/jackc/pgx/v5"
1417
"github.com/jackc/pgx/v5/pgtype"
15-
"net/http"
16-
"sync"
1718
)
1819

1920
var waitingReqs = map[string]RequestWait{}
@@ -47,7 +48,7 @@ func (a *PatronRequestApiHandler) GetPatronRequests(w http.ResponseWriter, r *ht
4748
writeJsonResponse(w, responseItems)
4849
}
4950

50-
func (a *PatronRequestApiHandler) PostPatronRequests(w http.ResponseWriter, r *http.Request) {
51+
func (a *PatronRequestApiHandler) PostPatronRequests(w http.ResponseWriter, r *http.Request, params proapi.PostPatronRequestsParams) {
5152
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
5253
Other: map[string]string{"method": "PostPatronRequests"},
5354
})
@@ -74,7 +75,7 @@ func (a *PatronRequestApiHandler) PostPatronRequests(w http.ResponseWriter, r *h
7475
_ = json.NewEncoder(w).Encode(toApiPatronRequest(pr))
7576
}
7677

77-
func (a *PatronRequestApiHandler) DeletePatronRequestsId(w http.ResponseWriter, r *http.Request, id string) {
78+
func (a *PatronRequestApiHandler) DeletePatronRequestsId(w http.ResponseWriter, r *http.Request, id string, params proapi.DeletePatronRequestsIdParams) {
7879
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
7980
Other: map[string]string{"method": "DeletePatronRequestsId", "id": id},
8081
})
@@ -97,7 +98,7 @@ func (a *PatronRequestApiHandler) DeletePatronRequestsId(w http.ResponseWriter,
9798
w.WriteHeader(http.StatusNoContent)
9899
}
99100

100-
func (a *PatronRequestApiHandler) GetPatronRequestsId(w http.ResponseWriter, r *http.Request, id string) {
101+
func (a *PatronRequestApiHandler) GetPatronRequestsId(w http.ResponseWriter, r *http.Request, id string, params proapi.GetPatronRequestsIdParams) {
101102
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
102103
Other: map[string]string{"method": "GetPatronRequestsId", "id": id},
103104
})
@@ -114,7 +115,7 @@ func (a *PatronRequestApiHandler) GetPatronRequestsId(w http.ResponseWriter, r *
114115
writeJsonResponse(w, toApiPatronRequest(pr))
115116
}
116117

117-
func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *http.Request, id string) {
118+
func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *http.Request, id string, params proapi.PutPatronRequestsIdParams) {
118119
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
119120
Other: map[string]string{"method": "GetPatronRequestsId", "id": id},
120121
})
@@ -145,7 +146,7 @@ func (a *PatronRequestApiHandler) PutPatronRequestsId(w http.ResponseWriter, r *
145146
writeJsonResponse(w, toApiPatronRequest(pr))
146147
}
147148

148-
func (a *PatronRequestApiHandler) GetPatronRequestsIdActions(w http.ResponseWriter, r *http.Request, id string) {
149+
func (a *PatronRequestApiHandler) GetPatronRequestsIdActions(w http.ResponseWriter, r *http.Request, id string, params proapi.GetPatronRequestsIdActionsParams) {
149150
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
150151
Other: map[string]string{"method": "GetPatronRequestsIdActions", "id": id},
151152
})
@@ -162,7 +163,7 @@ func (a *PatronRequestApiHandler) GetPatronRequestsIdActions(w http.ResponseWrit
162163
writeJsonResponse(w, prservice.GetBorrowerActionsByState(pr.State))
163164
}
164165

165-
func (a *PatronRequestApiHandler) PostPatronRequestsIdAction(w http.ResponseWriter, r *http.Request, id string) {
166+
func (a *PatronRequestApiHandler) PostPatronRequestsIdAction(w http.ResponseWriter, r *http.Request, id string, params proapi.PostPatronRequestsIdActionParams) {
166167
ctx := common.CreateExtCtxWithArgs(context.Background(), &common.LoggerArgs{
167168
Other: map[string]string{"method": "GetPatronRequestsIdActions", "id": id},
168169
})

broker/patron_request/api/api-handler_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
711
"github.com/indexdata/crosslink/broker/common"
812
"github.com/indexdata/crosslink/broker/events"
913
pr_db "github.com/indexdata/crosslink/broker/patron_request/db"
1014
proapi "github.com/indexdata/crosslink/broker/patron_request/oapi"
1115
"github.com/jackc/pgx/v5"
1216
"github.com/stretchr/testify/assert"
1317
"github.com/stretchr/testify/mock"
14-
"net/http"
15-
"net/http/httptest"
16-
"testing"
1718
)
1819

1920
var mockEventBus = new(MockEventBus)
@@ -51,7 +52,7 @@ func TestPostPatronRequests(t *testing.T) {
5152
assert.NoError(t, err, "failed to marshal patron request")
5253
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
5354
rr := httptest.NewRecorder()
54-
handler.PostPatronRequests(rr, req)
55+
handler.PostPatronRequests(rr, req, proapi.PostPatronRequestsParams{})
5556
if status := rr.Code; status != http.StatusInternalServerError {
5657
t.Errorf("handler returned wrong status code: got %v want %v",
5758
status, http.StatusInternalServerError)
@@ -62,7 +63,7 @@ func TestPostPatronRequestsInvalidJson(t *testing.T) {
6263
handler := NewApiHandler(new(PrRepoError), mockEventBus)
6364
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("a\": v\"")))
6465
rr := httptest.NewRecorder()
65-
handler.PostPatronRequests(rr, req)
66+
handler.PostPatronRequests(rr, req, proapi.PostPatronRequestsParams{})
6667
if status := rr.Code; status != http.StatusInternalServerError {
6768
t.Errorf("handler returned wrong status code: got %v want %v",
6869
status, http.StatusInternalServerError)
@@ -73,7 +74,7 @@ func TestDeletePatronRequestsIdNotFound(t *testing.T) {
7374
handler := NewApiHandler(new(PrRepoError), mockEventBus)
7475
req, _ := http.NewRequest("POST", "/", nil)
7576
rr := httptest.NewRecorder()
76-
handler.DeletePatronRequestsId(rr, req, "2")
77+
handler.DeletePatronRequestsId(rr, req, "2", proapi.DeletePatronRequestsIdParams{})
7778
if status := rr.Code; status != http.StatusNotFound {
7879
t.Errorf("handler returned wrong status code: got %v want %v",
7980
status, http.StatusNotFound)
@@ -84,7 +85,7 @@ func TestDeletePatronRequestsId(t *testing.T) {
8485
handler := NewApiHandler(new(PrRepoError), mockEventBus)
8586
req, _ := http.NewRequest("POST", "/", nil)
8687
rr := httptest.NewRecorder()
87-
handler.DeletePatronRequestsId(rr, req, "3")
88+
handler.DeletePatronRequestsId(rr, req, "3", proapi.DeletePatronRequestsIdParams{})
8889
if status := rr.Code; status != http.StatusInternalServerError {
8990
t.Errorf("handler returned wrong status code: got %v want %v",
9091
status, http.StatusInternalServerError)
@@ -95,7 +96,7 @@ func TestGetPatronRequestsIdNotFound(t *testing.T) {
9596
handler := NewApiHandler(new(PrRepoError), mockEventBus)
9697
req, _ := http.NewRequest("POST", "/", nil)
9798
rr := httptest.NewRecorder()
98-
handler.GetPatronRequestsId(rr, req, "2")
99+
handler.GetPatronRequestsId(rr, req, "2", proapi.GetPatronRequestsIdParams{})
99100
if status := rr.Code; status != http.StatusNotFound {
100101
t.Errorf("handler returned wrong status code: got %v want %v",
101102
status, http.StatusNotFound)
@@ -106,7 +107,7 @@ func TestGetPatronRequestsId(t *testing.T) {
106107
handler := NewApiHandler(new(PrRepoError), mockEventBus)
107108
req, _ := http.NewRequest("POST", "/", nil)
108109
rr := httptest.NewRecorder()
109-
handler.GetPatronRequestsId(rr, req, "1")
110+
handler.GetPatronRequestsId(rr, req, "1", proapi.GetPatronRequestsIdParams{})
110111
if status := rr.Code; status != http.StatusInternalServerError {
111112
t.Errorf("handler returned wrong status code: got %v want %v",
112113
status, http.StatusInternalServerError)
@@ -120,7 +121,7 @@ func TestPutPatronRequestsIdNotFound(t *testing.T) {
120121
assert.NoError(t, err, "failed to marshal patron request")
121122
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
122123
rr := httptest.NewRecorder()
123-
handler.PutPatronRequestsId(rr, req, "2")
124+
handler.PutPatronRequestsId(rr, req, "2", proapi.PutPatronRequestsIdParams{})
124125
if status := rr.Code; status != http.StatusNotFound {
125126
t.Errorf("handler returned wrong status code: got %v want %v",
126127
status, http.StatusNotFound)
@@ -134,7 +135,7 @@ func TestPutPatronRequestsId(t *testing.T) {
134135
assert.NoError(t, err, "failed to marshal patron request")
135136
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
136137
rr := httptest.NewRecorder()
137-
handler.PutPatronRequestsId(rr, req, "1")
138+
handler.PutPatronRequestsId(rr, req, "1", proapi.PutPatronRequestsIdParams{})
138139
if status := rr.Code; status != http.StatusInternalServerError {
139140
t.Errorf("handler returned wrong status code: got %v want %v",
140141
status, http.StatusInternalServerError)
@@ -148,7 +149,7 @@ func TestPutPatronRequestsIdSaveError(t *testing.T) {
148149
assert.NoError(t, err, "failed to marshal patron request")
149150
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
150151
rr := httptest.NewRecorder()
151-
handler.PutPatronRequestsId(rr, req, "3")
152+
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})
152153
if status := rr.Code; status != http.StatusInternalServerError {
153154
t.Errorf("handler returned wrong status code: got %v want %v",
154155
status, http.StatusInternalServerError)
@@ -159,7 +160,7 @@ func TestPutPatronRequestsIdInvalidJson(t *testing.T) {
159160
handler := NewApiHandler(new(PrRepoError), mockEventBus)
160161
req, _ := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("a\":v\"")))
161162
rr := httptest.NewRecorder()
162-
handler.PutPatronRequestsId(rr, req, "3")
163+
handler.PutPatronRequestsId(rr, req, "3", proapi.PutPatronRequestsIdParams{})
163164
if status := rr.Code; status != http.StatusInternalServerError {
164165
t.Errorf("handler returned wrong status code: got %v want %v",
165166
status, http.StatusInternalServerError)

broker/patron_request/oapi/open-api.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ info:
55
description: API for Patron Request CRUD and state actions
66

77
components:
8+
parameters:
9+
Tenant:
10+
name: X-Okapi-Tenant
11+
in: header
12+
description: Okapi Tenant
13+
required: false
14+
schema:
15+
type: string
16+
pattern: '^[_a-z][_a-z0-9]*$'
17+
818
schemas:
919
PatronRequest:
1020
type: object
@@ -133,6 +143,8 @@ paths:
133143
$ref: '#/components/schemas/Error'
134144
post:
135145
summary: Create a new patron request
146+
parameters:
147+
- $ref: '#/components/parameters/Tenant'
136148
requestBody:
137149
content:
138150
application/json:
@@ -168,6 +180,7 @@ paths:
168180
type: string
169181
required: true
170182
description: ID of the patron request to retrieve
183+
- $ref: '#/components/parameters/Tenant'
171184
responses:
172185
'200':
173186
description: Successful retrieval of the patron request
@@ -192,6 +205,7 @@ paths:
192205
type: string
193206
required: true
194207
description: ID of the patron request to update
208+
- $ref: '#/components/parameters/Tenant'
195209
requestBody:
196210
content:
197211
application/json:
@@ -225,6 +239,7 @@ paths:
225239
type: string
226240
required: true
227241
description: ID of the patron request to delete
242+
- $ref: '#/components/parameters/Tenant'
228243
responses:
229244
'204':
230245
description: Patron request deleted successfully (No Content)
@@ -247,6 +262,7 @@ paths:
247262
type: string
248263
required: true
249264
description: ID of the patron request
265+
- $ref: '#/components/parameters/Tenant'
250266
responses:
251267
'200':
252268
description: List of available actions
@@ -279,6 +295,7 @@ paths:
279295
type: string
280296
required: true
281297
description: ID of the patron request
298+
- $ref: '#/components/parameters/Tenant'
282299
requestBody:
283300
content:
284301
application/json:

0 commit comments

Comments
 (0)