diff options
Diffstat (limited to 'certServiceClient/src')
8 files changed, 105 insertions, 110 deletions
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java index beccd383..25a152bf 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java @@ -19,8 +19,15 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; + public class EnvProvider { - public String readEnvVariable(String envVariable) { - return System.getProperty(envVariable); + public Optional<String> readEnvVariable(String envVariableName) { + return Optional.ofNullable(System.getenv(envVariableName)) + .filter(EnvProvider::isEnvPresent); + } + + private static Boolean isEnvPresent(String envValue) { + return !"".equals(envValue); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java index b87df5be..e65d688d 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java @@ -34,10 +34,6 @@ public final class EnvValidationUtils { return caName.matches("^[a-zA-Z0-9]*$"); } - public static Boolean isEnvExists(String envValue) { - return envValue != null && !"".equals(envValue); - } - public static Boolean isCountryValid(String country) { return country.matches("^([A-Z][A-Z])$"); } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java index aad64f5b..1ce76370 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java @@ -19,33 +19,31 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; + public class EnvsForClient { - private static final EnvProvider envProvider = new EnvProvider(); - private final String urlToCertService; - private final String requestTimeOut; - private final String outputPath; - private final String caName; + private final EnvProvider envProvider = new EnvProvider(); public EnvsForClient() { - this.urlToCertService = envProvider.readEnvVariable(ClientConfigurationEnvs.REQUEST_URL.toString()); - this.requestTimeOut = envProvider.readEnvVariable(ClientConfigurationEnvs.REQUEST_TIMEOUT.toString()); - this.outputPath = envProvider.readEnvVariable(ClientConfigurationEnvs.OUTPUT_PATH.toString()); - this.caName = envProvider.readEnvVariable(ClientConfigurationEnvs.CA_NAME.toString()); } - public String getUrlToCertService() { - return urlToCertService; + public Optional<String> getUrlToCertService() { + return readEnv(ClientConfigurationEnvs.REQUEST_URL); + } + + public Optional<String> getRequestTimeOut() { + return readEnv(ClientConfigurationEnvs.REQUEST_TIMEOUT); } - public String getRequestTimeOut() { - return requestTimeOut; + public Optional<String> getOutputPath() { + return readEnv(ClientConfigurationEnvs.OUTPUT_PATH); } - public String getOutputPath() { - return outputPath; + public Optional<String> getCaName() { + return readEnv(ClientConfigurationEnvs.CA_NAME); } - public String getCaName() { - return caName; + private Optional<String> readEnv(ClientConfigurationEnvs envName) { + return envProvider.readEnvVariable(envName.toString()); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java index 0c948d3f..77efc198 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java @@ -19,52 +19,42 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; public class EnvsForCsr { private final EnvProvider envProvider = new EnvProvider(); - private String commonName; - private String organization; - private String organizationUnit; - private String location; - private String state; - private String country; - private String subjectAlternativesName; - public EnvsForCsr() { - this.commonName = envProvider.readEnvVariable(CsrConfigurationEnvs.COMMON_NAME.toString()); - this.organization = envProvider.readEnvVariable(CsrConfigurationEnvs.ORGANIZATION.toString()); - this.organizationUnit = envProvider.readEnvVariable(CsrConfigurationEnvs.ORGANIZATION_UNIT.toString()); - this.location = envProvider.readEnvVariable(CsrConfigurationEnvs.LOCATION.toString()); - this.state = envProvider.readEnvVariable(CsrConfigurationEnvs.STATE.toString()); - this.country = envProvider.readEnvVariable(CsrConfigurationEnvs.COUNTRY.toString()); - this.subjectAlternativesName = envProvider.readEnvVariable(CsrConfigurationEnvs.SANS.toString()); + public EnvsForCsr() {} + + public Optional<String> getCommonName() { + return readEnv(CsrConfigurationEnvs.COMMON_NAME); } - public String getCommonName() { - return commonName; + public Optional<String> getOrganization() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION); } - public String getOrganization() { - return organization; + public Optional<String> getOrganizationUnit() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION_UNIT); } - public String getOrganizationUnit() { - return organizationUnit; + public Optional<String> getLocation() { + return readEnv(CsrConfigurationEnvs.LOCATION); } - public String getLocation() { - return location; + public Optional<String> getState() { + return readEnv(CsrConfigurationEnvs.STATE); } - public String getState() { - return state; + public Optional<String> getCountry() { + return readEnv(CsrConfigurationEnvs.COUNTRY); } - public String getCountry() { - return country; + public Optional<String> getSubjectAlternativesName() { + return readEnv(CsrConfigurationEnvs.SANS); } - public String getSubjectAlternativesName() { - return subjectAlternativesName; + private Optional<String> readEnv(CsrConfigurationEnvs envName) { + return envProvider.readEnvVariable(envName.toString()); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java index 96b1fb8b..b7ee5d32 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java @@ -42,18 +42,18 @@ public class ClientConfigurationFactory implements AbstractConfigurationFactory< ClientConfiguration configuration = new ClientConfiguration(); - Optional.ofNullable(envsForClient.getUrlToCertService()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getUrlToCertService() .map(configuration::setUrlToCertService); - Optional.ofNullable(envsForClient.getRequestTimeOut()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getRequestTimeOut() .map(timeout -> configuration.setRequestTimeout(Integer.valueOf(timeout))); - Optional.ofNullable(envsForClient.getOutputPath()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getOutputPath() .filter(EnvValidationUtils::isPathValid) .map(configuration::setCertsOutputPath) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); - Optional.ofNullable(envsForClient.getCaName()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getCaName() .filter(EnvValidationUtils::isAlphaNumeric) .map(configuration::setCaName) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.CA_NAME + " is invalid.")); diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java index 61e1b3c3..1e5d5c53 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java @@ -43,32 +43,32 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory<Csr CsrConfiguration configuration = new CsrConfiguration(); - Optional.ofNullable(envsForCsr.getCommonName()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getCommonName() .filter(EnvValidationUtils::isCommonNameValid) .map(configuration::setCommonName) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")); - Optional.ofNullable(envsForCsr.getOrganization()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getOrganization() .filter(org -> !EnvValidationUtils.isSpecialCharsPresent(org)) .map(configuration::setOrganization) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")); - Optional.ofNullable(envsForCsr.getState()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getState() .map(configuration::setState) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); - Optional.ofNullable(envsForCsr.getCountry()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getCountry() .filter(EnvValidationUtils::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); - Optional.ofNullable(envsForCsr.getOrganizationUnit()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getOrganizationUnit() .map(configuration::setOrganizationUnit); - Optional.ofNullable(envsForCsr.getLocation()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getLocation() .map(configuration::setLocation); - Optional.ofNullable(envsForCsr.getSubjectAlternativesName()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getSubjectAlternativesName() .map(configuration::setSubjectAlternativeNames); return configuration; diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/ClientConfigurationFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/ClientConfigurationFactoryTest.java index f355de1a..2c875c24 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/ClientConfigurationFactoryTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/ClientConfigurationFactoryTest.java @@ -26,6 +26,8 @@ import org.onap.aaf.certservice.client.configuration.EnvsForClient; import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurationException; import org.onap.aaf.certservice.client.configuration.factory.ClientConfigurationFactory; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; @@ -33,22 +35,22 @@ import static org.mockito.Mockito.when; public class ClientConfigurationFactoryTest { - final String CA_NAME_VALID = "caaaftest2"; - final String TIME_OUT_VALID = "30000"; - final String OUTPUT_PATH_VALID = "/opt/app/osaaf"; - final String URL_TO_CERT_SERVICE_VALID = "http://cert-service:8080/v1/certificate/"; - final String CA_NAME_INVALID = "caaaftest2#$"; - final String OUTPUT_PATH_INVALID = "/opt//app/osaaf"; + private final String CA_NAME_VALID = "caaaftest2"; + private final String TIME_OUT_VALID = "30000"; + private final String OUTPUT_PATH_VALID = "/opt/app/osaaf"; + private final String URL_TO_CERT_SERVICE_VALID = "http://cert-service:8080/v1/certificate/"; + private final String CA_NAME_INVALID = "caaaftest2#$"; + private final String OUTPUT_PATH_INVALID = "/opt//app/osaaf"; private EnvsForClient envsForClient = mock(EnvsForClient.class); @Test void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws ClientConfigurationException { // given - when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID); - when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); - when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID); - when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID); + when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID)); + when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID)); + when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID)); + when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID)); // when ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create(); @@ -63,8 +65,8 @@ public class ClientConfigurationFactoryTest { @Test void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException { // given - when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID); - when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); + when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID)); + when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID)); // when ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create(); @@ -79,7 +81,7 @@ public class ClientConfigurationFactoryTest { @Test void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() { // given - when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); + when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID)); // when ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient); @@ -93,10 +95,10 @@ public class ClientConfigurationFactoryTest { @Test void create_shouldReturnClientExceptionWhenCANameContainsSpecialCharacters() { // given - when(envsForClient.getCaName()).thenReturn(CA_NAME_INVALID); - when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); - when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID); - when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID); + when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_INVALID)); + when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID)); + when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID)); + when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID)); // when ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient); @@ -110,10 +112,10 @@ public class ClientConfigurationFactoryTest { @Test void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() { // given - when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID); - when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_INVALID); - when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID); - when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID); + when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID)); + when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_INVALID)); + when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID)); + when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID)); // when ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java index d6bf431b..32298e9d 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java @@ -26,6 +26,8 @@ import org.onap.aaf.certservice.client.configuration.EnvsForCsr; import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory; +import java.util.Optional; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; @@ -33,14 +35,14 @@ import static org.mockito.Mockito.when; public class CsrConfigurationFactoryTest { - final String COMMON_NAME_VALID = "onap.org"; - final String SANS_VALID = "test-name"; - final String COUNTRY_VALID = "US"; - final String LOCATION_VALID = "San-Francisco"; - final String ORGANIZATION_VALID = "Linux-Foundation"; - final String ORGANIZATION_UNIT_VALID = "ONAP"; - final String STATE_VALID = "California"; - final String COMMON_NAME_INVALID = "onap.org*&"; + private final String COMMON_NAME_VALID = "onap.org"; + private final String SANS_VALID = "test-name"; + private final String COUNTRY_VALID = "US"; + private final String LOCATION_VALID = "San-Francisco"; + private final String ORGANIZATION_VALID = "Linux-Foundation"; + private final String ORGANIZATION_UNIT_VALID = "ONAP"; + private final String STATE_VALID = "California"; + private final String COMMON_NAME_INVALID = "onap.org*&"; private EnvsForCsr envsForCsr = mock(EnvsForCsr.class); @@ -48,13 +50,13 @@ public class CsrConfigurationFactoryTest { @Test void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException { // given - when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID); - when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID); - when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID); - when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID); - when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID); - when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID); - when(envsForCsr.getState()).thenReturn(STATE_VALID); + when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID)); + when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID)); + when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID)); + when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID)); + when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID)); + when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID)); + when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID)); // when CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create(); @@ -72,10 +74,10 @@ public class CsrConfigurationFactoryTest { @Test void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException { // given - when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID); - when(envsForCsr.getState()).thenReturn(STATE_VALID); - when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID); - when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID); + when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID)); + when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID)); + when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID)); + when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID)); // when CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create(); @@ -91,13 +93,13 @@ public class CsrConfigurationFactoryTest { @Test void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() { // given - when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_INVALID); - when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID); - when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID); - when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID); - when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID); - when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID); - when(envsForCsr.getState()).thenReturn(SANS_VALID); + when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID)); + when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID)); + when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID)); + when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID)); + when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID)); + when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID)); + when(envsForCsr.getState()).thenReturn(Optional.of(SANS_VALID)); // when CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr); |