Advertisement
avinash8989

Untitled

Jul 6th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.67 KB | None | 0 0
  1. package com.hackerrank.eshopping.product.dashboard.controller;
  2.  
  3. import com.hackerrank.test.utility.Order;
  4. import com.hackerrank.test.utility.OrderedTestRunner;
  5. import com.hackerrank.test.utility.ResultMatcher;
  6. import static org.junit.Assert.assertTrue;
  7. import org.junit.ClassRule;
  8. import org.junit.Rule;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.test.context.junit4.rules.SpringClassRule;
  16. import org.springframework.test.context.junit4.rules.SpringMethodRule;
  17. import org.springframework.test.web.servlet.MockMvc;
  18. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  19. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  20. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
  21. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  22.  
  23. @RunWith(OrderedTestRunner.class)
  24. @SpringBootTest
  25. @AutoConfigureMockMvc
  26. public class ProductsControllerTest {
  27. @ClassRule
  28. public static final SpringClassRule springClassRule = new SpringClassRule();
  29.  
  30. @Rule
  31. public final SpringMethodRule springMethodRule = new SpringMethodRule();
  32.  
  33. @Autowired
  34. private MockMvc mockMvc;
  35.  
  36. /**
  37. *
  38. * @throws Exception
  39. *
  40. * It tests creating a product
  41. */
  42. @Test
  43. @Order(1)
  44. public void createProduct() throws Exception {
  45. /**
  46. *
  47. * Create product with id 1
  48. *
  49. * The request body is:
  50. * {
  51. * "id": 1,
  52. * "name": "Dressing Gown",
  53. * "category": "Full Body Outfits",
  54. * "retail_price": 303.0,
  55. * "discounted_price": 251.49,
  56. * "availability": true
  57. * }
  58. */
  59. String json = "{\"id\": 1, \"name\": \"Dressing Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 303.0, \"discounted_price\": 251.49, \"availability\": true}";
  60.  
  61. mockMvc.perform(
  62. post("/products")
  63. .contentType(MediaType.APPLICATION_JSON_UTF8)
  64. .content(json)
  65. )
  66. .andExpect(status().isCreated());
  67.  
  68. /**
  69. *
  70. * Create product with id 2
  71. *
  72. * The request body is:
  73. * {
  74. * "id": 2,
  75. * "name": "Shoes",
  76. * "category": "Footwear",
  77. * "retail_price": 150.0,
  78. * "discounted_price": 123.0,
  79. * "availability": true
  80. * }
  81. */
  82. json = "{\"id\": 2, \"name\": \"Shoes\", \"category\": \"Footwear\", \"retail_price\": 150.0, \"discounted_price\": 123.0, \"availability\": true}";
  83.  
  84. mockMvc.perform(
  85. post("/products")
  86. .contentType(MediaType.APPLICATION_JSON_UTF8)
  87. .content(json)
  88. )
  89. .andExpect(status().isCreated());
  90.  
  91. /**
  92. *
  93. * Create product with id 3
  94. *
  95. * The request body is:
  96. * {
  97. * "id": 3,
  98. * "name": "Nightgown",
  99. * "category": "Full Body Outfits",
  100. * "retail_price": 307.0,
  101. * "discounted_price": 254.81,
  102. * "availability": true
  103. * }
  104. */
  105. json = "{\"id\": 3, \"name\": \"Nightgown\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}";
  106.  
  107. mockMvc.perform(
  108. post("/products")
  109. .contentType(MediaType.APPLICATION_JSON_UTF8)
  110. .content(json)
  111. )
  112. .andExpect(status().isCreated());
  113.  
  114. /**
  115. *
  116. * Create product with id 4
  117. *
  118. * The request body is:
  119. * {
  120. * "id": 4,
  121. * "name": "Boots",
  122. * "category": "Footwear",
  123. * "retail_price": 162.0,
  124. * "discounted_price": 132.84,
  125. * "availability": true
  126. * }
  127. */
  128. json = "{\"id\": 4, \"name\": \"Boots\", \"category\": \"Footwear\", \"retail_price\": 162.0, \"discounted_price\": 132.84, \"availability\": true}";
  129.  
  130. mockMvc.perform(
  131. post("/products")
  132. .contentType(MediaType.APPLICATION_JSON_UTF8)
  133. .content(json)
  134. )
  135. .andExpect(status().isCreated());
  136.  
  137. /**
  138. *
  139. * Create product with id 5
  140. *
  141. * The request body is:
  142. * {
  143. * "id": 5,
  144. * "name": "Ball Gown",
  145. * "category": "Full Body Outfits",
  146. * "retail_price": 337.0,
  147. * "discounted_price": 272.97,
  148. * "availability": true
  149. * }
  150. */
  151. json = "{\"id\": 5, \"name\": \"Ball Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 337.0, \"discounted_price\": 272.97, \"availability\": true}";
  152.  
  153. mockMvc.perform(
  154. post("/products")
  155. .contentType(MediaType.APPLICATION_JSON_UTF8)
  156. .content(json)
  157. )
  158. .andExpect(status().isCreated());
  159.  
  160. /**
  161. *
  162. * Create product with id 6
  163. *
  164. * The request body is:
  165. * {
  166. * "id": 6,
  167. * "name": "Shawl",
  168. * "category": "Accessories",
  169. * "retail_price": 283.0,
  170. * "discounted_price": 260.36,
  171. * "availability": true
  172. * }
  173. */
  174. json = "{\"id\": 6, \"name\": \"Shawl\", \"category\": \"Accessories\", \"retail_price\": 283.0, \"discounted_price\": 260.36, \"availability\": true}";
  175.  
  176. mockMvc.perform(
  177. post("/products")
  178. .contentType(MediaType.APPLICATION_JSON_UTF8)
  179. .content(json)
  180. )
  181. .andExpect(status().isCreated());
  182.  
  183. /**
  184. *
  185. * Create product with id 7
  186. *
  187. * The request body is:
  188. * {
  189. * "id": 7,
  190. * "name": "Belt",
  191. * "category": "Accessories",
  192. * "retail_price": 471.0,
  193. * "discounted_price": 419.19,
  194. * "availability": true
  195. * }
  196. */
  197. json = "{\"id\": 7, \"name\": \"Belt\", \"category\": \"Accessories\", \"retail_price\": 471.0, \"discounted_price\": 419.19, \"availability\": true}";
  198.  
  199. mockMvc.perform(
  200. post("/products")
  201. .contentType(MediaType.APPLICATION_JSON_UTF8)
  202. .content(json)
  203. )
  204. .andExpect(status().isCreated());
  205.  
  206. /**
  207. *
  208. * Create product with id 8
  209. *
  210. * The request body is:
  211. * {
  212. * "id": 8,
  213. * "name": "Kaftan",
  214. * "category": "Accessories",
  215. * "retail_price": 237.0,
  216. * "discounted_price": 215.67,
  217. * "availability": true
  218. * }
  219. */
  220. json = "{\"id\": 8, \"name\": \"Kaftan\", \"category\": \"Accessories\", \"retail_price\": 237.0, \"discounted_price\": 215.67, \"availability\": true}";
  221.  
  222. mockMvc.perform(
  223. post("/products")
  224. .contentType(MediaType.APPLICATION_JSON_UTF8)
  225. .content(json)
  226. )
  227. .andExpect(status().isCreated());
  228.  
  229. /**
  230. *
  231. * Create product with id 9
  232. *
  233. * The request body is:
  234. * {
  235. * "id": 9,
  236. * "name": "Overalls",
  237. * "category": "Full Body Outfits",
  238. * "retail_price": 374.0,
  239. * "discounted_price": 321.64,
  240. * "availability": true
  241. * }
  242. */
  243. json = "{\"id\": 9, \"name\": \"Overalls\", \"category\": \"Full Body Outfits\", \"retail_price\": 374.0, \"discounted_price\": 321.64, \"availability\": true}";
  244.  
  245. mockMvc.perform(
  246. post("/products")
  247. .contentType(MediaType.APPLICATION_JSON_UTF8)
  248. .content(json)
  249. )
  250. .andExpect(status().isCreated());
  251.  
  252. /**
  253. *
  254. * Create product with id 10
  255. *
  256. * The request body is:
  257. * {
  258. * "id": 10,
  259. * "name": "Cufflinks",
  260. * "category": "Accessories",
  261. * "retail_price": 284.0,
  262. * "discounted_price": 247.08,
  263. * "availability": true
  264. * }
  265. */
  266. json = "{\"id\": 10, \"name\": \"Cufflinks\", \"category\": \"Accessories\", \"retail_price\": 284.0, \"discounted_price\": 247.08, \"availability\": true}";
  267.  
  268. mockMvc.perform(
  269. post("/products")
  270. .contentType(MediaType.APPLICATION_JSON_UTF8)
  271. .content(json)
  272. )
  273. .andExpect(status().isCreated());
  274.  
  275. /**
  276. *
  277. * Create product with id 11
  278. *
  279. * The request body is:
  280. * {
  281. * "id": 11,
  282. * "name": "Cargos",
  283. * "category": "Bottoms",
  284. * "retail_price": 498.0,
  285. * "discounted_price": 428.28,
  286. * "availability": true
  287. * }
  288. */
  289. json = "{\"id\": 11, \"name\": \"Cargos\", \"category\": \"Bottoms\", \"retail_price\": 498.0, \"discounted_price\": 428.28, \"availability\": true}";
  290.  
  291. mockMvc.perform(
  292. post("/products")
  293. .contentType(MediaType.APPLICATION_JSON_UTF8)
  294. .content(json)
  295. )
  296. .andExpect(status().isCreated());
  297.  
  298. /**
  299. *
  300. * Create product with id 12
  301. *
  302. * The request body is:
  303. * {
  304. * "id": 12,
  305. * "name": "Poncho",
  306. * "category": "Accessories",
  307. * "retail_price": 280.0,
  308. * "discounted_price": 224.7,
  309. * "availability": true
  310. * }
  311. */
  312. json = "{\"id\": 12, \"name\": \"Poncho\", \"category\": \"Accessories\", \"retail_price\": 280.0, \"discounted_price\": 224.7, \"availability\": true}";
  313.  
  314. mockMvc.perform(
  315. post("/products")
  316. .contentType(MediaType.APPLICATION_JSON_UTF8)
  317. .content(json)
  318. )
  319. .andExpect(status().isCreated());
  320.  
  321. /**
  322. *
  323. * Create product with id 13
  324. *
  325. * The request body is:
  326. * {
  327. * "id": 13,
  328. * "name": "Cummerbund",
  329. * "category": "Accessories",
  330. * "retail_price": 435.0,
  331. * "discounted_price": 400.2,
  332. * "availability": true
  333. * }
  334. */
  335. json = "{\"id\": 13, \"name\": \"Cummerbund\", \"category\": \"Accessories\", \"retail_price\": 435.0, \"discounted_price\": 400.2, \"availability\": true}";
  336.  
  337. mockMvc.perform(
  338. post("/products")
  339. .contentType(MediaType.APPLICATION_JSON_UTF8)
  340. .content(json)
  341. )
  342. .andExpect(status().isCreated());
  343.  
  344. /**
  345. *
  346. * Create product with id 14
  347. *
  348. * The request body is:
  349. * {
  350. * "id": 14,
  351. * "name": "Dress",
  352. * "category": "Full Body Outfits",
  353. * "retail_price": 175.0,
  354. * "discounted_price": 140.0,
  355. * "availability": true
  356. * }
  357. */
  358. json = "{\"id\": 14, \"name\": \"Dress\", \"category\": \"Full Body Outfits\", \"retail_price\": 175.0, \"discounted_price\": 140.0, \"availability\": true}";
  359.  
  360. mockMvc.perform(
  361. post("/products")
  362. .contentType(MediaType.APPLICATION_JSON_UTF8)
  363. .content(json)
  364. )
  365. .andExpect(status().isCreated());
  366.  
  367. /**
  368. *
  369. * Create product with id 15
  370. *
  371. * The request body is:
  372. * {
  373. * "id": 15,
  374. * "name": "Trainers",
  375. * "category": "Footwear",
  376. * "retail_price": 228.0,
  377. * "discounted_price": 184.68,
  378. * "availability": true
  379. * }
  380. */
  381. json = "{\"id\": 15, \"name\": \"Trainers\", \"category\": \"Footwear\", \"retail_price\": 228.0, \"discounted_price\": 184.68, \"availability\": true}";
  382.  
  383. mockMvc.perform(
  384. post("/products")
  385. .contentType(MediaType.APPLICATION_JSON_UTF8)
  386. .content(json)
  387. )
  388. .andExpect(status().isCreated());
  389.  
  390. /**
  391. *
  392. * Create product with id 16
  393. *
  394. * The request body is:
  395. * {
  396. * "id": 16,
  397. * "name": "Tracksuit",
  398. * "category": "Full Body Outfits",
  399. * "retail_price": 471.0,
  400. * "discounted_price": 423.9,
  401. * "availability": false
  402. * }
  403. */
  404. json = "{\"id\": 16, \"name\": \"Tracksuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 471.0, \"discounted_price\": 423.9, \"availability\": false}";
  405.  
  406. mockMvc.perform(
  407. post("/products")
  408. .contentType(MediaType.APPLICATION_JSON_UTF8)
  409. .content(json)
  410. )
  411. .andExpect(status().isCreated());
  412.  
  413. /**
  414. *
  415. * Create product with id 17
  416. *
  417. * The request body is:
  418. * {
  419. * "id": 17,
  420. * "name": "Tailcoat",
  421. * "category": "Full Body Outfits",
  422. * "retail_price": 307.0,
  423. * "discounted_price": 254.81,
  424. * "availability": true
  425. * }
  426. */
  427. json = "{\"id\": 17, \"name\": \"Tailcoat\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}";
  428.  
  429. mockMvc.perform(
  430. post("/products")
  431. .contentType(MediaType.APPLICATION_JSON_UTF8)
  432. .content(json)
  433. )
  434. .andExpect(status().isCreated());
  435.  
  436. /**
  437. *
  438. * Create product with id 18
  439. *
  440. * The request body is:
  441. * {
  442. * "id": 18,
  443. * "name": "Vest",
  444. * "category": "Tops",
  445. * "retail_price": 446.0,
  446. * "discounted_price": 392.48,
  447. * "availability": true
  448. * }
  449. */
  450. json = "{\"id\": 18, \"name\": \"Vest\", \"category\": \"Tops\", \"retail_price\": 446.0, \"discounted_price\": 392.48, \"availability\": true}";
  451.  
  452. mockMvc.perform(
  453. post("/products")
  454. .contentType(MediaType.APPLICATION_JSON_UTF8)
  455. .content(json)
  456. )
  457. .andExpect(status().isCreated());
  458.  
  459. /**
  460. *
  461. * Create product with id 19
  462. *
  463. * The request body is:
  464. * {
  465. * "id": 19,
  466. * "name": "Suit",
  467. * "category": "Full Body Outfits",
  468. * "retail_price": 158.0,
  469. * "discounted_price": 135.88,
  470. * "availability": true
  471. * }
  472. */
  473. json = "{\"id\": 19, \"name\": \"Suit\", \"category\": \"Full Body Outfits\", \"retail_price\": 158.0, \"discounted_price\": 135.88, \"availability\": true}";
  474.  
  475. mockMvc.perform(
  476. post("/products")
  477. .contentType(MediaType.APPLICATION_JSON_UTF8)
  478. .content(json)
  479. )
  480. .andExpect(status().isCreated());
  481.  
  482. /**
  483. *
  484. * Create product with id 20
  485. *
  486. * The request body is:
  487. * {
  488. * "id": 20,
  489. * "name": "Catsuit",
  490. * "category": "Full Body Outfits",
  491. * "retail_price": 158.0,
  492. * "discounted_price": 135.88,
  493. * "availability": true
  494. * }
  495. */
  496. json = "{\"id\": 20, \"name\": \"Catsuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 158.0, \"discounted_price\": 135.88, \"availability\": true}";
  497.  
  498. mockMvc.perform(
  499. post("/products")
  500. .contentType(MediaType.APPLICATION_JSON_UTF8)
  501. .content(json)
  502. )
  503. .andExpect(status().isCreated());
  504.  
  505. /**
  506. *
  507. * Create product with id 21
  508. *
  509. * The request body is:
  510. * {
  511. * "id": 21,
  512. * "name": "Dungarees",
  513. * "category": "Full Body Outfits",
  514. * "retail_price": 437.0,
  515. * "discounted_price": 362.71,
  516. * "availability": false
  517. * }
  518. */
  519. json = "{\"id\": 21, \"name\": \"Dungarees\", \"category\": \"Full Body Outfits\", \"retail_price\": 437.0, \"discounted_price\": 362.71, \"availability\": false}";
  520.  
  521. mockMvc.perform(
  522. post("/products")
  523. .contentType(MediaType.APPLICATION_JSON_UTF8)
  524. .content(json)
  525. )
  526. .andExpect(status().isCreated());
  527. }
  528.  
  529. /**
  530. *
  531. * @throws Exception
  532. *
  533. * It tests creating a product
  534. */
  535. @Test
  536. @Order(2)
  537. public void createProductWithExistingId() throws Exception {
  538. /**
  539. *
  540. * Create product with existing id 1
  541. *
  542. * The request body is:
  543. * {
  544. * "id": 1,
  545. * "name": "Dressing Gown",
  546. * "category": "Underwear",
  547. * "retail_price": 303.0,
  548. * "discounted_price": 251.49,
  549. * "availability": true
  550. * }
  551. */
  552. String json = "{\"id\": 1, \"name\": \"Dressing Gown\", \"category\": \"Underwear\", \"retail_price\": 303.0, \"discounted_price\": 251.49, \"availability\": true}";
  553.  
  554. mockMvc.perform(
  555. post("/products")
  556. .contentType(MediaType.APPLICATION_JSON_UTF8)
  557. .content(json)
  558. )
  559. .andExpect(status().isBadRequest());
  560. }
  561.  
  562. /**
  563. *
  564. * @throws Exception
  565. *
  566. * It tests updating retail price of a product
  567. */
  568. @Test
  569. @Order(3)
  570. public void updateProductRetailPrice() throws Exception {
  571. /**
  572. *
  573. * Update retail price of a product with id 6
  574. *
  575. * The request body is:
  576. * {
  577. * "retail_price": 325.45,
  578. * "discounted_price": 260.36,
  579. * "availability": true
  580. * }
  581. */
  582. String json = "{\"retail_price\": 325.45, \"discounted_price\": 260.36, \"availability\": true}";
  583.  
  584. mockMvc.perform(
  585. put("/products/6")
  586. .contentType(MediaType.APPLICATION_JSON_UTF8)
  587. .content(json)
  588. )
  589. .andExpect(status().isOk());
  590. }
  591.  
  592. /**
  593. *
  594. * @throws Exception
  595. *
  596. * It tests updating discount price of a product
  597. */
  598. @Test
  599. @Order(4)
  600. public void updateProductDiscountPrice() throws Exception {
  601. /**
  602. *
  603. * Update discount price of a product with id 10
  604. *
  605. * The request body is:
  606. * {
  607. * "retail_price": 284.0,
  608. * "discounted_price": 227.2,
  609. * "availability": true
  610. * }
  611. */
  612. String json = "{\"retail_price\": 284.0, \"discounted_price\": 227.2, \"availability\": true}";
  613.  
  614. mockMvc.perform(
  615. put("/products/10")
  616. .contentType(MediaType.APPLICATION_JSON_UTF8)
  617. .content(json)
  618. )
  619. .andExpect(status().isOk());
  620. }
  621.  
  622. /**
  623. *
  624. * @throws Exception
  625. *
  626. * It tests updating availability of a product
  627. */
  628. @Test
  629. @Order(5)
  630. public void updateProductAvailability() throws Exception {
  631. /**
  632. *
  633. * Update availability of a product with id 2
  634. *
  635. * The request body is:
  636. * {
  637. * "retail_price": 150.0,
  638. * "discounted_price": 123.0,
  639. * "availability": false
  640. * }
  641. */
  642. String json = "{\"retail_price\": 150.0, \"discounted_price\": 123.0, \"availability\": false}";
  643.  
  644. mockMvc.perform(
  645. put("/products/2")
  646. .contentType(MediaType.APPLICATION_JSON_UTF8)
  647. .content(json)
  648. )
  649. .andExpect(status().isOk());
  650. }
  651.  
  652. /**
  653. *
  654. * @throws Exception
  655. *
  656. * It tests updating retail and discount price of a product
  657. */
  658. @Test
  659. @Order(6)
  660. public void updateProductRetailAndDiscountPrice() throws Exception {
  661. /**
  662. *
  663. * Update retail and discount price of a product with id 12
  664. *
  665. * The request body is:
  666. * {
  667. * "retail_price": 350.0,
  668. * "discounted_price": 283.5,
  669. * "availability": true
  670. * }
  671. */
  672. String json = "{\"retail_price\": 350.0, \"discounted_price\": 283.5, \"availability\": true}";
  673.  
  674. mockMvc.perform(
  675. put("/products/12")
  676. .contentType(MediaType.APPLICATION_JSON_UTF8)
  677. .content(json)
  678. )
  679. .andExpect(status().isOk());
  680. }
  681.  
  682. /**
  683. *
  684. * @throws Exception
  685. *
  686. * It tests updating retail price and availability of a product
  687. */
  688. @Test
  689. @Order(7)
  690. public void updateProductRetailPriceAndAvailability() throws Exception {
  691. /**
  692. *
  693. * Update retail price and availability of a product with id 19
  694. *
  695. * The request body is:
  696. * {
  697. * "retail_price": 125.0,
  698. * "discounted_price": 100.0,
  699. * "availability": false
  700. * }
  701. */
  702. String json = "{\"retail_price\": 125.0, \"discounted_price\": 100.0, \"availability\": false}";
  703.  
  704. mockMvc.perform(
  705. put("/products/19")
  706. .contentType(MediaType.APPLICATION_JSON_UTF8)
  707. .content(json)
  708. )
  709. .andExpect(status().isOk());
  710. }
  711.  
  712. /**
  713. *
  714. * @throws Exception
  715. *
  716. * It tests updating discount price and availability of a product
  717. */
  718. @Test
  719. @Order(8)
  720. public void updateProductDiscountPriceAndAvailability() throws Exception {
  721. /**
  722. *
  723. * Update discount price and availability of a product with id 20
  724. *
  725. * The request body is:
  726. * {
  727. * "retail_price": 250.0,
  728. * "discounted_price": 200.0,
  729. * "availability": false
  730. * }
  731. */
  732. String json = "{\"retail_price\": 250.0, \"discounted_price\": 200.0, \"availability\": false}";
  733.  
  734. mockMvc.perform(
  735. put("/products/20")
  736. .contentType(MediaType.APPLICATION_JSON_UTF8)
  737. .content(json)
  738. )
  739. .andExpect(status().isOk());
  740. }
  741.  
  742. /**
  743. *
  744. * @throws Exception
  745. *
  746. * It tests updating retail price, discount price and availability of a product
  747. */
  748. @Test
  749. @Order(9)
  750. public void updateProductRetailPriceDiscountPriceAndAvailability() throws Exception {
  751. /**
  752. *
  753. * Update retail price, discount price and availability of a product with id 13
  754. *
  755. * The request body is:
  756. * {
  757. * "retail_price": 500.0,
  758. * "discounted_price": 450.0,
  759. * "availability": false
  760. * }
  761. */
  762. String json = "{\"retail_price\": 500.0, \"discounted_price\": 450.0, \"availability\": false}";
  763.  
  764. mockMvc.perform(
  765. put("/products/13")
  766. .contentType(MediaType.APPLICATION_JSON_UTF8)
  767. .content(json)
  768. )
  769. .andExpect(status().isOk());
  770. }
  771.  
  772. /**
  773. *
  774. * @throws Exception
  775. *
  776. * It tests updating availability of non-existing product
  777. */
  778. @Test
  779. @Order(10)
  780. public void updateAvailabilityOfNonExistingProduct() throws Exception {
  781. /**
  782. *
  783. * Update availability of a non existing product with id 25
  784. *
  785. * The request body is:
  786. * {
  787. * "retail_price": 311.0,
  788. * "discounted_price": 258.13,
  789. * "availability": true
  790. * }
  791. */
  792. String json = "{\"retail_price\": 311.0, \"discounted_price\": 258.13, \"availability\": true}";
  793.  
  794. mockMvc.perform(
  795. put("/products/25")
  796. .contentType(MediaType.APPLICATION_JSON_UTF8)
  797. .content(json)
  798. )
  799. .andExpect(status().isBadRequest());
  800. }
  801.  
  802. /**
  803. *
  804. * @throws Exception
  805. *
  806. * It tests finding a product
  807. */
  808. @Test
  809. @Order(11)
  810. public void findProductById() throws Exception {
  811. /**
  812. *
  813. * Find product by id 2
  814. *
  815. * The request response is:
  816. * {
  817. * "id": 2,
  818. * "name": "Shoes",
  819. * "category": "Footwear",
  820. * "retail_price": 150.0,
  821. * "discounted_price": 123.0,
  822. * "availability": false
  823. * }
  824. */
  825. String res = "{\"id\": 2, \"name\": \"Shoes\", \"category\": \"Footwear\", \"retail_price\": 150.0, \"discounted_price\": 123.0, \"availability\": false}";
  826.  
  827. assertTrue(
  828. ResultMatcher.matchJson(
  829. mockMvc.perform(get("/products/2"))
  830. .andExpect(status().isOk())
  831. .andReturn()
  832. .getResponse()
  833. .getContentAsString(),
  834. res,
  835. true
  836. )
  837. );
  838. }
  839.  
  840. /**
  841. *
  842. * @throws Exception
  843. *
  844. * It tests finding a product
  845. */
  846. @Test
  847. @Order(12)
  848. public void findProductByNonExistingId() throws Exception {
  849. /**
  850. *
  851. * Find product by non-existing id 25
  852. */
  853. mockMvc.perform(get("/products/25"))
  854. .andExpect(status().isNotFound());
  855. }
  856.  
  857. /**
  858. *
  859. * @throws Exception
  860. *
  861. * It tests finding products belonging to a category
  862. */
  863. @Test
  864. @Order(13)
  865. public void findProductsByCategory() throws Exception {
  866. /**
  867. *
  868. * Find products belonging to Accesories category
  869. *
  870. * The request response is:
  871. * [
  872. * {
  873. * "id": 8,
  874. * "name": "Kaftan",
  875. * "category": "Accessories",
  876. * "retail_price": 237.0,
  877. * "discounted_price": 215.67,
  878. * "availability": true
  879. * },
  880. * {
  881. * "id": 10,
  882. * "name": "Cufflinks",
  883. * "category": "Accessories",
  884. * "retail_price": 284.0,
  885. * "discounted_price": 227.2,
  886. * "availability": true
  887. * },
  888. * {
  889. * "id": 6,
  890. * "name": "Shawl",
  891. * "category": "Accessories",
  892. * "retail_price": 325.45,
  893. * "discounted_price": 260.36,
  894. * "availability": true
  895. * },
  896. * {
  897. * "id": 12,
  898. * "name": "Poncho",
  899. * "category": "Accessories",
  900. * "retail_price": 350.0,
  901. * "discounted_price": 283.5,
  902. * "availability": true
  903. * },
  904. * {
  905. * "id": 7,
  906. * "name": "Belt",
  907. * "category": "Accessories",
  908. * "retail_price": 471.0,
  909. * "discounted_price": 419.19,
  910. * "availability": true
  911. * },
  912. * {
  913. * "id": 13,
  914. * "name": "Cummerbund",
  915. * "category": "Accessories",
  916. * "retail_price": 500.0,
  917. * "discounted_price": 450.0,
  918. * "availability": false
  919. * }
  920. * ]
  921. */
  922. String res = "[{\"id\": 8, \"name\": \"Kaftan\", \"category\": \"Accessories\", \"retail_price\": 237.0, \"discounted_price\": 215.67, \"availability\": true}, {\"id\": 10, \"name\": \"Cufflinks\", \"category\": \"Accessories\", \"retail_price\": 284.0, \"discounted_price\": 227.2, \"availability\": true}, {\"id\": 6, \"name\": \"Shawl\", \"category\": \"Accessories\", \"retail_price\": 325.45, \"discounted_price\": 260.36, \"availability\": true}, {\"id\": 12, \"name\": \"Poncho\", \"category\": \"Accessories\", \"retail_price\": 350.0, \"discounted_price\": 283.5, \"availability\": true}, {\"id\": 7, \"name\": \"Belt\", \"category\": \"Accessories\", \"retail_price\": 471.0, \"discounted_price\": 419.19, \"availability\": true}, {\"id\": 13, \"name\": \"Cummerbund\", \"category\": \"Accessories\", \"retail_price\": 500.0, \"discounted_price\": 450.0, \"availability\": false}]";
  923.  
  924. assertTrue(
  925. ResultMatcher.matchJsonArray(
  926. mockMvc.perform(get("/products?category=Accessories"))
  927. .andExpect(status().isOk())
  928. .andReturn()
  929. .getResponse()
  930. .getContentAsString(),
  931. res,
  932. true
  933. )
  934. );
  935. }
  936.  
  937. /**
  938. *
  939. * @throws Exception
  940. *
  941. * It tests finding products belonging to a category
  942. */
  943. @Test
  944. @Order(14)
  945. public void findProductsByNonExistingCategory() throws Exception {
  946. /**
  947. *
  948. * Find products belonging to Swimwear category
  949. *
  950. * The request response is:
  951. * []
  952. */
  953. String res = "[]";
  954.  
  955. assertTrue(
  956. ResultMatcher.matchJsonArray(
  957. mockMvc.perform(get("/products?category=Swimwear"))
  958. .andExpect(status().isOk())
  959. .andReturn()
  960. .getResponse()
  961. .getContentAsString(),
  962. res,
  963. true
  964. )
  965. );
  966. }
  967.  
  968. /**
  969. *
  970. * @throws Exception
  971. *
  972. * It tests finding a product with a given category and availability
  973. */
  974. @Test
  975. @Order(15)
  976. public void findProductsByCategoryAndAvailability() throws Exception {
  977. /**
  978. *
  979. * Find products belonging to a given category and with availability
  980. *
  981. * The request response is:
  982. * [
  983. * {
  984. * "id": 14,
  985. * "name": "Dress",
  986. * "category": "Full Body Outfits",
  987. * "retail_price": 175.0,
  988. * "discounted_price": 140.0,
  989. * "availability": true
  990. * },
  991. * {
  992. * "id": 5,
  993. * "name": "Ball Gown",
  994. * "category": "Full Body Outfits",
  995. * "retail_price": 337.0,
  996. * "discounted_price": 272.97,
  997. * "availability": true
  998. * },
  999. * {
  1000. * "id": 1,
  1001. * "name": "Dressing Gown",
  1002. * "category": "Full Body Outfits",
  1003. * "retail_price": 303.0,
  1004. * "discounted_price": 251.49,
  1005. * "availability": true
  1006. * },
  1007. * {
  1008. * "id": 3,
  1009. * "name": "Nightgown",
  1010. * "category": "Full Body Outfits",
  1011. * "retail_price": 307.0,
  1012. * "discounted_price": 254.81,
  1013. * "availability": true
  1014. * },
  1015. * {
  1016. * "id": 17,
  1017. * "name": "Tailcoat",
  1018. * "category": "Full Body Outfits",
  1019. * "retail_price": 307.0,
  1020. * "discounted_price": 254.81,
  1021. * "availability": true
  1022. * },
  1023. * {
  1024. * "id": 9,
  1025. * "name": "Overalls",
  1026. * "category": "Full Body Outfits",
  1027. * "retail_price": 374.0,
  1028. * "discounted_price": 321.64,
  1029. * "availability": true
  1030. * }
  1031. * ]
  1032. */
  1033. String res = "[{\"id\": 14, \"name\": \"Dress\", \"category\": \"Full Body Outfits\", \"retail_price\": 175.0, \"discounted_price\": 140.0, \"availability\": true}, {\"id\": 5, \"name\": \"Ball Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 337.0, \"discounted_price\": 272.97, \"availability\": true}, {\"id\": 1, \"name\": \"Dressing Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 303.0, \"discounted_price\": 251.49, \"availability\": true}, {\"id\": 3, \"name\": \"Nightgown\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}, {\"id\": 17, \"name\": \"Tailcoat\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}, {\"id\": 9, \"name\": \"Overalls\", \"category\": \"Full Body Outfits\", \"retail_price\": 374.0, \"discounted_price\": 321.64, \"availability\": true}]";
  1034.  
  1035. assertTrue(
  1036. ResultMatcher.matchJsonArray(
  1037. mockMvc.perform(get("/products?category=Full%20Body%20Outfits&availability=1"))
  1038. .andExpect(status().isOk())
  1039. .andReturn()
  1040. .getResponse()
  1041. .getContentAsString(),
  1042. res,
  1043. true
  1044. )
  1045. );
  1046.  
  1047. /**
  1048. *
  1049. * Find products belonging to a given category and with availability
  1050. *
  1051. * The request response is:
  1052. * [
  1053. * {
  1054. * "id": 19,
  1055. * "name": "Suit",
  1056. * "category": "Full Body Outfits",
  1057. * "retail_price": 125.0,
  1058. * "discounted_price": 100.0,
  1059. * "availability": false
  1060. * },
  1061. * {
  1062. * "id": 20,
  1063. * "name": "Catsuit",
  1064. * "category": "Full Body Outfits",
  1065. * "retail_price": 250.0,
  1066. * "discounted_price": 200.0,
  1067. * "availability": false
  1068. * },
  1069. * {
  1070. * "id": 21,
  1071. * "name": "Dungarees",
  1072. * "category": "Full Body Outfits",
  1073. * "retail_price": 437.0,
  1074. * "discounted_price": 362.71,
  1075. * "availability": false
  1076. * },
  1077. * {
  1078. * "id": 16,
  1079. * "name": "Tracksuit",
  1080. * "category": "Full Body Outfits",
  1081. * "retail_price": 471.0,
  1082. * "discounted_price": 423.9,
  1083. * "availability": false
  1084. * }
  1085. * ]
  1086. */
  1087. res = "[{\"id\": 19, \"name\": \"Suit\", \"category\": \"Full Body Outfits\", \"retail_price\": 125.0, \"discounted_price\": 100.0, \"availability\": false}, {\"id\": 20, \"name\": \"Catsuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 250.0, \"discounted_price\": 200.0, \"availability\": false}, {\"id\": 21, \"name\": \"Dungarees\", \"category\": \"Full Body Outfits\", \"retail_price\": 437.0, \"discounted_price\": 362.71, \"availability\": false}, {\"id\": 16, \"name\": \"Tracksuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 471.0, \"discounted_price\": 423.9, \"availability\": false}]";
  1088.  
  1089. assertTrue(
  1090. ResultMatcher.matchJsonArray(
  1091. mockMvc.perform(get("/products?category=Full%20Body%20Outfits&availability=0"))
  1092. .andExpect(status().isOk())
  1093. .andReturn()
  1094. .getResponse()
  1095. .getContentAsString(),
  1096. res,
  1097. true
  1098. )
  1099. );
  1100. }
  1101.  
  1102. /**
  1103. *
  1104. * @throws Exception
  1105. *
  1106. * It tests finding a product with a given category and availability
  1107. */
  1108. @Test
  1109. @Order(16)
  1110. public void findProductsByNonExistingCategoryAndAvailability() throws Exception {
  1111. /**
  1112. *
  1113. * Find products belonging to a given category and with availability
  1114. *
  1115. * The request response is:
  1116. * []
  1117. */
  1118. String res = "[]";
  1119.  
  1120. assertTrue(
  1121. ResultMatcher.matchJsonArray(
  1122. mockMvc.perform(get("/products?category=Swimwear&availability=1"))
  1123. .andExpect(status().isOk())
  1124. .andReturn()
  1125. .getResponse()
  1126. .getContentAsString(),
  1127. res,
  1128. true
  1129. )
  1130. );
  1131. }
  1132.  
  1133. /**
  1134. *
  1135. * @throws Exception
  1136. *
  1137. * It tests finding all products
  1138. */
  1139. @Test
  1140. @Order(17)
  1141. public void findAllProducts() throws Exception {
  1142. /**
  1143. *
  1144. * Find all products
  1145. *
  1146. * The request response is:
  1147. * [
  1148. * {
  1149. * "id": 1,
  1150. * "name": "Dressing Gown",
  1151. * "category": "Full Body Outfits",
  1152. * "retail_price": 303.0,
  1153. * "discounted_price": 251.49,
  1154. * "availability": true
  1155. * },
  1156. * {
  1157. * "id": 2,
  1158. * "name": "Shoes",
  1159. * "category": "Footwear",
  1160. * "retail_price": 150.0,
  1161. * "discounted_price": 123.0,
  1162. * "availability": false
  1163. * },
  1164. * {
  1165. * "id": 3,
  1166. * "name": "Nightgown",
  1167. * "category": "Full Body Outfits",
  1168. * "retail_price": 307.0,
  1169. * "discounted_price": 254.81,
  1170. * "availability": true
  1171. * },
  1172. * {
  1173. * "id": 4,
  1174. * "name": "Boots",
  1175. * "category": "Footwear",
  1176. * "retail_price": 162.0,
  1177. * "discounted_price": 132.84,
  1178. * "availability": true
  1179. * },
  1180. * {
  1181. * "id": 5,
  1182. * "name": "Ball Gown",
  1183. * "category": "Full Body Outfits",
  1184. * "retail_price": 337.0,
  1185. * "discounted_price": 272.97,
  1186. * "availability": true
  1187. * },
  1188. * {
  1189. * "id": 6,
  1190. * "name": "Shawl",
  1191. * "category": "Accessories",
  1192. * "retail_price": 325.45,
  1193. * "discounted_price": 260.36,
  1194. * "availability": true
  1195. * },
  1196. * {
  1197. * "id": 7,
  1198. * "name": "Belt",
  1199. * "category": "Accessories",
  1200. * "retail_price": 471.0,
  1201. * "discounted_price": 419.19,
  1202. * "availability": true
  1203. * },
  1204. * {
  1205. * "id": 8,
  1206. * "name": "Kaftan",
  1207. * "category": "Accessories",
  1208. * "retail_price": 237.0,
  1209. * "discounted_price": 215.67,
  1210. * "availability": true
  1211. * },
  1212. * {
  1213. * "id": 9,
  1214. * "name": "Overalls",
  1215. * "category": "Full Body Outfits",
  1216. * "retail_price": 374.0,
  1217. * "discounted_price": 321.64,
  1218. * "availability": true
  1219. * },
  1220. * {
  1221. * "id": 10,
  1222. * "name": "Cufflinks",
  1223. * "category": "Accessories",
  1224. * "retail_price": 284.0,
  1225. * "discounted_price": 227.2,
  1226. * "availability": true
  1227. * },
  1228. * {
  1229. * "id": 11,
  1230. * "name": "Cargos",
  1231. * "category": "Bottoms",
  1232. * "retail_price": 498.0,
  1233. * "discounted_price": 428.28,
  1234. * "availability": true
  1235. * },
  1236. * {
  1237. * "id": 12,
  1238. * "name": "Poncho",
  1239. * "category": "Accessories",
  1240. * "retail_price": 350.0,
  1241. * "discounted_price": 283.5,
  1242. * "availability": true
  1243. * },
  1244. * {
  1245. * "id": 13,
  1246. * "name": "Cummerbund",
  1247. * "category": "Accessories",
  1248. * "retail_price": 500.0,
  1249. * "discounted_price": 450.0,
  1250. * "availability": false
  1251. * },
  1252. * {
  1253. * "id": 14,
  1254. * "name": "Dress",
  1255. * "category": "Full Body Outfits",
  1256. * "retail_price": 175.0,
  1257. * "discounted_price": 140.0,
  1258. * "availability": true
  1259. * },
  1260. * {
  1261. * "id": 15,
  1262. * "name": "Trainers",
  1263. * "category": "Footwear",
  1264. * "retail_price": 228.0,
  1265. * "discounted_price": 184.68,
  1266. * "availability": true
  1267. * },
  1268. * {
  1269. * "id": 16,
  1270. * "name": "Tracksuit",
  1271. * "category": "Full Body Outfits",
  1272. * "retail_price": 471.0,
  1273. * "discounted_price": 423.9,
  1274. * "availability": false
  1275. * },
  1276. * {
  1277. * "id": 17,
  1278. * "name": "Tailcoat",
  1279. * "category": "Full Body Outfits",
  1280. * "retail_price": 307.0,
  1281. * "discounted_price": 254.81,
  1282. * "availability": true
  1283. * },
  1284. * {
  1285. * "id": 18,
  1286. * "name": "Vest",
  1287. * "category": "Tops",
  1288. * "retail_price": 446.0,
  1289. * "discounted_price": 392.48,
  1290. * "availability": true
  1291. * },
  1292. * {
  1293. * "id": 19,
  1294. * "name": "Suit",
  1295. * "category": "Full Body Outfits",
  1296. * "retail_price": 125.0,
  1297. * "discounted_price": 100.0,
  1298. * "availability": false
  1299. * },
  1300. * {
  1301. * "id": 20,
  1302. * "name": "Catsuit",
  1303. * "category": "Full Body Outfits",
  1304. * "retail_price": 250.0,
  1305. * "discounted_price": 200.0,
  1306. * "availability": false
  1307. * },
  1308. * {
  1309. * "id": 21,
  1310. * "name": "Dungarees",
  1311. * "category": "Full Body Outfits",
  1312. * "retail_price": 437.0,
  1313. * "discounted_price": 362.71,
  1314. * "availability": false
  1315. * }
  1316. * ]
  1317. */
  1318. String res = "[{\"id\": 1, \"name\": \"Dressing Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 303.0, \"discounted_price\": 251.49, \"availability\": true}, {\"id\": 2, \"name\": \"Shoes\", \"category\": \"Footwear\", \"retail_price\": 150.0, \"discounted_price\": 123.0, \"availability\": false}, {\"id\": 3, \"name\": \"Nightgown\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}, {\"id\": 4, \"name\": \"Boots\", \"category\": \"Footwear\", \"retail_price\": 162.0, \"discounted_price\": 132.84, \"availability\": true}, {\"id\": 5, \"name\": \"Ball Gown\", \"category\": \"Full Body Outfits\", \"retail_price\": 337.0, \"discounted_price\": 272.97, \"availability\": true}, {\"id\": 6, \"name\": \"Shawl\", \"category\": \"Accessories\", \"retail_price\": 325.45, \"discounted_price\": 260.36, \"availability\": true}, {\"id\": 7, \"name\": \"Belt\", \"category\": \"Accessories\", \"retail_price\": 471.0, \"discounted_price\": 419.19, \"availability\": true}, {\"id\": 8, \"name\": \"Kaftan\", \"category\": \"Accessories\", \"retail_price\": 237.0, \"discounted_price\": 215.67, \"availability\": true}, {\"id\": 9, \"name\": \"Overalls\", \"category\": \"Full Body Outfits\", \"retail_price\": 374.0, \"discounted_price\": 321.64, \"availability\": true}, {\"id\": 10, \"name\": \"Cufflinks\", \"category\": \"Accessories\", \"retail_price\": 284.0, \"discounted_price\": 227.2, \"availability\": true}, {\"id\": 11, \"name\": \"Cargos\", \"category\": \"Bottoms\", \"retail_price\": 498.0, \"discounted_price\": 428.28, \"availability\": true}, {\"id\": 12, \"name\": \"Poncho\", \"category\": \"Accessories\", \"retail_price\": 350.0, \"discounted_price\": 283.5, \"availability\": true}, {\"id\": 13, \"name\": \"Cummerbund\", \"category\": \"Accessories\", \"retail_price\": 500.0, \"discounted_price\": 450.0, \"availability\": false}, {\"id\": 14, \"name\": \"Dress\", \"category\": \"Full Body Outfits\", \"retail_price\": 175.0, \"discounted_price\": 140.0, \"availability\": true}, {\"id\": 15, \"name\": \"Trainers\", \"category\": \"Footwear\", \"retail_price\": 228.0, \"discounted_price\": 184.68, \"availability\": true}, {\"id\": 16, \"name\": \"Tracksuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 471.0, \"discounted_price\": 423.9, \"availability\": false}, {\"id\": 17, \"name\": \"Tailcoat\", \"category\": \"Full Body Outfits\", \"retail_price\": 307.0, \"discounted_price\": 254.81, \"availability\": true}, {\"id\": 18, \"name\": \"Vest\", \"category\": \"Tops\", \"retail_price\": 446.0, \"discounted_price\": 392.48, \"availability\": true}, {\"id\": 19, \"name\": \"Suit\", \"category\": \"Full Body Outfits\", \"retail_price\": 125.0, \"discounted_price\": 100.0, \"availability\": false}, {\"id\": 20, \"name\": \"Catsuit\", \"category\": \"Full Body Outfits\", \"retail_price\": 250.0, \"discounted_price\": 200.0, \"availability\": false}, {\"id\": 21, \"name\": \"Dungarees\", \"category\": \"Full Body Outfits\", \"retail_price\": 437.0, \"discounted_price\": 362.71, \"availability\": false}]";
  1319.  
  1320. assertTrue(
  1321. ResultMatcher.matchJsonArray(
  1322. mockMvc.perform(get("/products"))
  1323. .andExpect(status().isOk())
  1324. .andReturn()
  1325. .getResponse()
  1326. .getContentAsString(),
  1327. res,
  1328. true
  1329. )
  1330. );
  1331. }
  1332.  
  1333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement