@@ -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
2629var basePath = "/patron_requests"
30+ var illRepo ill_db.IllRepo
2731
2832func 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
6075func 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
0 commit comments