123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- unit utOpenAPIWriter;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry, fpjson, fpjson.schema.schema, fpopenapi.objects, fpopenapi.writer;
- type
- { TTestOpenApiWriterBase }
- TTestOpenApiWriterBase = class(TTestCase)
- private
- FDocument: TJSONObject;
- FOpenAPI: TOpenAPI;
- procedure AddSource(Const aFmt : string; aArgs : Array of const);
- Procedure AddSource(Const aLine : string);
- procedure AddDecl(Const aFmt : string; aArgs : Array of const);
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- Procedure WriteOpenAPI;
- procedure CheckJSON(const Msg: String; aJSON: TJSONStringType);
- procedure TestWrite(const Msg: String; aJSON: TJSONStringType);
- Public
- Property OpenApi : TOpenAPI Read FOpenAPI;
- Property Document : TJSONObject Read FDocument;
- published
- procedure TestHookUp;
- end;
- { TTestOpenApiWriter }
- TTestOpenApiWriterOpenAPI = class(TTestOpenApiWriterBase)
- Published
- procedure TestEmpty;
- Procedure TestOpenAPI;
- Procedure TestJSONSchemaDialect;
- end;
- TTestOpenApiWriterInfo = class(TTestOpenApiWriterBase)
- Published
- procedure TestVersion;
- procedure TestTitle;
- procedure TestSummary;
- procedure TestDescription;
- procedure TestTermsOfService;
- procedure TestContactEmail;
- procedure TestContactName;
- procedure TestContactURL;
- procedure TestLicenseUrl;
- procedure TestLicenseIdentifier;
- procedure TestLicenseName;
- end;
- TTestOpenApiWriterTags = class(TTestOpenApiWriterBase)
- Published
- Procedure TestName;
- Procedure TestDescription;
- Procedure TestExternalDocs;
- Procedure Test2Tags;
- end;
- TTestOpenApiWriterExternalDocs = class(TTestOpenApiWriterBase)
- Published
- procedure TestUrl;
- procedure TestDescription;
- end;
- { TTestOpenApiWriterServers }
- TTestOpenApiWriterServers = class(TTestOpenApiWriterBase)
- Published
- Procedure TestUrl;
- Procedure TestDescription;
- procedure TestVariablesDefault;
- procedure TestVariablesDescription;
- procedure TestVariablesEnum;
- Procedure Test2Url;
- end;
- { TTestOpenApiWriterPaths }
- TTestOpenApiWriterPathBase = class(TTestOpenApiWriterBase)
- Public
- Function AddPath(aPath : String) : TPathItem;
- end;
- TTestOpenApiWriterPaths = class(TTestOpenApiWriterPathBase)
- Published
- Procedure TestEmpty;
- Procedure TestRef;
- Procedure TestSummary;
- Procedure TestDescription;
- Procedure TestGetEmpty;
- Procedure TestGetSummary;
- Procedure TestGetDescription;
- Procedure TestGetOperationId;
- Procedure TestGetDeprecated;
- Procedure TestGetExternalDocsEmpty;
- Procedure TestGetExternalDocsURL;
- Procedure TestGetServers;
- end;
- { TTestOpenApiWriterPathRequestBody }
- TTestOpenApiWriterPathRequestBody = class(TTestOpenApiWriterPathBase)
- Public
- Function AddBody(aPath : String) : TRequestBody;
- Published
- procedure TestEmpty;
- procedure TestDescription;
- procedure TestRequired;
- procedure TestContent;
- procedure TestContentSchema;
- procedure TestContentExample;
- procedure TestContentExamples;
- procedure TestContentEncodingContentType;
- procedure TestContentEncodingExplode;
- procedure TestContentEncodingStyle;
- procedure TestContentEncodingAllowReserved;
- procedure TestContentEncodingHeaders;
- end;
- { TTestOpenApiWriterPathParameters }
- TTestOpenApiWriterPathParameters = class(TTestOpenApiWriterPathBase)
- Public
- Function AddParam(aPath : String) : TParameter;
- Published
- Procedure TestGetParametersEmpty;
- end;
- { TTestOpenApiWriterPathResponses }
- TTestOpenApiWriterPathResponses = class(TTestOpenApiWriterPathBase)
- Public
- Function AddResponse(aPath : String) : TResponse;
- Published
- procedure TestEmpty;
- procedure TestHeaders;
- procedure TestDescription;
- procedure TestContentEmpty;
- procedure TestContentSchema;
- procedure TestContentExample;
- procedure TestContentExamples;
- procedure TestContentEncodingEmpty;
- procedure TestContentEncodingContentType;
- end;
- { TTestOpenApiWriterPathResponsesLinks }
- TTestOpenApiWriterPathResponsesLinks = class(TTestOpenApiWriterPathBase)
- Public
- Function AddLink(aPath : String) : TLink;
- Published
- procedure TestEmpty;
- Procedure TestOperatonRef;
- Procedure TestOperatonId;
- Procedure TestParameters;
- Procedure TestRequestBody;
- Procedure TestDescription;
- Procedure TestServer;
- end;
- { TTestOpenApiWriterPathSecurity }
- TTestOpenApiWriterPathSecurity = class(TTestOpenApiWriterPathBase)
- Published
- Procedure TestEmpty;
- Procedure TestAPIKey;
- end;
- { TTestOpenApiWriterWebHooks }
- TTestOpenApiWriterWebHooks = class(TTestOpenApiWriterBase)
- Public
- function AddWebHook(const aname : string) : TPathItem;
- Published
- procedure TestEmpty;
- procedure TestSummary;
- // At the moment, no more tests needed: the items are identical to path items.
- end;
- { TTestOpenApiWriterComponents }
- TTestOpenApiWriterComponents = class(TTestOpenApiWriterBase)
- Public
- function AddComponentSchema(const aname: string): TJSONSchema;
- Published
- procedure TestSchemaEmpty;
- procedure TestSchemaContentEncoding;
- end;
- { TTestOpenApiWriterSecurity }
- TTestOpenApiWriterSecurity = class(TTestOpenApiWriterBase)
- Published
- procedure TestOne;
- end;
- (*
- property Components: TComponents Read Get_Components write Set_Components;
- property Security: TSecurityRequirementList Read Get_Security write Set_Security;
- *)
- implementation
- uses jsoncomparer;
- var
- _Sources : TStrings;
- _Decl : TStrings;
- _LastClass : String;
- function ChangeRead(S : String) : String;
- begin
- Result:=StringReplace(S,'Write','Reade',[rfReplaceAll,rfIgnoreCase]);
- end;
- procedure StartSources; forward;
- Procedure AddMethod(aClassName,aTestName : string; const aJSON : String);
- var
- lClass,lTest : String;
- J,S : TJSONObject;
- D : TJSONData;
- P,N : String;
- begin
- lClass:=ChangeRead(aClassName);
- lTest:=ChangeRead(aTestName);
- if lClass<>_LastClass then
- begin
- if _LastClass<>'' then
- _Decl.Add(' end;');
- _Decl.Add('');
- _Decl.Add(' %s = Class(TOpenAPITestRead)',[lClass]);
- _LastClass:=lClass;
- end;
- _Decl.Add(' Procedure %s;',[lTest]);
- _Sources.Add('');
- _Sources.Add('procedure %s.%s;',[lClass,lTest]);
- _Sources.Add('');
- _Sources.Add('begin');
- _Sources.Add(' TestRead(''%s'');',[aJSON]);
- J:=GetJSON(aJSON) as TJSONObject;
- P:='OpenAPI';
- S:=Nil;
- try
- if J.Count>0 then
- begin
- D:=J.Items[0];
- if D is TJSONObject then
- S:=TJSONObject(D);
- N:=J.Names[0];
- While S is TJSONObject do
- begin
- P:=P+'.'+N;
- _Sources.Add(' AssertNotNull(''%s'',%s);',[P,P]);
- if S.Count=0 then
- begin
- D:=Nil;
- S:=Nil;
- Continue;
- end;
- D:=S.Items[0];
- N:=S.Names[0];
- if (D is TJSONArray) and (TJSONArray(D).Count>0) then
- begin
- D:=TJSONArray(D)[0];
- N:=N+'[0]';
- end;
- if D is TJSONObject then
- S:=TJSONObject(D)
- else
- S:=Nil;
- end;
- P:=P+'.'+N;
- if assigned(D) then
- Case D.JSONType of
- jtString : _Sources.Add(' AssertEquals(''%s'',''%s'',%s);',[P,D.AsString,P]);
- jtBoolean : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
- jtNumber : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
- end;
- end;
- finally
- J.Free;
- end;
- _Sources.Add('end;');
- _Sources.Add('');
- end;
- procedure TTestOpenApiWriterBase.AddSource(const aFmt: string; aArgs: array of const);
- begin
- AddSource(Format(aFmt,aArgs));
- end;
- procedure TTestOpenApiWriterBase.AddSource(const aLine: string);
- begin
- _Sources.Add(aLine);
- end;
- procedure TTestOpenApiWriterBase.AddDecl(const aFmt: string; aArgs: array of const);
- begin
- _Decl.Add(aFmt,aArgs);
- end;
- procedure TTestOpenApiWriterBase.SetUp;
- begin
- FOpenAPI:=TOpenAPI.Create;
- if _Decl=Nil then
- StartSources;
- end;
- procedure TTestOpenApiWriterBase.TearDown;
- begin
- FreeAndNil(FOpenAPI);
- FreeAndNil(FDocument);
- end;
- procedure TTestOpenApiWriterBase.WriteOpenAPI;
- var
- W : TOpenAPIWriter;
- begin
- W:=TOpenAPIWriter.Create(Nil);
- try
- FDocument:=W.WriteToJSON(OpenAPI);
- finally
- W.Free;
- end;
- end;
- procedure TTestOpenApiWriterBase.CheckJSON(const Msg: String; aJSON: TJSONStringType);
- var
- Data : TJSONData;
- Obj : TJSONObject absolute data;
- Errors : TStrings;
- begin
- Errors:=Nil;
- Data:=GetJSON(aJSON,true) as TJSONObject;
- try
- AssertEquals('Json object',TJSONObject,Data.ClassType);
- Errors:=TStringList.Create;
- // Writeln('Comparing ',Obj.AsJSON,' === ',Document.AsJSON);
- TJSONComparer.Compare(Obj,Document,Errors);
- if (Errors.Count<>0) then
- Fail(Msg+':'#10+Errors.Text);
- finally
- Errors.Free;
- Data.Free;
- end;
- end;
- procedure TTestOpenApiWriterBase.TestWrite(const Msg: String; aJSON: TJSONStringType);
- begin
- AddMethod(ClassName,TestName,aJSON);
- WriteOpenAPI;
- CheckJSON(msg,aJSON);
- end;
- procedure TTestOpenApiWriterBase.TestHookUp;
- begin
- AssertNotNull('have openapi',OpenAPI);
- AssertNull('have no document',Document);
- end;
- procedure TTestopenApiWriterOpenApi.TestEmpty;
- begin
- WriteOpenApi;
- CheckJSON('Empty','{}');
- end;
- procedure TTestopenApiWriterOpenAPI.TestOpenAPI;
- begin
- OpenAPI.OpenAPI:='123';
- TestWrite('openapi version','{ "openapi" : "123" }');
- end;
- procedure TTestopenApiWriterOpenAPI.TestJSONSchemaDialect;
- begin
- OpenAPI.JSONSchemaDialect:='123';
- TestWrite('json schema dialect','{ "jsonSchemaDialect" : "123" }');
- end;
- procedure TTestopenApiWriterInfo.TestVersion;
- begin
- OpenAPI.Info.Version:='123';
- TestWrite('Version','{ "info" : { "version" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestTitle;
- begin
- OpenAPI.Info.title:='123';
- TestWrite('Title','{ "info" : { "title" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestSummary;
- begin
- OpenAPI.Info.summary:='123';
- TestWrite('Summary','{ "info" : { "summary" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestDescription;
- begin
- OpenAPI.Info.Description:='123';
- TestWrite('Description','{ "info" : { "description" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestTermsOfService;
- begin
- OpenAPI.Info.TermsOfService:='123';
- TestWrite('TermsOfService','{ "info" : { "termsOfService" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactEmail;
- begin
- OpenAPI.Info.Contact.Email:='123';
- TestWrite('Contact mail','{ "info" : { "contact" : { "email" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactName;
- begin
- OpenAPI.Info.Contact.Name:='123';
- TestWrite('Contact name','{ "info" : { "contact" : { "name" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactURL;
- begin
- OpenAPI.Info.Contact.URL:='123';
- TestWrite('Contact url','{ "info" : { "contact" : { "url" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseUrl;
- begin
- OpenAPI.Info.License.Url:='123';
- TestWrite('license url','{ "info" : { "license" : { "url" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseName;
- begin
- OpenAPI.Info.License.Name:='123';
- TestWrite('license name','{ "info" : { "license" : { "name" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseIdentifier;
- begin
- OpenAPI.Info.License.Identifier:='123';
- TestWrite('license name','{ "info" : { "license" : { "identifier" : "123" } } }');
- end;
- procedure TTestopenApiWriterTags.TestName;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[0]');
- T.Name:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('name','{ "tags" : [ { "name" : "123" } ] }');
- end;
- procedure TTestopenApiWriterTags.TestDescription;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.Description:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('description','{ "tags" : [ { "description" : "123" } ] }');
- end;
- procedure TTestopenApiWriterTags.TestExternalDocs;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.ExternalDocs.Url:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('externaldocs','{ "tags" : [ { "externalDocs" : { "url" :"123" } } ] }');
- end;
- procedure TTestopenApiWriterTags.Test2Tags;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[0]');
- T.Name:='123';
- OpenAPI.Tags.Add(T);
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.Name:='456';
- OpenAPI.Tags.Add(T);
- TestWrite('multi','{ "tags" : [ { "name" : "123" }, { "name" : "456" } ] }');
- end;
- procedure TTestopenApiWriterExternalDocs.TestUrl;
- begin
- OpenAPI.ExternalDocs.Url:='123';
- TestWrite('Url','{ "externalDocs" : { "url" :"123" } }');
- end;
- procedure TTestopenApiWriterExternalDocs.TestDescription;
- begin
- OpenAPI.ExternalDocs.Description:='123';
- TestWrite('description','{ "externalDocs" : { "description" :"123" } }');
- end;
- { TTestOpenApiWriterServers }
- procedure TTestOpenApiWriterServers.TestUrl;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "url" :"123" } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestDescription;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.description:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "description" :"123" } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesDefault;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Default:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('var.default','{ "servers" : [ { "variables" : { "user": { "default": "123" } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesDescription;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Description:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('var.description','{ "servers" : [ { "variables" : { "user": { "description": "123" } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesEnum;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Enum.Add('123');
- ServerVar.Enum.Add('456');
- OpenAPI.Servers.Add(Server);
- TestWrite('var.enum','{ "servers" : [ { "variables" : { "user": { "enum": [ "123", "456" ] } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.Test2Url;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='123';
- OpenAPI.Servers.Add(Server);
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='456';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "url" :"123" }, { "url" :"456" } ] }');
- end;
- { TTestOpenApiWriterPaths }
- function TTestOpenApiWriterPathBase.AddPath(aPath: String): TPathItem;
- begin
- Result:=OpenAPI.Paths.AddItem(aPath);
- end;
- procedure TTestOpenApiWriterPaths.TestEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertNotNull('Have path',P);
- TestWrite('Path','{ "paths" : { "api" : {} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestRef;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Ref:='abc';
- TestWrite('Path ref','{ "paths" : { "api" : { "$ref": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestSummary;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Summary:='abc';
- TestWrite('Path summary','{ "paths" : { "api" : { "summary": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestDescription;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.description:='abc';
- TestWrite('Path description','{ "paths" : { "api" : { "description": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetServers;
- var
- P : TPathItem;
- S : TServer;
- begin
- P:=AddPath('api');
- S:=TServer.Create(P,'servers[0]');
- P.Servers.Add(S);
- TestWrite('Path description','{ "paths" : { "api" : { "servers": [ {} ] } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertEquals('Empty','',P.Get.Summary);
- TestWrite('Path get','{ "paths" : { "api" : { "get": { } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetSummary;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Summary:='123';
- TestWrite('Path get summary','{ "paths" : { "api" : { "get": { "summary" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetDescription;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Description:='123';
- TestWrite('Path get description','{ "paths" : { "api" : { "get": { "description" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetOperationId;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.operationId:='123';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "operationId" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetDeprecated;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Deprecated:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "deprecated" : true } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetExternalDocsEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertNotNull('Get creates',P.Get.ExternalDocs);
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "externalDocs" : { } } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetExternalDocsURL;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.ExternalDocs.Url:='123';
- TestWrite('Path get externaldocs','{ "paths" : { "api" : { "get": { "externalDocs" : { "url" : "123" } } } } }');
- end;
- { TTestOpenApiWriterPathRequestBody }
- function TTestOpenApiWriterPathRequestBody.AddBody(aPath: String): TRequestBody;
- begin
- Result:=AddPath(aPath).Get.RequestBody;
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestEmpty;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- AssertNotNull('Get creates',R);
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestDescription;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Description:='123';
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "description" : "123" } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestRequired;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Required:=True;
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "required" : true} } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContent;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123');
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentSchema;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Schema:=TJSONSchema.Create();
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "schema" : true } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentExample;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').example:=TJSONObject.Create;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "example" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentExamples;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').examples.AddItem('abc').Summary:='x';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingContentType;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').ContentType:='def';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "contentType": "def"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingExplode;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Explode:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "explode": true} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingStyle;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Style:='def';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "style": "def"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingAllowReserved;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').AllowReserved:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "allowReserved": true} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingHeaders;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Headers.AddItem('def').Explode:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "headers": { "def" : { "explode" : true } } } } } } } } } } }');
- end;
- function TTestOpenApiWriterPathParameters.AddParam(aPath: String): TParameter;
- var
- P : TPathItem;
- begin
- P:=addPath(aPath);
- Result:=P.Parameters.AddParam(P);
- end;
- procedure TTestOpenApiWriterPathParameters.TestGetParametersEmpty;
- var
- P : TParameter;
- begin
- P:=AddParam('api');
- AssertNotNull('have p',p);
- TestWrite('Path param','{ "paths" : { "api" : { "parameters": [ {} ] } } }');
- end;
- function TTestOpenApiWriterPathResponses.AddResponse(aPath: String): TResponse;
- begin
- Result:=AddPath(aPath).Get.Responses.AddItem('default');
- end;
- procedure TTestOpenApiWriterPathResponses.TestEmpty;
- begin
- AssertNotNull('Have response',AddResponse('api'));
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : {} } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestHeaders;
- begin
- AddResponse('api').Headers.AddItem('x-content');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "headers" : { "x-content" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestDescription;
- begin
- AddResponse('api').Description:='abc';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "description" : "abc" } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEmpty;
- begin
- AddResponse('api').content.AddItem('application/json');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentSchema;
- begin
- AssertNotNull('Have schema',AddResponse('api').content.AddItem('application/json').Schema);
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "schema" : true } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentExample;
- begin
- AddResponse('api').content.AddItem('application/json').Example:=TJSONObject.Create;
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "example" : {} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentExamples;
- begin
- AddResponse('api').content.AddItem('application/json').Examples.AddItem('abc').Summary:='x';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEncodingEmpty;
- begin
- AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc'); //.ContentType:='application/json';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { } } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEncodingContentType;
- begin
- AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc').ContentType:='application/json';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { "contentType" : "application/json" } } } } } } } } } }');
- end;
- { TTestOpenApiWriterPathResponsesLinks }
- function TTestOpenApiWriterPathResponsesLinks.AddLink(aPath: String): TLink;
- begin
- Result:=AddPath(aPath).Get.Responses.AddItem('default').links.AddItem('abc');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestEmpty;
- begin
- AddLink('api');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonRef;
- begin
- AddLink('api').OperationRef:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationRef" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonId;
- begin
- AddLink('api').OperationId:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationId" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestParameters;
- begin
- AddLink('api').Parameters.Add('x','y');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "parameters" : { "x" : "y" } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestRequestBody;
- begin
- AddLink('api').RequestBody:=TJSONObject.Create();
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "requestBody" : {} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestDescription;
- begin
- AddLink('api').Description:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "description" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestServer;
- begin
- AddLink('api').Server.Url:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "server" : { "url" : "123" } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathSecurity.TestEmpty;
- var
- O : TAPIOperation;
- begin
- O:=AddPath('api').Get;
- O.Security.AddSecurity(O);
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ {} ] } } } }');
- end;
- procedure TTestOpenApiWriterPathSecurity.TestAPIKey;
- var
- O : TAPIOperation;
- begin
- O:=AddPath('api').Get;
- O.Security.AddSecurity(O).AddItem('api_key').Add('akey');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ { "api_key" : ["akey"] } ] } } } }');
- end;
- { TTestOpenApiWriterWebHooks }
- function TTestOpenApiWriterWebHooks.AddWebHook(const aname: string): TPathItem;
- begin
- Result:=OpenAPI.WebHooks.AddItem(aName);
- end;
- procedure TTestOpenApiWriterWebHooks.TestEmpty;
- begin
- AssertNotNull(OpenAPI.WebHooks.AddItem('newitem'));
- TestWrite('Path param','{ "webhooks" : { "newitem" : { } } }');
- end;
- procedure TTestOpenApiWriterWebHooks.TestSummary;
- begin
- OpenAPI.WebHooks.AddItem('newitem').Summary:='abc';
- TestWrite('Path param','{ "webhooks" : { "newitem" : { "summary" : "abc" } } }');
- end;
- { TTestOpenApiWriterComponents }
- function TTestOpenApiWriterComponents.AddComponentSchema(const aname: string): TJSONSchema;
- begin
- Result:=OpenAPI.Components.Schemas.Add(aName);
- end;
- procedure TTestOpenApiWriterComponents.TestSchemaEmpty;
- begin
- AssertNotNull('Schema',AddComponentSchema('abc'));
- TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : true } } }');
- end;
- procedure TTestOpenApiWriterComponents.TestSchemaContentEncoding;
- begin
- AddComponentSchema('abc').Validations.contentEncoding:='utf8';
- TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : { "contentEncoding" : "utf8" } } } }');
- end;
- { TTestOpenApiWriterSecurity }
- procedure TTestOpenApiWriterSecurity.TestOne;
- begin
- OpenAPI.Security.AddSecurity(OpenAPI).AddItem('abc');
- TestWrite('Security','{ "security" : [ { "abc" : [] } ] }');
- end;
- procedure StartSources;
- begin
- _Sources:=TStringList.Create;
- _Decl:=TStringList.Create;
- _Decl.Add('unit ReadTests;');
- _Decl.Add('');
- _Decl.Add('interface');
- _Decl.Add('');
- _Decl.Add('uses fpcunit;');
- _Decl.Add('');
- _Decl.Add('type');
- _Decl.Add('');
- end;
- procedure WriteSources;
- begin
- _Decl.Add(' end;');
- _Decl.Add('');
- _Decl.Add('implementation');
- _Decl.Add('');
- _Decl.AddStrings(_Sources);
- _Decl.Add('');
- _Decl.Add('end.');
- _Decl.SaveToFile('ReadTests.pp');
- _Sources.Free;
- _Decl.Free;
- end;
- initialization
- // StartSources;
- RegisterTests('Writer',[
- TTestopenApiWriterOpenAPI,
- TTestopenApiWriterInfo,
- TTestopenApiWriterTags,
- TTestOpenApiWriterExternalDocs,
- TTestOpenApiWriterServers,
- TTestOpenApiWriterPaths,
- TTestOpenApiWriterPathRequestBody,
- TTestOpenApiWriterPathParameters,
- TTestOpenApiWriterPathResponses,
- TTestOpenApiWriterPathSecurity,
- TTestOpenApiWriterPathResponsesLinks,
- TTestOpenApiWriterWebHooks,
- TTestOpenApiWriterComponents,
- TTestOpenApiWriterSecurity
- ]);
- finalization
- if assigned(_Decl) then
- WriteSources;
- end.
|