testsqldbopenapi.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. unit testsqldbopenapi;
  2. {$mode ObjFPC}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, fpjson, fpjson.schema.types, fpjson.schema.schema,
  6. fpopenapi.types, fpopenapi.objects, sqldbrestschema, sqldbrestopenapi, fpopenapi.writer,
  7. jsonwriter;
  8. Type
  9. { TTestSQLDBRestOpenAPI }
  10. TTestSQLDBRestOpenAPI = class(TTestCase)
  11. private
  12. FConverter: TSLQDBRestSchemaToOpenAPI;
  13. FOpenAPI: TOpenAPI;
  14. FSchema: TSQLDBRestSchema;
  15. protected
  16. procedure AssertGetOperation(aComponent: String);
  17. procedure AssertPostOperation(aComponent: String);
  18. procedure AssertListComponent(aComponent: string);
  19. procedure AssertListOperation(aComponent: String);
  20. procedure AssertSimpleComponent(aComponent: string; aExtraProperty: TSchemaSimpleType=sstNone);
  21. procedure Convert;
  22. function CreateResource(withID: boolean; aSecondFieldType: TRestFieldType): TSQLDBRestResource;
  23. Public
  24. Procedure SetUp; override;
  25. Procedure TearDown; override;
  26. Property Converter : TSLQDBRestSchemaToOpenAPI Read FConverter;
  27. Property OpenAPI : TOpenAPI Read FOpenAPI;
  28. Property Schema : TSQLDBRestSchema Read FSchema;
  29. Published
  30. Procedure TestHookup;
  31. procedure TestResourceReadOnly;
  32. procedure TestResourceReadOnlyWithID;
  33. procedure TestResourcePostOnly;
  34. end;
  35. implementation
  36. { TTestSQLDBRestOpenAPI }
  37. procedure TTestSQLDBRestOpenAPI.SetUp;
  38. begin
  39. inherited SetUp;
  40. FConverter:=TSLQDBRestSchemaToOpenAPI.Create(Nil);
  41. FOpenAPI:=TOpenAPI.Create;
  42. FSchema:=TSQLDBRestSchema.Create(Nil);
  43. end;
  44. procedure TTestSQLDBRestOpenAPI.TearDown;
  45. begin
  46. FreeAndNil(FSchema);
  47. FreeAndNil(FOpenAPI);
  48. FreeAndNil(FConverter);
  49. inherited TearDown;
  50. end;
  51. procedure TTestSQLDBRestOpenAPI.TestHookup;
  52. begin
  53. AssertNotNull('Have converter',Converter);
  54. end;
  55. function TTestSQLDBRestOpenAPI.CreateResource(withID : boolean; aSecondFieldType: TRestFieldType) : TSQLDBRestResource;
  56. var
  57. lField : TSQLDBRestField;
  58. begin
  59. Result:=Schema.Resources.AddResource('simple','simple');
  60. lField:=Result.Fields.AddField('id',rftInteger,[]);
  61. if WithID then
  62. lField.Options:=lField.Options+[foInKey];
  63. if aSecondFieldType<>rftUnknown then
  64. Result.Fields.AddField('b',aSecondFieldType,[]);
  65. end;
  66. procedure TTestSQLDBRestOpenAPI.Convert;
  67. var
  68. Writer : TOpenAPIWriter;
  69. J : TJSONDataWriter;
  70. D : TJSONData;
  71. begin
  72. Converter.Convert(Schema,OpenAPI);
  73. Writer:=TOpenAPIWriter.Create(Nil);
  74. J:=TJSONDataWriter.Create;
  75. try
  76. Writer.Write(OpenAPI,J);
  77. Writeln(TestName,' OpenAPI:');
  78. D:=J.ExtractData;
  79. Writeln(D.FormatJSON);
  80. finally
  81. D.Free;
  82. J.Free;
  83. end;
  84. end;
  85. procedure TTestSQLDBRestOpenAPI.AssertGetOperation(aComponent : String);
  86. var
  87. lPath : TPathItem;
  88. Op : TAPIOperation;
  89. Res : TResponse;
  90. lMedia : TMediaType;
  91. begin
  92. lPath:=OpenAPI.Paths[aComponent+'/{ID}'];
  93. AssertNotNull('have '+aComponent+'/{ID} path',lPath);
  94. AssertTrue('Get Operation',lPath.HasKeyWord(pkGet));
  95. OP:=lPath.Get;
  96. AssertEquals('Get OperationID','Get'+aComponent,OP.OperationId);
  97. AssertEquals('response count',1, OP.Responses.Count);
  98. AssertNotNull('Get default response',OP.Responses['default']);
  99. AssertEquals('response count',1, OP.Responses.Count);
  100. Res:=OP.Responses['default'];
  101. AssertNotNull('Have default response',Res);
  102. AssertTrue('Havemedia count',Res.HasKeyWord(rkContent));
  103. lMedia:=Res.Content.MediaTypes['application/json'];
  104. AssertNotNull('Have media',lMedia);
  105. AssertTrue('Have schema',lMedia.HasKeyWord(mtkSchema));
  106. AssertEquals('Have component ref','#components/schema/'+aComponent,lMedia.Schema.Ref);
  107. end;
  108. procedure TTestSQLDBRestOpenAPI.AssertPostOperation(aComponent: String);
  109. var
  110. lPath : TPathItem;
  111. Op : TAPIOperation;
  112. Res : TResponse;
  113. lMedia : TMediaType;
  114. begin
  115. lPath:=OpenAPI.Paths[aComponent];
  116. AssertNotNull('have '+aComponent+' path',lPath);
  117. AssertTrue('Post Operation',lPath.HasKeyWord(pkPost));
  118. OP:=lPath.Post;
  119. AssertEquals('Get OperationID','Create'+aComponent,OP.OperationId);
  120. AssertEquals('response count',1, OP.Responses.Count);
  121. AssertNotNull('Get default response',OP.Responses['default']);
  122. AssertEquals('response count',1, OP.Responses.Count);
  123. Res:=OP.Responses['default'];
  124. AssertNotNull('Have default response',Res);
  125. AssertTrue('Havemedia count',Res.HasKeyWord(rkContent));
  126. lMedia:=Res.Content.MediaTypes['application/json'];
  127. AssertNotNull('Have media',lMedia);
  128. AssertTrue('Have schema',lMedia.HasKeyWord(mtkSchema));
  129. AssertEquals('Have component ref','#components/schema/'+aComponent,lMedia.Schema.Ref);
  130. end;
  131. procedure TTestSQLDBRestOpenAPI.AssertListOperation(aComponent : String);
  132. var
  133. lPath : TPathItem;
  134. Op : TAPIOperation;
  135. Res : TResponse;
  136. lMedia : TMediaType;
  137. begin
  138. lPath:=OpenAPI.Paths[aComponent];
  139. AssertNotNull('have '+acomponent+' path',lPath);
  140. AssertTrue('Get Operation',lPath.HasKeyWord(pkGet));
  141. OP:=lPath.Get;
  142. AssertEquals('Get OperationID','List'+aComponent,OP.OperationId);
  143. AssertEquals('response count',1, OP.Responses.Count);
  144. AssertNotNull('Get default response',OP.Responses['default']);
  145. AssertEquals('response count',1, OP.Responses.Count);
  146. Res:=OP.Responses['default'];
  147. AssertNotNull('Have default response',Res);
  148. AssertTrue('Havemedia count',Res.HasKeyWord(rkContent));
  149. lMedia:=Res.Content.MediaTypes['application/json'];
  150. AssertNotNull('Have media',lMedia);
  151. AssertTrue('Have schema',lMedia.HasKeyWord(mtkSchema));
  152. AssertEquals('Have component ref','#components/schema/'+aComponent+'List',lMedia.Schema.Ref);
  153. end;
  154. Procedure TTestSQLDBRestOpenAPI.AssertSimpleComponent(aComponent : string; aExtraProperty : TSchemaSimpleType = sstNone);
  155. var
  156. S,el : TJSONSchema;
  157. begin
  158. AssertTrue('Components',OpenAPI.HasKeyWord(oakComponents));
  159. AssertTrue('Components.Schemas',OpenAPI.Components.HasKeyWord(ckSchemas));
  160. S:=OpenAPI.Components.Schemas[aComponent];
  161. AssertNotNull('Component '+aComponent+' Schema',S);
  162. AssertTrue(aComponent+' is array',S.Validations.Types=[sstObject]);
  163. AssertEquals(aComponent+' property count',1+Ord(aExtraProperty<>sstNone),S.properties.Count);
  164. el:=S.Properties[0];
  165. AssertNotNull(aComponent+'property 0 is valid',el);
  166. AssertEquals(aComponent+'property 0 is valid','id',el.Name);
  167. AssertTrue(aComponent+'property id type',el.Validations.Types=[sstInteger]);
  168. if aExtraProperty<>sstNone then
  169. begin
  170. el:=S.Properties[1];
  171. AssertNotNull(aComponent+'property 1 is valid',el);
  172. AssertEquals(aComponent+'property 1 is valid','b',el.Name);
  173. AssertTrue(aComponent+'property b type',el.Validations.Types=[aExtraProperty]);
  174. end
  175. end;
  176. Procedure TTestSQLDBRestOpenAPI.AssertListComponent(aComponent : string);
  177. var
  178. S,el : TJSONSchema;
  179. begin
  180. AssertTrue('Components',OpenAPI.HasKeyWord(oakComponents));
  181. AssertTrue('Components.Schemas',OpenAPI.Components.HasKeyWord(ckSchemas));
  182. S:=OpenAPI.Components.Schemas[aComponent+'List'];
  183. AssertNotNull('Component '+aComponent+'List Schema',S);
  184. AssertTrue(aComponent+' is array',S.Validations.Types=[sstArray]);
  185. AssertTrue(aComponent+' has 1 item',S.items.Count=1);
  186. el:=S.Items[0];
  187. AssertNotNull(aComponent+' item is valid',el);
  188. AssertEquals(aComponent+' reference to component','#components/schemas/'+aComponent,el.ref);
  189. end;
  190. procedure TTestSQLDBRestOpenAPI.TestResourceReadOnly;
  191. var
  192. R : TSQLDBRestResource;
  193. begin
  194. R:=CreateResource(False,rftUnknown);
  195. R.AllowedOperations:=[roGet];
  196. Convert;
  197. AssertTrue('Component schemas',OpenAPI.Components.HasKeyWord(ckSchemas));
  198. AssertEquals('Component Count',2, OpenAPI.Components.Schemas.Count);
  199. AssertSimpleComponent('simple');
  200. AssertListComponent('simple');
  201. AssertTrue('PathItems',OpenAPI.HasKeyWord(oakPaths));
  202. AssertEquals('Path Count',1, OpenAPI.Paths.Count);
  203. AssertListOperation('simple');
  204. end;
  205. procedure TTestSQLDBRestOpenAPI.TestResourceReadOnlyWithID;
  206. var
  207. R : TSQLDBRestResource;
  208. begin
  209. R:=CreateResource(True,rftUnknown);
  210. R.AllowedOperations:=[roGet];
  211. Convert;
  212. AssertTrue('Components',OpenAPI.HasKeyWord(oakComponents));
  213. AssertTrue('Component schemas',OpenAPI.Components.HasKeyWord(ckSchemas));
  214. AssertEquals('Component Count',2, OpenAPI.Components.Schemas.Count);
  215. AssertSimpleComponent('simple');
  216. AssertListComponent('simple');
  217. AssertTrue('PathItems',OpenAPI.HasKeyWord(oakPaths));
  218. AssertEquals('Path Count',2, OpenAPI.Paths.Count);
  219. AssertListOperation('simple');
  220. AssertGetOperation('simple');
  221. end;
  222. procedure TTestSQLDBRestOpenAPI.TestResourcePostOnly;
  223. var
  224. R : TSQLDBRestResource;
  225. begin
  226. R:=CreateResource(True,rftUnknown);
  227. R.AllowedOperations:=[roPost];
  228. Convert;
  229. AssertTrue('Components',OpenAPI.HasKeyWord(oakComponents));
  230. AssertTrue('Component schemas',OpenAPI.Components.HasKeyWord(ckSchemas));
  231. AssertEquals('Component Count',1, OpenAPI.Components.Schemas.Count);
  232. AssertSimpleComponent('simple');
  233. // AssertListComponent('simple');
  234. AssertTrue('PathItems',OpenAPI.HasKeyWord(oakPaths));
  235. AssertEquals('Path Count',1, OpenAPI.Paths.Count);
  236. AssertPostOperation('simple');
  237. end;
  238. initialization
  239. RegisterTest(TTestSQLDBRestOpenAPI);
  240. end.