-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCustomerServiceImplTest.java
More file actions
351 lines (265 loc) · 14.7 KB
/
Copy pathCustomerServiceImplTest.java
File metadata and controls
351 lines (265 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package com.ainigma100.customerapi.service.impl;
import com.ainigma100.customerapi.dto.CustomerDTO;
import com.ainigma100.customerapi.dto.CustomerEmailUpdateDTO;
import com.ainigma100.customerapi.dto.CustomerSearchCriteriaDTO;
import com.ainigma100.customerapi.entity.Customer;
import com.ainigma100.customerapi.mapper.CustomerMapper;
import com.ainigma100.customerapi.repository.CustomerRepository;
import jakarta.persistence.EntityExistsException;
import jakarta.persistence.EntityNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.Mockito.*;
/*
* @ExtendWith(MockitoExtension.class) informs Mockito that we are using
* mockito annotations to mock the dependencies
*/
@ExtendWith(MockitoExtension.class)
class CustomerServiceImplTest {
// @InjectMocks creates the mock object of the class and injects the mocks
// that are marked with the annotations @Mock into it.
@InjectMocks
private CustomerServiceImpl customerService;
@Mock
private CustomerRepository customerRepository;
@Mock
private CustomerMapper customerMapper;
private Customer customer;
private CustomerDTO customerDTO;
private CustomerSearchCriteriaDTO customerSearchCriteriaDTO;
/**
* This method will be executed before each and every test inside this class
*/
@BeforeEach
void setUp() {
customer = new Customer();
customer.setId(1L);
customer.setFirstName("John");
customer.setLastName("Wick");
customer.setEmail("jwick@tester.com");
customer.setPhoneNumber("0123456789");
customer.setDateOfBirth(LocalDate.now().minusYears(18));
customer.setCreatedDate(LocalDateTime.now());
customer.setUpdatedDate(LocalDateTime.now());
customerDTO = new CustomerDTO();
customerDTO.setId(1L);
customerDTO.setFirstName("John");
customerDTO.setLastName("Wick");
customerDTO.setEmail("jwick@tester.com");
customerDTO.setPhoneNumber("0123456789");
customerDTO.setDateOfBirth(LocalDate.now().minusYears(18));
customerSearchCriteriaDTO = new CustomerSearchCriteriaDTO();
customerSearchCriteriaDTO.setPage(0);
customerSearchCriteriaDTO.setSize(10);
}
@Test
@DisplayName("Test creating a new customer")
void givenCustomerDTO_whenCreateCustomer_thenReturnCustomerDTO() {
// given - precondition or setup
String email = customerDTO.getEmail();
given(customerRepository.findByEmail(email)).willReturn(Optional.empty());
given(customerMapper.customerDTOToCustomer(customerDTO)).willReturn(customer);
given(customerRepository.save(customer)).willReturn(customer);
given(customerMapper.customerToCustomerDTO(customer)).willReturn(customerDTO);
// when - action or behaviour that we are going to test
CustomerDTO result = customerService.createCustomer(customerDTO);
// then - verify the output
assertThat(result).isNotNull();
assertThat(result.getFirstName()).isEqualTo(customerDTO.getFirstName());
assertThat(result.getLastName()).isEqualTo(customerDTO.getLastName());
assertThat(result.getEmail()).isEqualTo(customerDTO.getEmail());
assertThat(result.getPhoneNumber()).isEqualTo(customerDTO.getPhoneNumber());
verify(customerRepository, times(1)).findByEmail(email);
verify(customerMapper, times(1)).customerDTOToCustomer(customerDTO);
verify(customerRepository, times(1)).save(customer);
verify(customerMapper, times(1)).customerToCustomerDTO(customer);
}
@Test
@DisplayName("Test creating a customer with existing email throws EntityExistsException")
void givenExistingEmail_whenCreateCustomer_thenThrowEntityExistsException() {
// given - precondition or setup
String email = customerDTO.getEmail();
given(customerRepository.findByEmail(email)).willReturn(Optional.of(customer));
// when/then - verify that the EntityExistsException is thrown
assertThatThrownBy(() -> customerService.createCustomer(customerDTO))
.isInstanceOf(EntityExistsException.class)
.hasMessageContaining("A customer with email '" + customerDTO.getEmail() + "' already exists");
verify(customerRepository, times(1)).findByEmail(customerDTO.getEmail());
verify(customerMapper, never()).customerDTOToCustomer(any(CustomerDTO.class));
verify(customerRepository, never()).save(any(Customer.class));
verify(customerMapper, never()).customerToCustomerDTO(any(Customer.class));
}
@Test
@DisplayName("Test retrieving a customer by ID")
void givenValidId_whenGetCustomerById_thenReturnCustomerDTO() {
// given - precondition or setup
Long id = 1L;
given(customerRepository.findById(id)).willReturn(Optional.of(customer));
given(customerMapper.customerToCustomerDTO(customer)).willReturn(customerDTO);
// when - action or behaviour that we are going to test
CustomerDTO result = customerService.getCustomerById(id);
// then - verify the output
assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(customerDTO.getId());
assertThat(result.getFirstName()).isEqualTo(customerDTO.getFirstName());
assertThat(result.getLastName()).isEqualTo(customerDTO.getLastName());
assertThat(result.getEmail()).isEqualTo(customerDTO.getEmail());
assertThat(result.getPhoneNumber()).isEqualTo(customerDTO.getPhoneNumber());
verify(customerRepository, times(1)).findById(id);
verify(customerMapper, times(1)).customerToCustomerDTO(customer);
}
@Test
@DisplayName("Test retrieving a customer by invalid ID throws EntityNotFoundException")
void givenInvalidId_whenGetCustomerById_thenThrowEntityNotFoundException() {
// given - precondition or setup
Long id = 100L;
given(customerRepository.findById(id)).willReturn(Optional.empty());
// when/then - verify that the EntityNotFoundException is thrown
assertThatThrownBy(() -> customerService.getCustomerById(id))
.isInstanceOf(EntityNotFoundException.class)
.hasMessage("Customer with id : '" + id + "' not found");
verify(customerRepository, times(1)).findById(id);
verify(customerMapper, never()).customerToCustomerDTO(any(Customer.class));
}
@Test
@DisplayName("Test updating a customer by ID")
void givenValidIdAndCustomerDTO_whenUpdateCustomer_thenReturnUpdatedCustomerDTO() {
// given - precondition or setup
Long id = 1L;
given(customerRepository.findById(id)).willReturn(Optional.of(customer));
willDoNothing().given(customerMapper).updateCustomerFromDto(customerDTO, customer);
given(customerRepository.save(customer)).willReturn(customer);
given(customerMapper.customerToCustomerDTO(customer)).willReturn(customerDTO);
// when - action or behaviour that we are going to test
CustomerDTO result = customerService.updateCustomer(id, customerDTO);
// then - verify the output
assertThat(result).isNotNull();
assertThat(result.getFirstName()).isEqualTo(customerDTO.getFirstName());
assertThat(result.getLastName()).isEqualTo(customerDTO.getLastName());
assertThat(result.getEmail()).isEqualTo(customerDTO.getEmail());
assertThat(result.getPhoneNumber()).isEqualTo(customerDTO.getPhoneNumber());
verify(customerRepository, times(1)).findById(id);
verify(customerMapper, times(1)).updateCustomerFromDto(customerDTO, customer);
verify(customerRepository, times(1)).save(customer);
verify(customerMapper, times(1)).customerToCustomerDTO(customer);
}
@Test
@DisplayName("Test updating a customer by invalid ID throws EntityNotFoundException")
void givenInvalidIdAndCustomerDTO_whenUpdateCustomer_thenThrowEntityNotFoundException() {
// given - precondition or setup
Long id = 100L;
given(customerRepository.findById(id)).willReturn(Optional.empty());
// when/then - verify that the EntityNotFoundException is thrown
assertThatThrownBy(() -> customerService.updateCustomer(id, customerDTO))
.isInstanceOf(EntityNotFoundException.class)
.hasMessage("Customer with id : '" + id + "' not found");
verify(customerRepository, times(1)).findById(id);
verify(customerMapper, never()).customerDTOToCustomer(any(CustomerDTO.class));
verify(customerRepository, never()).save(any(Customer.class));
verify(customerMapper, never()).customerToCustomerDTO(any(Customer.class));
}
@Test
@DisplayName("Test updating a customer's email by ID")
void givenValidIdAndCustomerEmailUpdateDTO_whenUpdateCustomerEmail_thenReturnCustomerDTO() {
// given - precondition or setup
Long id = 1L;
CustomerEmailUpdateDTO customerEmailUpdateDTO = new CustomerEmailUpdateDTO();
customerEmailUpdateDTO.setEmail("loco@gmail.com");
customer.setEmail(customerEmailUpdateDTO.getEmail());
given(customerRepository.findById(id)).willReturn(Optional.of(customer));
given(customerRepository.save(customer)).willReturn(customer);
given(customerMapper.customerToCustomerDTO(customer)).willReturn(customerDTO);
// when - action or behaviour that we are going to test
CustomerDTO result = customerService.updateCustomerEmail(id, customerEmailUpdateDTO);
// then - verify the output
assertThat(result).isNotNull();
assertThat(result.getFirstName()).isEqualTo(customerDTO.getFirstName());
assertThat(result.getLastName()).isEqualTo(customerDTO.getLastName());
assertThat(result.getEmail()).isEqualTo(customerDTO.getEmail());
assertThat(result.getPhoneNumber()).isEqualTo(customerDTO.getPhoneNumber());
verify(customerRepository, times(1)).findById(id);
verify(customerRepository, times(1)).save(customer);
verify(customerMapper, times(1)).customerToCustomerDTO(customer);
}
@Test
@DisplayName("Test updating a customer's email by invalid ID throws EntityNotFoundException")
void givenInvalidIdAndCustomerEmailUpdateDTO_whenUpdateCustomerEmail_thenThrowEntityNotFoundException() {
// given - precondition or setup
Long id = 100L;
CustomerEmailUpdateDTO customerEmailUpdateDTO = new CustomerEmailUpdateDTO();
customerEmailUpdateDTO.setEmail("loco@gmail.com");
customer.setEmail(customerEmailUpdateDTO.getEmail());
given(customerRepository.findById(id)).willReturn(Optional.empty());
// when/then - verify that the EntityNotFoundException is thrown
assertThatThrownBy(() -> customerService.updateCustomerEmail(id, customerEmailUpdateDTO))
.isInstanceOf(EntityNotFoundException.class)
.hasMessage("Customer with id : '" + id + "' not found");
verify(customerRepository, times(1)).findById(id);
verify(customerRepository, never()).save(any(Customer.class));
verify(customerMapper, never()).customerToCustomerDTO(any(Customer.class));
}
@Test
@DisplayName("Test deleting a customer by ID")
void givenValidId_whenDeleteCustomer_thenDeleteCustomer() {
// given - precondition or setup
Long id = 1L;
given(customerRepository.findById(id)).willReturn(Optional.of(customer));
doNothing().when(customerRepository).delete(customer);
// when - action or behaviour that we are going to test
customerService.deleteCustomer(id);
// then - verify the output
verify(customerRepository, times(1)).findById(id);
verify(customerRepository, times(1)).delete(customer);
}
@Test
@DisplayName("Test deleting a customer by invalid ID throws EntityNotFoundException")
void givenInvalidId_whenDeleteCustomer_thenThrowEntityNotFoundException() {
// given - precondition or setup
Long id = 1L;
given(customerRepository.findById(id)).willReturn(Optional.empty());
// when/then - verify that the EntityNotFoundException is thrown
assertThatThrownBy(() -> customerService.deleteCustomer(id))
.isInstanceOf(EntityNotFoundException.class)
.hasMessage("Customer with id : '" + id + "' not found");
verify(customerRepository, times(1)).findById(id);
verify(customerRepository, never()).delete(any(Customer.class));
}
@Test
void givenCustomerSearchCriteriaDTO_whenGetAllCustomersUsingPagination_thenReturnCustomerDTOPage() {
// given - precondition or setup
List<Customer> customerList = Collections.singletonList(customer);
Page<Customer> customerPage = new PageImpl<>(customerList);
given(customerRepository.getAllCustomersUsingPagination(eq(customerSearchCriteriaDTO), any(Pageable.class)))
.willReturn(customerPage);
given(customerMapper.customerListToCustomerDTOList(customerPage.getContent()))
.willReturn(Collections.singletonList(customerDTO));
// when - action or behaviour that we are going to test
Page<CustomerDTO> result = customerService.getAllCustomersUsingPagination(customerSearchCriteriaDTO);
// then - verify the output
assertThat(result.getContent()).hasSize(1);
assertThat(result.getContent().get(0).getFirstName()).isEqualTo(customer.getFirstName());
assertThat(result.getContent().get(0).getLastName()).isEqualTo(customer.getLastName());
assertThat(result.getContent().get(0).getEmail()).isEqualTo(customer.getEmail());
assertThat(result.getContent().get(0).getPhoneNumber()).isEqualTo(customer.getPhoneNumber());
assertThat(result.getContent().get(0).getDateOfBirth()).isEqualTo(customer.getDateOfBirth());
verify(customerRepository, times(1)).getAllCustomersUsingPagination(eq(customerSearchCriteriaDTO), any(Pageable.class));
verify(customerMapper, times(1)).customerListToCustomerDTOList(customerPage.getContent());
}
}