Clean up in tests and run method.
Issue-ID: AAF-996
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: I2abbfa9af4a77960cb65e9b9ecfcb058eb69cf12
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;
}
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());
}
+
}
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);
*/
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);
}
this.keySize = keySize;
}
- public KeyPair create() {
+ public KeyPair create() throws KeyPairGenerationException {
try {
return createKeyPairGenerator().generateKeyPair();
} catch (NoSuchAlgorithmException e) {
* 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);
}
}
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;
}
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
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);
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);
}
@Test
- void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() {
+ void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException {
// given
when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID);
when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
@Test
- void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() {
+ void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
// given
when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID);
}
@Test
- void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() {
+ void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
// given
when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
when(envsForCsr.getState()).thenReturn(STATE_VALID);