From e3283b7f953eca13ae68933b6d7b8cceb237acc4 Mon Sep 17 00:00:00 2001 From: Remigiusz Janeczek Date: Wed, 9 Jun 2021 12:12:30 +0200 Subject: Fix sonar issues Issue-ID: OOM-2764 Signed-off-by: Remigiusz Janeczek Change-Id: Iab71cbcac1982207e6f29b4b046280ad27143e03 --- .../postprocessor/merger/model/PemTruststore.java | 11 +++++----- .../postprocessor/merger/model/Truststore.java | 4 ++-- .../postprocessor/common/PasswordReaderTest.java | 5 +++-- .../path/DelimitedPathsSplitterTest.java | 21 ++++++++++--------- .../postprocessor/copier/KeystoreCopierTest.java | 24 +++++++++++----------- .../postprocessor/merger/model/TruststoreTest.java | 16 +++------------ 6 files changed, 36 insertions(+), 45 deletions(-) (limited to 'certServicePostProcessor') diff --git a/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststore.java b/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststore.java index d7f4bfd2..642721cc 100644 --- a/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststore.java +++ b/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststore.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -37,10 +37,10 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator; import org.bouncycastle.util.io.pem.PemObjectGenerator; import org.bouncycastle.util.io.pem.PemWriter; +import org.onap.oom.certservice.postprocessor.common.FileTools; import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException; import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException; import org.onap.oom.certservice.postprocessor.merger.exception.WriteTruststoreFileException; -import org.onap.oom.certservice.postprocessor.common.FileTools; import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias; import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAliasFactory; import org.slf4j.Logger; @@ -110,8 +110,8 @@ public class PemTruststore extends Truststore { private List extractCertificatesFromFile() throws TruststoreDataOperationException { try (FileInputStream inputStream = new FileInputStream(storeFile)) { Security.addProvider(new BouncyCastleProvider()); - CertificateFactory factory = CertificateFactory.getInstance(X_509_CERTIFICATE, BOUNCY_CASTLE_PROVIDER); - return new ArrayList<>(factory.generateCertificates(inputStream)); + CertificateFactory certFactory = CertificateFactory.getInstance(X_509_CERTIFICATE, BOUNCY_CASTLE_PROVIDER); + return new ArrayList<>(certFactory.generateCertificates(inputStream)); } catch (Exception e) { LOGGER.error("Cannot read certificates from file: {}", storeFile.getPath()); throw new TruststoreDataOperationException(e); @@ -145,8 +145,7 @@ public class PemTruststore extends Truststore { } private void appendToFile(String certificatesAsString) throws WriteTruststoreFileException { - try { - FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE); + try (FileOutputStream fileOutputStream = new FileOutputStream(storeFile, APPEND_TO_FILE)) { fileOutputStream.write(certificatesAsString.getBytes()); } catch (Exception e) { LOGGER.error("Cannot write certificates to file"); diff --git a/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/Truststore.java b/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/Truststore.java index 058613a9..307fc9e6 100644 --- a/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/Truststore.java +++ b/certServicePostProcessor/src/main/java/org/onap/oom/certservice/postprocessor/merger/model/Truststore.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -30,7 +30,7 @@ public abstract class Truststore { private final FileTools fileTools; - public Truststore(File storeFile, FileTools fileTools) { + protected Truststore(File storeFile, FileTools fileTools) { this.storeFile = storeFile; this.fileTools = fileTools; } diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java index 697eaa83..1e229821 100644 --- a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java +++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -37,7 +37,8 @@ class PasswordReaderTest { @Test void shouldThrowExceptionForNonExistingFile() { + final File file = new File("src/test/resources/non-esisting-file.pass"); assertThatExceptionOfType(PasswordReaderException.class) - .isThrownBy(() -> PasswordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); + .isThrownBy(() -> PasswordReader.readPassword(file)); } } diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java index be1bc394..c546604c 100644 --- a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java +++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.oom.certservice.postprocessor.configuration.exception.CertificatesPathsValidationException; +import org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable; @ExtendWith(MockitoExtension.class) class DelimitedPathsSplitterTest { @@ -58,16 +59,13 @@ class DelimitedPathsSplitterTest { @Test void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() { // when, then - assertThatExceptionOfType(CertificatesPathsValidationException.class) - .isThrownBy(() -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(""))); + assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, ""); } @Test void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { // when, then - assertThatExceptionOfType(CertificatesPathsValidationException.class) - .isThrownBy(() -> delimitedPathsSplitter - .getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(INVALID_TRUSTSTORES))); + assertCorrectExceptionIsThrownFor(TRUSTSTORES_PATHS, INVALID_TRUSTSTORES); } @Test @@ -92,16 +90,19 @@ class DelimitedPathsSplitterTest { @Test void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() { // when, then - assertThatExceptionOfType(CertificatesPathsValidationException.class) - .isThrownBy( - () -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(""))); + assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, ""); } @Test void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { // when, then + assertCorrectExceptionIsThrownFor(TRUSTSTORES_PASSWORDS_PATHS, INVALID_TRUSTSTORES_PASSWORDS); + } + + private void assertCorrectExceptionIsThrownFor(EnvVariable envVariable, String envValue) { + final Optional envValueOptional = Optional.of(envValue); assertThatExceptionOfType(CertificatesPathsValidationException.class) .isThrownBy(() -> delimitedPathsSplitter - .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(INVALID_TRUSTSTORES_PASSWORDS))); + .getValidatedPaths(envVariable, envValueOptional)); } } diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java index 99193a8d..c4a34c50 100644 --- a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java +++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -34,7 +34,7 @@ import org.onap.oom.certservice.postprocessor.copier.exception.KeystoreNotExistE import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -public class KeystoreCopierTest { +class KeystoreCopierTest { private static final String SOURCE_CONTENT = "source content"; private static final String DESTINATION_CONTENT = "destination content"; @@ -50,7 +50,7 @@ public class KeystoreCopierTest { copier.copyKeystores(configuration); - assertThat(dir.listFiles()).isEmpty(); + assertThat(dir).isEmptyDirectory(); } @@ -64,7 +64,7 @@ public class KeystoreCopierTest { copier.copyKeystores(configuration); assertThat(readFile(destination)).isEqualTo(readFile(source)); - assertThat(backup.exists()).isTrue(); + assertThat(backup).exists(); assertThat(readFile(backup)).isEqualTo(DESTINATION_CONTENT); } @@ -77,9 +77,9 @@ public class KeystoreCopierTest { copier.copyKeystores(configuration); - assertThat(destination.exists()).isTrue(); + assertThat(destination).exists(); assertThat(readFile(destination)).isEqualTo(readFile(source)); - assertThat(backup.exists()).isFalse(); + assertThat(backup).doesNotExist(); } @Test @@ -93,9 +93,9 @@ public class KeystoreCopierTest { copier.copyKeystores(configuration) ); - assertThat(source.exists()).isFalse(); - assertThat(destination.exists()).isFalse(); - assertThat(backup.exists()).isFalse(); + assertThat(source).doesNotExist(); + assertThat(destination).doesNotExist(); + assertThat(backup).doesNotExist(); } @Test @@ -110,9 +110,9 @@ public class KeystoreCopierTest { copier.copyKeystores(configuration) ); - assertThat(source.exists()).isTrue(); - assertThat(destination.exists()).isFalse(); - assertThat(backup.exists()).isFalse(); + assertThat(source).exists(); + assertThat(destination).doesNotExist(); + assertThat(backup).doesNotExist(); } private AppConfiguration createConfiguration(File source, File destination) { diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java index 8ef148a8..6150310d 100644 --- a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java +++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java @@ -1,7 +1,7 @@ /*============LICENSE_START======================================================= * oom-truststore-merger * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. + * Copyright (C) 2020-2021 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. @@ -38,7 +38,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.onap.oom.certservice.postprocessor.api.CertificateConstants; import org.onap.oom.certservice.postprocessor.api.ExitableException; import org.onap.oom.certservice.postprocessor.merger.exception.CreateBackupException; import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException; @@ -69,8 +68,8 @@ class TruststoreTest { //then File backupFile = new File(PEM_BACKUP_FILE_PATH); - assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue(); - assertThat(backupFile.isFile()).isTrue(); + assertThat(backupFile.getName()).endsWith(BACKUP_EXTENSION); + assertThat(backupFile).isFile(); } @ParameterizedTest @@ -194,13 +193,4 @@ class TruststoreTest { TestCertificateProvider.removeTemporaryFiles(); } - private static Stream truststoreProvider() - throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { - return Stream.of( - Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()), - Arguments.of(TestCertificateProvider.getSampleP12Truststore()), - Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile()) - ); - } - } -- cgit 1.2.3-korg