Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions broker/oapi/pr-cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ output: patron_request/proapi/pr-openapi_gen.go
output-options:
include-tags:
- patron-requests-api
overlay:
path: oapi/pr-overlay.yaml
Comment thread
jakub-id marked this conversation as resolved.
generate:
models: true
std-http-server: true
15 changes: 15 additions & 0 deletions broker/oapi/pr-overlay.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
overlay: 1.0.0
info:
title: Patron request codegen overrides
version: 0.0.0
actions:
- target: $.components.schemas.PatronRequest.properties.illRequest
update:
x-go-type: iso18626.Request
x-go-type-import:
path: github.com/indexdata/crosslink/iso18626
- target: $.components.schemas.CreatePatronRequest.properties.illRequest
update:
x-go-type: iso18626.Request
x-go-type-import:
path: github.com/indexdata/crosslink/iso18626
21 changes: 7 additions & 14 deletions broker/patron_request/api/api-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -560,7 +561,7 @@ func toApiPatronRequest(request pr_db.PatronRequest, illRequest iso18626.Request
Patron: toString(request.Patron),
RequesterSymbol: toString(request.RequesterSymbol),
SupplierSymbol: toString(request.SupplierSymbol),
IllRequest: utils.Must(common.StructToMap(illRequest)),
IllRequest: illRequest,
RequesterRequestId: toString(request.RequesterReqID),
NeedsAttention: request.NeedsAttention,
LastAction: toString(request.LastAction),
Expand Down Expand Up @@ -605,7 +606,7 @@ func (a *PatronRequestApiHandler) parseAndValidateIllRequest(
}
requesterReqId = hrid
}
illRequest, err := parseAndValidateIllRequestPayload(
illRequest, err := prepareAndValidateIllRequest(
request.IllRequest,
reqSymbolType,
reqSymbolValue,
Expand Down Expand Up @@ -644,25 +645,17 @@ func parseAgencySymbol(symbol string) (string, string, error) {
return scheme, value, nil
}

func parseAndValidateIllRequestPayload(
rawIllRequest map[string]interface{},
func prepareAndValidateIllRequest(
rawIllRequest iso18626.Request,
reqSymbolType string,
reqSymbolValue string,
requesterReqId string,
creationTime time.Time,
) (iso18626.Request, error) {
var illRequest iso18626.Request
if rawIllRequest == nil {
if reflect.ValueOf(rawIllRequest).IsZero() {
return iso18626.Request{}, fmt.Errorf("%w: missing required illRequest payload", errInvalidPatronRequest)
Comment thread
jakub-id marked this conversation as resolved.
Outdated
}
illRequestBytes, err := json.Marshal(rawIllRequest)
if err != nil {
return iso18626.Request{}, fmt.Errorf("%w: illRequest: %w", errInvalidPatronRequest, err)
}
err = json.Unmarshal(illRequestBytes, &illRequest)
if err != nil {
return iso18626.Request{}, fmt.Errorf("%w: illRequest: %w", errInvalidPatronRequest, err)
}
illRequest := rawIllRequest
suppSymbolType, suppSymbolValue, err := parseAgencySymbol(brokerSymbol)
if err != nil {
return iso18626.Request{}, fmt.Errorf("invalid BROKER_SYMBOL %q: %w", brokerSymbol, err)
Expand Down
57 changes: 20 additions & 37 deletions broker/patron_request/api/api-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/indexdata/crosslink/broker/patron_request/proapi"
prservice "github.com/indexdata/crosslink/broker/patron_request/service"
"github.com/indexdata/crosslink/broker/test/mocks"
"github.com/indexdata/crosslink/iso18626"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/stretchr/testify/assert"
Expand All @@ -32,6 +33,17 @@ var lendingString = string(prservice.SideLending)
var proapiBorrowingSide = proapi.Side(prservice.SideBorrowing)
var proapiLendingSide = proapi.Side(prservice.SideLending)

func validIllRequest() iso18626.Request {
return iso18626.Request{
BibliographicInfo: iso18626.BibliographicInfo{
Title: "Test title",
},
ServiceInfo: &iso18626.ServiceInfo{
ServiceType: iso18626.TypeServiceTypeCopy,
},
}
}

func TestGetId(t *testing.T) {
assert.True(t, getId("") != "")
assert.Equal(t, "id1", getId("id1"))
Expand Down Expand Up @@ -117,14 +129,7 @@ func TestPostPatronRequests(t *testing.T) {
toCreate := proapi.CreatePatronRequest{
Id: &id,
RequesterSymbol: &symbol,
IllRequest: map[string]interface{}{
"bibliographicInfo": map[string]interface{}{
"title": "test",
},
"serviceInfo": map[string]interface{}{
"serviceType": "Copy",
},
},
IllRequest: validIllRequest(),
}
jsonBytes, err := json.Marshal(toCreate)
assert.NoError(t, err, "failed to marshal patron request")
Expand Down Expand Up @@ -163,15 +168,11 @@ func TestPostPatronRequestsInvalidJson(t *testing.T) {

func TestPostPatronRequestsInvalidIllRequestShape(t *testing.T) {
handler := NewPrApiHandler(new(PrRepoError), mockEventBus, mockEventRepo, common.NewTenant(""), 10)
toCreate := proapi.CreatePatronRequest{
Id: ptr("1"),
RequesterSymbol: &symbol,
IllRequest: map[string]interface{}{
"header": "invalid",
},
}
jsonBytes, err := json.Marshal(toCreate)
assert.NoError(t, err, "failed to marshal patron request")
jsonBytes := []byte(`{
"id":"1",
"requesterSymbol":"` + symbol + `",
"illRequest":{"header":"invalid"}
}`)
req, err := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes))
assert.NoError(t, err, "failed to create request")
rr := httptest.NewRecorder()
Expand Down Expand Up @@ -402,14 +403,7 @@ func TestParseAndValidateIllRequestAndBuildDbPatronRequest(t *testing.T) {
reqWithID := &proapi.CreatePatronRequest{
Id: &id,
RequesterSymbol: &symbol,
IllRequest: map[string]interface{}{
"serviceInfo": map[string]interface{}{
"serviceType": "Copy",
},
"bibliographicInfo": map[string]interface{}{
"title": "Test title",
},
},
IllRequest: validIllRequest(),
}

illRequest, requesterReqID, err := handler.parseAndValidateIllRequest(ctx, reqWithID, creationTime)
Expand Down Expand Up @@ -449,14 +443,7 @@ func TestParseAndValidateIllRequestInvalidBrokerSymbol(t *testing.T) {

_, _, err := handler.parseAndValidateIllRequest(ctx, &proapi.CreatePatronRequest{
RequesterSymbol: &symbol,
IllRequest: map[string]interface{}{
"serviceInfo": map[string]interface{}{
"serviceType": "Copy",
},
"bibliographicInfo": map[string]interface{}{
"title": "Test title",
},
},
IllRequest: validIllRequest(),
}, time.Now())
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid BROKER_SYMBOL")
Expand Down Expand Up @@ -525,7 +512,3 @@ type MockEventBus struct {
mock.Mock
events.EventBus
}

func ptr(value string) *string {
return &value
}
26 changes: 9 additions & 17 deletions broker/test/patron_request/api/api-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCrud(t *testing.T) {
Id: &id,
RequesterSymbol: &requesterSymbol,
Patron: &patron,
IllRequest: utils.Must(common.StructToMap(request)),
IllRequest: request,
}
newPrBytes, err := json.Marshal(newPr)
assert.NoError(t, err, "failed to marshal patron request")
Expand Down Expand Up @@ -287,17 +287,9 @@ func TestCrud(t *testing.T) {
//httpRequest(t, "DELETE", thisPrPath, []byte{}, 404)
}

func assertPatronRequestIllRequest(t *testing.T, payload map[string]interface{}, assertFn func(iso18626.Request)) {
func assertPatronRequestIllRequest(t *testing.T, payload iso18626.Request, assertFn func(iso18626.Request)) {
t.Helper()

data, err := json.Marshal(payload)
assert.NoError(t, err)

var request iso18626.Request
err = json.Unmarshal(data, &request)
assert.NoError(t, err)

assertFn(request)
assertFn(payload)
}

func TestActionsToCompleteState(t *testing.T) {
Expand Down Expand Up @@ -328,7 +320,7 @@ func TestActionsToCompleteState(t *testing.T) {
newPr := proapi.CreatePatronRequest{
RequesterSymbol: &requesterSymbol,
Patron: &patron,
IllRequest: utils.Must(common.StructToMap(request)),
IllRequest: request,
}
newPrBytes, err := json.Marshal(newPr)
assert.NoError(t, err, "failed to marshal patron request")
Expand Down Expand Up @@ -537,12 +529,12 @@ func TestPostPatronRequestRejectsInvalidIllRequest(t *testing.T) {

newPr := proapi.CreatePatronRequest{
RequesterSymbol: &requesterSymbol,
IllRequest: map[string]interface{}{
"bibliographicInfo": map[string]interface{}{
"title": "Invalid request",
IllRequest: iso18626.Request{
BibliographicInfo: iso18626.BibliographicInfo{
Title: "Invalid request",
},
"serviceInfo": map[string]interface{}{
"serviceType": "Broken",
ServiceInfo: &iso18626.ServiceInfo{
ServiceType: iso18626.TypeServiceType("Broken"),
},
},
}
Expand Down
Loading