package Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . controllers . products _ controller Ċ Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . Application Test Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . models . Business Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . models . New Business Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . models . New Product Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . models . Product Ċ import Ġar . edu . un q . ar qs 1 . M erc ad illo Lib re Back . models . d tos . Credentials Ċ import Ġorg . hamcrest . Core Matchers .` is ` Ċ import Ġorg . hamcrest . Core Matchers . notNull Value Ċ import Ġorg . junit . jupiter . api . Assertions . assertEquals Ċ import Ġorg . junit . jupiter . api . Assertions . assertNotNull Ċ import Ġorg . junit . jupiter . api . BeforeEach Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . springframework . http . HttpMethod Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . test . web . servlet . Result Actions Ċ import Ġorg . springframework . test . web . servlet . result . MockMvc Result Matchers . json Path Ċ import Ġorg . springframework . test . web . servlet . result . MockMvc Result Matchers . status Ċ Ċ class ĠCreate Product Test Ġ: ĠApplication Test () Ġ{ ĊĊĠĠĠ Ġprivate Ġvar Ġsell er : ĠBusiness ? Ġ= Ġnull ĊĊĠĠĠ Ġ@ BeforeEach ĊĠĠĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnew Business Ġ= ĠNew Business ( name Ġ= Ġ" name ", Ġemail Ġ= Ġ" email @ email . com ", Ġpassword Ġ= Ġ" sar lang a ") ĊĠĠĠĠĠĠĠ Ġcredentials Ġ= ĠCredentials ( new Business . email , Ġnew Business . password ) ĊĠĠĠĠĠĠĠ Ġsell er Ġ= Ġcreate Business ( new Business ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcreate Product ( product : ĠNew Product ): ĠResult Actions Ġ= ĊĠĠĠĠĠĠĠ Ġbusiness Authenticated Exchange ( credentials !! , Ġ"/ v 1 / products ", ĠHttpMethod . POST , Ġproduct ) ĊĊĠĠĠ Ġfun Ġnew Product ( ĊĠĠĠĠĠĠĠ Ġname : ĠString ? Ġ= Ġ" name ", ĊĠĠĠĠĠĠĠ Ġdescription : ĠString ? Ġ= Ġ" description ", ĊĠĠĠĠĠĠĠ Ġprice : ĠInt ? Ġ= Ġ 1 0 , ĊĠĠĠĠĠĠĠ Ġstock : ĠInt ? Ġ= Ġ 1 0 ĊĠĠĠ Ġ) Ġ= ĊĊĠĠĠĠĠĠĠ ĠNew Product ( name , Ġdescription , Ġprice , Ġstock ) ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Name Is Missing _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( name Ġ= Ġnull )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Description Is Missing _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( description Ġ= Ġnull )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Price Is Missing _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( price Ġ= Ġnull )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Stock Is Missing _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( stock Ġ= Ġnull )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Name Is Blank _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( name Ġ= Ġ"")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen The Description Blank _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( description Ġ= Ġ"")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Price Is B ellow 0 _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( price Ġ= Ġ- 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen Stock Is B ellow 0 _ then Returns Status 4 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ( stock Ġ= Ġ- 1 )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is BadRequest ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġwhen All The Fields Are Valid _ then Returns Status 2 0 0 () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnew Product : ĠNew Product Ġ= Ġnew Product () ĊĠĠĠĠĠĠĠ Ġcreate Product ( new Product ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path ("$ ", Ġnot NullValue ())) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. id ", Ġnot NullValue ())) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. name ", Ġ` is `( new Product . name ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. description ", Ġ` is `( new Product . description ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. price ", Ġ` is `( new Product . price ))) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( json Path (" $. stock ", Ġ` is `( new Product . stock ))) ĊĠĠĠ Ġ} Ċ }