Skip to content

Commit 70628ff

Browse files
CROSSLINK-181 Fix PR comments
1 parent 2afa6fb commit 70628ff

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

broker/client/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (c *Iso18626Client) checkConfirmationError(ctx common.ExtendedContext, resp
440440
return status
441441
}
442442

443-
func (c *Iso18626Client) SendIllMessage(ctx common.ExtendedContext, peer *ill_db.Peer, msg *iso18626.ISO18626Message) (*iso18626.ISO18626Message, error) {
443+
func (c *Iso18626Client) HandleIllMessage(ctx common.ExtendedContext, peer *ill_db.Peer, msg *iso18626.ISO18626Message) (*iso18626.ISO18626Message, error) {
444444
if strings.Contains(peer.Name, "local") { // TODO Implement real check of local peer
445445
return c.prMessageHandler.HandleMessage(ctx, msg)
446446
} else {
@@ -705,7 +705,7 @@ func (c *Iso18626Client) sendAndUpdateStatus(ctx common.ExtendedContext, trCtx t
705705
resData.CustomData = map[string]any{common.DO_NOT_SEND: true}
706706
resData.OutgoingMessage = nil
707707
} else {
708-
response, err := c.SendIllMessage(ctx, trCtx.requester, message)
708+
response, err := c.HandleIllMessage(ctx, trCtx.requester, message)
709709
if response != nil {
710710
resData.IncomingMessage = response
711711
}
@@ -800,7 +800,7 @@ func (c *Iso18626Client) sendAndUpdateSupplier(ctx common.ExtendedContext, trCtx
800800
resData := events.EventResult{}
801801
resData.OutgoingMessage = message
802802
if !isDoNotSend(trCtx.event) {
803-
response, err := c.SendIllMessage(ctx, trCtx.selectedPeer, message)
803+
response, err := c.HandleIllMessage(ctx, trCtx.selectedPeer, message)
804804
if response != nil {
805805
resData.IncomingMessage = response
806806
}

broker/client/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ func TestUpdateSupplierNote(t *testing.T) {
850850
assert.Equal(t, "Supplier: SUP1, Original note 2", sam.MessageInfo.Note)
851851
}
852852

853-
func TestSendIllMessage(t *testing.T) {
853+
func TestHandleIllMessage(t *testing.T) {
854854
mockPrMessageHandler := prservice.CreatePatronRequestMessageHandler(new(MockPrRepo), new(events.PgEventRepo), new(ill_db.PgIllRepo), new(events.PostgresEventBus))
855855
appCtx := common.CreateExtCtxWithArgs(context.Background(), nil)
856856

@@ -864,10 +864,10 @@ func TestSendIllMessage(t *testing.T) {
864864
}
865865

866866
// To local peer
867-
_, err := client.SendIllMessage(appCtx, &ill_db.Peer{Name: "local peer"}, &sam)
867+
_, err := client.HandleIllMessage(appCtx, &ill_db.Peer{Name: "local peer"}, &sam)
868868
assert.Equal(t, "searching pr with id=req-1", err.Error())
869869

870-
_, err = client.SendIllMessage(appCtx, &ill_db.Peer{Name: "random peer"}, &sam)
870+
_, err = client.HandleIllMessage(appCtx, &ill_db.Peer{Name: "random peer"}, &sam)
871871
assert.Equal(t, "Post \"\": unsupported protocol scheme \"\"", err.Error())
872872
}
873873

broker/test/patron_request/api/api-handler_test.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"context"
66
"encoding/json"
77
"github.com/google/uuid"
8+
"github.com/indexdata/crosslink/broker/adapter"
89
"github.com/indexdata/crosslink/broker/app"
10+
"github.com/indexdata/crosslink/broker/ill_db"
911
proapi "github.com/indexdata/crosslink/broker/patron_request/oapi"
1012
prservice "github.com/indexdata/crosslink/broker/patron_request/service"
1113
apptest "github.com/indexdata/crosslink/broker/test/apputils"
1214
test "github.com/indexdata/crosslink/broker/test/utils"
15+
mockapp "github.com/indexdata/crosslink/illmock/app"
1316
"github.com/indexdata/go-utils/utils"
1417
"github.com/stretchr/testify/assert"
1518
"github.com/testcontainers/testcontainers-go"
@@ -24,6 +27,7 @@ import (
2427
)
2528

2629
var basePath = "/patron_requests"
30+
var illRepo ill_db.IllRepo
2731

2832
func TestMain(m *testing.M) {
2933
app.TENANT_TO_SYMBOL = "ISIL:DK-{tenant}"
@@ -45,9 +49,20 @@ func TestMain(m *testing.M) {
4549
app.ConnectionString = connStr
4650
app.MigrationsFolder = "file://../../../migrations"
4751
app.HTTP_PORT = utils.Must(test.GetFreePort())
52+
mockPort := strconv.Itoa(utils.Must(test.GetFreePort()))
53+
localAddress := "http://localhost:" + strconv.Itoa(app.HTTP_PORT) + "/iso18626"
54+
test.Expect(os.Setenv("HTTP_PORT", mockPort), "failed to set mock client port")
55+
test.Expect(os.Setenv("PEER_URL", localAddress), "failed to set peer URL")
56+
57+
adapter.MOCK_CLIENT_URL = "http://localhost:" + mockPort + "/iso18626"
58+
59+
go func() {
60+
var mockApp mockapp.MockApp
61+
test.Expect(mockApp.Run(), "failed to start illmock client")
62+
}()
4863

4964
ctx, cancel := context.WithCancel(context.Background())
50-
_, _, _ = apptest.StartApp(ctx)
65+
_, illRepo, _ = apptest.StartApp(ctx)
5166
test.WaitForServiceUp(app.HTTP_PORT)
5267

5368
defer cancel()
@@ -58,16 +73,16 @@ func TestMain(m *testing.M) {
5873
}
5974

6075
func TestCrud(t *testing.T) {
76+
reqPeer := apptest.CreatePeer(t, illRepo, "localISIL:REQ"+uuid.NewString(), adapter.MOCK_CLIENT_URL)
77+
supPeer := apptest.CreatePeer(t, illRepo, "ISIL:SUP1", adapter.MOCK_CLIENT_URL)
6178
// POST
62-
landingId := "l1"
63-
borrowingId := "b1"
6479
requester := "r1"
6580
illMessage := "{\"request\": {}}"
6681
newPr := proapi.CreatePatronRequest{
6782
ID: uuid.NewString(),
6883
Timestamp: time.Now(),
69-
LendingPeerId: &landingId,
70-
BorrowingPeerId: &borrowingId,
84+
LendingPeerId: &supPeer.ID,
85+
BorrowingPeerId: &reqPeer.ID,
7186
Requester: &requester,
7287
IllRequest: &illMessage,
7388
}
@@ -106,8 +121,8 @@ func TestCrud(t *testing.T) {
106121
assert.Equal(t, newPr.ID, foundPr.ID)
107122

108123
// PUT update
109-
landingId = "l2"
110-
borrowingId = "b2"
124+
landingId := "l2"
125+
borrowingId := "b2"
111126
requester = "r2"
112127
updatedPr := proapi.PatronRequest{
113128
ID: newPr.ID,
@@ -128,8 +143,8 @@ func TestCrud(t *testing.T) {
128143
assert.True(t, foundPr.State != "ACCEPTED")
129144
assert.Equal(t, prservice.SideBorrowing, foundPr.Side)
130145
assert.Equal(t, newPr.Timestamp.YearDay(), foundPr.Timestamp.YearDay())
131-
assert.Equal(t, "l1", *foundPr.LendingPeerId)
132-
assert.Equal(t, "b1", *foundPr.BorrowingPeerId)
146+
assert.Equal(t, supPeer.ID, *foundPr.LendingPeerId)
147+
assert.Equal(t, reqPeer.ID, *foundPr.BorrowingPeerId)
133148
assert.Equal(t, *updatedPr.Requester, *foundPr.Requester) // Only requester can be updated now
134149
assert.Equal(t, *newPr.IllRequest, *foundPr.IllRequest)
135150

@@ -144,7 +159,22 @@ func TestCrud(t *testing.T) {
144159
actionBytes, err := json.Marshal(action)
145160
assert.NoError(t, err, "failed to marshal patron request action")
146161
respBytes = httpRequest(t, "POST", thisPrPath+"/action", actionBytes, 200)
147-
assert.Equal(t, "{\"actionResult\":\"ERROR\"}\n", string(respBytes))
162+
assert.Equal(t, "{\"actionResult\":\"SUCCESS\"}\n", string(respBytes))
163+
164+
// Wait till requester response processed
165+
test.WaitForPredicateToBeTrue(func() bool {
166+
respBytes = httpRequest(t, "GET", thisPrPath+"/actions", []byte{}, 200)
167+
return string(respBytes) == "[\"receive\"]\n"
168+
})
169+
170+
// POST blocking action
171+
action = proapi.ExecuteAction{
172+
Action: "receive",
173+
}
174+
actionBytes, err = json.Marshal(action)
175+
assert.NoError(t, err, "failed to marshal patron request action")
176+
respBytes = httpRequest(t, "POST", thisPrPath+"/action", actionBytes, 200)
177+
assert.Equal(t, "{\"actionResult\":\"SUCCESS\"}\n", string(respBytes))
148178

149179
// TODO Do we really want to delete from DB or just add DELETED status ?
150180
//// DELETE patron request
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package service

0 commit comments

Comments
 (0)