From 478b1393e63bf8d9d71beb578b82e6ee6a375aea Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Thu, 2 Sep 2021 21:34:58 +0100 Subject: [PATCH 1/4] Revert "PHR-8766 Add interfaces for all services in testsupport module. " --- .../config/ITestControlServiceConfig.java | 11 +++-- .../config/TestControlServiceConfig.java | 35 +++++++------- .../services/ClearTestStatesService.java | 42 ++++++++++++++++- .../DefaultClearTestStatesService.java | 47 ------------------- .../DefaultInjectConfigValueService.java | 37 --------------- .../services/DefaultLogTestNameService.java | 20 -------- .../services/DefaultMoveTimeService.java | 33 ------------- .../DefaultSetFixedTimestampService.java | 36 -------------- .../DefaultToggleDetailedLoggingService.java | 24 ---------- .../services/InjectConfigValueService.java | 32 ++++++++++++- .../services/LogTestNameService.java | 30 +++++++++++- .../testsupport/services/MoveTimeService.java | 28 ++++++++++- .../services/SetFixedTimestampService.java | 31 +++++++++++- .../ToggleDetailedLoggingService.java | 31 +++++++++++- 14 files changed, 208 insertions(+), 229 deletions(-) delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java delete mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java index 71a74e1c..58b600f3 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java @@ -1,6 +1,10 @@ package com.pkb.common.testsupport.config; +import java.util.Optional; +import java.util.Set; + import com.pkb.common.ClearableInternalState; +import com.pkb.common.config.BaseConfig; import com.pkb.common.config.ConfigStorage; import com.pkb.common.datetime.DateTimeService; import com.pkb.common.testlogging.DetailLoggingProvider; @@ -12,9 +16,6 @@ import com.pkb.common.testsupport.services.SetFixedTimestampService; import com.pkb.common.testsupport.services.ToggleDetailedLoggingService; -import java.util.Optional; -import java.util.Set; - public interface ITestControlServiceConfig { Optional getEmulatorEndpoint(); @@ -39,9 +40,11 @@ public interface ITestControlServiceConfig { boolean getShouldStartListener(); DateTimeService getDateTimeService(); - + ConfigStorage getConfigStorage(); + BaseConfig getBaseConfig(); + Set getClearables(); DetailLoggingProvider getTestLoggingService(); diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java b/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java index ddc8ab8d..aec74cea 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java @@ -1,16 +1,14 @@ package com.pkb.common.testsupport.config; +import java.util.Optional; +import java.util.Set; + import com.pkb.common.ClearableInternalState; +import com.pkb.common.config.BaseConfig; import com.pkb.common.config.ConfigStorage; import com.pkb.common.datetime.DateTimeService; import com.pkb.common.testlogging.DetailLoggingProvider; import com.pkb.common.testsupport.services.ClearTestStatesService; -import com.pkb.common.testsupport.services.DefaultClearTestStatesService; -import com.pkb.common.testsupport.services.DefaultInjectConfigValueService; -import com.pkb.common.testsupport.services.DefaultLogTestNameService; -import com.pkb.common.testsupport.services.DefaultMoveTimeService; -import com.pkb.common.testsupport.services.DefaultSetFixedTimestampService; -import com.pkb.common.testsupport.services.DefaultToggleDetailedLoggingService; import com.pkb.common.testsupport.services.InjectConfigValueService; import com.pkb.common.testsupport.services.LogTestNameService; import com.pkb.common.testsupport.services.MoveTimeService; @@ -18,9 +16,6 @@ import com.pkb.common.testsupport.services.SetFixedTimestampService; import com.pkb.common.testsupport.services.ToggleDetailedLoggingService; -import java.util.Optional; -import java.util.Set; - /** * Configuration required to handle test control requests */ @@ -33,6 +28,7 @@ public class TestControlServiceConfig implements ITestControlServiceConfig { private final boolean shouldStartListener; private final DateTimeService dateTimeService; private final ConfigStorage configStorage; + private final BaseConfig baseConfig; private final Set clearables; private final DetailLoggingProvider testLoggingService; private final PubSubNamespaceService namespaceService; @@ -54,6 +50,7 @@ public TestControlServiceConfig(String applicationName, ConfigStorage configStorage, Set clearables, DetailLoggingProvider testLoggingService, + BaseConfig baseConfig, PubSubNamespaceService namespaceService) { this.applicationName = applicationName; @@ -65,14 +62,15 @@ public TestControlServiceConfig(String applicationName, this.configStorage = configStorage; this.clearables = clearables; this.testLoggingService = testLoggingService; + this.baseConfig = baseConfig; this.namespaceService = namespaceService; - this.setFixedTimestampService = new DefaultSetFixedTimestampService(dateTimeService); - this.moveTimeService = new DefaultMoveTimeService(dateTimeService); - this.injectConfigValueService = new DefaultInjectConfigValueService(configStorage); - this.clearTestStatesService = new DefaultClearTestStatesService(dateTimeService, configStorage, clearables); - this.toggleDetailedLoggingService = new DefaultToggleDetailedLoggingService(testLoggingService); - this.logTestNameService = new DefaultLogTestNameService(); + this.setFixedTimestampService = new SetFixedTimestampService(dateTimeService); + this.moveTimeService = new MoveTimeService(dateTimeService); + this.injectConfigValueService = new InjectConfigValueService(configStorage); + this.clearTestStatesService = new ClearTestStatesService(dateTimeService, configStorage, clearables); + this.logTestNameService = new LogTestNameService(baseConfig); + this.toggleDetailedLoggingService = new ToggleDetailedLoggingService(baseConfig, testLoggingService); } @Override @@ -109,7 +107,12 @@ public DateTimeService getDateTimeService() { public ConfigStorage getConfigStorage() { return configStorage; } - + + @Override + public BaseConfig getBaseConfig() { + return baseConfig; + } + @Override public Set getClearables() { return clearables; diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java index 46033d4d..14b2a223 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java @@ -1,8 +1,46 @@ package com.pkb.common.testsupport.services; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.ClearableInternalState; +import com.pkb.common.config.ConfigStorage; +import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.ClearTestStatesRequest; import com.pkb.pubsub.testsupport.payload.ClearTestStatesResponse; -public interface ClearTestStatesService { - ClearTestStatesResponse process(ClearTestStatesRequest message); +public class ClearTestStatesService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private final DateTimeService dateTimeService; + private final ConfigStorage configStorage; + private final Set clearables; + + public ClearTestStatesService(DateTimeService dateTimeService, ConfigStorage configStorage, Set clearables) { + this.dateTimeService = dateTimeService; + this.configStorage = configStorage; + this.clearables = clearables; + } + + public ClearTestStatesResponse process(ClearTestStatesRequest message) { + LOGGER.info("ClearTestStatesService.process message received"); + + boolean clearFixedTimestamp = message.getClearFixedTimestamp(); + if (clearFixedTimestamp) { + dateTimeService.forgetFixedCurrentTimeForTesting(); + } + + if (configStorage.isMutableConfigEnabled()) { + configStorage.reset(); + } + + clearables.forEach(ClearableInternalState::clearState); + + LOGGER.info("ClearTestStatesService.process done."); + + return ClearTestStatesResponse.newBuilder().build(); + } } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java deleted file mode 100644 index 8bfc9c96..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.pkb.common.testsupport.services; - -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.ClearableInternalState; -import com.pkb.common.config.ConfigStorage; -import com.pkb.common.datetime.DateTimeService; -import com.pkb.pubsub.testsupport.payload.ClearTestStatesRequest; -import com.pkb.pubsub.testsupport.payload.ClearTestStatesResponse; - -public class DefaultClearTestStatesService implements ClearTestStatesService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private final DateTimeService dateTimeService; - private final ConfigStorage configStorage; - private final Set clearables; - - public DefaultClearTestStatesService(DateTimeService dateTimeService, ConfigStorage configStorage, Set clearables) { - this.dateTimeService = dateTimeService; - this.configStorage = configStorage; - this.clearables = clearables; - } - - @Override - public ClearTestStatesResponse process(ClearTestStatesRequest message) { - LOGGER.info("ClearTestStatesService.process message received"); - - boolean clearFixedTimestamp = message.getClearFixedTimestamp(); - if (clearFixedTimestamp) { - dateTimeService.forgetFixedCurrentTimeForTesting(); - } - - if (configStorage.isMutableConfigEnabled()) { - configStorage.reset(); - } - - clearables.forEach(ClearableInternalState::clearState); - - LOGGER.info("ClearTestStatesService.process done."); - - return ClearTestStatesResponse.newBuilder().build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java deleted file mode 100644 index 9d2bd0ab..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.pkb.common.testsupport.services; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.config.ConfigStorage; -import com.pkb.pubsub.testsupport.payload.InjectConfigRequest; -import com.pkb.pubsub.testsupport.payload.InjectConfigResponse; - -public class DefaultInjectConfigValueService implements InjectConfigValueService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private ConfigStorage configStorage; - - public DefaultInjectConfigValueService(ConfigStorage configStorage) { - this.configStorage = configStorage; - } - - @Override - public InjectConfigResponse process(InjectConfigRequest message) { - LOGGER.info("InjectConfigValueService.process message received"); - String key = message.getKey(); - String value = message.getValue(); - LOGGER.info(String.format("InjectConfigValueService.process setting key %s to value %s", key, value)); - - boolean success = true; - if (configStorage.isMutableConfigEnabled()) { - configStorage.setValue(key, value); - } else { - success = false; - } - - LOGGER.info("InjectConfigValueService.process done."); - return InjectConfigResponse.newBuilder().setKey(key).setValue(value).setSuccess(success).build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java deleted file mode 100644 index 77a3908b..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.pkb.common.testsupport.services; - -import com.pkb.pubsub.testsupport.payload.LogTestNameRequest; -import com.pkb.pubsub.testsupport.payload.LogTestNameResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DefaultLogTestNameService implements LogTestNameService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - @Override - public LogTestNameResponse process(LogTestNameRequest message) { - LOGGER.info("LogTestNameService.process message received"); - String testName = message.getTestName(); - LOGGER.info("test name is now {}", testName); - LOGGER.info("LogTestNameService.process done."); - return LogTestNameResponse.newBuilder().setTestName(testName).setSuccess(true).build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java deleted file mode 100644 index 51e0ddf5..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.pkb.common.testsupport.services; - -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.datetime.DateTimeService; -import com.pkb.pubsub.testsupport.payload.MoveTimeRequest; -import com.pkb.pubsub.testsupport.payload.MoveTimeResponse; - -public class DefaultMoveTimeService implements MoveTimeService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private DateTimeService dateTimeService; - - public DefaultMoveTimeService(DateTimeService dateTimeService) { - this.dateTimeService = dateTimeService; - } - - @Override - public MoveTimeResponse process(MoveTimeRequest message) { - LOGGER.info("MoveTimeService.process received message"); - long amount = message.getAmount(); - TemporalUnit unit = ChronoUnit.valueOf(message.getUnit()); - LOGGER.info("MoveTimeService.process moving by {} {}", amount, unit); - dateTimeService.moveTime(amount, unit); - LOGGER.info("MoveTimeService.process done."); - return MoveTimeResponse.newBuilder().setAmount(amount).setUnit(unit.toString()).build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java deleted file mode 100644 index 0219d0d5..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.pkb.common.testsupport.services; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Strings; - -import com.pkb.common.datetime.DateTimeService; -import com.pkb.pubsub.testsupport.payload.SetFixedTimestampRequest; -import com.pkb.pubsub.testsupport.payload.SetFixedTimestampResponse; - -public class DefaultSetFixedTimestampService implements SetFixedTimestampService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private DateTimeService dateTimeService; - - public DefaultSetFixedTimestampService(DateTimeService dateTimeService) { - this.dateTimeService = dateTimeService; - } - - @Override - public SetFixedTimestampResponse process(SetFixedTimestampRequest message) { - LOGGER.info("SetFixedTimestampService.process received message"); - String timestamp = message.getTimestamp(); - LOGGER.info(String.format("SetFixedTimestampService.process timestamp=%s", timestamp)); - if (Strings.isNullOrEmpty(timestamp)) { - dateTimeService.forgetFixedCurrentTimeForTesting(); - } - else { - dateTimeService.setFixedCurrentTimeForTesting(timestamp); - } - LOGGER.info("SetFixedTimestampService.process done."); - return SetFixedTimestampResponse.newBuilder().setTimestamp(timestamp).build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java deleted file mode 100644 index 4b6c487a..00000000 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.pkb.common.testsupport.services; - -import com.pkb.common.testlogging.DetailLoggingProvider; -import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingRequest; -import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DefaultToggleDetailedLoggingService implements ToggleDetailedLoggingService { - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - private final DetailLoggingProvider testLoggingService; - - public DefaultToggleDetailedLoggingService(DetailLoggingProvider testLoggingService) { - this.testLoggingService = testLoggingService; - } - - @Override - public ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message) { - LOGGER.info("ToggleDetailedLoggingService.process message received"); - testLoggingService.setDetailedLoggingRequired(message.getEnableDetailedLogging()); - LOGGER.info("ToggleDetailedLoggingService.process done."); - return ToggleDetailedLoggingResponse.newBuilder().setSuccess(true).build(); - } -} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java index f39e24c9..821a1dbe 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java @@ -1,8 +1,36 @@ package com.pkb.common.testsupport.services; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.config.ConfigStorage; import com.pkb.pubsub.testsupport.payload.InjectConfigRequest; import com.pkb.pubsub.testsupport.payload.InjectConfigResponse; -public interface InjectConfigValueService { - InjectConfigResponse process(InjectConfigRequest message); +public class InjectConfigValueService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private ConfigStorage configStorage; + + public InjectConfigValueService(ConfigStorage configStorage) { + this.configStorage = configStorage; + } + + public InjectConfigResponse process(InjectConfigRequest message) { + LOGGER.info("InjectConfigValueService.process message received"); + String key = message.getKey(); + String value = message.getValue(); + LOGGER.info(String.format("InjectConfigValueService.process setting key %s to value %s", key, value)); + + boolean success = true; + if (configStorage.isMutableConfigEnabled()) { + configStorage.setValue(key, value); + } else { + success = false; + } + + LOGGER.info("InjectConfigValueService.process done."); + return InjectConfigResponse.newBuilder().setKey(key).setValue(value).setSuccess(success).build(); + } } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java index 50ba5257..9d7f5c84 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java @@ -1,8 +1,34 @@ package com.pkb.common.testsupport.services; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.config.BaseConfig; import com.pkb.pubsub.testsupport.payload.LogTestNameRequest; import com.pkb.pubsub.testsupport.payload.LogTestNameResponse; -public interface LogTestNameService { - LogTestNameResponse process(LogTestNameRequest message); +public class LogTestNameService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private final BaseConfig config; + + public LogTestNameService(BaseConfig config) { + this.config = config; + } + + public LogTestNameResponse process(LogTestNameRequest message) { + LOGGER.info("LogTestNameService.process message received"); + String testName = message.getTestName(); + boolean success = true; + if (config.isFakeDateTimeServiceEnabled()) { + LOGGER.info("test name is now {}", testName); + } else { + success = false; + } + + LOGGER.info("LogTestNameService.process done."); + + return LogTestNameResponse.newBuilder().setTestName(testName).setSuccess(success).build(); + } } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java index a3c8050a..12424639 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java @@ -1,8 +1,32 @@ package com.pkb.common.testsupport.services; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.MoveTimeRequest; import com.pkb.pubsub.testsupport.payload.MoveTimeResponse; -public interface MoveTimeService { - MoveTimeResponse process(MoveTimeRequest message); +public class MoveTimeService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private DateTimeService dateTimeService; + + public MoveTimeService(DateTimeService dateTimeService) { + this.dateTimeService = dateTimeService; + } + + public MoveTimeResponse process(MoveTimeRequest message) { + LOGGER.info("MoveTimeService.process received message"); + long amount = message.getAmount(); + TemporalUnit unit = ChronoUnit.valueOf(message.getUnit()); + LOGGER.info("MoveTimeService.process moving by {} {}", amount, unit); + dateTimeService.moveTime(amount, unit); + LOGGER.info("MoveTimeService.process done."); + return MoveTimeResponse.newBuilder().setAmount(amount).setUnit(unit.toString()).build(); + } } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java index 71c8f217..c21f8737 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java @@ -1,8 +1,35 @@ package com.pkb.common.testsupport.services; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Strings; + +import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.SetFixedTimestampRequest; import com.pkb.pubsub.testsupport.payload.SetFixedTimestampResponse; -public interface SetFixedTimestampService { - SetFixedTimestampResponse process(SetFixedTimestampRequest message); +public class SetFixedTimestampService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private DateTimeService dateTimeService; + + public SetFixedTimestampService(DateTimeService dateTimeService) { + this.dateTimeService = dateTimeService; + } + + public SetFixedTimestampResponse process(SetFixedTimestampRequest message) { + LOGGER.info("SetFixedTimestampService.process received message"); + String timestamp = message.getTimestamp(); + LOGGER.info(String.format("SetFixedTimestampService.process timestamp=%s", timestamp)); + if (Strings.isNullOrEmpty(timestamp)) { + dateTimeService.forgetFixedCurrentTimeForTesting(); + } + else { + dateTimeService.setFixedCurrentTimeForTesting(timestamp); + } + LOGGER.info("SetFixedTimestampService.process done."); + return SetFixedTimestampResponse.newBuilder().setTimestamp(timestamp).build(); + } } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java index df28037c..f6c64c7f 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java @@ -1,8 +1,35 @@ package com.pkb.common.testsupport.services; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.config.BaseConfig; +import com.pkb.common.testlogging.DetailLoggingProvider; import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingRequest; import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingResponse; -public interface ToggleDetailedLoggingService { - ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message); +public class ToggleDetailedLoggingService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + private final DetailLoggingProvider testLoggingService; + private final BaseConfig config; + + public ToggleDetailedLoggingService(BaseConfig config, DetailLoggingProvider testLoggingService) { + this.config = config; + this.testLoggingService = testLoggingService; + } + + public ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message) { + LOGGER.info("ToggleDetailedLoggingService.process message received"); + boolean success = true; + if (config.isFakeDateTimeServiceEnabled()) { + testLoggingService.setDetailedLoggingRequired(message.getEnableDetailedLogging()); + } else { + success = false; + } + + LOGGER.info("ToggleDetailedLoggingService.process done."); + + return ToggleDetailedLoggingResponse.newBuilder().setSuccess(success).build(); + } } From 665bf82b58b98f77ac57fbbc018bdaf3ab1e5d62 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 20 Sep 2021 10:55:19 -0400 Subject: [PATCH 2/4] Revert "Revert "PHR-8766 Add interfaces for all services in testsupport module. "" --- .../config/ITestControlServiceConfig.java | 11 ++--- .../config/TestControlServiceConfig.java | 35 +++++++------- .../services/ClearTestStatesService.java | 42 +---------------- .../DefaultClearTestStatesService.java | 47 +++++++++++++++++++ .../DefaultInjectConfigValueService.java | 37 +++++++++++++++ .../services/DefaultLogTestNameService.java | 20 ++++++++ .../services/DefaultMoveTimeService.java | 33 +++++++++++++ .../DefaultSetFixedTimestampService.java | 36 ++++++++++++++ .../DefaultToggleDetailedLoggingService.java | 24 ++++++++++ .../services/InjectConfigValueService.java | 32 +------------ .../services/LogTestNameService.java | 30 +----------- .../testsupport/services/MoveTimeService.java | 28 +---------- .../services/SetFixedTimestampService.java | 31 +----------- .../ToggleDetailedLoggingService.java | 31 +----------- 14 files changed, 229 insertions(+), 208 deletions(-) create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java create mode 100644 testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java index 58b600f3..71a74e1c 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java @@ -1,10 +1,6 @@ package com.pkb.common.testsupport.config; -import java.util.Optional; -import java.util.Set; - import com.pkb.common.ClearableInternalState; -import com.pkb.common.config.BaseConfig; import com.pkb.common.config.ConfigStorage; import com.pkb.common.datetime.DateTimeService; import com.pkb.common.testlogging.DetailLoggingProvider; @@ -16,6 +12,9 @@ import com.pkb.common.testsupport.services.SetFixedTimestampService; import com.pkb.common.testsupport.services.ToggleDetailedLoggingService; +import java.util.Optional; +import java.util.Set; + public interface ITestControlServiceConfig { Optional getEmulatorEndpoint(); @@ -40,11 +39,9 @@ public interface ITestControlServiceConfig { boolean getShouldStartListener(); DateTimeService getDateTimeService(); - + ConfigStorage getConfigStorage(); - BaseConfig getBaseConfig(); - Set getClearables(); DetailLoggingProvider getTestLoggingService(); diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java b/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java index aec74cea..ddc8ab8d 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/config/TestControlServiceConfig.java @@ -1,14 +1,16 @@ package com.pkb.common.testsupport.config; -import java.util.Optional; -import java.util.Set; - import com.pkb.common.ClearableInternalState; -import com.pkb.common.config.BaseConfig; import com.pkb.common.config.ConfigStorage; import com.pkb.common.datetime.DateTimeService; import com.pkb.common.testlogging.DetailLoggingProvider; import com.pkb.common.testsupport.services.ClearTestStatesService; +import com.pkb.common.testsupport.services.DefaultClearTestStatesService; +import com.pkb.common.testsupport.services.DefaultInjectConfigValueService; +import com.pkb.common.testsupport.services.DefaultLogTestNameService; +import com.pkb.common.testsupport.services.DefaultMoveTimeService; +import com.pkb.common.testsupport.services.DefaultSetFixedTimestampService; +import com.pkb.common.testsupport.services.DefaultToggleDetailedLoggingService; import com.pkb.common.testsupport.services.InjectConfigValueService; import com.pkb.common.testsupport.services.LogTestNameService; import com.pkb.common.testsupport.services.MoveTimeService; @@ -16,6 +18,9 @@ import com.pkb.common.testsupport.services.SetFixedTimestampService; import com.pkb.common.testsupport.services.ToggleDetailedLoggingService; +import java.util.Optional; +import java.util.Set; + /** * Configuration required to handle test control requests */ @@ -28,7 +33,6 @@ public class TestControlServiceConfig implements ITestControlServiceConfig { private final boolean shouldStartListener; private final DateTimeService dateTimeService; private final ConfigStorage configStorage; - private final BaseConfig baseConfig; private final Set clearables; private final DetailLoggingProvider testLoggingService; private final PubSubNamespaceService namespaceService; @@ -50,7 +54,6 @@ public TestControlServiceConfig(String applicationName, ConfigStorage configStorage, Set clearables, DetailLoggingProvider testLoggingService, - BaseConfig baseConfig, PubSubNamespaceService namespaceService) { this.applicationName = applicationName; @@ -62,15 +65,14 @@ public TestControlServiceConfig(String applicationName, this.configStorage = configStorage; this.clearables = clearables; this.testLoggingService = testLoggingService; - this.baseConfig = baseConfig; this.namespaceService = namespaceService; - this.setFixedTimestampService = new SetFixedTimestampService(dateTimeService); - this.moveTimeService = new MoveTimeService(dateTimeService); - this.injectConfigValueService = new InjectConfigValueService(configStorage); - this.clearTestStatesService = new ClearTestStatesService(dateTimeService, configStorage, clearables); - this.logTestNameService = new LogTestNameService(baseConfig); - this.toggleDetailedLoggingService = new ToggleDetailedLoggingService(baseConfig, testLoggingService); + this.setFixedTimestampService = new DefaultSetFixedTimestampService(dateTimeService); + this.moveTimeService = new DefaultMoveTimeService(dateTimeService); + this.injectConfigValueService = new DefaultInjectConfigValueService(configStorage); + this.clearTestStatesService = new DefaultClearTestStatesService(dateTimeService, configStorage, clearables); + this.toggleDetailedLoggingService = new DefaultToggleDetailedLoggingService(testLoggingService); + this.logTestNameService = new DefaultLogTestNameService(); } @Override @@ -107,12 +109,7 @@ public DateTimeService getDateTimeService() { public ConfigStorage getConfigStorage() { return configStorage; } - - @Override - public BaseConfig getBaseConfig() { - return baseConfig; - } - + @Override public Set getClearables() { return clearables; diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java index 14b2a223..46033d4d 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/ClearTestStatesService.java @@ -1,46 +1,8 @@ package com.pkb.common.testsupport.services; -import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.ClearableInternalState; -import com.pkb.common.config.ConfigStorage; -import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.ClearTestStatesRequest; import com.pkb.pubsub.testsupport.payload.ClearTestStatesResponse; -public class ClearTestStatesService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private final DateTimeService dateTimeService; - private final ConfigStorage configStorage; - private final Set clearables; - - public ClearTestStatesService(DateTimeService dateTimeService, ConfigStorage configStorage, Set clearables) { - this.dateTimeService = dateTimeService; - this.configStorage = configStorage; - this.clearables = clearables; - } - - public ClearTestStatesResponse process(ClearTestStatesRequest message) { - LOGGER.info("ClearTestStatesService.process message received"); - - boolean clearFixedTimestamp = message.getClearFixedTimestamp(); - if (clearFixedTimestamp) { - dateTimeService.forgetFixedCurrentTimeForTesting(); - } - - if (configStorage.isMutableConfigEnabled()) { - configStorage.reset(); - } - - clearables.forEach(ClearableInternalState::clearState); - - LOGGER.info("ClearTestStatesService.process done."); - - return ClearTestStatesResponse.newBuilder().build(); - } +public interface ClearTestStatesService { + ClearTestStatesResponse process(ClearTestStatesRequest message); } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java new file mode 100644 index 00000000..8bfc9c96 --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultClearTestStatesService.java @@ -0,0 +1,47 @@ +package com.pkb.common.testsupport.services; + +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.ClearableInternalState; +import com.pkb.common.config.ConfigStorage; +import com.pkb.common.datetime.DateTimeService; +import com.pkb.pubsub.testsupport.payload.ClearTestStatesRequest; +import com.pkb.pubsub.testsupport.payload.ClearTestStatesResponse; + +public class DefaultClearTestStatesService implements ClearTestStatesService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private final DateTimeService dateTimeService; + private final ConfigStorage configStorage; + private final Set clearables; + + public DefaultClearTestStatesService(DateTimeService dateTimeService, ConfigStorage configStorage, Set clearables) { + this.dateTimeService = dateTimeService; + this.configStorage = configStorage; + this.clearables = clearables; + } + + @Override + public ClearTestStatesResponse process(ClearTestStatesRequest message) { + LOGGER.info("ClearTestStatesService.process message received"); + + boolean clearFixedTimestamp = message.getClearFixedTimestamp(); + if (clearFixedTimestamp) { + dateTimeService.forgetFixedCurrentTimeForTesting(); + } + + if (configStorage.isMutableConfigEnabled()) { + configStorage.reset(); + } + + clearables.forEach(ClearableInternalState::clearState); + + LOGGER.info("ClearTestStatesService.process done."); + + return ClearTestStatesResponse.newBuilder().build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java new file mode 100644 index 00000000..9d2bd0ab --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultInjectConfigValueService.java @@ -0,0 +1,37 @@ +package com.pkb.common.testsupport.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.config.ConfigStorage; +import com.pkb.pubsub.testsupport.payload.InjectConfigRequest; +import com.pkb.pubsub.testsupport.payload.InjectConfigResponse; + +public class DefaultInjectConfigValueService implements InjectConfigValueService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private ConfigStorage configStorage; + + public DefaultInjectConfigValueService(ConfigStorage configStorage) { + this.configStorage = configStorage; + } + + @Override + public InjectConfigResponse process(InjectConfigRequest message) { + LOGGER.info("InjectConfigValueService.process message received"); + String key = message.getKey(); + String value = message.getValue(); + LOGGER.info(String.format("InjectConfigValueService.process setting key %s to value %s", key, value)); + + boolean success = true; + if (configStorage.isMutableConfigEnabled()) { + configStorage.setValue(key, value); + } else { + success = false; + } + + LOGGER.info("InjectConfigValueService.process done."); + return InjectConfigResponse.newBuilder().setKey(key).setValue(value).setSuccess(success).build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java new file mode 100644 index 00000000..77a3908b --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultLogTestNameService.java @@ -0,0 +1,20 @@ +package com.pkb.common.testsupport.services; + +import com.pkb.pubsub.testsupport.payload.LogTestNameRequest; +import com.pkb.pubsub.testsupport.payload.LogTestNameResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DefaultLogTestNameService implements LogTestNameService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + @Override + public LogTestNameResponse process(LogTestNameRequest message) { + LOGGER.info("LogTestNameService.process message received"); + String testName = message.getTestName(); + LOGGER.info("test name is now {}", testName); + LOGGER.info("LogTestNameService.process done."); + return LogTestNameResponse.newBuilder().setTestName(testName).setSuccess(true).build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java new file mode 100644 index 00000000..51e0ddf5 --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultMoveTimeService.java @@ -0,0 +1,33 @@ +package com.pkb.common.testsupport.services; + +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.pkb.common.datetime.DateTimeService; +import com.pkb.pubsub.testsupport.payload.MoveTimeRequest; +import com.pkb.pubsub.testsupport.payload.MoveTimeResponse; + +public class DefaultMoveTimeService implements MoveTimeService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private DateTimeService dateTimeService; + + public DefaultMoveTimeService(DateTimeService dateTimeService) { + this.dateTimeService = dateTimeService; + } + + @Override + public MoveTimeResponse process(MoveTimeRequest message) { + LOGGER.info("MoveTimeService.process received message"); + long amount = message.getAmount(); + TemporalUnit unit = ChronoUnit.valueOf(message.getUnit()); + LOGGER.info("MoveTimeService.process moving by {} {}", amount, unit); + dateTimeService.moveTime(amount, unit); + LOGGER.info("MoveTimeService.process done."); + return MoveTimeResponse.newBuilder().setAmount(amount).setUnit(unit.toString()).build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java new file mode 100644 index 00000000..0219d0d5 --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultSetFixedTimestampService.java @@ -0,0 +1,36 @@ +package com.pkb.common.testsupport.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Strings; + +import com.pkb.common.datetime.DateTimeService; +import com.pkb.pubsub.testsupport.payload.SetFixedTimestampRequest; +import com.pkb.pubsub.testsupport.payload.SetFixedTimestampResponse; + +public class DefaultSetFixedTimestampService implements SetFixedTimestampService { + + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + + private DateTimeService dateTimeService; + + public DefaultSetFixedTimestampService(DateTimeService dateTimeService) { + this.dateTimeService = dateTimeService; + } + + @Override + public SetFixedTimestampResponse process(SetFixedTimestampRequest message) { + LOGGER.info("SetFixedTimestampService.process received message"); + String timestamp = message.getTimestamp(); + LOGGER.info(String.format("SetFixedTimestampService.process timestamp=%s", timestamp)); + if (Strings.isNullOrEmpty(timestamp)) { + dateTimeService.forgetFixedCurrentTimeForTesting(); + } + else { + dateTimeService.setFixedCurrentTimeForTesting(timestamp); + } + LOGGER.info("SetFixedTimestampService.process done."); + return SetFixedTimestampResponse.newBuilder().setTimestamp(timestamp).build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java new file mode 100644 index 00000000..4b6c487a --- /dev/null +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/DefaultToggleDetailedLoggingService.java @@ -0,0 +1,24 @@ +package com.pkb.common.testsupport.services; + +import com.pkb.common.testlogging.DetailLoggingProvider; +import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingRequest; +import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DefaultToggleDetailedLoggingService implements ToggleDetailedLoggingService { + private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); + private final DetailLoggingProvider testLoggingService; + + public DefaultToggleDetailedLoggingService(DetailLoggingProvider testLoggingService) { + this.testLoggingService = testLoggingService; + } + + @Override + public ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message) { + LOGGER.info("ToggleDetailedLoggingService.process message received"); + testLoggingService.setDetailedLoggingRequired(message.getEnableDetailedLogging()); + LOGGER.info("ToggleDetailedLoggingService.process done."); + return ToggleDetailedLoggingResponse.newBuilder().setSuccess(true).build(); + } +} diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java index 821a1dbe..f39e24c9 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/InjectConfigValueService.java @@ -1,36 +1,8 @@ package com.pkb.common.testsupport.services; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.config.ConfigStorage; import com.pkb.pubsub.testsupport.payload.InjectConfigRequest; import com.pkb.pubsub.testsupport.payload.InjectConfigResponse; -public class InjectConfigValueService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private ConfigStorage configStorage; - - public InjectConfigValueService(ConfigStorage configStorage) { - this.configStorage = configStorage; - } - - public InjectConfigResponse process(InjectConfigRequest message) { - LOGGER.info("InjectConfigValueService.process message received"); - String key = message.getKey(); - String value = message.getValue(); - LOGGER.info(String.format("InjectConfigValueService.process setting key %s to value %s", key, value)); - - boolean success = true; - if (configStorage.isMutableConfigEnabled()) { - configStorage.setValue(key, value); - } else { - success = false; - } - - LOGGER.info("InjectConfigValueService.process done."); - return InjectConfigResponse.newBuilder().setKey(key).setValue(value).setSuccess(success).build(); - } +public interface InjectConfigValueService { + InjectConfigResponse process(InjectConfigRequest message); } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java index 9d7f5c84..50ba5257 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/LogTestNameService.java @@ -1,34 +1,8 @@ package com.pkb.common.testsupport.services; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.config.BaseConfig; import com.pkb.pubsub.testsupport.payload.LogTestNameRequest; import com.pkb.pubsub.testsupport.payload.LogTestNameResponse; -public class LogTestNameService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private final BaseConfig config; - - public LogTestNameService(BaseConfig config) { - this.config = config; - } - - public LogTestNameResponse process(LogTestNameRequest message) { - LOGGER.info("LogTestNameService.process message received"); - String testName = message.getTestName(); - boolean success = true; - if (config.isFakeDateTimeServiceEnabled()) { - LOGGER.info("test name is now {}", testName); - } else { - success = false; - } - - LOGGER.info("LogTestNameService.process done."); - - return LogTestNameResponse.newBuilder().setTestName(testName).setSuccess(success).build(); - } +public interface LogTestNameService { + LogTestNameResponse process(LogTestNameRequest message); } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java index 12424639..a3c8050a 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/MoveTimeService.java @@ -1,32 +1,8 @@ package com.pkb.common.testsupport.services; -import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.MoveTimeRequest; import com.pkb.pubsub.testsupport.payload.MoveTimeResponse; -public class MoveTimeService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private DateTimeService dateTimeService; - - public MoveTimeService(DateTimeService dateTimeService) { - this.dateTimeService = dateTimeService; - } - - public MoveTimeResponse process(MoveTimeRequest message) { - LOGGER.info("MoveTimeService.process received message"); - long amount = message.getAmount(); - TemporalUnit unit = ChronoUnit.valueOf(message.getUnit()); - LOGGER.info("MoveTimeService.process moving by {} {}", amount, unit); - dateTimeService.moveTime(amount, unit); - LOGGER.info("MoveTimeService.process done."); - return MoveTimeResponse.newBuilder().setAmount(amount).setUnit(unit.toString()).build(); - } +public interface MoveTimeService { + MoveTimeResponse process(MoveTimeRequest message); } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java index c21f8737..71c8f217 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/SetFixedTimestampService.java @@ -1,35 +1,8 @@ package com.pkb.common.testsupport.services; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.base.Strings; - -import com.pkb.common.datetime.DateTimeService; import com.pkb.pubsub.testsupport.payload.SetFixedTimestampRequest; import com.pkb.pubsub.testsupport.payload.SetFixedTimestampResponse; -public class SetFixedTimestampService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - - private DateTimeService dateTimeService; - - public SetFixedTimestampService(DateTimeService dateTimeService) { - this.dateTimeService = dateTimeService; - } - - public SetFixedTimestampResponse process(SetFixedTimestampRequest message) { - LOGGER.info("SetFixedTimestampService.process received message"); - String timestamp = message.getTimestamp(); - LOGGER.info(String.format("SetFixedTimestampService.process timestamp=%s", timestamp)); - if (Strings.isNullOrEmpty(timestamp)) { - dateTimeService.forgetFixedCurrentTimeForTesting(); - } - else { - dateTimeService.setFixedCurrentTimeForTesting(timestamp); - } - LOGGER.info("SetFixedTimestampService.process done."); - return SetFixedTimestampResponse.newBuilder().setTimestamp(timestamp).build(); - } +public interface SetFixedTimestampService { + SetFixedTimestampResponse process(SetFixedTimestampRequest message); } diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java b/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java index f6c64c7f..df28037c 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/services/ToggleDetailedLoggingService.java @@ -1,35 +1,8 @@ package com.pkb.common.testsupport.services; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.pkb.common.config.BaseConfig; -import com.pkb.common.testlogging.DetailLoggingProvider; import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingRequest; import com.pkb.pubsub.testsupport.payload.ToggleDetailedLoggingResponse; -public class ToggleDetailedLoggingService { - - private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass()); - private final DetailLoggingProvider testLoggingService; - private final BaseConfig config; - - public ToggleDetailedLoggingService(BaseConfig config, DetailLoggingProvider testLoggingService) { - this.config = config; - this.testLoggingService = testLoggingService; - } - - public ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message) { - LOGGER.info("ToggleDetailedLoggingService.process message received"); - boolean success = true; - if (config.isFakeDateTimeServiceEnabled()) { - testLoggingService.setDetailedLoggingRequired(message.getEnableDetailedLogging()); - } else { - success = false; - } - - LOGGER.info("ToggleDetailedLoggingService.process done."); - - return ToggleDetailedLoggingResponse.newBuilder().setSuccess(success).build(); - } +public interface ToggleDetailedLoggingService { + ToggleDetailedLoggingResponse process(ToggleDetailedLoggingRequest message); } From 0b3724d2e568c36a24a5ca306db6422728681ce2 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Wed, 1 Sep 2021 17:13:21 +0100 Subject: [PATCH 3/4] Added internal timeout to handling for test control messages. Hanging threads will be interrupted, which means that subsequent test control requests shouldn't be affected by still-running threads from the last test control message. PHR-8677 --- .../AbstractTestSupportCamelRouteBuilder.java | 10 +++++++ .../config/ITestControlServiceConfig.java | 26 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/camel/route/AbstractTestSupportCamelRouteBuilder.java b/testsupport/src/main/java/com/pkb/common/testsupport/camel/route/AbstractTestSupportCamelRouteBuilder.java index 94a58298..41686f43 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/camel/route/AbstractTestSupportCamelRouteBuilder.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/camel/route/AbstractTestSupportCamelRouteBuilder.java @@ -81,6 +81,16 @@ public void configure() throws Exception { if (config().getShouldStartListener()) { from(testControlRequestSubscription) .threads(1) //One thread is enough + // We really want to just enable timeout, we don't need circuit breaker in e2e + .circuitBreaker() + .faultToleranceConfiguration() + .timeoutEnabled(config().testControlTimeoutEnabled()) + .timeoutDuration(config().testControlTimeoutMillis()) + .delay(config().testControlHandlerDelayMillis()) + .requestVolumeThreshold(config().testControlHandlerRequestVolumeThreshold()) + .failureRatio(config().testControlHandlerFailureRatio()) + .successThreshold(config().testControlHandlerSuccessThreshold()) + .end() .routeId("testSupportReceiver") .unmarshal().avro(TestControlRequest.getClassSchema()) .log(config().getApplicationName() + ": receiving a ${mandatoryBodyAs(" + TestControlRequest.class.getCanonicalName() + ").getMessageType} request") diff --git a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java index 71a74e1c..f22110d5 100644 --- a/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java +++ b/testsupport/src/main/java/com/pkb/common/testsupport/config/ITestControlServiceConfig.java @@ -39,7 +39,7 @@ public interface ITestControlServiceConfig { boolean getShouldStartListener(); DateTimeService getDateTimeService(); - + ConfigStorage getConfigStorage(); Set getClearables(); @@ -47,4 +47,28 @@ public interface ITestControlServiceConfig { DetailLoggingProvider getTestLoggingService(); PubSubNamespaceService getNamespaceService(); + + default boolean testControlTimeoutEnabled() { + return true; + } + + default long testControlTimeoutMillis() { + return 30000; + } + + default long testControlHandlerDelayMillis() { + return 0; + } + + default int testControlHandlerRequestVolumeThreshold() { + return 1000; + } + + default int testControlHandlerFailureRatio() { + return 100; + } + + default int testControlHandlerSuccessThreshold() { + return 100; + } } From 82c6f3e71853a2a8586eb1868233e576503860a9 Mon Sep 17 00:00:00 2001 From: Lucian Holland Date: Fri, 17 Sep 2021 17:52:58 +0200 Subject: [PATCH 4/4] Simple auto-advancing DateTimeService for unit/integration tests --- .../AutoAdvanceFakeDateTimeService.java | 28 ++++++++++ .../pkb/common/datetime/DateTimeService.java | 16 +++--- .../datetime/DefaultDateTimeService.java | 6 +++ .../common/datetime/FakeDateTimeService.java | 16 ++++-- .../AutoAdvanceFakeDateTimeServiceTest.java | 51 +++++++++++++++++++ 5 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 date-time/src/main/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeService.java create mode 100644 date-time/src/test/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeServiceTest.java diff --git a/date-time/src/main/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeService.java b/date-time/src/main/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeService.java new file mode 100644 index 00000000..ab66c054 --- /dev/null +++ b/date-time/src/main/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeService.java @@ -0,0 +1,28 @@ +package com.pkb.common.datetime; + +import java.time.Clock; +import java.time.temporal.TemporalAmount; + +/** + * Override of FakeDateTimeService that supports automatically advancing + * time by a fixed amount each time the services is asked for a time. + * DO NOT USE in E2E under any circumstances, it will cause extreme + * flakiness. This is intended for use in unit and integration tests only + * for classes/subsystems that use temporal ordering. + */ +public class AutoAdvanceFakeDateTimeService extends FakeDateTimeService { + + private TemporalAmount autoAdvanceDuration = null; + + @Override + public Clock clock() { + if (this.currentFixedClock != null && autoAdvanceDuration != null) { + moveTime(autoAdvanceDuration); + } + return super.clock(); + } + + public void setAutoAdvanceDuration(TemporalAmount autoAdvanceDuration) { + this.autoAdvanceDuration = autoAdvanceDuration; + } +} diff --git a/date-time/src/main/java/com/pkb/common/datetime/DateTimeService.java b/date-time/src/main/java/com/pkb/common/datetime/DateTimeService.java index 50d1f61d..ac9e25bb 100644 --- a/date-time/src/main/java/com/pkb/common/datetime/DateTimeService.java +++ b/date-time/src/main/java/com/pkb/common/datetime/DateTimeService.java @@ -1,5 +1,7 @@ package com.pkb.common.datetime; +import io.vavr.Tuple2; + import java.text.ParsePosition; import java.time.Clock; import java.time.Instant; @@ -12,11 +14,10 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalUnit; import java.util.Date; -import io.vavr.Tuple2; - @SuppressWarnings({ "UseOfObsoleteDateTimeApi", "SSBasedInspection" }) public interface DateTimeService { @@ -33,11 +34,12 @@ public interface DateTimeService { void moveTime(long amountToAdd, TemporalUnit unit); + void moveTime(TemporalAmount duration); + long nowNanoTime(); /** - * @throws IllegalStateException - * outside of testing environments + * @throws IllegalStateException outside of testing environments */ void forgetFixedCurrentTimeForTesting(); @@ -88,9 +90,6 @@ default ZonedDateTime firstDayOfMonth() { /** * DO NOT USE THIS! Only here temporarily to fix up some legacy (static) code. - * @param input - * @param formatter - * @return */ static Tuple2 parseToInstantBackwardCompatibleWayStatic(String input, DateTimeFormatter formatter){ ParsePosition remainder = new ParsePosition(0); @@ -103,9 +102,6 @@ default Tuple2 parseToInstantBackwardCompatibleWay(Strin /** * @deprecated Use {@link java.time} instead of {@link Date}, then there is no need for this method. - * @param input - * @param formatter - * @return */ @Deprecated default String dateToString(Date input, DateTimeFormatter formatter) { diff --git a/date-time/src/main/java/com/pkb/common/datetime/DefaultDateTimeService.java b/date-time/src/main/java/com/pkb/common/datetime/DefaultDateTimeService.java index 14e8f20d..4ae09b32 100644 --- a/date-time/src/main/java/com/pkb/common/datetime/DefaultDateTimeService.java +++ b/date-time/src/main/java/com/pkb/common/datetime/DefaultDateTimeService.java @@ -1,6 +1,7 @@ package com.pkb.common.datetime; import java.time.Clock; +import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalUnit; public class DefaultDateTimeService implements DateTimeService { @@ -20,6 +21,11 @@ public void moveTime(long amountToAdd, TemporalUnit unit) { throw new IllegalStateException("Not currently in a test environment"); } + @Override + public void moveTime(TemporalAmount duration) { + throw new IllegalStateException("Not currently in a test environment"); + } + @Override public long nowNanoTime() { return System.nanoTime(); diff --git a/date-time/src/main/java/com/pkb/common/datetime/FakeDateTimeService.java b/date-time/src/main/java/com/pkb/common/datetime/FakeDateTimeService.java index 132bfe50..95a207de 100644 --- a/date-time/src/main/java/com/pkb/common/datetime/FakeDateTimeService.java +++ b/date-time/src/main/java/com/pkb/common/datetime/FakeDateTimeService.java @@ -1,20 +1,21 @@ package com.pkb.common.datetime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.lang.invoke.MethodHandles; import java.time.Clock; import java.time.Instant; import java.time.ZonedDateTime; +import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalUnit; import java.util.concurrent.TimeUnit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class FakeDateTimeService implements DateTimeService { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - private volatile ZonedDateTime currentFixedTime; - private volatile Clock currentFixedClock; + protected volatile ZonedDateTime currentFixedTime; + protected volatile Clock currentFixedClock; private final DateTimeService fallbackService; @@ -44,6 +45,11 @@ public void moveTime(long amountToAdd, TemporalUnit unit) { fixTime(currentFixedTime.plus(amountToAdd, unit)); } + @Override + public void moveTime(TemporalAmount duration) { + fixTime(currentFixedTime.plus(duration)); + } + @Override public long nowNanoTime() { Instant now = now(); diff --git a/date-time/src/test/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeServiceTest.java b/date-time/src/test/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeServiceTest.java new file mode 100644 index 00000000..72d91c42 --- /dev/null +++ b/date-time/src/test/java/com/pkb/common/datetime/AutoAdvanceFakeDateTimeServiceTest.java @@ -0,0 +1,51 @@ +package com.pkb.common.datetime; + +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.not; + +class AutoAdvanceFakeDateTimeServiceTest { + + private AutoAdvanceFakeDateTimeService underTest = new AutoAdvanceFakeDateTimeService(); + + @Test + void testAdvancesTimeBetweenCalls() { + underTest.setFixedCurrentTimeForTesting("2020-01-01T00:00:00Z"); + underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); + + assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); + assertThat(underTest.now(), equalTo(Instant.parse("2020-01-03T00:00:00Z"))); + } + + @Test + void testStopsAdvancingWhenCleared() { + underTest.setFixedCurrentTimeForTesting("2020-01-01T00:00:00Z"); + underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); + + assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); + underTest.setAutoAdvanceDuration(null); + assertThat(underTest.now(), equalTo(Instant.parse("2020-01-02T00:00:00Z"))); + } + + @Test + void testDoesNothingIfTimeNotFixed() throws InterruptedException { + + underTest.setAutoAdvanceDuration(Duration.of(1, ChronoUnit.DAYS)); + + Instant start = underTest.now(); + Thread.sleep(1); + Instant end = underTest.now(); + + + assertThat(start, not(end)); + assertThat(Duration.between(start, end), lessThan(Duration.of(1, ChronoUnit.SECONDS))); + } + +}