Replacement of RuntimeException with Exception class
authorkjaniak <kornel.janiak@nokia.com>
Thu, 27 Feb 2020 08:56:08 +0000 (09:56 +0100)
committerkjaniak <kornel.janiak@nokia.com>
Thu, 27 Feb 2020 10:27:46 +0000 (11:27 +0100)
Clean up in tests and run method.

Issue-ID: AAF-996
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: I2abbfa9af4a77960cb65e9b9ecfcb058eb69cf12

certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java
certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitCode.java
certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitableException.java
certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/KeyPairFactory.java
certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java
certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java
certServiceClient/src/test/java/org/onap/aaf/certservice/client/CertServiceClientTest.java
certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/KeyPairFactoryTest.java
certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/ClientConfigurationFactoryTest.java
certServiceClient/src/test/java/org/onap/aaf/certservice/client/configuration/model/CsrConfigurationFactoryTest.java

index ac1062a..f886784 100644 (file)
@@ -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());
     }
+
 }
index aed9f3f..295738f 100644 (file)
@@ -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);
index e884d11..51981a4 100644 (file)
@@ -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);
     }
index 6ad6528..6413686 100644 (file)
@@ -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) {
index 9592ac3..beccd38 100644 (file)
  * 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);
     }
 }
index 28a5cf4..2464cc5 100644 (file)
 
 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;
 }
index 22baab5..9e73301 100644 (file)
@@ -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
index b92660f..6a4741a 100644 (file)
@@ -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);
index 7cf9e0c..f355de1 100644 (file)
@@ -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);
index 4a4eb24..d6bf431 100644 (file)
@@ -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);