aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/traccar/reports/ReportUtilsTest.java
blob: 22d70c93a7f4c3ff18cf6f40b605c1cfb2e84b0b (plain)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package org.traccar.reports;

import org.apache.velocity.app.VelocityEngine;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.traccar.BaseTest;
import org.traccar.api.security.PermissionsService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Position;
import org.traccar.reports.common.ReportUtils;
import org.traccar.reports.model.StopReportItem;
import org.traccar.reports.model.TripReportItem;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ReportUtilsTest extends BaseTest {
    
    private Storage storage;
    
    @BeforeEach
    public void init() throws StorageException {
        storage = mock(Storage.class);
        when(storage.getObject(any(), any())).thenReturn(mock(Device.class));
    }

    private Date date(String time) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.parse(time);
    }

    private Position position(String time, double speed, double totalDistance) throws ParseException {

        Position position = new Position();

        position.setTime(date(time));
        position.setValid(true);
        position.setSpeed(speed);
        position.set(Position.KEY_MOTION, speed > 0);
        position.set(Position.KEY_TOTAL_DISTANCE, totalDistance);

        return position;
    }

    private Device mockDevice(
            double minimalTripDistance, long minimalTripDuration, long minimalParkingDuration,
            long minimalNoDataDuration, boolean useIgnition) {
        Device device = mock(Device.class);
        when(device.getAttributes()).thenReturn(Map.of(
                Keys.REPORT_TRIP_MINIMAL_TRIP_DISTANCE.getKey(), minimalTripDistance,
                Keys.REPORT_TRIP_MINIMAL_TRIP_DURATION.getKey(), minimalTripDuration,
                Keys.REPORT_TRIP_MINIMAL_PARKING_DURATION.getKey(), minimalParkingDuration,
                Keys.REPORT_TRIP_MINIMAL_NO_DATA_DURATION.getKey(), minimalNoDataDuration,
                Keys.REPORT_TRIP_USE_IGNITION.getKey(), useIgnition));
        return device;
    }
    
    @Test
    public void testCalculateDistance() {
        Position startPosition = new Position();
        startPosition.set(Position.KEY_TOTAL_DISTANCE, 500.0);
        Position endPosition = new Position();
        endPosition.set(Position.KEY_TOTAL_DISTANCE, 700.0);
        assertEquals(PositionUtil.calculateDistance(startPosition, endPosition, true), 200.0, 10);
        startPosition.set(Position.KEY_ODOMETER, 50000);
        endPosition.set(Position.KEY_ODOMETER, 51000);
        assertEquals(PositionUtil.calculateDistance(startPosition, endPosition, true), 1000.0, 10);
    }

    @Test
    public void testCalculateSpentFuel() {
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);
        Position startPosition = new Position();
        Position endPosition = new Position();
        assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.0, 0.01);
        startPosition.set(Position.KEY_FUEL_LEVEL, 0.7);
        endPosition.set(Position.KEY_FUEL_LEVEL, 0.5);
        assertEquals(reportUtils.calculateFuel(startPosition, endPosition), 0.2, 0.01);
    }

    @Test
    public void testDetectTripsSimple() throws Exception {

        List<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 0, 0),
                position("2016-01-01 00:01:00.000", 0, 0),
                position("2016-01-01 00:02:00.000", 10, 0),
                position("2016-01-01 00:03:00.000", 10, 1000),
                position("2016-01-01 00:04:00.000", 10, 2000),
                position("2016-01-01 00:05:00.000", 0, 3000),
                position("2016-01-01 00:15:00.000", 0, 3000),
                position("2016-01-01 00:25:00.000", 0, 3000));

        Device device = mockDevice(500, 300, 180, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class);

        assertNotNull(trips);
        assertFalse(trips.isEmpty());

        TripReportItem itemTrip = trips.iterator().next();

        assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime());
        assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime());
        assertEquals(180000, itemTrip.getDuration());
        assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01);
        assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
        assertEquals(3000, itemTrip.getDistance(), 0.01);

        var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(stops);
        assertFalse(stops.isEmpty());

        Iterator<StopReportItem> iterator = stops.iterator();

        StopReportItem itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getEndTime());
        assertEquals(120000, itemStop.getDuration());

        itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:25:00.000"), itemStop.getEndTime());
        assertEquals(1200000, itemStop.getDuration());

    }

    @Test
    public void testDetectTripsSimpleWithIgnition() throws Exception {

        List<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 0, 0),
                position("2016-01-01 00:01:00.000", 0, 0),
                position("2016-01-01 00:02:00.000", 10, 0),
                position("2016-01-01 00:03:00.000", 10, 1000),
                position("2016-01-01 00:04:00.000", 10, 2000),
                position("2016-01-01 00:05:00.000", 0, 3000),
                position("2016-01-01 00:15:00.000", 0, 3000),
                position("2016-01-01 00:25:00.000", 0, 3000));

        data.get(5).set(Position.KEY_IGNITION, false);

        Device device = mockDevice(500, 300, 180, 900, true);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class);

        assertNotNull(trips);
        assertFalse(trips.isEmpty());

        TripReportItem itemTrip = trips.iterator().next();

        assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime());
        assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime());
        assertEquals(180000, itemTrip.getDuration());
        assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01);
        assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
        assertEquals(3000, itemTrip.getDistance(), 0.01);

        trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class);

        assertNotNull(trips);
        assertFalse(trips.isEmpty());

        itemTrip = trips.iterator().next();

        assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime());
        assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime());
        assertEquals(180000, itemTrip.getDuration());
        assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01);
        assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
        assertEquals(3000, itemTrip.getDistance(), 0.01);

        var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(stops);
        assertFalse(stops.isEmpty());

        Iterator<StopReportItem> iterator = stops.iterator();

        StopReportItem itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getEndTime());
        assertEquals(120000, itemStop.getDuration());

        itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:25:00.000"), itemStop.getEndTime());
        assertEquals(1200000, itemStop.getDuration());

    }

    @Test
    public void testDetectTripsWithFluctuation() throws Exception {

        List<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 0, 0),
                position("2016-01-01 00:01:00.000", 0, 0),
                position("2016-01-01 00:02:00.000", 10, 0),
                position("2016-01-01 00:03:00.000", 10, 1000),
                position("2016-01-01 00:04:00.000", 10, 2000),
                position("2016-01-01 00:05:00.000", 10, 3000),
                position("2016-01-01 00:06:00.000", 10, 4000),
                position("2016-01-01 00:07:00.000", 0, 5000),
                position("2016-01-01 00:08:00.000", 10, 6000),
                position("2016-01-01 00:09:00.000", 0, 7000),
                position("2016-01-01 00:19:00.000", 0, 7000),
                position("2016-01-01 00:29:00.000", 0, 7000));

        Device device = mockDevice(500, 300, 180, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class);

        assertNotNull(trips);
        assertFalse(trips.isEmpty());

        TripReportItem itemTrip = trips.iterator().next();

        assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime());
        assertEquals(date("2016-01-01 00:09:00.000"), itemTrip.getEndTime());
        assertEquals(420000, itemTrip.getDuration());
        assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01);
        assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
        assertEquals(7000, itemTrip.getDistance(), 0.01);

        var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(stops);
        assertFalse(stops.isEmpty());

        Iterator<StopReportItem> iterator = stops.iterator();

        StopReportItem itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getEndTime());
        assertEquals(120000, itemStop.getDuration());

        itemStop = iterator.next();

        assertEquals(date("2016-01-01 00:09:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:29:00.000"), itemStop.getEndTime());
        assertEquals(1200000, itemStop.getDuration());

    }

    @Test
    public void testDetectStopsOnly() throws Exception {

        Collection<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 0, 0),
                position("2016-01-01 00:01:00.000", 0, 0),
                position("2016-01-01 00:02:00.000", 1, 0),
                position("2016-01-01 00:03:00.000", 0, 0),
                position("2016-01-01 00:04:00.000", 1, 0),
                position("2016-01-01 00:05:00.000", 0, 0));

        Device device = mockDevice(500, 300, 200, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(result);
        assertFalse(result.isEmpty());

        StopReportItem itemStop = result.iterator().next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime());
        assertEquals(300000, itemStop.getDuration());

    }

    @Test
    public void testDetectStopsWithTripCut() throws Exception {

        Collection<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 0, 0),
                position("2016-01-01 00:01:00.000", 0, 0),
                position("2016-01-01 00:02:00.000", 0, 0),
                position("2016-01-01 00:03:00.000", 0, 0),
                position("2016-01-01 00:04:00.000", 1, 0),
                position("2016-01-01 00:05:00.000", 2, 0));

        Device device = mockDevice(500, 300, 200, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(result);
        assertFalse(result.isEmpty());

        StopReportItem itemStop = result.iterator().next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getEndTime());
        assertEquals(240000, itemStop.getDuration());

    }

    @Test
    public void testDetectStopsStartedFromTrip() throws Exception {

        Collection<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 2, 0),
                position("2016-01-01 00:01:00.000", 1, 0),
                position("2016-01-01 00:02:00.000", 0, 0),
                position("2016-01-01 00:12:00.000", 0, 0),
                position("2016-01-01 00:22:00.000", 0, 0),
                position("2016-01-01 00:32:00.000", 0, 0));

        Device device = mockDevice(500, 300, 200, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(result);
        assertFalse(result.isEmpty());

        StopReportItem itemStop = result.iterator().next();

        assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:32:00.000"), itemStop.getEndTime());
        assertEquals(1800000, itemStop.getDuration());

    }

    @Test
    public void testDetectStopsMoving() throws Exception {

        Collection<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 5, 0),
                position("2016-01-01 00:01:00.000", 5, 0),
                position("2016-01-01 00:02:00.000", 3, 0),
                position("2016-01-01 00:03:00.000", 5, 0),
                position("2016-01-01 00:04:00.000", 5, 0),
                position("2016-01-01 00:05:00.000", 5, 0));

        Device device = mockDevice(500, 300, 200, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var result = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(result);
        assertTrue(result.isEmpty());

    }

    @Test
    public void testDetectTripAndStopByGap() throws Exception {

        Collection<Position> data = Arrays.asList(
                position("2016-01-01 00:00:00.000", 7, 100),
                position("2016-01-01 00:01:00.000", 7, 300),
                position("2016-01-01 00:02:00.000", 5, 500),
                position("2016-01-01 00:03:00.000", 5, 600),
                position("2016-01-01 00:04:00.000", 3, 700),
                position("2016-01-01 00:23:00.000", 2, 700),
                position("2016-01-01 00:24:00.000", 5, 800),
                position("2016-01-01 00:25:00.000", 5, 900));

        Device device = mockDevice(500, 200, 200, 900, false);
        ReportUtils reportUtils = new ReportUtils(
                mock(Config.class), storage, mock(PermissionsService.class), mock(VelocityEngine.class), null);

        var trips = reportUtils.detectTripsAndStops(device, data, false, TripReportItem.class);

        assertNotNull(trips);
        assertFalse(trips.isEmpty());

        TripReportItem itemTrip = trips.iterator().next();

        assertEquals(date("2016-01-01 00:00:00.000"), itemTrip.getStartTime());
        assertEquals(date("2016-01-01 00:04:00.000"), itemTrip.getEndTime());
        assertEquals(240000, itemTrip.getDuration());
        assertEquals(4.86, itemTrip.getAverageSpeed(), 0.01);
        assertEquals(7, itemTrip.getMaxSpeed(), 0.01);
        assertEquals(600, itemTrip.getDistance(), 0.01);

        var stops = reportUtils.detectTripsAndStops(device, data, false, StopReportItem.class);

        assertNotNull(stops);
        assertFalse(stops.isEmpty());

        StopReportItem itemStop = stops.iterator().next();

        assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getStartTime());
        assertEquals(date("2016-01-01 00:24:00.000"), itemStop.getEndTime());
        assertEquals(1200000, itemStop.getDuration());
    }

}