100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added new methods to pseudo clock for testing (#2316)
Eric Bariaux540d21ff54f6
diff --git a/container/build.gradle b/container/build.gradleindex f89440ea0..e3bc70418 100644--- a/container/build.gradle+++ b/container/build.gradle@@ -53,6 +53,13 @@ dependencies {api "com.thetransactioncompany:cors-filter:$corsFilterVersion"}+dependencies {+ testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"+ testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"+ testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"+ testRuntimeOnly "org.junit.platform:junit-platform-launcher"+}+base {archivesName = "openremote-${project.name}"}diff --git a/container/src/main/java/org/openremote/container/timer/TimerService.java b/container/src/main/java/org/openremote/container/timer/TimerService.javaindex 475fce252..1e3859e1d 100644--- a/container/src/main/java/org/openremote/container/timer/TimerService.java+++ b/container/src/main/java/org/openremote/container/timer/TimerService.java@@ -23,7 +23,13 @@ import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.rules.RulesClock;+import java.time.Duration;import java.time.Instant;+import java.time.LocalDate;+import java.time.LocalTime;+import java.time.ZoneId;+import java.time.ZonedDateTime;+import java.time.temporal.Temporal;import java.util.Date;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicLong;@@ -59,12 +65,17 @@ public class TimerService implements ContainerService, RulesClock {}@Override- public void stop() {- // NOOP+ public long setTime(LocalDate date, LocalTime time, ZoneId zoneId) {+ throw new UnsupportedOperationException("Wall clock can not be set manually");+ }++ @Override+ public long setTime(String iso8601Timestamp) {+ throw new UnsupportedOperationException("Wall clock can not be set manually");}@Override- public void stopAtTime(long time) {+ public void stop() {// NOOP}@@ -101,6 +112,22 @@ public class TimerService implements ContainerService, RulesClock {return currentMillis;}+ @Override+ public long setTime(LocalDate date, LocalTime time, ZoneId zoneId) {+ ZonedDateTime current = Instant.ofEpochMilli(getCurrentTimeMillis()).atZone(zoneId);+ ZonedDateTime target = date.atTime(time).atZone(zoneId);+ return advanceTime(Duration.between(current, target).toMillis(), TimeUnit.MILLISECONDS);+ // Above will log current time, no need to log again here+ }++ @Override+ public long setTime(String iso8601Timestamp) {+ Temporal current = Instant.ofEpochMilli(getCurrentTimeMillis());+ Temporal target = Instant.parse(iso8601Timestamp);+ return advanceTime(Duration.between(current, target).toMillis() , TimeUnit.MILLISECONDS);+ // Above will log current time, no need to log again here+ }+@Overridepublic synchronized void stop() {if (stopTime == null) {@@ -109,12 +136,6 @@ public class TimerService implements ContainerService, RulesClock {}}- @Override- public synchronized void stopAtTime(long time) {- stopTime = time;- LOG.info("Clock stopped at: " + (stopTime) + "/" + new Date(stopTime));- }-@Overridepublic synchronized void start() {stopTime = null;@@ -131,10 +152,11 @@ public class TimerService implements ContainerService, RulesClock {public abstract void init();public abstract long getCurrentTimeMillis();public abstract void stop();- public abstract void stopAtTime(long time);public abstract void start();public abstract void reset();public abstract long advanceTime(long amount, TimeUnit unit);+ public abstract long setTime(LocalDate date, LocalTime time, ZoneId zoneId);+ public abstract long setTime(String iso8601Timestamp);}protected Clock clock;diff --git a/container/src/test/java/org/openremote/container/timer/PseudoClockTest.java b/container/src/test/java/org/openremote/container/timer/PseudoClockTest.javanew file mode 100644index 000000000..4ea0d1422--- /dev/null+++ b/container/src/test/java/org/openremote/container/timer/PseudoClockTest.java@@ -0,0 +1,52 @@+package org.openremote.container.timer;++import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.time.LocalDate;+import java.time.LocalTime;+import java.time.ZoneId;++import static org.junit.jupiter.api.Assertions.assertEquals;++public class PseudoClockTest {++ private TimerService.Clock clock;++ @BeforeEach+ public void setup() {+ clock = TimerService.Clock.PSEUDO;+ clock.init();+ clock.stop();+ }+ @Test+ public void testSetTime() {+ clock.setTime(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0), ZoneId.of("UTC"));+ assertEquals(0, clock.getCurrentTimeMillis());++ clock.setTime(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0), ZoneId.of("UTC"));+ assertEquals(24 * 3_600_000, clock.getCurrentTimeMillis());++ clock.setTime(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0), ZoneId.of("CET"));+ assertEquals(-3_600_000, clock.getCurrentTimeMillis());++ clock.setTime(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0), ZoneId.of("CET"));+ assertEquals(23 * 3_600_000, clock.getCurrentTimeMillis());+ }++ @Test+ public void testSetTimeISO() {+ clock.setTime("1970-01-01T00:00:00.000Z");+ assertEquals(0, clock.getCurrentTimeMillis());++ clock.setTime("1970-01-02T00:00:00.000Z");+ assertEquals(24 * 3_600_000, clock.getCurrentTimeMillis());++ clock.setTime("1970-01-01T00:00:00.000+01:00");+ assertEquals(-3_600_000, clock.getCurrentTimeMillis());++ clock.setTime("1970-01-02T00:00:00.000+01:00");+ assertEquals(23 * 3_600_000, clock.getCurrentTimeMillis());+ }++}diff --git a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovyindex 348f51e19..5559e1193 100644--- a/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy+++ b/test/src/main/groovy/org/openremote/test/ContainerTrait.groovy@@ -36,7 +36,6 @@ import org.openremote.container.security.PasswordAuthFormimport org.openremote.container.security.keycloak.KeycloakIdentityProviderimport org.openremote.container.timer.TimerServiceimport org.openremote.container.util.LogUtil-import org.openremote.model.util.MapAccessimport org.openremote.container.web.WebClientimport org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetProcessingService@@ -60,6 +59,7 @@ import org.openremote.model.rules.GlobalRulesetimport org.openremote.model.rules.RealmRulesetimport org.openremote.model.rules.Rulesetimport org.openremote.model.security.User+import org.openremote.model.util.MapAccessimport org.slf4j.Loggerimport org.slf4j.LoggerFactorydiff --git a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovyindex c31191fbe..db03d936a 100644--- a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy+++ b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy@@ -11,6 +11,9 @@ import org.openremote.model.util.TextUtilimport spock.util.concurrent.PollingConditionsimport java.time.Instant+import java.time.LocalDate+import java.time.LocalTime+import java.time.ZoneIdimport java.util.concurrent.TimeUnitimport java.util.stream.Collectors@@ -76,16 +79,17 @@ trait ManagerContainerTrait extends ContainerTrait {withClockOf(container) { it.advanceTime(amount, unit) }}- void stopPseudoClock() {- withClockOf(container) { it.stop() }+ // Used in custom project tests+ void setPseudoClock(LocalDate date, LocalTime time, ZoneId zoneId) {+ withClockOf(container) { it.setTime(date, time, zoneId) }}- void resetPseudoClockAt() {- withClockOf(container) { it.reset() }+ void setPseudoClock(String iso8601Timestamp) {+ withClockOf(container) { it.setTime(iso8601Timestamp) }}- void stopPseudoClockAt(long amount) {- withClockOf(container) { it.stopAtTime(amount) }+ void stopPseudoClock() {+ withClockOf(container) { it.stop() }}void startPseudoClock() {diff --git a/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovyindex 9c28dd8fd..3642a255b 100644--- a/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/simulator/SimulatorProtocolTest.groovy@@ -108,8 +108,8 @@ class SimulatorProtocolTest extends Specification implements ManagerContainerTra}def setup() {- resetPseudoClockAt()- stopPseudoClockAt(Instant.parse("1970-01-01T00:00:00.000Z").toEpochMilli())+ stopPseudoClock()+ setPseudoClock("1970-01-01T00:00:00.000Z")future = Mock(ScheduledFuture)executor = Mock(ScheduledExecutorService)