From 563243a8da888244e910a4a7a10fb86ad525fdd4 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Feb 2019 15:23:35 -0800 Subject: Handler refactoring --- .../traccar/handler/ComputedAttributesTest.java | 68 +++++++++++++++++ test/org/traccar/handler/FilterHandlerTest.java | 88 ++++++++++++++++++++++ .../traccar/processing/ComputedAttributesTest.java | 68 ----------------- test/org/traccar/processing/FilterHandlerTest.java | 88 ---------------------- 4 files changed, 156 insertions(+), 156 deletions(-) create mode 100644 test/org/traccar/handler/ComputedAttributesTest.java create mode 100644 test/org/traccar/handler/FilterHandlerTest.java delete mode 100644 test/org/traccar/processing/ComputedAttributesTest.java delete mode 100644 test/org/traccar/processing/FilterHandlerTest.java (limited to 'test') diff --git a/test/org/traccar/handler/ComputedAttributesTest.java b/test/org/traccar/handler/ComputedAttributesTest.java new file mode 100644 index 000000000..8f4f69bcd --- /dev/null +++ b/test/org/traccar/handler/ComputedAttributesTest.java @@ -0,0 +1,68 @@ +package org.traccar.handler; + +import java.util.Date; + +import org.junit.Test; +import org.traccar.model.Attribute; +import org.traccar.model.Position; + +import static org.junit.Assert.assertEquals; + +public class ComputedAttributesTest { + + @Test + public void testComputedAttributes() { + Position position = new Position(); + ComputedAttributesHandler computedAttributesHandler = new ComputedAttributesHandler(); + Date date = new Date(); + position.setTime(date); + position.setSpeed(42); + position.setValid(false); + position.set("adc1", 128); + position.set("booleanFlag", true); + position.set("adc2", 100); + position.set("bitFlag", 7); + position.set("event", 42); + position.set("result", "success"); + Attribute attribute = new Attribute(); + + attribute.setExpression("adc1"); + assertEquals(128, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("!booleanFlag"); + assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("adc2 * 2 + 50"); + assertEquals(250, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("(bitFlag & 4) != 0"); + assertEquals(true, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("if (event == 42) \"lowBattery\""); + assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("speed > 5 && valid"); + assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("fixTime"); + assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position)); + + attribute.setExpression("math:pow(adc1, 2)"); + assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position)); + + // modification tests + attribute.setExpression("adc1 = 256"); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals(128, position.getInteger("adc1")); + + attribute.setExpression("result = \"fail\""); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals("success", position.getString("result")); + + attribute.setExpression("fixTime = \"2017-10-18 10:00:01\""); + computedAttributesHandler.computeAttribute(attribute, position); + assertEquals(date, position.getFixTime()); + + } + +} diff --git a/test/org/traccar/handler/FilterHandlerTest.java b/test/org/traccar/handler/FilterHandlerTest.java new file mode 100644 index 000000000..ad8d244a6 --- /dev/null +++ b/test/org/traccar/handler/FilterHandlerTest.java @@ -0,0 +1,88 @@ +package org.traccar.handler; + +import org.junit.Before; +import org.junit.Test; +import org.traccar.BaseTest; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.model.Position; + +import java.util.Date; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class FilterHandlerTest extends BaseTest { + + private FilterHandler passingHandler = new FilterHandler(new Config()); + private FilterHandler filteringHandler; + + @Before + public void before() { + Config config = new Config(); + config.setString(Keys.FILTER_INVALID, String.valueOf(true)); + config.setString(Keys.FILTER_ZERO, String.valueOf(true)); + config.setString(Keys.FILTER_DUPLICATE, String.valueOf(true)); + config.setString(Keys.FILTER_FUTURE, String.valueOf(5 * 60)); + config.setString(Keys.FILTER_APPROXIMATE, String.valueOf(true)); + config.setString(Keys.FILTER_STATIC, String.valueOf(true)); + config.setString(Keys.FILTER_DISTANCE, String.valueOf(10)); + config.setString(Keys.FILTER_MAX_SPEED, String.valueOf(500)); + config.setString(Keys.FILTER_SKIP_LIMIT, String.valueOf(10)); + config.setString(Keys.FILTER_SKIP_ATTRIBUTES_ENABLE, String.valueOf(true)); + filteringHandler = new FilterHandler(config); + } + + private Position createPosition( + long deviceId, + Date time, + boolean valid, + double latitude, + double longitude, + double altitude, + double speed, + double course) { + + Position position = new Position(); + position.setDeviceId(deviceId); + position.setTime(time); + position.setValid(valid); + position.setLatitude(latitude); + position.setLongitude(longitude); + position.setAltitude(altitude); + position.setSpeed(speed); + position.setCourse(course); + return position; + } + + @Test + public void testFilter() { + + Position position = createPosition(0, new Date(), true, 10, 10, 10, 10, 10); + + assertNotNull(filteringHandler.handlePosition(position)); + assertNotNull(passingHandler.handlePosition(position)); + + position = createPosition(0, new Date(Long.MAX_VALUE), true, 10, 10, 10, 10, 10); + + assertNull(filteringHandler.handlePosition(position)); + assertNotNull(passingHandler.handlePosition(position)); + + position = createPosition(0, new Date(), false, 10, 10, 10, 10, 10); + + assertNull(filteringHandler.handlePosition(position)); + assertNotNull(passingHandler.handlePosition(position)); + + } + + @Test + public void testSkipAttributes() { + + Position position = createPosition(0, new Date(), true, 10, 10, 10, 0, 10); + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + + assertNotNull(filteringHandler.handlePosition(position)); + + } + +} diff --git a/test/org/traccar/processing/ComputedAttributesTest.java b/test/org/traccar/processing/ComputedAttributesTest.java deleted file mode 100644 index 160067915..000000000 --- a/test/org/traccar/processing/ComputedAttributesTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.traccar.processing; - -import java.util.Date; - -import org.junit.Test; -import org.traccar.model.Attribute; -import org.traccar.model.Position; - -import static org.junit.Assert.assertEquals; - -public class ComputedAttributesTest { - - @Test - public void testComputedAttributes() { - Position position = new Position(); - ComputedAttributesHandler computedAttributesHandler = new ComputedAttributesHandler(); - Date date = new Date(); - position.setTime(date); - position.setSpeed(42); - position.setValid(false); - position.set("adc1", 128); - position.set("booleanFlag", true); - position.set("adc2", 100); - position.set("bitFlag", 7); - position.set("event", 42); - position.set("result", "success"); - Attribute attribute = new Attribute(); - - attribute.setExpression("adc1"); - assertEquals(128, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("!booleanFlag"); - assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("adc2 * 2 + 50"); - assertEquals(250, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("(bitFlag & 4) != 0"); - assertEquals(true, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("if (event == 42) \"lowBattery\""); - assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("speed > 5 && valid"); - assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("fixTime"); - assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position)); - - attribute.setExpression("math:pow(adc1, 2)"); - assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position)); - - // modification tests - attribute.setExpression("adc1 = 256"); - computedAttributesHandler.computeAttribute(attribute, position); - assertEquals(128, position.getInteger("adc1")); - - attribute.setExpression("result = \"fail\""); - computedAttributesHandler.computeAttribute(attribute, position); - assertEquals("success", position.getString("result")); - - attribute.setExpression("fixTime = \"2017-10-18 10:00:01\""); - computedAttributesHandler.computeAttribute(attribute, position); - assertEquals(date, position.getFixTime()); - - } - -} diff --git a/test/org/traccar/processing/FilterHandlerTest.java b/test/org/traccar/processing/FilterHandlerTest.java deleted file mode 100644 index a497f1043..000000000 --- a/test/org/traccar/processing/FilterHandlerTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package org.traccar.processing; - -import org.junit.Before; -import org.junit.Test; -import org.traccar.BaseTest; -import org.traccar.config.Config; -import org.traccar.config.Keys; -import org.traccar.model.Position; - -import java.util.Date; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -public class FilterHandlerTest extends BaseTest { - - private FilterHandler passingHandler = new FilterHandler(new Config()); - private FilterHandler filteringHandler; - - @Before - public void before() { - Config config = new Config(); - config.setString(Keys.FILTER_INVALID, String.valueOf(true)); - config.setString(Keys.FILTER_ZERO, String.valueOf(true)); - config.setString(Keys.FILTER_DUPLICATE, String.valueOf(true)); - config.setString(Keys.FILTER_FUTURE, String.valueOf(5 * 60)); - config.setString(Keys.FILTER_APPROXIMATE, String.valueOf(true)); - config.setString(Keys.FILTER_STATIC, String.valueOf(true)); - config.setString(Keys.FILTER_DISTANCE, String.valueOf(10)); - config.setString(Keys.FILTER_MAX_SPEED, String.valueOf(500)); - config.setString(Keys.FILTER_SKIP_LIMIT, String.valueOf(10)); - config.setString(Keys.FILTER_SKIP_ATTRIBUTES_ENABLE, String.valueOf(true)); - filteringHandler = new FilterHandler(config); - } - - private Position createPosition( - long deviceId, - Date time, - boolean valid, - double latitude, - double longitude, - double altitude, - double speed, - double course) { - - Position position = new Position(); - position.setDeviceId(deviceId); - position.setTime(time); - position.setValid(valid); - position.setLatitude(latitude); - position.setLongitude(longitude); - position.setAltitude(altitude); - position.setSpeed(speed); - position.setCourse(course); - return position; - } - - @Test - public void testFilter() { - - Position position = createPosition(0, new Date(), true, 10, 10, 10, 10, 10); - - assertNotNull(filteringHandler.handlePosition(position)); - assertNotNull(passingHandler.handlePosition(position)); - - position = createPosition(0, new Date(Long.MAX_VALUE), true, 10, 10, 10, 10, 10); - - assertNull(filteringHandler.handlePosition(position)); - assertNotNull(passingHandler.handlePosition(position)); - - position = createPosition(0, new Date(), false, 10, 10, 10, 10, 10); - - assertNull(filteringHandler.handlePosition(position)); - assertNotNull(passingHandler.handlePosition(position)); - - } - - @Test - public void testSkipAttributes() { - - Position position = createPosition(0, new Date(), true, 10, 10, 10, 0, 10); - position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); - - assertNotNull(filteringHandler.handlePosition(position)); - - } - -} -- cgit v1.2.3