From f84e2710e05660822633ec9e61cde44c03a42d7e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 30 Mar 2024 18:46:37 -0700 Subject: Refactor position and event handlers --- .../org/traccar/handler/DistanceHandlerTest.java | 5 +++-- .../org/traccar/handler/MotionHandlerTest.java | 3 ++- .../handler/events/AlertEventHandlerTest.java | 12 ++++++----- .../events/CommandResultEventHandlerTest.java | 10 ++++++--- .../handler/events/IgnitionEventHandlerTest.java | 8 ++----- .../events/MaintenanceEventHandlerTest.java | 25 +++++++++++++++------- 6 files changed, 38 insertions(+), 25 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/test/java/org/traccar/handler/DistanceHandlerTest.java b/src/test/java/org/traccar/handler/DistanceHandlerTest.java index 7d2f1e2e3..04a27ad79 100644 --- a/src/test/java/org/traccar/handler/DistanceHandlerTest.java +++ b/src/test/java/org/traccar/handler/DistanceHandlerTest.java @@ -15,14 +15,15 @@ public class DistanceHandlerTest { DistanceHandler distanceHandler = new DistanceHandler(new Config(), mock(CacheManager.class)); - Position position = distanceHandler.handlePosition(new Position()); + Position position = new Position(); + distanceHandler.handlePosition(position, p -> {}); assertEquals(0.0, position.getAttributes().get(Position.KEY_DISTANCE)); assertEquals(0.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); position.set(Position.KEY_DISTANCE, 100); - position = distanceHandler.handlePosition(position); + distanceHandler.handlePosition(position, p -> {}); assertEquals(100.0, position.getAttributes().get(Position.KEY_DISTANCE)); assertEquals(100.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); diff --git a/src/test/java/org/traccar/handler/MotionHandlerTest.java b/src/test/java/org/traccar/handler/MotionHandlerTest.java index 10cdf6a90..927c803d9 100644 --- a/src/test/java/org/traccar/handler/MotionHandlerTest.java +++ b/src/test/java/org/traccar/handler/MotionHandlerTest.java @@ -26,7 +26,8 @@ public class MotionHandlerTest { MotionHandler motionHandler = new MotionHandler(cacheManager); - Position position = motionHandler.handlePosition(new Position()); + Position position = new Position(); + motionHandler.handlePosition(position, p -> {}); assertEquals(false, position.getAttributes().get(Position.KEY_MOTION)); diff --git a/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java b/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java index 66dc55c85..7b1991553 100644 --- a/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/AlertEventHandlerTest.java @@ -7,10 +7,11 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.session.cache.CacheManager; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.Mockito.mock; public class AlertEventHandlerTest extends BaseTest { @@ -22,9 +23,10 @@ public class AlertEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - Map events = alertEventHandler.analyzePosition(position); - assertNotNull(events); - Event event = events.keySet().iterator().next(); + List events = new ArrayList<>(); + alertEventHandler.analyzePosition(position, events::add); + assertFalse(events.isEmpty()); + Event event = events.iterator().next(); assertEquals(Event.TYPE_ALARM, event.getType()); } diff --git a/src/test/java/org/traccar/handler/events/CommandResultEventHandlerTest.java b/src/test/java/org/traccar/handler/events/CommandResultEventHandlerTest.java index bc24e42f5..58e198b7f 100644 --- a/src/test/java/org/traccar/handler/events/CommandResultEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/CommandResultEventHandlerTest.java @@ -5,9 +5,12 @@ import org.traccar.BaseTest; import org.traccar.model.Event; import org.traccar.model.Position; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; public class CommandResultEventHandlerTest extends BaseTest { @@ -19,9 +22,10 @@ public class CommandResultEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_RESULT, "Test Result"); - Map events = commandResultEventHandler.analyzePosition(position); - assertNotNull(events); - Event event = events.keySet().iterator().next(); + List events = new ArrayList<>(); + commandResultEventHandler.analyzePosition(position, events::add); + assertFalse(events.isEmpty()); + Event event = events.iterator().next(); assertEquals(Event.TYPE_COMMAND_RESULT, event.getType()); } diff --git a/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java b/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java index 972932df4..61ff50efa 100644 --- a/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/IgnitionEventHandlerTest.java @@ -1,14 +1,11 @@ package org.traccar.handler.events; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.traccar.BaseTest; -import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.session.cache.CacheManager; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.mock; public class IgnitionEventHandlerTest extends BaseTest { @@ -21,8 +18,7 @@ public class IgnitionEventHandlerTest extends BaseTest { Position position = new Position(); position.set(Position.KEY_IGNITION, true); position.setValid(true); - Map events = ignitionEventHandler.analyzePosition(position); - assertNull(events); + ignitionEventHandler.analyzePosition(position, Assertions::assertNull); } } diff --git a/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java b/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java index 661336d76..f0bd4a3f9 100644 --- a/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java +++ b/src/test/java/org/traccar/handler/events/MaintenanceEventHandlerTest.java @@ -2,11 +2,14 @@ package org.traccar.handler.events; import org.junit.jupiter.api.Test; import org.traccar.BaseTest; +import org.traccar.model.Event; import org.traccar.model.Maintenance; import org.traccar.model.Position; import org.traccar.session.cache.CacheManager; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,24 +41,30 @@ public class MaintenanceEventHandlerTest extends BaseTest { MaintenanceEventHandler eventHandler = new MaintenanceEventHandler(cacheManager); when(maintenance.getStart()).thenReturn(10000.0); - when(maintenance.getPeriod()).thenReturn(2000.0); + when(maintenance.getPeriod()).thenReturn(2000.0); + + List events = new ArrayList<>(); lastPosition.set(Position.KEY_TOTAL_DISTANCE, 1999); - position.set(Position.KEY_TOTAL_DISTANCE, 2001); - assertTrue(eventHandler.analyzePosition(position).isEmpty()); + position.set(Position.KEY_TOTAL_DISTANCE, 2001); + eventHandler.analyzePosition(position, events::add); + assertTrue(events.isEmpty()); lastPosition.set(Position.KEY_TOTAL_DISTANCE, 3999); - position.set(Position.KEY_TOTAL_DISTANCE, 4001); - assertTrue(eventHandler.analyzePosition(position).isEmpty()); + position.set(Position.KEY_TOTAL_DISTANCE, 4001); + eventHandler.analyzePosition(position, events::add); + assertTrue(events.isEmpty()); lastPosition.set(Position.KEY_TOTAL_DISTANCE, 9999); position.set(Position.KEY_TOTAL_DISTANCE, 10001); - assertEquals(1, eventHandler.analyzePosition(position).size()); + eventHandler.analyzePosition(position, events::add); + assertEquals(1, events.size()); lastPosition.set(Position.KEY_TOTAL_DISTANCE, 11999); position.set(Position.KEY_TOTAL_DISTANCE, 12001); - assertEquals(1, eventHandler.analyzePosition(position).size()); - + eventHandler.analyzePosition(position, events::add); + assertEquals(2, events.size()); + } } -- cgit v1.2.3