-
Notifications
You must be signed in to change notification settings - Fork 1
/
RentalServiceTest.java
212 lines (181 loc) · 7.02 KB
/
RentalServiceTest.java
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
package service;
import dao.RentalDao;
import entity.Machine;
import entity.Rental;
import entity.User;
import enums.MachineType;
import enums.PersonType;
import enums.Role;
import org.hibernate.service.spi.ServiceException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import service.config.ServiceConfiguration;
import javax.persistence.PersistenceException;
import java.math.BigDecimal;
import java.util.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.testng.Assert.fail;
/**
* Created by pato on 23.11.2016.
*/
//@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes=ServiceConfiguration.class)
public class RentalServiceTest extends AbstractTransactionalTestNGSpringContextTests {
@Mock
private RentalDao rentalDao;
@Autowired
@InjectMocks
private RentalService rentalService;
@Autowired
@InjectMocks
private MachineService machineService;
private User user1;
private User user2;
private Machine machine1;
private Machine machine2;
private Rental rental1;
private Rental rental2;
private Rental rental3;
@BeforeTest
public void setUp() {
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, 2016);
cal2.set(Calendar.MONTH, Calendar.JANUARY);
cal2.set(Calendar.DAY_OF_MONTH, 20);
user1 = new User();
user1.setId(8L);
user1.setGivenName("test");
user1.setSurname("test");
user1.setEmail("[email protected]");
user1.setPhone("0915702446");
user1.setPasswordHash("test");
user1.setPersonType(PersonType.LEGAL);
user1.setRole(Role.EMPLOYEE);
user2 = new User();
user2.setGivenName("Albus");
user2.setSurname("Dumbledore");
user2.setPasswordHash("adfbgnh");
user2.setEmail("[email protected]");
user2.setPersonType(PersonType.NATURAL);
user2.setJoinedDate(cal2.getTime());
user2.setPhone("800123456");
user2.setRole(Role.EMPLOYEE);
machine1 = new Machine();
machine1.setName("Test1");
machine1.setPricePerDay(BigDecimal.TEN);
machine1.setMachineType(MachineType.CRANE);
machine1.setDateOfBuy(cal2.getTime());
machine1.setDateOfLastRevision(cal2.getTime());
machine2 = new Machine();
machine2.setName("Test2");
machine2.setPricePerDay(BigDecimal.TEN);
machine2.setMachineType(MachineType.EXCAVATOR);
machine2.setDateOfBuy(cal2.getTime());
machine2.setDateOfLastRevision(cal2.getTime());
rental1 = new Rental();
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_MONTH,-15);
rental1.setDateFrom(cal1.getTime());
Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.DAY_OF_MONTH,-15);
rental1.setDateTo(cal3.getTime());
rental1.setPrice(5000);
rental1.setUser(user1);
rental1.setMachine(machine2);
rental2 = new Rental();
rental2.setId(2L);
rental2.setDateFrom(new Date());
rental2.setDateTo(new Date());
rental2.setPrice(5000);
rental2.setUser(user2);
rental2.setMachine(machine1);
}
@BeforeMethod
public void setup() throws ServiceException
{
MockitoAnnotations.initMocks(this);
}
@Test
public void findAllRentals() {
// when(rentalService.findAllRentals()).thenReturn(Arrays.asList(rental1, rental2));
rentalService.findAllRentals();
verify(rentalDao, times(1)).findAllRentals();
// Assert.assertEquals(rentalService.findAllRentals().size(), 2);
}
@Test
public void findById() {
// when(rentalService.findRentalById(2L)).thenReturn(rental2);
rentalService.findRentalById(2L);
verify(rentalDao, times(1)).findById(2L);
// Assert.assertEquals(rentalService.findRentalById(2L), rental2);
}
@Test(expectedExceptions = {PersistenceException.class})
public void findByNonExistingId() {
// long nonExitstingId = -3L;
// when(rentalService.findRentalById(-3L)).thenReturn(null);
doThrow(new PersistenceException("")).when(rentalDao).findById(any(Long.class));
rentalService.findRentalById(2L);
fail("Expected PersistanceException");
// Assert.assertNull(rentalService.findRentalById(-3L));
}
@Test
public void testCreate() {
// Long userId = 1L;
// doAnswer(invocation -> {
// Object arg = invocation.getArguments()[0];
// Rental rental = (Rental) arg;
// rental.setId(userId);
// return null;
// }).when(rentalDao).create(any(Rental.class));
rentalService.create(rental1);
verify(rentalDao, times(1)).create(rental1);
// Assert.assertEquals(userId, rental1.getId());
}
@Test
public void testDelete() {
// when(rentalService.findRentalById(eq(rental2.getId()))).thenReturn(rental2).thenReturn(null);
// Assert.assertNotNull(rentalService.findRentalById(rental2.getId()));
rentalService.delete(rental2);
verify(rentalDao, times(1)).delete(rental2);
// Assert.assertNull(rentalService.findRentalById(rental2.getId()));
}
@Test(expectedExceptions = {DataAccessException.class})
public void testDeleteNonExistingRental(){
// doThrow(new DataAccessException("Deleting non existing rental") {}).when(rentalDao).delete(eq(rental3));
doThrow(new DataAccessException("deleting") {}).when(rentalDao).delete(any(Rental.class));
rentalService.delete(rental3);
fail("Expected dataAccessException");
}
@Test
public void testUpdate() {
// when(rentalService.findRentalById(2L)).thenReturn(rental2);
// doAnswer(invocation -> {
// Object arg = invocation.getArguments()[0];
// Rental rental = (Rental) arg;
// return rental;
// }).when(rentalDao).update(any(Rental.class));
// rental2.setPrice(999);
rentalService.update(rental2);
verify(rentalDao, times(1)).update(rental2);
// Rental updated = rentalService.findRentalById(2L);
// Assert.assertEquals(updated.getPrice(), rental2.getPrice());
}
@Test
public void testfindAllMachinesRentedCurrentWeek() {
when(rentalService.findAllRentals()).thenReturn(Arrays.asList(rental1, rental2));
List<Machine> machines = rentalService.findAllMachinesRentedCurrentWeek();
Assert.assertEquals(machines.size(), 1);
}
}