diff options
author | kjaniak <kornel.janiak@nokia.com> | 2020-02-27 09:56:08 +0100 |
---|---|---|
committer | kjaniak <kornel.janiak@nokia.com> | 2020-02-27 11:27:46 +0100 |
commit | 5b1830d2eec4eddbeaeea9b4076404fbdb78e5e2 (patch) | |
tree | edee4ec658596157dbd149df23e76b9dd72c0f02 /certServiceClient | |
parent | 628ed81f0e56f7163b08b57a8d54833b646239d5 (diff) |
Replacement of RuntimeException with Exception class
Clean up in tests and run method.
Issue-ID: AAF-996
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: I2abbfa9af4a77960cb65e9b9ecfcb058eb69cf12
Diffstat (limited to 'certServiceClient')
10 files changed, 26 insertions, 57 deletions
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java index ac1062a0..f8867846 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java @@ -29,8 +29,8 @@ import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration; import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration; import java.security.KeyPair; -import java.util.Optional; +import static org.onap.aaf.certservice.client.api.ExitCode.SUCCESS_EXIT_CODE; import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE; import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM; @@ -42,23 +42,15 @@ public class CertServiceClient { } public void run() { - ClientConfiguration clientConfiguration; - CsrConfiguration csrConfiguration; - clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create(); - csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create(); - KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE); - Optional<KeyPair> keyPair = generateKeyPair(keyPairFactory); - - appExitHandler.exit(0); - } - - public Optional<KeyPair> generateKeyPair(KeyPairFactory keyPairFactory) { try { - return Optional.of(keyPairFactory.create()); + ClientConfiguration clientConfiguration = new ClientConfigurationFactory(new EnvsForClient()).create(); + CsrConfiguration csrConfiguration = new CsrConfigurationFactory(new EnvsForCsr()).create(); + KeyPair keyPair = keyPairFactory.create(); } catch (ExitableException e) { appExitHandler.exit(e.applicationExitCode()); } - return Optional.empty(); + appExitHandler.exit(SUCCESS_EXIT_CODE.getValue()); } + } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java index aed9f3fe..295738f4 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java @@ -19,6 +19,7 @@ package org.onap.aaf.certservice.client.api; public enum ExitCode { + SUCCESS_EXIT_CODE(0), CLIENT_CONFIGURATION_EXCEPTION(1), CSR_CONFIGURATION_EXCEPTION(2), KEY_PAIR_GENERATION_EXCEPTION(3); diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java index e884d11a..51981a48 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java @@ -18,7 +18,7 @@ */ package org.onap.aaf.certservice.client.api; -public abstract class ExitableException extends RuntimeException { +public abstract class ExitableException extends Exception { public ExitableException(Throwable e) { super(e); } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java index 6ad6528a..64136863 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java @@ -37,7 +37,7 @@ public class KeyPairFactory { this.keySize = keySize; } - public KeyPair create() { + public KeyPair create() throws KeyPairGenerationException { try { return createKeyPairGenerator().generateKeyPair(); } catch (NoSuchAlgorithmException e) { 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 9592ac3e..beccd383 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 @@ -17,14 +17,10 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.aaf.certservice.client.configuration; - -import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurationException; - public class EnvProvider { - public String readEnvVariable(String envVariable) throws ClientConfigurationException { + public String readEnvVariable(String envVariable) { return System.getProperty(envVariable); } } 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 28a5cf41..2464cc58 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 @@ -20,8 +20,10 @@ package org.onap.aaf.certservice.client.configuration.factory; +import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurationException; +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(); + T create() throws ClientConfigurationException, CsrConfigurationException; } diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java index 22baab50..9e733017 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java @@ -22,48 +22,26 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; -import org.onap.aaf.certservice.client.certification.KeyPairFactory; -import java.security.KeyPair; -import java.util.Optional; - -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.KEY_SIZE; -import static org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM; +import static org.onap.aaf.certservice.client.api.ExitCode.CLIENT_CONFIGURATION_EXCEPTION; +import static org.onap.aaf.certservice.client.api.ExitCode.SUCCESS_EXIT_CODE; @ExtendWith(MockitoExtension.class) class CertServiceClientTest { - private static final int DUMMY_EXIT_CODE = 888; @Spy AppExitHandler appExitHandler = new AppExitHandler(); - - @Test - public void shouldExitWithDefinedExitCode_onGenerateKeyPairCallWhereExitableExceptionIsThrown() { - // given - KeyPairFactory keyPairFactory = mock(KeyPairFactory.class); - when(keyPairFactory.create()).thenThrow(new DummyExitableException()); - doNothing().when(appExitHandler).exit(DUMMY_EXIT_CODE); - CertServiceClient certServiceClient = new CertServiceClient(appExitHandler); - // when - Optional<KeyPair> keyPair = certServiceClient.generateKeyPair(keyPairFactory); - // then - verify(appExitHandler).exit(DUMMY_EXIT_CODE); - assertThat(keyPair).isEmpty(); - } - @Test - public void shouldReturnKeyPair_onGenerateKeyPairCall() { + public void shouldExitWithDefinedExitCode_onRunCallWhenNoEnvsPresent() { // given - KeyPairFactory keyPairFactory = new KeyPairFactory(RSA_ENCRYPTION_ALGORITHM, KEY_SIZE); + doNothing().when(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION.getValue()); + doNothing().when(appExitHandler).exit(SUCCESS_EXIT_CODE.getValue()); CertServiceClient certServiceClient = new CertServiceClient(appExitHandler); // when - Optional<KeyPair> keyPair = certServiceClient.generateKeyPair(keyPairFactory); + certServiceClient.run(); // then - assertThat(keyPair).hasValueSatisfying(value -> assertThat(value).isInstanceOf(KeyPair.class)); + verify(appExitHandler).exit(CLIENT_CONFIGURATION_EXCEPTION.getValue()); + verify(appExitHandler).exit(SUCCESS_EXIT_CODE.getValue()); } - }
\ No newline at end of file diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java index b92660fa..6a4741a2 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java @@ -30,7 +30,7 @@ class KeyPairFactoryTest { private static final String NOT_EXISTING_ENCRYPTION_ALGORITHM = "FAKE_ALGORITHM"; @Test - public void shouldProvideKeyPair_whenCreateKeyPairCalledWithCorrectArguments() { + public void shouldProvideKeyPair_whenCreateKeyPairCalledWithCorrectArguments() throws KeyPairGenerationException { // given KeyPairFactory keyPairFactory = new KeyPairFactory(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM, EncryptionAlgorithmConstants.KEY_SIZE); 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 7cf9e0ce..f355de1a 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 @@ -43,7 +43,7 @@ public class ClientConfigurationFactoryTest { private EnvsForClient envsForClient = mock(EnvsForClient.class); @Test - void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() { + void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws ClientConfigurationException { // given when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID); when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); @@ -61,7 +61,7 @@ public class ClientConfigurationFactoryTest { } @Test - void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() { + void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException { // given when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID); when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID); 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 4a4eb247..d6bf431b 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 @@ -46,7 +46,7 @@ public class CsrConfigurationFactoryTest { @Test - void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() { + void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException { // given when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID); when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID); @@ -70,7 +70,7 @@ public class CsrConfigurationFactoryTest { } @Test - void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() { + void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException { // given when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID); when(envsForCsr.getState()).thenReturn(STATE_VALID); |