11package ru .teamtwo .telegrambot .service .impl .bot ;
22
3- import org .assertj .core .api .Assertions ;
43import org .hamcrest .Matchers ;
54import org .junit .jupiter .api .BeforeEach ;
65import org .junit .jupiter .api .Test ;
76import org .mockito .InjectMocks ;
87import org .mockito .Mock ;
9- import org .mockito .Mockito ;
108import org .mockito .hamcrest .MockitoHamcrest ;
119import org .springframework .boot .test .context .SpringBootTest ;
1210import org .springframework .http .HttpStatus ;
1513import ru .teamtwo .core .dtos .customer .CartItemDto ;
1614import ru .teamtwo .core .dtos .customer .CustomerDto ;
1715import ru .teamtwo .core .dtos .customer .OrderDto ;
16+ import ru .teamtwo .core .dtos .customer .OrderItemDto ;
1817import ru .teamtwo .core .dtos .product .ProductDto ;
18+ import ru .teamtwo .core .dtos .product .ProductOfferDto ;
19+ import ru .teamtwo .core .dtos .product .StoreDto ;
1920import ru .teamtwo .telegrambot .CustomerStateTestUtils ;
2021import ru .teamtwo .telegrambot .mapper .CustomerStateMapper ;
2122import ru .teamtwo .telegrambot .model .customer .CustomerOrder ;
2728import ru .teamtwo .telegrambot .service .impl .rest .clients .customer .CustomerClient ;
2829import ru .teamtwo .telegrambot .service .impl .rest .clients .customer .OrderClient ;
2930import ru .teamtwo .telegrambot .service .impl .rest .clients .customer .OrderItemClient ;
31+ import ru .teamtwo .telegrambot .service .impl .rest .clients .product .ProductClient ;
3032import ru .teamtwo .telegrambot .service .impl .rest .clients .product .ProductOfferClient ;
33+ import ru .teamtwo .telegrambot .service .impl .rest .clients .product .StoreClient ;
3134
3235import java .time .Instant ;
3336import java .util .Collection ;
3437import java .util .HashSet ;
3538import java .util .Set ;
3639import java .util .stream .Collectors ;
3740
41+ import static org .assertj .core .api .Assertions .assertThat ;
42+ import static org .mockito .Mockito .any ;
43+ import static org .mockito .Mockito .eq ;
44+ import static org .mockito .Mockito .times ;
45+ import static org .mockito .Mockito .verify ;
46+ import static org .mockito .Mockito .when ;
47+
3848@ SpringBootTest
3949class RESTHandlerImplTest {
4050 static final Long USER_ID = 12345L ;
4151
4252 @ Mock
4353 Set <CartItemDto > testCart = new HashSet <>();
4454 Set <OrderDto > testOrders = new HashSet <>();
55+ Set <OrderItemDto > testOrderItems = new HashSet <>();
56+ Set <ProductOfferDto > testProductOffers = new HashSet <>();
4557 Set <CustomerOrder > testCustomerOrders = new HashSet <>();
4658 Set <ProductDto > testQueryResult = new HashSet <>();
4759 CustomerState filledCustomerState ;
4860 CustomerDto customerDto ;
4961 @ Mock
5062 CustomerStateMapper customerStateMapper ;
5163 @ Mock
52- ResponseEntity responseEntity ;
53- @ Mock
54- ResponseEntity responseEntityWithOrders ;
55- @ Mock
56- ResponseEntity responseEntityWithCartItems ;
57- @ Mock
5864 CartItemClient cartItemClient ;
5965 @ Mock
6066 CustomerClient customerClient ;
@@ -64,55 +70,61 @@ class RESTHandlerImplTest {
6470 OrderItemClient orderItemClient ;
6571 @ Mock
6672 ProductOfferClient productOfferClient ;
73+ @ Mock
74+ StoreClient storeClient ;
75+ @ Mock
76+ ProductClient productClient ;
6777 @ InjectMocks
6878 RESTHandlerImpl restHandler ;
6979
7080 @ BeforeEach
7181 void setUp () {
7282 testCart .add (new CartItemDto (1L , 1L , 1L , 1 ));
7383 testOrders .add (new OrderDto (1L , 1L , Instant .now ()));
84+ testOrderItems .add (new OrderItemDto (1L , 1L , 1L , 10 ));
85+ testProductOffers .add (new ProductOfferDto (1L , 1L , 1L , 10 ));
7486
7587 filledCustomerState = CustomerStateTestUtils .getCustomerState ();
7688 customerDto = CustomerStateTestUtils .getCustomerDto ();
7789
78- Mockito . when (customerStateMapper .convertToDto (filledCustomerState )).thenReturn (customerDto );
79- Mockito . when (customerStateMapper .convertToEntity (Mockito . eq (customerDto ), Mockito . eq (testCart ), Mockito . any ())).thenReturn (filledCustomerState );
80-
81- Mockito . when (responseEntity . getStatusCode ()) .thenReturn (HttpStatus . OK );
82- Mockito . when (responseEntityWithOrders . getStatusCode ()) .thenReturn (HttpStatus . OK );
83- Mockito . when (responseEntityWithOrders . getBody ()) .thenReturn (testOrders );
84- Mockito . when (responseEntityWithCartItems . getStatusCode ()) .thenReturn (HttpStatus . OK );
85- Mockito . when (responseEntityWithCartItems . getBody ()) .thenReturn (testCart );
86-
87- Mockito . when (cartItemClient . getAllByCustomer ( Mockito . any ())).thenReturn (responseEntityWithCartItems );
88- Mockito . when (orderClient . getAllByCustomer ( Mockito . any ())).thenReturn (responseEntityWithOrders );
89- Mockito . when (orderItemClient . getAllByOrder ( Mockito . any ())).thenReturn (responseEntity );
90- Mockito . when (customerClient . save ( Mockito . any ())).thenReturn (responseEntity );
91- Mockito . when ( cartItemClient . save ( Mockito . any ())). thenReturn ( responseEntity );
92- Mockito . when ( orderClient . save ( Mockito . any ())). thenReturn ( responseEntity );
93- Mockito . when ( orderItemClient . save ( Mockito . any ())). thenReturn ( responseEntity );
94- Mockito . when ( productOfferClient . query ( Mockito . any ())). thenReturn ( responseEntity );
90+ when (customerStateMapper .convertToDto (filledCustomerState )).thenReturn (customerDto );
91+ when (customerStateMapper .convertToEntity (eq (customerDto ), eq (testCart ), any ())).thenReturn (filledCustomerState );
92+
93+ when (cartItemClient . getAllByCustomer ( any ())) .thenReturn (createResponseEntity ( testCart ) );
94+ when (orderClient . getAllByCustomer ( any ())) .thenReturn (createResponseEntity ( testOrders ) );
95+ when (orderItemClient . getAllByOrder ( any ())) .thenReturn (createResponseEntity ( testOrderItems ) );
96+ when (customerClient . save ( any ())) .thenReturn (createResponseEntity ( null ) );
97+ when (cartItemClient . save ( any ())) .thenReturn (createResponseEntity ( null ) );
98+ when ( orderClient . save ( any ())). thenReturn ( createResponseEntity ( null ));
99+ when (orderItemClient . save ( any ())).thenReturn (createResponseEntity ( null ) );
100+ when (productOfferClient . query ( any ())).thenReturn (createResponseEntity ( testProductOffers ) );
101+ when (storeClient . get ( any ())).thenReturn (createResponseEntity ( new StoreDto ( 1L , "name" , 1 )) );
102+ when (productClient . get ( any ())).thenReturn (createResponseEntity ( new ProductDto ( 1L , "name" , "name" , "name" , "name" , "name" , 1 , 1 )) );
103+ }
104+
105+ private ResponseEntity createResponseEntity ( Object body ){
106+ return new ResponseEntity ( body , HttpStatus . OK );
95107 }
96108
97109 @ Test
98110 void getCustomerState () throws RESTHandlerException {
99111 ResponseEntity <CustomerDto > response = new ResponseEntity <>(customerDto , HttpStatus .OK );
100- Mockito . when (customerClient .get (USER_ID )).thenReturn (response );
112+ when (customerClient .get (USER_ID )).thenReturn (response );
101113
102114 CustomerState customerState = restHandler .getCustomerState (USER_ID );
103115
104- Mockito . verify (customerClient , Mockito . times (1 )).get (USER_ID );
105- Assertions . assertThat (filledCustomerState .getUserId ()).isEqualTo (customerState .getUserId ());
116+ verify (customerClient , times (1 )).get (USER_ID );
117+ assertThat (filledCustomerState .getUserId ()).isEqualTo (customerState .getUserId ());
106118 }
107119
108120 @ Test
109121 void saveCustomerState () throws RESTHandlerException {
110122 restHandler .saveCustomerState (filledCustomerState );
111123
112- Mockito . verify (customerClient , Mockito . times (1 )).save ((Set <CustomerDto >) MockitoHamcrest .argThat (Matchers .hasItem (customerDto )));
113- Mockito . verify (cartItemClient , Mockito . times (1 )).save (filledCustomerState .getCart ());
114- Mockito . verify (orderClient , Mockito . times (1 )).save (filledCustomerState .getOrders ().stream ().map (CustomerOrder ::getOrderDto ).collect (Collectors .toSet ()));
115- Mockito . verify (orderItemClient , Mockito . times (1 )).save (filledCustomerState .getOrders ().stream ().map (CustomerOrder ::getOrderItemDtos ).flatMap (Collection ::stream ).collect (Collectors .toSet ()));
124+ verify (customerClient , times (1 )).save ((Set <CustomerDto >) MockitoHamcrest .argThat (Matchers .hasItem (customerDto )));
125+ verify (cartItemClient , times (1 )).save (filledCustomerState .getCart ());
126+ verify (orderClient , times (1 )).save (filledCustomerState .getOrders ().stream ().map (CustomerOrder ::getOrderDto ).collect (Collectors .toSet ()));
127+ verify (orderItemClient , times (1 )).save (filledCustomerState .getOrders ().stream ().map (CustomerOrder ::getOrderItemDtos ).flatMap (Collection ::stream ).collect (Collectors .toSet ()));
116128 }
117129
118130 @ Test
@@ -121,7 +133,13 @@ void queryProducts() throws RESTHandlerException {
121133
122134 Set <Product > productQueryResult = restHandler .queryProducts (productQuery );
123135
124- Mockito .verify (productOfferClient , Mockito .times (1 )).query (productQuery );
136+ verify (productOfferClient , times (1 )).query (productQuery );
137+ verify (productClient , times (1 )).get (1L );
138+ verify (storeClient , times (1 )).get (1L );
139+
140+ assertThat (productQueryResult ).hasSize (1 );
141+ Product product = productQueryResult .stream ().collect (Collectors .toList ()).get (0 );
142+ assertThat (product .productOfferDto ().id ()).isEqualTo (1L );
125143 }
126144
127145}
0 commit comments