1+ package com .sample .ecommerce .order ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+ import static org .mockito .ArgumentMatchers .any ;
6+ import static org .mockito .ArgumentMatchers .anyInt ;
7+
8+ import java .util .Optional ;
9+ import java .util .ArrayList ;
10+ import java .util .List ;
11+
12+ import org .junit .jupiter .api .DisplayName ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .mockito .Mockito ;
15+ import org .springframework .beans .factory .annotation .Autowired ;
16+ import org .springframework .boot .test .context .SpringBootTest ;
17+ import org .springframework .boot .test .mock .mockito .MockBean ;
18+ import org .springframework .test .context .ActiveProfiles ;
19+
20+ import com .sample .ecommerce .order .constants .OrderStatus ;
21+ import com .sample .ecommerce .order .dto .OrderDto ;
22+ import com .sample .ecommerce .order .dto .OrderItemDto ;
23+ import com .sample .ecommerce .order .exception .ObjectNotFoundException ;
24+ import com .sample .ecommerce .order .model .Category ;
25+ import com .sample .ecommerce .order .model .Customer ;
26+ import com .sample .ecommerce .order .model .Orders ;
27+ import com .sample .ecommerce .order .model .OrderItem ;
28+ import com .sample .ecommerce .order .model .Product ;
29+ import com .sample .ecommerce .order .repository .CustomerRepository ;
30+ import com .sample .ecommerce .order .repository .OrderRepository ;
31+ import com .sample .ecommerce .order .repository .ProductRepository ;
32+ import com .sample .ecommerce .order .service .OrderService ;
33+
34+ @ SpringBootTest
35+ @ ActiveProfiles ("test" )
36+ public class OrderServiceTests {
37+
38+ @ MockBean
39+ private OrderRepository orderRepository ;
40+
41+ @ MockBean
42+ private ProductRepository productRepository ;
43+
44+ @ MockBean
45+ private CustomerRepository customerRepository ;
46+
47+ @ Autowired
48+ OrderService orderService ;
49+
50+ @ Test
51+ @ DisplayName ("Should be able to get order by id" )
52+ void getOrderByIdTest () {
53+ int randomId = 2 ;
54+ final int totalPrice = 20 ;
55+ final int quantity = 4 ;
56+ Orders order = Orders .builder ()
57+ .id (randomId )
58+ .totalQuantity (quantity )
59+ .totalPrice (totalPrice )
60+ .build ();
61+
62+ Mockito .when (orderRepository .findById (anyInt ())).thenReturn (Optional .of (order ));
63+ Orders obtainedOrder = orderService .findOne (1 );
64+ assertEquals (obtainedOrder , order );
65+ }
66+
67+ @ Test
68+ @ DisplayName ("Should be able to create an order" )
69+ void createdOrderTest (){
70+ final int randomId = 5 ;
71+ final int customerId =1 ;
72+ final int productId = 2 ;
73+ final int totalPrice = 20 ;
74+ final int totalQuantity = 4 ;
75+
76+ Mockito .when (customerRepository .findById (customerId ))
77+ .thenReturn (Optional .of (getSampleCustomer (customerId )));
78+ Mockito .when (productRepository .findById (productId ))
79+ .thenReturn (Optional .of (getSampleProduct (productId )));
80+
81+ Orders order = Orders .builder ()
82+ .id (randomId )
83+ .customer (getSampleCustomer (customerId ))
84+ .status (OrderStatus .INITIATED )
85+ .totalQuantity (totalQuantity )
86+ .totalPrice (totalPrice )
87+ .build ();
88+
89+ List <OrderItem > orderItems = getSampleOrderItems (order ,productId ,totalQuantity );
90+ order .setOrderItems (orderItems );
91+
92+ OrderDto orderDto = OrderDto .builder ()
93+ .customerId (customerId )
94+ .status (OrderStatus .INITIATED )
95+ .orderItems (getSampleOrderItemsDto (productId ,totalQuantity ))
96+ .build ();
97+
98+ Mockito .when (orderRepository .save (any ())).thenReturn (order );
99+ Orders obtainedOrder = orderService .create (orderDto );
100+ assertEquals (obtainedOrder , order );
101+ }
102+
103+ @ Test
104+ @ DisplayName ("Should fail on creating order with invalid product" )
105+ void failOrderOnInvalidProductTest (){
106+
107+ Mockito .when (customerRepository .findById (anyInt ()))
108+ .thenReturn (Optional .of (getSampleCustomer (1 )));
109+ Mockito .when (productRepository .findById (anyInt ()))
110+ .thenThrow (new ObjectNotFoundException (Product .class ));
111+
112+ OrderDto orderDto = OrderDto .builder ()
113+ .customerId (1 )
114+ .status (OrderStatus .INITIATED )
115+ .orderItems (getSampleOrderItemsDto (1 ,4 ))
116+ .build ();
117+
118+ assertThrows (ObjectNotFoundException .class ,() -> orderService .create (orderDto ));
119+ }
120+
121+ private Customer getSampleCustomer (int customerId ){
122+ return Customer .builder ()
123+ .id (customerId )
124+ .name ("Manu" )
125+ .phone ("+91934345" )
126+ 127+ .street ("acd" )
128+ .state ("asd" )
129+ .profilePic ("http://imageurl" )
130+ .countryCode ("KWI" )
131+ .build ();
132+
133+ }
134+
135+ private OrderItemDto [] getSampleOrderItemsDto (int productId ,int quantity ){
136+ OrderItemDto [] orderItems = new OrderItemDto [1 ] ;
137+ orderItems [0 ]= OrderItemDto .builder ()
138+ .productId (productId )
139+ .quantity (quantity )
140+ .build ();
141+ return orderItems ;
142+ }
143+
144+ private Product getSampleProduct (int productId ){
145+ return Product .builder ()
146+ .name ("Mobile" )
147+ .image ("http//:ughuogwxsjhg.com" )
148+ .description (("Samsung Galaxy A7" ))
149+ .unitPrice (5 )
150+ .category (getSampleCategory ())
151+ .build ();
152+ }
153+
154+ private Category getSampleCategory (){
155+ return Category .builder ()
156+ .name ("Electronics" )
157+ .build ();
158+ }
159+
160+ private List <OrderItem > getSampleOrderItems (Orders order ,int productId , int quantity ){
161+ List <OrderItem > orderItems = new ArrayList <OrderItem >();
162+ orderItems .add (OrderItem .builder ().order (order ).quantity (quantity ).product (getSampleProduct (productId )).build ());
163+ return orderItems ;
164+ }
165+
166+ }
0 commit comments