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
|
package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidRegistrationException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.PersistenceException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.ServiceException;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.RegistrationDAO;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.VehicleDAO;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Employee.EducationLevel;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Registration;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.Builder;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.ConstructionType;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.Status;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.VehicleType;
import java.time.Instant;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
public class RegistrationServiceTest {
@Mock private RegistrationDAO registrationDAO;
@Mock private VehicleDAO vehicleDAO;
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
@Rule public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() throws ElementNotFoundException, PersistenceException {
MockitoAnnotations.initMocks(this);
Builder b =
Vehicle.builder()
.name("RTW-1")
.constructionType(ConstructionType.HOCHDACH)
.status(Status.ABGEMELDET)
.type(VehicleType.RTW)
.hasNef(true);
when(vehicleDAO.get(anyLong())).thenAnswer(ans -> b.id(ans.getArgument(0)).build());
}
@Test
public void addValidRegistrationsShouldSucceed()
throws InvalidRegistrationException, ServiceException, InvalidVehicleException {
addValidRegistrations(registrationDAO, vehicleDAO);
}
@Test
public void addOnlyOnePersonToRTWShouldFail()
throws InvalidRegistrationException, ServiceException, InvalidVehicleException {
addOnlyOnePersonToRTW(thrown, registrationDAO, vehicleDAO);
}
static void addValidRegistrations(RegistrationDAO registrationDAO, VehicleDAO vehicleDAO)
throws InvalidVehicleException, InvalidRegistrationException, ServiceException {
RegistrationService registrationService =
new RegistrationServiceImpl(registrationDAO, vehicleDAO);
Set<Registration> registrations = new HashSet<>();
Vehicle vehicle =
Vehicle.builder()
.id(1)
.name("RTW-1")
.constructionType(ConstructionType.HOCHDACH)
.type(VehicleType.RTW)
.status(Status.ABGEMELDET)
.hasNef(true)
.build();
Employee employee1 =
Employee.builder()
.id(1)
.name("John Doe")
.birthday(LocalDate.now()) // incorrect, but should be irrelevant
.educationLevel(EducationLevel.RS)
.isDriver(true)
.isPilot(true)
.build();
Employee employee2 =
Employee.builder()
.id(2)
.name("Nick \"Kage\" Verily")
.birthday(LocalDate.now()) // incorrect, but should be irrelevant
.educationLevel(EducationLevel.NKV)
.isDriver(true)
.isPilot(false)
.build();
Employee employee3 =
Employee.builder()
.id(3)
.name("Nicht Arzt")
.birthday(LocalDate.now()) // incorrect, but should be irrelevant
.educationLevel(EducationLevel.NA)
.isDriver(false)
.isPilot(false)
.build();
Instant start = Instant.now();
Instant end = start.plus(8, ChronoUnit.HOURS);
Registration registration1 =
Registration.builder().start(start).end(end).employee(employee1).build();
Registration registration2 =
Registration.builder().start(start).end(end).employee(employee2).build();
Registration registration3 =
Registration.builder().start(start).end(end).employee(employee3).build();
registrations.add(registration1);
registrations.add(registration2);
registrations.add(registration3);
registrationService.add(vehicle.id(), registrations);
}
static void addOnlyOnePersonToRTW(
ExpectedException thrown, RegistrationDAO registrationDAO, VehicleDAO vehicleDAO)
throws InvalidVehicleException, InvalidRegistrationException, ServiceException {
thrown.expect(InvalidRegistrationException.class);
RegistrationService registrationService =
new RegistrationServiceImpl(registrationDAO, vehicleDAO);
Set<Registration> registrations = new HashSet<>();
Vehicle vehicle =
Vehicle.builder()
.id(1)
.name("RTW-1")
.constructionType(ConstructionType.HOCHDACH)
.type(VehicleType.RTW)
.status(Status.ABGEMELDET)
.hasNef(true)
.build();
Employee employee =
Employee.builder()
.id(1)
.name("John Doe")
.birthday(LocalDate.now()) // incorrect, but should be irrelevant
.educationLevel(EducationLevel.RS)
.isDriver(true)
.isPilot(true)
.build();
Registration registration =
Registration.builder()
.start(Instant.MIN)
.end(Instant.MAX)
.employee(employee)
.build();
registrations.add(registration);
registrationService.add(vehicle.id(), registrations);
}
}
|