aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/at/ac/tuwien/sepm/assignment/groupphase/missioncontrol/service/OperationServiceTest.java
blob: 4c1eaf18f440c682bca813de68b84c72cb9ce8b0 (plain) (blame)
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
package at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import at.ac.tuwien.sepm.assignment.groupphase.exception.ElementNotFoundException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidOperationException;
import at.ac.tuwien.sepm.assignment.groupphase.exception.InvalidVehicleException;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.OperationDAO;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dao.VehicleDAO;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Severity;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Operation.Status;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle;
import at.ac.tuwien.sepm.assignment.groupphase.missioncontrol.dto.Vehicle.ConstructionType;
import java.time.Instant;
import java.util.Collections;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class OperationServiceTest {

    @Mock private OperationDAO operationDAO;

    @Mock private VehicleDAO vehicleDAO;

    @InjectMocks private OperationServiceImpl operationService;

    private Set<Vehicle> vehicles;
    private Vehicle v1, v2, v3, v4, v5;
    private Operation baseOp, o1, o2;

    @Before
    public void setUp() throws Exception {
        v1 =
                Vehicle.builder()
                        .id(1)
                        .name("RTW-1")
                        .constructionType(ConstructionType.HOCHDACH)
                        .type(Vehicle.VehicleType.RTW)
                        .status(Vehicle.Status.FREI_FUNK)
                        .hasNef(true)
                        .build();

        v2 =
                Vehicle.builder()
                        .id(2)
                        .name("KTW-1")
                        .constructionType(ConstructionType.HOCHDACH)
                        .type(Vehicle.VehicleType.KTW)
                        .status(Vehicle.Status.FREI_WACHE)
                        .hasNef(true)
                        .build();

        v3 =
                Vehicle.builder()
                        .id(3)
                        .name("KTW-2")
                        .constructionType(ConstructionType.MITTELHOCHDACH)
                        .type(Vehicle.VehicleType.KTW_B)
                        .status(Vehicle.Status.FREI_FUNK)
                        .hasNef(false)
                        .build();

        v4 =
                Vehicle.builder()
                        .id(4)
                        .name("BKTW-2")
                        .constructionType(ConstructionType.HOCHDACH)
                        .type(Vehicle.VehicleType.BKTW)
                        .status(Vehicle.Status.FREI_FUNK)
                        .hasNef(false)
                        .build();

        v5 =
                Vehicle.builder()
                        .id(5)
                        .name("NEF-1")
                        .constructionType(ConstructionType.NORMAL)
                        .type(Vehicle.VehicleType.NEF)
                        .status(Vehicle.Status.FREI_WACHE)
                        .hasNef(true)
                        .build();

        Vehicle v6 =
                Vehicle.builder()
                        .id(6)
                        .name("NAH-1")
                        .constructionType(ConstructionType.MITTELHOCHDACH)
                        .type(Vehicle.VehicleType.NAH)
                        .status(Vehicle.Status.ABGEMELDET)
                        .hasNef(true)
                        .build();

        vehicles = Set.of(v1, v2, v3, v4, v5, v6);

        baseOp =
                Operation.builder()
                        .opCode("ALP-95E7")
                        .severity(Severity.E)
                        .status(Status.ACTIVE)
                        .vehicles(Collections.singleton(v1))
                        .destination("Wiedner Hauptstraße 35, Wien")
                        .build();

        o1 = baseOp.toBuilder().id(1).created(Instant.now()).build();
        o2 = o1.toBuilder().id(5).status(Status.CANCELLED).build();

        when(operationDAO.get(anyLong()))
                .thenAnswer(
                        ans -> {
                            long arg = ans.getArgument(0);
                            if (arg == 1L) return o1;
                            else if (arg == 5L) return o2;
                            else throw new ElementNotFoundException("");
                        });

        when(vehicleDAO.get(anyLong()))
                .thenAnswer(
                        ans -> {
                            int arg = ((Long) ans.getArgument(0)).intValue();
                            return vehicles.stream()
                                    .filter(v -> v.id() == arg)
                                    .findFirst()
                                    .orElseThrow(() -> new ElementNotFoundException(""));
                        });
    }

    @Test
    public void requestNormal() throws Exception {
        Set<Long> vehicleIds = Set.of(2L, 3L, 4L, 5L);
        operationService.requestVehicles(1, vehicleIds);

        Operation result =
                operationDAO.get(1).toBuilder().vehicles(Set.of(v1, v2, v3, v4, v5)).build();
        verify(operationDAO, times(1)).update(result);
        verify(operationDAO, times(0)).get(6L);
    }

    @Test
    public void requestExistingVehicle() throws Exception {
        operationService.requestVehicles(1, Set.of(1L));

        Operation result = operationDAO.get(1);
        verify(operationDAO, times(0)).update(result);
    }

    @Test(expected = InvalidVehicleException.class)
    public void requestInvalidVehicle() throws Exception {
        operationService.requestVehicles(1, Set.of(5L, 6L));
    }

    @Test(expected = InvalidOperationException.class)
    public void requestInvalidOperation() throws Exception {
        operationService.requestVehicles(2, Set.of(1L));
    }

    @Test(expected = InvalidVehicleException.class)
    public void requestInactiveVehicle() throws Exception {
        operationService.requestVehicles(1, Set.of(6L));
    }

    @Test(expected = InvalidOperationException.class)
    public void requestInactiveOperation() throws Exception {
        operationService.requestVehicles(5, Set.of(1L));
    }

    @Test
    public void addOperation() throws Exception {
        operationService.add(baseOp.toBuilder().severity(null).build());

        verify(operationDAO, times(1))
                .add(baseOp.toBuilder().created(any()).status(Status.ACTIVE).build());
        verify(vehicleDAO, times(1)).get(v1.id());
    }

    @Test(expected = InvalidOperationException.class)
    public void addWithSeverity() throws Exception {
        operationService.add(baseOp.toBuilder().severity(Severity.E).build());
    }

    @Test(expected = InvalidOperationException.class)
    public void addWithoutVehicles() throws Exception {
        operationService.add(baseOp.toBuilder().vehicles(Set.of()).build());
    }

    @Test(expected = InvalidOperationException.class)
    public void addInvalidOpcode() throws Exception {
        operationService.add(baseOp.toBuilder().opCode("ABC").build());
    }

    @Test(expected = InvalidOperationException.class)
    public void addWithSetCreated() throws Exception {
        operationService.add(baseOp.toBuilder().created(Instant.now()).build());
    }
}