@@ -44,6 +44,24 @@ paths:
4444 content: {}
4545`
4646
47+ const testUpdatedYamlSpecification = `openapi: 3.0.1
48+ info:
49+ title: Service
50+ version: 1.1.1
51+ servers:
52+ - url: /
53+ paths:
54+ /test/updated:
55+ get:
56+ tags:
57+ - Redirects
58+ summary: Absolutely 302 Redirects n times.
59+ responses:
60+ ''200'':
61+ description: A redirection.
62+ content: {}
63+ `
64+
4765var currentDBPath = "./wallarm_api2_update.db"
4866
4967var cfgV2 = config.APIMode {
@@ -95,6 +113,39 @@ func insertSpecV2(dbFilePath, newSpec, state string) (*EntryV2, error) {
95113 return & entry , nil
96114}
97115
116+ func updateSpecV2 (dbFilePath string , schemaID int , newState string , newSchema string ) (* EntryV2 , error ) {
117+
118+ db , err := sql .Open ("sqlite3" , dbFilePath )
119+ if err != nil {
120+ return nil , err
121+ }
122+ defer db .Close ()
123+
124+ q := fmt .Sprintf ("UPDATE openapi_schemas SET status = '%s', schema_content='%s' WHERE schema_id == %d" , newState , newSchema , schemaID )
125+ _ , err = db .Exec (q )
126+ if err != nil {
127+ return nil , err
128+ }
129+
130+ // entry of the V2
131+ entry := EntryV2 {}
132+
133+ rows , err := db .Query (fmt .Sprintf ("SELECT * FROM openapi_schemas WHERE schema_id == %d" , schemaID ))
134+ if err != nil {
135+ return nil , err
136+ }
137+ defer rows .Close ()
138+
139+ for rows .Next () {
140+ err = rows .Scan (& entry .SchemaID , & entry .SchemaVersion , & entry .SchemaFormat , & entry .SchemaContent , & entry .Status )
141+ if err != nil {
142+ return nil , err
143+ }
144+ }
145+
146+ return & entry , nil
147+ }
148+
98149// check that row is applied and delete this row
99150func cleanSpecV2 (dbFilePath string , schemaID int ) error {
100151
@@ -388,6 +439,61 @@ func TestUpdaterBasicV2(t *testing.T) {
388439 }
389440 }
390441
442+ // update the current entry state
443+ _ , err = updateSpecV2 (currentDBPath , entry .SchemaID , "new" , testUpdatedYamlSpecification )
444+ if err != nil {
445+ t .Fatal (err )
446+ }
447+
448+ // start updater second time.
449+ updNewSpecErrors := make (chan error , 1 )
450+ updater = handlersAPI .NewHandlerUpdater (& lock , logger , specStorage , & cfgV2 , & api , shutdown , & health , nil , nil )
451+ go func () {
452+ t .Logf ("starting specification regular update process every %.0f seconds" , cfg .SpecificationUpdatePeriod .Seconds ())
453+ updNewSpecErrors <- updater .Start ()
454+ }()
455+
456+ time .Sleep (3 * time .Second )
457+
458+ if err := updater .Shutdown (); err != nil {
459+ t .Fatal (err )
460+ }
461+
462+ // valid route in the updated spec
463+ req = fasthttp .AcquireRequest ()
464+ req .SetRequestURI ("/test/updated" )
465+ req .Header .SetMethod ("GET" )
466+ req .Header .Add (web .XWallarmSchemaIDHeader , fmt .Sprintf ("%d" , entry .SchemaID ))
467+
468+ reqCtx = fasthttp.RequestCtx {
469+ Request : * req ,
470+ }
471+
472+ lock .RLock ()
473+ api .Handler (& reqCtx )
474+ lock .RUnlock ()
475+
476+ if reqCtx .Response .StatusCode () != 200 {
477+ t .Errorf ("Incorrect response status code. Expected: 200 and got %d" ,
478+ reqCtx .Response .StatusCode ())
479+ }
480+
481+ apifwResponse = validator.ValidationResponse {}
482+ if err := json .Unmarshal (reqCtx .Response .Body (), & apifwResponse ); err != nil {
483+ t .Errorf ("Error while JSON response parsing: %v" , err )
484+ }
485+
486+ if len (apifwResponse .Summary ) > 0 {
487+ if * apifwResponse .Summary [0 ].SchemaID != entry .SchemaID {
488+ t .Errorf ("Incorrect error code. Expected: %d and got %d" ,
489+ entry .SchemaID , * apifwResponse .Summary [0 ].SchemaID )
490+ }
491+ if * apifwResponse .Summary [0 ].StatusCode != fasthttp .StatusOK {
492+ t .Errorf ("Incorrect result status. Expected: %d and got %d" ,
493+ fasthttp .StatusOK , * apifwResponse .Summary [0 ].StatusCode )
494+ }
495+ }
496+
391497}
392498
393499func TestUpdaterFromEmptyDBV2 (t * testing.T ) {
0 commit comments