diff options
Diffstat (limited to 'certServiceClient/src')
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java | 63 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java | 42 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java | 11 | ||||
-rw-r--r-- | certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java | 12 | ||||
-rw-r--r-- | certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactoryTest.java (renamed from certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtilsTest.java) | 47 |
5 files changed, 72 insertions, 103 deletions
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 deleted file mode 100644 index b0405274..00000000 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.client.configuration; - -import java.util.regex.Pattern; - -public final class EnvValidationUtils { - - private EnvValidationUtils() {} - - public static Boolean isPathValid(String path) { - return path.matches("^/|(/[a-zA-Z0-9_-]+)+/?$"); - } - - public static Boolean isAlphaNumeric(String caName) { - return caName.matches("^[a-zA-Z0-9]*$"); - } - - public static Boolean isCountryValid(String country) { - return country.matches("^([A-Z][A-Z])$"); - } - - public static Boolean isCommonNameValid(String commonName) { - return !isSpecialCharsPresent(commonName) && - !isHttpProtocolsPresent(commonName) && - !isIpAddressPresent(commonName) && - !isPortNumberPresent(commonName); - } - - static Boolean isPortNumberPresent(String stringToCheck) { - return Pattern.compile(":[0-9]{1,5}").matcher(stringToCheck).find(); - } - - static Boolean isIpAddressPresent(String stringToCheck) { - return Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").matcher(stringToCheck).find(); - } - - static Boolean isHttpProtocolsPresent(String stringToCheck) { - return Pattern.compile("[h][t][t][p][:][/][/]|[h][t][t][p][s][:][/][/]").matcher(stringToCheck).find(); - } - - public static Boolean isSpecialCharsPresent(String stringToCheck) { - return Pattern.compile("[~#@*$+%!()?/{}<>\\|_^]").matcher(stringToCheck).find(); - } -} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java index 2464cc58..a914891f 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java @@ -24,6 +24,44 @@ import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurati import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.model.ConfigurationModel; -public interface AbstractConfigurationFactory<T extends ConfigurationModel> { - T create() throws ClientConfigurationException, CsrConfigurationException; +import java.util.regex.Pattern; + +public abstract class AbstractConfigurationFactory<T extends ConfigurationModel> { + + abstract T create() throws ClientConfigurationException, CsrConfigurationException; + + public boolean isPathValid(String path) { + return path.matches("^/|(/[a-zA-Z0-9_-]+)+/?$"); + } + + public boolean isAlphaNumeric(String caName) { + return caName.matches("^[a-zA-Z0-9]*$"); + } + + public boolean isCountryValid(String country) { + return country.matches("^([A-Z][A-Z])$"); + } + + public boolean isCommonNameValid(String commonName) { + return !isSpecialCharsPresent(commonName) && + !isHttpProtocolsPresent(commonName) && + !isIpAddressPresent(commonName) && + !isPortNumberPresent(commonName); + } + + public boolean isSpecialCharsPresent(String stringToCheck) { + return Pattern.compile("[~#@*$+%!()?/{}<>\\|_^]").matcher(stringToCheck).find(); + } + + private boolean isPortNumberPresent(String stringToCheck) { + return Pattern.compile(":[0-9]{1,5}").matcher(stringToCheck).find(); + } + + private boolean isIpAddressPresent(String stringToCheck) { + return Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").matcher(stringToCheck).find(); + } + + private boolean isHttpProtocolsPresent(String stringToCheck) { + return Pattern.compile("[h][t][t][p][:][/][/]|[h][t][t][p][s][:][/][/]").matcher(stringToCheck).find(); + } } 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 3bd15288..26a2b1b1 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 @@ -20,20 +20,15 @@ package org.onap.aaf.certservice.client.configuration.factory; - - -import java.util.Optional; import org.onap.aaf.certservice.client.configuration.ClientConfigurationEnvs; -import org.onap.aaf.certservice.client.configuration.EnvValidationUtils; import org.onap.aaf.certservice.client.configuration.EnvsForClient; import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurationException; import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration; -public class ClientConfigurationFactory implements AbstractConfigurationFactory<ClientConfiguration> { +public class ClientConfigurationFactory extends AbstractConfigurationFactory<ClientConfiguration> { private final EnvsForClient envsForClient; - public ClientConfigurationFactory(EnvsForClient envsForClient) { this.envsForClient = envsForClient; } @@ -50,12 +45,12 @@ public class ClientConfigurationFactory implements AbstractConfigurationFactory< .map(timeout -> configuration.setRequestTimeout(Integer.valueOf(timeout))); envsForClient.getOutputPath() - .filter(EnvValidationUtils::isPathValid) + .filter(this::isPathValid) .map(configuration::setCertsOutputPath) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); envsForClient.getCaName() - .filter(EnvValidationUtils::isAlphaNumeric) + .filter(this::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 1e5d5c53..a6e86187 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 @@ -21,14 +21,11 @@ package org.onap.aaf.certservice.client.configuration.factory; import org.onap.aaf.certservice.client.configuration.CsrConfigurationEnvs; -import org.onap.aaf.certservice.client.configuration.EnvValidationUtils; import org.onap.aaf.certservice.client.configuration.EnvsForCsr; import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration; -import java.util.Optional; - -public class CsrConfigurationFactory implements AbstractConfigurationFactory<CsrConfiguration> { +public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrConfiguration> { private final EnvsForCsr envsForCsr; @@ -37,19 +34,18 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory<Csr this.envsForCsr = envsForCsr; } - @Override public CsrConfiguration create() throws CsrConfigurationException { CsrConfiguration configuration = new CsrConfiguration(); envsForCsr.getCommonName() - .filter(EnvValidationUtils::isCommonNameValid) + .filter(this::isCommonNameValid) .map(configuration::setCommonName) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")); envsForCsr.getOrganization() - .filter(org -> !EnvValidationUtils.isSpecialCharsPresent(org)) + .filter(org -> !isSpecialCharsPresent(org)) .map(configuration::setOrganization) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")); @@ -58,7 +54,7 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory<Csr .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); envsForCsr.getCountry() - .filter(EnvValidationUtils::isCountryValid) + .filter(this::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtilsTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactoryTest.java index 36b3b1a4..edefc4de 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtilsTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactoryTest.java @@ -1,4 +1,5 @@ -/*============LICENSE_START======================================================= +/* + * ============LICENSE_START======================================================= * aaf-certservice-client * ================================================================================ * Copyright (C) 2020 Nokia. All rights reserved. @@ -16,62 +17,64 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - -package org.onap.aaf.certservice.client.configuration; +package org.onap.aaf.certservice.client.configuration.factory; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.Mockito; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +public class AbstractConfigurationFactoryTest { -class EnvValidationUtilsTest { + private AbstractConfigurationFactory cut = mock(AbstractConfigurationFactory.class, Mockito.CALLS_REAL_METHODS); @ParameterizedTest @ValueSource(strings = {"/var/log", "/", "/var/log/", "/second_var", "/second-var"}) - public void shouldAcceptValidPath(String path){ - assertTrue(EnvValidationUtils.isPathValid(path)); + public void shouldAcceptValidPath(String path) { + assertThat(cut.isPathValid(path)).isTrue(); } @ParameterizedTest @ValueSource(strings = {"/var/log?", "", "var_", "var", "//", "/var//log"}) - public void shouldRejectInvalidPath(String path){ - assertFalse(EnvValidationUtils.isPathValid(path)); + public void shouldRejectInvalidPath(String path) { + assertThat(cut.isPathValid(path)).isFalse(); } @ParameterizedTest @ValueSource(strings = {"PL", "DE", "PT", "US"}) - public void shouldAcceptValidCountryCode(String countryCode){ - assertTrue(EnvValidationUtils.isCountryValid(countryCode)); + public void shouldAcceptValidCountryCode(String countryCode) { + assertThat(cut.isCountryValid(countryCode)).isTrue(); } @ParameterizedTest @ValueSource(strings = {"1P", "PLP", "P#", "&*"}) - public void shouldRejectInvalidCountryCode(String countryCode){ - assertFalse(EnvValidationUtils.isCountryValid(countryCode)); + public void shouldRejectInvalidCountryCode(String countryCode) { + assertThat(cut.isCountryValid(countryCode)).isFalse(); } @ParameterizedTest @ValueSource(strings = {"caname", "caname1", "123caName", "ca1name"}) - public void shouldAcceptValidAlphanumeric(String caName){ - assertTrue(EnvValidationUtils.isAlphaNumeric(caName)); + public void shouldAcceptValidAlphanumeric(String caName) { + assertThat(cut.isAlphaNumeric(caName)).isTrue(); } @ParameterizedTest @ValueSource(strings = {"44caname$", "#caname1", "1c_aname", "ca1-name"}) - public void shouldRejectInvalidAlphanumeric(String caName){ - assertFalse(EnvValidationUtils.isAlphaNumeric(caName)); + public void shouldRejectInvalidAlphanumeric(String caName) { + assertThat(cut.isAlphaNumeric(caName)).isFalse(); } @ParameterizedTest @ValueSource(strings = {"example.com", "www.example.com"}) - public void shouldAcceptValidCommonName(String commonName){ - assertTrue(EnvValidationUtils.isCommonNameValid(commonName)); + public void shouldAcceptValidCommonName(String commonName) { + assertThat(cut.isCommonNameValid(commonName)).isTrue(); } @ParameterizedTest @ValueSource(strings = {"https://example.com", "http://example.com", "example.com:8080", "0.0.0.0", "@#$%.com"}) - public void shouldRejectInvalidCommonName(String commonName){ - assertFalse(EnvValidationUtils.isCommonNameValid(commonName)); + public void shouldRejectInvalidCommonName(String commonName) { + assertThat(cut.isCommonNameValid(commonName)).isFalse(); } }
\ No newline at end of file |