mirror of
https://github.com/brmlab/wekan-mailer.git
synced 2025-06-08 18:04:13 +02:00
Refactor evn configuration
This commit is contained in:
parent
6b284ab003
commit
06e65bc708
6 changed files with 60 additions and 45 deletions
|
@ -1,7 +1,7 @@
|
|||
package cz.brmlab.wm;
|
||||
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
import cz.brmlab.wm.wekan.WekanConfiguration;
|
||||
import cz.brmlab.wm.config.WekanConfiguration;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
|
||||
import cz.brmlab.wm.wekan.rest.CardPost;
|
||||
import cz.brmlab.wm.wekan.rest.LoginPost;
|
||||
|
|
25
src/main/java/cz/brmlab/wm/config/EnvConfig.java
Normal file
25
src/main/java/cz/brmlab/wm/config/EnvConfig.java
Normal file
|
@ -0,0 +1,25 @@
|
|||
package cz.brmlab.wm.config;
|
||||
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.Exceptions.ExitCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
interface EnvConfig {
|
||||
|
||||
/**
|
||||
* Checks if environment variables does contains keys from provided list of keys.
|
||||
*
|
||||
* @param props List of required environment properties.
|
||||
* @throws BrmException if required property key is missing in environment variables.
|
||||
*/
|
||||
default void checkProps(List<String> props) throws BrmException {
|
||||
for (String prop : props) {
|
||||
if (System.getenv(prop) == null) {
|
||||
String message = ExitCode.CONFIGURATION_MISSING.getReason() + prop;
|
||||
throw new BrmException(message, ExitCode.CONFIGURATION_MISSING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package cz.brmlab.wm.wekan;
|
||||
package cz.brmlab.wm.config;
|
||||
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.Exceptions.ExitCode;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
@ -10,37 +9,17 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class WekanConfiguration {
|
||||
public class WekanConfiguration implements EnvConfig {
|
||||
|
||||
//ENV variables for wekan
|
||||
private static final String WEKAN_URL = "WEKAN_URL";
|
||||
private static final String WEKAN_USER = "WEKAN_USER";
|
||||
private static final String WEKAN_PASSWORD = "WEKAN_PASSWORD";
|
||||
private static final String WEKAN_PASS = "WEKAN_PASS";
|
||||
private static final String WEKAN_TARGET_BOARD = "WEKAN_TARGET_BOARD";
|
||||
private static final String WEKAN_TARGET_LIST = "WEKAN_TARGET_LIST";
|
||||
|
||||
//List of wekan ENV vars
|
||||
private static final List<String> properties = new ArrayList<>(Arrays.asList(WEKAN_URL, WEKAN_USER, WEKAN_PASSWORD, WEKAN_TARGET_BOARD, WEKAN_TARGET_LIST));
|
||||
|
||||
/**
|
||||
* Configuration for wekan. Taken from the container ENV variables.
|
||||
*
|
||||
* @throws BrmException if some of the properties is missing in ENV variables.
|
||||
*/
|
||||
public WekanConfiguration() throws BrmException {
|
||||
log.trace("{}() - start.", this.getClass().getSimpleName());
|
||||
|
||||
for (String prop : properties) {
|
||||
checkProp(prop);
|
||||
}
|
||||
this.wekanUrl = System.getenv(WEKAN_URL);
|
||||
this.wekanUser = System.getenv(WEKAN_USER);
|
||||
this.wekanPassword = System.getenv(WEKAN_PASSWORD);
|
||||
this.wekanBoard = System.getenv(WEKAN_TARGET_BOARD);
|
||||
this.wekanList = System.getenv(WEKAN_TARGET_LIST);
|
||||
|
||||
log.info("Wekan config loaded successfully.");
|
||||
}
|
||||
private static final List<String> properties = new ArrayList<>(Arrays.asList(WEKAN_URL, WEKAN_USER, WEKAN_PASS, WEKAN_TARGET_BOARD, WEKAN_TARGET_LIST));
|
||||
|
||||
@Getter
|
||||
private String wekanUrl;
|
||||
|
@ -57,13 +36,23 @@ public class WekanConfiguration {
|
|||
@Getter
|
||||
private String wekanList;
|
||||
|
||||
private void checkProp(String prop) throws BrmException {
|
||||
log.trace("checkProp({}) - start.", prop);
|
||||
/**
|
||||
* Configuration for wekan. Taken from the container ENV variables.
|
||||
*
|
||||
* @throws BrmException if some of the properties is missing in ENV variables.
|
||||
*/
|
||||
public WekanConfiguration() throws BrmException {
|
||||
log.trace("{}() - start.", this.getClass().getSimpleName());
|
||||
|
||||
if (System.getenv(prop) == null) {
|
||||
String message = ExitCode.CONFIGURATION_MISSING.getReason() + prop;
|
||||
log.error(message, ExitCode.CONFIGURATION_MISSING);
|
||||
throw new BrmException(message, ExitCode.CONFIGURATION_MISSING);
|
||||
}
|
||||
checkProps(properties);
|
||||
|
||||
this.wekanUrl = System.getenv(WEKAN_URL);
|
||||
this.wekanUser = System.getenv(WEKAN_USER);
|
||||
this.wekanPassword = System.getenv(WEKAN_PASS);
|
||||
this.wekanBoard = System.getenv(WEKAN_TARGET_BOARD);
|
||||
this.wekanList = System.getenv(WEKAN_TARGET_LIST);
|
||||
|
||||
log.info("Wekan config loaded successfully.");
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@ package cz.brmlab.wm.wekan.rest;
|
|||
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.Exceptions.ExitCode;
|
||||
import cz.brmlab.wm.wekan.WekanConfiguration;
|
||||
import cz.brmlab.wm.config.WekanConfiguration;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardRequest;
|
||||
import cz.brmlab.wm.wekan.pojo.card.PostCardResponse;
|
||||
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package cz.brmlab.wm.wekan.rest;
|
||||
|
||||
import cz.brmlab.wm.utils.LogMarker.LogMarker;
|
||||
import cz.brmlab.wm.wekan.WekanConfiguration;
|
||||
import cz.brmlab.wm.config.WekanConfiguration;
|
||||
import cz.brmlab.wm.wekan.pojo.login.LoginRequest;
|
||||
import cz.brmlab.wm.wekan.pojo.login.LoginToken;
|
||||
import lombok.Getter;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cz.brmlab.wm.wekan;
|
||||
package cz.brmlab.wm.config;
|
||||
|
||||
import cz.brmlab.wm.config.WekanConfiguration;
|
||||
import cz.brmlab.wm.utils.Exceptions.BrmException;
|
||||
import cz.brmlab.wm.utils.Exceptions.ExitCode;
|
||||
import org.junit.After;
|
||||
|
@ -21,8 +22,8 @@ public class WekanConfigurationTest {
|
|||
private static final String WEKAN_USER = "WEKAN_USER";
|
||||
private static final String WEKAN_USER_VALUE = "someuser";
|
||||
|
||||
private static final String WEKAN_PASSWORD = "WEKAN_PASSWORD";
|
||||
private static final String WEKAN_PASSWORD_VALUE = "somepass";
|
||||
private static final String WEKAN_PASS = "WEKAN_PASS";
|
||||
private static final String WEKAN_PASS_VALUE = "somepass";
|
||||
|
||||
private static final String WEKAN_TARGET_BOARD = "WEKAN_TARGET_BOARD";
|
||||
private static final String WEKAN_TARGET_BOARD_VALUE = "someboardif";
|
||||
|
@ -33,14 +34,14 @@ public class WekanConfigurationTest {
|
|||
|
||||
@After
|
||||
public void cleanEnvVars() {
|
||||
environmentVariables.clear(WEKAN_URL, WEKAN_USER, WEKAN_PASSWORD, WEKAN_TARGET_BOARD, WEKAN_TARGET_LIST);
|
||||
environmentVariables.clear(WEKAN_URL, WEKAN_USER, WEKAN_PASS, WEKAN_TARGET_BOARD, WEKAN_TARGET_LIST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configurationOk() {
|
||||
environmentVariables.set(WEKAN_URL, WEKAN_URL_VALUE);
|
||||
environmentVariables.set(WEKAN_USER, WEKAN_USER_VALUE);
|
||||
environmentVariables.set(WEKAN_PASSWORD, WEKAN_PASSWORD_VALUE);
|
||||
environmentVariables.set(WEKAN_PASS, WEKAN_PASS_VALUE);
|
||||
environmentVariables.set(WEKAN_TARGET_BOARD, WEKAN_TARGET_BOARD_VALUE);
|
||||
environmentVariables.set(WEKAN_TARGET_LIST, WEKAN_TARGET_LIST_VALUE);
|
||||
|
||||
|
@ -53,7 +54,7 @@ public class WekanConfigurationTest {
|
|||
}
|
||||
assertEquals(WEKAN_URL_VALUE, configuration.getWekanUrl());
|
||||
assertEquals(WEKAN_USER_VALUE, configuration.getWekanUser());
|
||||
assertEquals(WEKAN_PASSWORD_VALUE, configuration.getWekanPassword());
|
||||
assertEquals(WEKAN_PASS_VALUE, configuration.getWekanPassword());
|
||||
assertEquals(WEKAN_TARGET_BOARD_VALUE, configuration.getWekanBoard());
|
||||
assertEquals(WEKAN_TARGET_LIST_VALUE, configuration.getWekanList());
|
||||
}
|
||||
|
@ -63,7 +64,7 @@ public class WekanConfigurationTest {
|
|||
WekanConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new WekanConfiguration();
|
||||
fail("Missing whole configuration should not throw an error!");
|
||||
fail("Missing whole configuration should throw an error!");
|
||||
} catch (BrmException ignored) {
|
||||
|
||||
}
|
||||
|
@ -75,16 +76,16 @@ public class WekanConfigurationTest {
|
|||
|
||||
environmentVariables.set(WEKAN_URL, WEKAN_URL_VALUE);
|
||||
environmentVariables.set(WEKAN_USER, WEKAN_USER_VALUE);
|
||||
//environmentVariables.set(WEKAN_PASSWORD, WEKAN_PASSWORD_VALUE);
|
||||
//environmentVariables.set(WEKAN_PASS, WEKAN_PASS_VALUE);
|
||||
environmentVariables.set(WEKAN_TARGET_BOARD, WEKAN_TARGET_BOARD_VALUE);
|
||||
environmentVariables.set(WEKAN_TARGET_LIST, WEKAN_TARGET_LIST_VALUE);
|
||||
|
||||
WekanConfiguration configuration = null;
|
||||
try {
|
||||
configuration = new WekanConfiguration();
|
||||
fail("Missing one property in configuration should not throw an error!");
|
||||
fail("Missing one property in configuration should throw an error!");
|
||||
} catch (BrmException ex) {
|
||||
assertEquals(ExitCode.CONFIGURATION_MISSING.getReason() + WEKAN_PASSWORD, ex.getMessage());
|
||||
assertEquals(ExitCode.CONFIGURATION_MISSING.getReason() + WEKAN_PASS, ex.getMessage());
|
||||
}
|
||||
assertNull(null, configuration);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue