Skip to content

Commit

Permalink
chore: Use Lombok + parameterise logging (#169)
Browse files Browse the repository at this point in the history
* Some misc test code cleanup too.
  • Loading branch information
gazbert committed Nov 14, 2024
1 parent 3327108 commit aee6ce3
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ public ConfigurationManager() {
*/
public synchronized <T> T loadConfig(final Class<T> configClass, String yamlConfigFile) {

log.info("Loading configuration for [" + configClass + "] from: " + yamlConfigFile + " ...");
log.info("Loading configuration for [{}] from: {} ...", configClass, yamlConfigFile);

try (final FileInputStream fileInputStream = new FileInputStream(yamlConfigFile)) {

final LoaderOptions options = new LoaderOptions();
final Yaml yaml = new Yaml(new Constructor(configClass, options));
final T requestedConfig = yaml.load(fileInputStream);

log.info("Loaded and set configuration for [" + configClass + "] successfully!");
log.info("Loaded and set configuration for [{}] successfully!", configClass);
return requestedConfig;

} catch (IOException e) {
Expand All @@ -104,7 +104,7 @@ public synchronized <T> T loadConfig(final Class<T> configClass, String yamlConf
*/
public synchronized <T> void saveConfig(Class<T> configClass, T config, String yamlConfigFile) {

log.info("Saving configuration for [" + configClass + "] to: " + yamlConfigFile + " ...");
log.info("Saving configuration for [{}] to: {} ...", configClass, yamlConfigFile);

try (final FileOutputStream fileOutputStream = new FileOutputStream(yamlConfigFile);
final PrintWriter writer =
Expand All @@ -119,7 +119,7 @@ public synchronized <T> void saveConfig(Class<T> configClass, T config, String y
final StringBuilder sb = new StringBuilder(YAML_HEADER);
sb.append(yaml.dumpAs(config, Tag.MAP, DumperOptions.FlowStyle.BLOCK));

log.debug("YAML file content:\n" + sb);
log.debug("YAML file content:\n{}", sb);
writer.print(sb);

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
package com.gazbert.bxbot.datastore.yaml.emailalerts;

import com.gazbert.bxbot.domain.emailalerts.EmailAlertsConfig;
import lombok.Getter;
import lombok.Setter;

/**
* Wraps Email Alerts config for dumping to and loading from YAML.
*
* @author gazbert
*/
@Setter
@Getter
public class EmailAlertsType {

private EmailAlertsConfig emailAlerts;
Expand All @@ -38,22 +42,4 @@ public class EmailAlertsType {
public EmailAlertsType() {
// No extra init needed.
}

/**
* Returns the email alerts config.
*
* @return the email alerts config.
*/
public EmailAlertsConfig getEmailAlerts() {
return emailAlerts;
}

/**
* Sets the email alerts config.
*
* @param emailAlerts the email alerts config.
*/
public void setEmailAlerts(EmailAlertsConfig emailAlerts) {
this.emailAlerts = emailAlerts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
package com.gazbert.bxbot.datastore.yaml.engine;

import com.gazbert.bxbot.domain.engine.EngineConfig;
import lombok.Getter;
import lombok.Setter;

/**
* Wraps Engine config for dumping to and loading from YAML.
*
* @author gazbert
*/
@Setter
@Getter
public class EngineType {

private EngineConfig engine;
Expand All @@ -38,22 +42,4 @@ public class EngineType {
public EngineType() {
// No extra init needed.
}

/**
* Returns the engine config.
*
* @return the engine config.
*/
public EngineConfig getEngine() {
return engine;
}

/**
* Sets the engine config.
*
* @param engine the engine config.
*/
public void setEngine(EngineConfig engine) {
this.engine = engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
package com.gazbert.bxbot.datastore.yaml.exchange;

import com.gazbert.bxbot.domain.exchange.ExchangeConfig;
import lombok.Getter;
import lombok.Setter;

/**
* Wraps Exchange config for dumping to and loading from YAML.
*
* @author gazbert
*/
@Setter
@Getter
public class ExchangeType {

private ExchangeConfig exchange;
Expand All @@ -38,22 +42,4 @@ public class ExchangeType {
public ExchangeType() {
// No extra init needed.
}

/**
* Returns the exchange config.
*
* @return the exchange config.
*/
public ExchangeConfig getExchange() {
return exchange;
}

/**
* Sets the exchange config.
*
* @param exchange the exchange config.
*/
public void setExchange(ExchangeConfig exchange) {
this.exchange = exchange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import com.gazbert.bxbot.domain.market.MarketConfig;
import java.util.ArrayList;
import java.util.List;
import lombok.Setter;

/**
* Wraps a list of Market configs for dumping to and loading from YAML.
*
* @author gazbert
*/
@Setter
public class MarketsType {

private List<MarketConfig> markets;
Expand All @@ -52,13 +54,4 @@ public List<MarketConfig> getMarkets() {
}
return markets;
}

/**
* Sets the markets config.
*
* @param markets the markets' config.
*/
public void setMarkets(List<MarketConfig> markets) {
this.markets = markets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,7 @@ void testLoadingInvalidYamlConfigFileThrowsException() {

@Test
void testSavingConfigToXmlIsSuccessful() throws Exception {
final SmtpConfig smtpConfig = new SmtpConfig();
smtpConfig.setAccountUsername(ACCOUNT_USERNAME);
smtpConfig.setAccountPassword(ACCOUNT_PASSWORD);
smtpConfig.setHost(HOST);
smtpConfig.setTlsPort(TLS_PORT);
smtpConfig.setFromAddress(FROM_ADDRESS);
smtpConfig.setToAddress(TO_ADDRESS);

final EmailAlertsConfig emailAlertsConfig = new EmailAlertsConfig();
emailAlertsConfig.setEnabled(true);
emailAlertsConfig.setSmtpConfig(smtpConfig);

final EmailAlertsType emailAlertsType = new EmailAlertsType();
emailAlertsType.setEmailAlerts(emailAlertsConfig);
final EmailAlertsType emailAlertsType = getAlertsType();

final ConfigurationManager configurationManager = new ConfigurationManager();

Expand Down Expand Up @@ -149,6 +136,22 @@ void testSavingConfigToXmlIsSuccessful() throws Exception {

@Test
void testSavingConfigToInvalidYamlFileIsHandled() {
final EmailAlertsType emailAlertsType = getAlertsType();

final ConfigurationManager configurationManager = new ConfigurationManager();

assertThrows(
IllegalStateException.class,
() ->
configurationManager.saveConfig(
EmailAlertsType.class, emailAlertsType, INVALID_YAML_CONFIG_TO_SAVE_FILENAME));
}

// --------------------------------------------------------------------------
// Canned data
// --------------------------------------------------------------------------

private static EmailAlertsType getAlertsType() {
final SmtpConfig smtpConfig = new SmtpConfig();
smtpConfig.setAccountUsername(ACCOUNT_USERNAME);
smtpConfig.setAccountPassword(ACCOUNT_PASSWORD);
Expand All @@ -163,13 +166,6 @@ void testSavingConfigToInvalidYamlFileIsHandled() {

final EmailAlertsType emailAlertsType = new EmailAlertsType();
emailAlertsType.setEmailAlerts(emailAlertsConfig);

final ConfigurationManager configurationManager = new ConfigurationManager();

assertThrows(
IllegalStateException.class,
() ->
configurationManager.saveConfig(
EmailAlertsType.class, emailAlertsType, INVALID_YAML_CONFIG_TO_SAVE_FILENAME));
return emailAlertsType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,7 @@ void testSavingConfigToYamlIsSuccessful() throws Exception {
networkConfig.setNonFatalErrorCodes(NON_FATAL_ERROR_CODES);
networkConfig.setNonFatalErrorMessages(NON_FATAL_ERROR_MESSAGES);

final Map<String, String> otherConfig = new HashMap<>();
otherConfig.put(BUY_FEE_CONFIG_ITEM_KEY, BUY_FEE_CONFIG_ITEM_VALUE);
otherConfig.put(SELL_FEE_CONFIG_ITEM_KEY, SELL_FEE_CONFIG_ITEM_VALUE);

final ExchangeConfig exchangeConfig = new ExchangeConfig();
exchangeConfig.setName(EXCHANGE_NAME);
exchangeConfig.setAdapter(EXCHANGE_ADAPTER);
exchangeConfig.setAuthenticationConfig(authenticationConfig);
exchangeConfig.setNetworkConfig(networkConfig);
exchangeConfig.setOtherConfig(otherConfig);
final ExchangeConfig exchangeConfig = getExchangeConfig(authenticationConfig, networkConfig);

final ExchangeType exchangeType = new ExchangeType();
exchangeType.setExchange(exchangeConfig);
Expand Down Expand Up @@ -227,16 +218,7 @@ void testSavingConfigToInvalidYamlFileIsHandled() {
networkConfig.setNonFatalErrorCodes(NON_FATAL_ERROR_CODES);
networkConfig.setNonFatalErrorMessages(NON_FATAL_ERROR_MESSAGES);

final Map<String, String> otherConfig = new HashMap<>();
otherConfig.put(BUY_FEE_CONFIG_ITEM_KEY, BUY_FEE_CONFIG_ITEM_VALUE);
otherConfig.put(SELL_FEE_CONFIG_ITEM_KEY, SELL_FEE_CONFIG_ITEM_VALUE);

final ExchangeConfig exchangeConfig = new ExchangeConfig();
exchangeConfig.setName(EXCHANGE_NAME);
exchangeConfig.setAdapter(EXCHANGE_ADAPTER);
exchangeConfig.setAuthenticationConfig(authenticationConfig);
exchangeConfig.setNetworkConfig(networkConfig);
exchangeConfig.setOtherConfig(otherConfig);
final ExchangeConfig exchangeConfig = getExchangeConfig(authenticationConfig, networkConfig);

final ExchangeType exchangeType = new ExchangeType();
exchangeType.setExchange(exchangeConfig);
Expand All @@ -249,4 +231,23 @@ void testSavingConfigToInvalidYamlFileIsHandled() {
configurationManager.saveConfig(
ExchangeType.class, exchangeType, INVALID_YAML_CONFIG_TO_SAVE_FILENAME));
}

// --------------------------------------------------------------------------
// Canned data
// --------------------------------------------------------------------------

private static ExchangeConfig getExchangeConfig(
Map<String, String> authenticationConfig, NetworkConfig networkConfig) {
final Map<String, String> otherConfig = new HashMap<>();
otherConfig.put(BUY_FEE_CONFIG_ITEM_KEY, BUY_FEE_CONFIG_ITEM_VALUE);
otherConfig.put(SELL_FEE_CONFIG_ITEM_KEY, SELL_FEE_CONFIG_ITEM_VALUE);

final ExchangeConfig exchangeConfig = new ExchangeConfig();
exchangeConfig.setName(EXCHANGE_NAME);
exchangeConfig.setAdapter(EXCHANGE_ADAPTER);
exchangeConfig.setAuthenticationConfig(authenticationConfig);
exchangeConfig.setNetworkConfig(networkConfig);
exchangeConfig.setOtherConfig(otherConfig);
return exchangeConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

package com.gazbert.bxbot.datastore.yaml.market;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

package com.gazbert.bxbot.datastore.yaml.strategy;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -160,16 +160,7 @@ void testLoadingInvalidYamlConfigFileThrowsException() {
@Test
void testSavingConfigToYamlIsSuccessful() throws Exception {
// Strat 1
final StrategyConfig strategy1 = new StrategyConfig();
strategy1.setId(STRAT_ID_1);
strategy1.setName(STRAT_NAME_1);
strategy1.setDescription(STRAT_DESCRIPTION_1);
strategy1.setClassName(STRAT_CLASSNAME_1);

final Map<String, String> strat1ConfigItems = new HashMap<>();
strat1ConfigItems.put(BUY_PRICE_CONFIG_ITEM_KEY, BUY_PRICE_CONFIG_ITEM_VALUE);
strat1ConfigItems.put(AMOUNT_TO_BUY_CONFIG_ITEM_KEY, AMOUNT_TO_BUY_CONFIG_ITEM_VALUE);
strategy1.setConfigItems(strat1ConfigItems);
final StrategyConfig strategy1 = getStrategyConfig();

// Strat 2
final StrategyConfig strategy2 = new StrategyConfig();
Expand Down Expand Up @@ -247,6 +238,20 @@ void testSavingConfigToYamlIsSuccessful() throws Exception {
Files.delete(FileSystems.getDefault().getPath(YAML_CONFIG_TO_SAVE_FILENAME));
}

private static StrategyConfig getStrategyConfig() {
final StrategyConfig strategy1 = new StrategyConfig();
strategy1.setId(STRAT_ID_1);
strategy1.setName(STRAT_NAME_1);
strategy1.setDescription(STRAT_DESCRIPTION_1);
strategy1.setClassName(STRAT_CLASSNAME_1);

final Map<String, String> strat1ConfigItems = new HashMap<>();
strat1ConfigItems.put(BUY_PRICE_CONFIG_ITEM_KEY, BUY_PRICE_CONFIG_ITEM_VALUE);
strat1ConfigItems.put(AMOUNT_TO_BUY_CONFIG_ITEM_KEY, AMOUNT_TO_BUY_CONFIG_ITEM_VALUE);
strategy1.setConfigItems(strat1ConfigItems);
return strategy1;
}

@Test
void testSavingConfigToInvalidYamlFileIsHandled() {
// Strat 1
Expand Down

0 comments on commit aee6ce3

Please sign in to comment.