package Ġcom . merc ate o . json schema Ċ Ċ import Ġcom . fasterxml . jackson . annotation . Json Un wrapped Ċ import Ġcom . merc ate o . json schema . gener ict ype . GenericType Ċ import Ġcom . merc ate o . json schema . mapper . Property Checker Ċ import Ġcom . merc ate o . json schema . property . Property Ċ import Ġcom . merc ate o . json schema . property . collector . Field Collector Ċ import Ġcom . merc ate o . json schema . property . mapper . Un wrapped Property Updater Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Test Ċ Ċ class ĠSchema Generator Test Ġ{ ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġschema Generator : ĠSchema Generator ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġschema Generator Ġ= ĠSchema Generator ( property Collectors Ġ= ĠlistOf ( Field Collector ())) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Create Schema () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple :: class . java ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( schema Class ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" ba ar \":{\" type \":\" integer \" }, \" bar \":{\" type \":\" integer \" }, \" baz \":{\" type \":\" number \" }, \" foo \":{\" type \":\" string \" }, \" qu ux \":{\" type \":\" boolean \" }, \" qu x \":{\" type \":\" number \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Create Schema From GenericType () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple :: class . java ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( GenericType . of ( schema Class )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" ba ar \":{\" type \":\" integer \" }, \" bar \":{\" type \":\" integer \" }, \" baz \":{\" type \":\" number \" }, \" foo \":{\" type \":\" string \" }, \" qu ux \":{\" type \":\" boolean \" }, \" qu x \":{\" type \":\" number \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Create Schema For Other Types () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple Other :: class . java ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( schema Class ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" bar \":{\" type \":\" number \" }, \" baz \":{\" type \":\" number \" }, \" foo \":{\" type \":\" integer \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Ignore Static Methods () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple :: class . java ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( schema Class ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" ba ar \":{\" type \":\" integer \" }, \" bar \":{\" type \":\" integer \" }, \" baz \":{\" type \":\" number \" }, \" foo \":{\" type \":\" string \" }, \" qu ux \":{\" type \":\" boolean \" }, \" qu x \":{\" type \":\" number \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Create Schema WithDefault Values () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple :: class . java ĊĊĠĠĠĠĠĠĠ Ġval ĠdefaultValue Ġ= ĠSchema Generator Classes . Simple () ĊĠĠĠĠĠĠĠ ĠdefaultValue . foo Ġ= Ġ" foo " ĊĠĠĠĠĠĠĠ ĠdefaultValue . bar Ġ= Ġ 1 0 ĊĠĠĠĠĠĠĠ ĠdefaultValue . ba ar Ġ= Ġ 1 0 0 L ĊĠĠĠĠĠĠĠ ĠdefaultValue . baz Ġ= Ġ 4 . 8 f ĊĠĠĠĠĠĠĠ ĠdefaultValue . qu x Ġ= Ġ 5 . 1 ĊĠĠĠĠĠĠĠ ĠdefaultValue . qu ux Ġ= Ġtrue ĊĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( schema Class , ĠdefaultValue Ġ= ĠdefaultValue ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" ba ar \":{\" type \":\" integer \",\" default \": 1 0 0 }, \" bar \":{\" type \":\" integer \",\" default \": 1 0 }, \" baz \":{\" type \":\" number \",\" default \": 4 . 8 }, \" foo \":{\" type \":\" string \",\" default \":\" foo \" }, \" qu ux \":{\" type \":\" boolean \",\" default \": true }, \" qu x \":{\" type \":\" number \",\" default \": 5 . 1 }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Create Schema With Allowed Values () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Class Ġ= ĠSchema Generator Classes . Simple :: class . java ĊĊĠĠĠĠĠĠĠ Ġval Ġallowed Value Ġ= ĠSchema Generator Classes . Simple () ĊĠĠĠĠĠĠĠ Ġallowed Value . foo Ġ= Ġ" foo " ĊĠĠĠĠĠĠĠ Ġallowed Value . bar Ġ= Ġ 1 0 ĊĠĠĠĠĠĠĠ Ġallowed Value . ba ar Ġ= Ġ 1 0 0 L ĊĠĠĠĠĠĠĠ Ġallowed Value . baz Ġ= Ġ 4 . 8 f ĊĠĠĠĠĠĠĠ Ġallowed Value . qu x Ġ= Ġ 5 . 1 ĊĠĠĠĠĠĠĠ Ġallowed Value . qu ux Ġ= Ġtrue ĊĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( schema Class , Ġallowed Values Ġ= ĠarrayOf ( allowed Value )) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" ba ar \":{\" type \":\" integer \",\" enum \": [ 1 0 0 ]}, \" bar \":{\" type \":\" integer \",\" enum \": [ 1 0 ]}, \" baz \":{\" type \":\" number \",\" enum \": [ 4 . 8 ]}, \" foo \":{\" type \":\" string \",\" enum \":[\" foo \"] }, \" qu ux \":{\" type \":\" boolean \",\" enum \": [ true ]}, \" qu x \":{\" type \":\" number \",\" enum \": [ 5 . 1 ]}} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġhandles Java Optional () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Option Types :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" optional String \":{\" type \":\" string \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġhandles Java Collections () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Collections :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" strings \":{\" type \":\" array \",\" items \":{\" type \":\" array \",\" items \":{\" type \":\" string \" }} }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġhandles References () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . References :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" bar \":{\" type \":\" object \",\" properties \":{\" id \":{\" type \":\" string \" }, \" name \":{\" type \":\" string \" }}, \" id \":\" #/ bar \" }, \" foo \": { \"\ $ ref \":\" #/ bar \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġhandles Class H ierarch ies () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Subclass :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" bar \":{\" type \":\" string \" }, \" foo \":{\" type \":\" string \" }, \" qu x \":{\" type \":\" string \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Required Properties () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Required :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" bar \":{\" type \":\" string \" }, \" baz \":{\" type \":\" string \" }, \" foo \":{\" type \":\" string \" }, \" qu x \":{\" type \":\" string \",\" min Length \": 1 }}, \" required \":[\" bar \",\" qu x \"] }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Int Validation Properties () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Int Validation :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" max \":{\" type \":\" integer \",\" maximum \": 1 0 }, \" min \":{\" type \":\" integer \",\" minimum \": 5 }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows String Validation Properties () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . String Validation :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" max \":{\" type \":\" string \",\" maxLength \": 1 0 }, \" min \":{\" type \":\" string \",\" min Length \": 5 }, \" range \":{\" type \":\" string \",\" maxLength \": 9 ,\" min Length \": 6 }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Pattern Validation Properties () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Pattern Validation :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" pattern \":{\" type \":\" string \",\" pattern \":\" ^( the Ġ) ? pattern $ \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Integer Validation Properties () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Integer Annotations :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" max \":{\" type \":\" integer \",\" maximum \": 1 0 }, \" min \":{\" type \":\" integer \",\" minimum \": 5 }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Enum Values Allowed Values () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . EnumValue :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" enum Value \":{\" type \":\" string \",\" enum \":[\" TRUE \",\" FALSE \"] }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshows Allowed Values For Enum () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġallowed Values Ġ= ĠarrayOf ( Schema Generator Classes . EnumValue (). apply Ġ{ Ġenum Value Ġ= ĠSchema Generator Classes . Value . TRUE Ġ}) ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . EnumValue :: class . java , Ġnull , Ġallowed Values ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" enum Value \":{\" type \":\" string \",\" enum \":[\" TRUE \"] }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Add Un wrapped Property Mapper () Ġ{ ĊĠĠĠĠĠĠĠ Ġschema Generator Ġ= ĠSchema Generator ( property Collectors Ġ= ĠlistOf ( Field Collector ()), Ġunwrap Annotations Ġ= ĠlistOf ( object Ġ: ĠUn wrapped Property Updater < Json Un wrapped >( Json Un wrapped :: class . java ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġupdate Name ( name : ĠString , Ġannotation : ĠJson Un wrapped ): ĠString Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġannotation . prefix Ġ+ Ġname Ġ+ Ġannotation . suffix ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ})) ĊĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . EnumValue :: class . java , Ġnull , Ġnull ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" enum Value \":{\" type \":\" string \",\" enum \":[\" TRUE \",\" FALSE \"] }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Render Pol ym orphism () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Pol ym orphism :: class . java ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" contact \":{\" type \":\" object \",\" any Of \": [{ \" type \":\" object \",\" properties \":{\" email Address \":{\" type \":\" string \" }, \" @ type \":{\" type \":\" string \",\" enum \":[\" email \"] }}, \" required \":[\" email Address \"] },{ \" type \":\" object \",\" properties \":{\" fax Number \":{\" type \":\" string \" }, \" person Name \":{\" type \":\" string \" }, \" @ type \":{\" type \":\" string \",\" enum \":[\" fax \"] }}, \" required \":[\" fax Number \"] }] }, \" name \":{\" type \":\" string \" }} }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġshould Remove Properties Acc ording To Property Checker Result () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġschema Generator . generate Schema ( Schema Generator Classes . Checked :: class . java , Ġnull , Ġnull , Ġobject Ġ: ĠProperty Checker Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġtest ( t : ĠProperty < *, Ġ* >): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġreturn Ġ! t . annotations . containsKey ( Schema Generator Classes . Inactive :: class . java ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( schema . toString ()). isEqualTo ("{ \" type \":\" object \",\" properties \":{\" bar \":{\" type \":\" string \" }} }") ĊĠĠĠ Ġ} Ċ }