[OOM-CERT-SERVICE] Fix sonar and checkstyle issues, code cleanup 96/122596/1
authorRemigiusz Janeczek <remigiusz.janeczek@nokia.com>
Tue, 13 Jul 2021 13:52:23 +0000 (15:52 +0200)
committerRemigiusz Janeczek <remigiusz.janeczek@nokia.com>
Tue, 13 Jul 2021 13:52:23 +0000 (15:52 +0200)
Issue-ID: OOM-2753
Signed-off-by: Remigiusz Janeczek <remigiusz.janeczek@nokia.com>
Change-Id: Id88b6b2bceba7258745e4ce999dd375fb9ce438f

14 files changed:
certService/src/main/java/org/onap/oom/certservice/api/CertificationController.java
certService/src/main/java/org/onap/oom/certservice/certification/CertificationResponseModelFactory.java
certService/src/main/java/org/onap/oom/certservice/certification/conversion/OldCertificateModelFactory.java
certService/src/main/java/org/onap/oom/certservice/certification/exception/CertificateDecryptionException.java
certService/src/main/java/org/onap/oom/certservice/certification/model/CertificateData.java
certService/src/main/java/org/onap/oom/certservice/certification/model/CertificateUpdateModel.java
certService/src/main/java/org/onap/oom/certservice/cmpv2client/impl/CmpClientImpl.java
certService/src/main/java/org/onap/oom/certservice/cmpv2client/validation/CmpCertificationValidator.java
certService/src/test/java/org/onap/oom/certservice/api/CertificationControllerTest.java
certService/src/test/java/org/onap/oom/certservice/certification/CertificationProviderTest.java
certService/src/test/java/org/onap/oom/certservice/certification/CertificationResponseModelFactoryTest.java
certService/src/test/java/org/onap/oom/certservice/certification/TestData.java
certService/src/test/java/org/onap/oom/certservice/cmpv2client/ClientTestData.java
certService/src/test/java/org/onap/oom/certservice/cmpv2client/Cmpv2ClientTest.java

index 8e2a378..a4389ec 100644 (file)
@@ -28,7 +28,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
 import io.swagger.v3.oas.annotations.responses.ApiResponses;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.onap.oom.certservice.certification.CertificationResponseModelFactory;
-import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
 import org.onap.oom.certservice.certification.exception.DecryptionException;
 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
@@ -112,7 +111,7 @@ public class CertificationController {
             @RequestHeader("PK") String encodedPrivateKey,
             @RequestHeader("OLD_CERT") String encodedOldCert,
             @RequestHeader("OLD_PK") String encodedOldPrivateKey
-    ) throws DecryptionException, CmpClientException, CertificateDecryptionException {
+    ) throws DecryptionException, CmpClientException {
         caName = replaceWhiteSpaceChars(caName);
         LOGGER.info("Received certificate update request for CA named: {}", caName);
         CertificateUpdateModel certificateUpdateModel = new CertificateUpdateModel.CertificateUpdateModelBuilder()
index 0e793bb..af90bf7 100644 (file)
@@ -25,7 +25,6 @@ import org.onap.oom.certservice.certification.configuration.model.Cmpv2Server;
 import org.onap.oom.certservice.certification.conversion.CsrModelFactory;
 import org.onap.oom.certservice.certification.conversion.OldCertificateModelFactory;
 import org.onap.oom.certservice.certification.conversion.StringBase64;
-import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
 import org.onap.oom.certservice.certification.exception.DecryptionException;
 import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
 import org.onap.oom.certservice.certification.model.CertificationResponseModel;
@@ -79,10 +78,9 @@ public class CertificationResponseModelFactory {
     }
 
     public CertificationResponseModel provideCertificationModelFromUpdateRequest(CertificateUpdateModel certificateUpdateModel)
-        throws DecryptionException, CmpClientException, CertificateDecryptionException {
-        LOGGER.info("CSR: " + certificateUpdateModel.getEncodedCsr() +
-                ", old cert: " + certificateUpdateModel.getEncodedOldCert() +
-                ", CA: " + certificateUpdateModel.getCaName());
+        throws DecryptionException, CmpClientException {
+        LOGGER.info("CSR: {}, old cert: {}, CA: {}", certificateUpdateModel.getEncodedCsr(),
+                        certificateUpdateModel.getEncodedOldCert(), certificateUpdateModel.getCaName());
         final CsrModel csrModel = csrModelFactory.createCsrModel(
             new StringBase64(certificateUpdateModel.getEncodedCsr()),
             new StringBase64(certificateUpdateModel.getEncodedPrivateKey())
index f5c199f..d88b6bb 100644 (file)
@@ -72,7 +72,6 @@ public class OldCertificateModelFactory {
             return new OldCertificateModel(certificate, subjectData, sans, oldPrivateKey);
         } catch (StringToCertificateConversionException e) {
             throw new CertificateDecryptionException("Cannot convert certificate", e);
-
         } catch (CertificateParsingException e) {
             throw new CertificateDecryptionException("Cannot read Subject Alternative Names from certificate");
         } catch (NoSuchAlgorithmException | KeyDecryptionException | CertificateEncodingException | InvalidKeySpecException e) {
index 16fdb44..20df03c 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.oom.certservice.certification.exception;
 
-public class CertificateDecryptionException extends Exception {
+public class CertificateDecryptionException extends DecryptionException {
 
     public CertificateDecryptionException(String message, Throwable cause) {
         super(message, cause);
index 3a00c91..bc701e0 100644 (file)
 
 package org.onap.oom.certservice.certification.model;
 
-import org.bouncycastle.asn1.x500.X500Name;
-import org.bouncycastle.asn1.x509.GeneralName;
-
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
+import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.asn1.x509.GeneralName;
 
 public class CertificateData {
 
@@ -49,10 +48,14 @@ public class CertificateData {
     }
 
     @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-        CertificateData that = (CertificateData) o;
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        CertificateData that = (CertificateData) obj;
         return Objects.equals(subject, that.subject) && Objects.equals(sortedSans, that.sortedSans);
     }
 
index 699ffe7..770d881 100644 (file)
@@ -31,7 +31,7 @@ public final class CertificateUpdateModel {
     private final String caName;
 
     private CertificateUpdateModel(String encodedCsr, String encodedPrivateKey, String encodedOldCert,
-                                   String encodedOldPrivateKey, String caName) {
+        String encodedOldPrivateKey, String caName) {
         this.encodedCsr = encodedCsr;
         this.encodedPrivateKey = encodedPrivateKey;
         this.encodedOldCert = encodedOldCert;
@@ -60,15 +60,19 @@ public final class CertificateUpdateModel {
     }
 
     @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-        CertificateUpdateModel that = (CertificateUpdateModel) o;
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        CertificateUpdateModel that = (CertificateUpdateModel) obj;
         return Objects.equals(encodedCsr, that.encodedCsr)
-                && Objects.equals(encodedPrivateKey, that.encodedPrivateKey)
-                && Objects.equals(encodedOldCert, that.encodedOldCert)
-                && Objects.equals(encodedOldPrivateKey, that.encodedOldPrivateKey)
-                && Objects.equals(caName, that.caName);
+            && Objects.equals(encodedPrivateKey, that.encodedPrivateKey)
+            && Objects.equals(encodedOldCert, that.encodedOldCert)
+            && Objects.equals(encodedOldPrivateKey, that.encodedOldPrivateKey)
+            && Objects.equals(caName, that.caName);
     }
 
     @Override
@@ -110,7 +114,8 @@ public final class CertificateUpdateModel {
         }
 
         public CertificateUpdateModel build() {
-            return new CertificateUpdateModel(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey, caName);
+            return new CertificateUpdateModel(encodedCsr, encodedPrivateKey, encodedOldCert, encodedOldPrivateKey,
+                caName);
         }
     }
 }
index bbca91b..fd2d708 100644 (file)
@@ -100,7 +100,7 @@ public class CmpClientImpl implements CmpClient {
         final CreateCertRequest certRequest =
             getCmpMessageBuilderWithCommonRequestValues(csrModel, cmpv2Server)
                 .with(CreateCertRequest::setCmpRequestType, PKIBody.TYPE_KEY_UPDATE_REQ)
-                .with(CreateCertRequest::setExtraCerts, getCMPCertificate(oldCertificateModel.getOldCertificate()))
+                .with(CreateCertRequest::setExtraCerts, getCmpCertificate(oldCertificateModel.getOldCertificate()))
                 .with(CreateCertRequest::setProtection, pkiMessageProtection)
                 .build();
 
@@ -154,7 +154,7 @@ public class CmpClientImpl implements CmpClient {
             return new SignatureProtection(oldCertificateModel.getOldPrivateKey());
     }
 
-    private CMPCertificate[] getCMPCertificate(Certificate oldCertificate) {
+    private CMPCertificate[] getCmpCertificate(Certificate oldCertificate) {
             CMPCertificate cert = new CMPCertificate(oldCertificate);
             return new CMPCertificate[]{cert};
     }
index 40a2a1d..f2601b9 100644 (file)
@@ -52,7 +52,7 @@ public class CmpCertificationValidator {
     private static final ASN1ObjectIdentifier PASSWORD_BASED_MAC = new ASN1ObjectIdentifier("1.2.840.113533.7.66.13");
     private static final Logger LOG = LoggerFactory.getLogger(CmpCertificationValidator.class);
 
-    public static void validate(
+    public void validate(
         final CsrModel csrModel,
         final Cmpv2Server server,
         final CloseableHttpClient httpClient,
index 81c2d39..d373874 100644 (file)
@@ -162,7 +162,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
-        throws DecryptionException, CmpClientException, CertificateDecryptionException {
+        throws DecryptionException, CmpClientException {
         // Given
         CertificationResponseModel testCertificationResponseModel = new CertificationResponseModel(
                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
@@ -183,7 +183,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
-        throws DecryptionException, CertificateDecryptionException, CmpClientException {
+        throws DecryptionException, CmpClientException {
         // Given
         String expectedMessage = "Incorrect certificate, decryption failed";
         when(certificationResponseModelFactory.provideCertificationModelFromUpdateRequest(TEST_CERTIFICATE_UPDATE_MODEL))
index 192050d..042b2ae 100644 (file)
@@ -140,7 +140,7 @@ class CertificationProviderTest {
         // When
         when(
             cmpClient.executeKeyUpdateRequest(any(CsrModel.class), any(Cmpv2Server.class), any(OldCertificateModel.class))
-        ).thenReturn(getCMPv2CertificationModel());
+        ).thenReturn(getCmpv2CertificationModel());
 
         CertificationResponseModel certificationModel = certificationProvider
             .executeKeyUpdateRequest(csrModel, server, oldCertificateModel);
@@ -162,7 +162,7 @@ class CertificationProviderTest {
 
         when(
             cmpClient.executeInitializationRequest(any(CsrModel.class), any(Cmpv2Server.class))
-        ).thenReturn(getCMPv2CertificationModel());
+        ).thenReturn(getCmpv2CertificationModel());
 
         CertificationResponseModel certificationModel = certificationProvider
             .executeInitializationRequest(csrModel, server);
@@ -214,7 +214,7 @@ class CertificationProviderTest {
         return string.replace("\n", "").replace("\r", "");
     }
 
-    private Cmpv2CertificationModel getCMPv2CertificationModel() throws IOException, CertificateException {
+    private Cmpv2CertificationModel getCmpv2CertificationModel() throws IOException, CertificateException {
         List<X509Certificate> certificateChain = getX509CertificateFromPem(TEST_CMPv2_KEYSTORE);
         List<X509Certificate> trustedCertificates = getX509CertificateFromPem(TEST_CMPv2_TRUSTSTORE);
         return new Cmpv2CertificationModel(certificateChain, trustedCertificates);
index 205513f..90dc235 100644 (file)
@@ -204,7 +204,7 @@ class CertificationResponseModelFactoryTest {
 
     @Test
     void shouldPerformKurWhenCsrAndOldCertDataMatch()
-        throws CertificateDecryptionException, DecryptionException, CmpClientException {
+        throws DecryptionException, CmpClientException {
         // Given
         CsrModel csrModel = mockCsrFactoryModelCreation();
         Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
@@ -229,7 +229,7 @@ class CertificationResponseModelFactoryTest {
 
     @Test
     void shouldThrowCmpClientExceptionWhenUpdateRequestFailed()
-        throws DecryptionException, CmpClientException, CertificateDecryptionException {
+        throws DecryptionException, CmpClientException {
 
         // Given
         String expectedMessage = "Exception occurred while send request to CMPv2 Server";
@@ -254,7 +254,7 @@ class CertificationResponseModelFactoryTest {
 
     @Test
     void shouldPerformCrWhenCsrAndOldCertDataDontMatch()
-        throws CertificateDecryptionException, DecryptionException, CmpClientException {
+        throws DecryptionException, CmpClientException {
         // Given
         CsrModel csrModel = mockCsrFactoryModelCreation();
         Cmpv2Server testServer = mockCmpv2ProviderServerSelection();
index 3c47d86..8a4ba64 100644 (file)
@@ -29,7 +29,7 @@ public final class TestData {
 
     public static final String EXPECTED_CERT_SUBJECT = "C=US,ST=California,L=San-Francisco,O=Linux-Foundation,OU=ONAP,CN=onap.org";
     public static final String EXPECTED_CERT_SANS =
-        "SANs: [onap@onap.org, localhost, onap.org, test.onap.org, onap://cluster.local/, " + LOCALHOST_IP_IN_HEX +"]";
+        "SANs: [onap@onap.org, localhost, onap.org, test.onap.org, onap://cluster.local/, " + LOCALHOST_IP_IN_HEX + "]";
 
 
     public static final String TEST_CSR = "-----BEGIN CERTIFICATE REQUEST-----\n"
index 845361e..e3896ac 100644 (file)
@@ -43,27 +43,27 @@ public final class ClientTestData {
     private static final OldCertificateModelFactory factory =
         new OldCertificateModelFactory(new PemStringToCertificateConverter(), new X509CertificateParser());
 
-    static final OldCertificateModel createCorrectOldCertificateModel() throws CertificateDecryptionException {
+    static OldCertificateModel createCorrectOldCertificateModel() throws CertificateDecryptionException {
         return createOldCertificateModel(TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PRIVATE_KEY);
     }
 
-    static final OldCertificateModel createOldCertificateModelWithWrongCert() throws CertificateDecryptionException {
+    static OldCertificateModel createOldCertificateModelWithWrongCert() throws CertificateDecryptionException {
         return createOldCertificateModel(WRONG_OLD_CERT, TEST_ENCODED_OLD_PRIVATE_KEY);
     }
 
-    static final OldCertificateModel createOldCertificateModelWithWrongPrivateKey() throws CertificateDecryptionException {
+    static OldCertificateModel createOldCertificateModelWithWrongPrivateKey() throws CertificateDecryptionException {
         return createOldCertificateModel(TEST_ENCODED_OLD_CERT, WRONG_OLD_PRIVATE_KEY);
     }
 
-    static final OldCertificateModel createOldCertificateModelWithPrivateKeyInPKCS1() throws CertificateDecryptionException {
+    static OldCertificateModel createOldCertificateModelWithPrivateKeyInPkcs1() throws CertificateDecryptionException {
         return createOldCertificateModel(TEST_ENCODED_OLD_CERT, TEST_ENCODED_PRIVATE_KEY_IN_PKCS1);
     }
 
-    static final OldCertificateModel createOldCertificateModelWithPrivateKeyInPKCS8() throws CertificateDecryptionException {
+    static OldCertificateModel createOldCertificateModelWithPrivateKeyInPkcs8() throws CertificateDecryptionException {
         return createOldCertificateModel(TEST_ENCODED_OLD_CERT, TEST_ENCODED_PRIVATE_KEY_IN_PKCS8);
     }
 
-    private static final OldCertificateModel createOldCertificateModel(String certificate, String privateKey) throws CertificateDecryptionException {
+    private static OldCertificateModel createOldCertificateModel(String certificate, String privateKey) throws CertificateDecryptionException {
         StringBase64 base64EncodedCertificate = new StringBase64(certificate);
         return factory.createCertificateModel(base64EncodedCertificate, privateKey);
     }
index 93dcbb7..23964ea 100644 (file)
@@ -26,8 +26,8 @@ import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 import static org.mockito.MockitoAnnotations.initMocks;
-import static org.onap.oom.certservice.cmpv2client.ClientTestData.createOldCertificateModelWithPrivateKeyInPKCS1;
-import static org.onap.oom.certservice.cmpv2client.ClientTestData.createOldCertificateModelWithPrivateKeyInPKCS8;
+import static org.onap.oom.certservice.cmpv2client.ClientTestData.createOldCertificateModelWithPrivateKeyInPkcs1;
+import static org.onap.oom.certservice.cmpv2client.ClientTestData.createOldCertificateModelWithPrivateKeyInPkcs8;
 
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
@@ -115,7 +115,7 @@ class Cmpv2ClientTest {
 
     private static KeyPair keyPair;
 
-    private final static Decoder BASE64_DECODER = Base64.getDecoder();
+    private static final Decoder BASE64_DECODER = Base64.getDecoder();
 
     @BeforeEach
     void setUp()
@@ -233,7 +233,7 @@ class Cmpv2ClientTest {
 
 
     @Test
-    void shouldThrowCMPClientExceptionWhenCannotParseOldCertificate() {
+    void shouldThrowCmpClientExceptionWhenCannotParseOldCertificate() {
         setCsrModelAndServerTestDefaultValues();
 
         CmpClientImpl cmpClient = new CmpClientImpl(httpClient);
@@ -375,7 +375,7 @@ class Cmpv2ClientTest {
 
         try (
             BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(
-                preparePKIMessageWithoutProtectionAlgorithm().getEncoded()
+                preparePkiMessageWithoutProtectionAlgorithm().getEncoded()
             ))) {
 
             byte[] ba = IOUtils.toByteArray(bis);
@@ -473,7 +473,7 @@ class Cmpv2ClientTest {
         server.setIssuerDN(dn);
     }
 
-    private PKIMessage preparePKIMessageWithoutProtectionAlgorithm() {
+    private PKIMessage preparePkiMessageWithoutProtectionAlgorithm() {
 
         CertTemplateBuilder certTemplateBuilder = new CertTemplateBuilder();
         X500Name issuerDN = getTestIssuerDN();
@@ -504,8 +504,8 @@ class Cmpv2ClientTest {
     private static Stream<Arguments> getTestUpdateModelWithSupportedPrivateKeys()
         throws CertificateDecryptionException {
         return Stream.of(
-            Arguments.of(createOldCertificateModelWithPrivateKeyInPKCS1()),
-            Arguments.of(createOldCertificateModelWithPrivateKeyInPKCS8())
+            Arguments.of(createOldCertificateModelWithPrivateKeyInPkcs1()),
+            Arguments.of(createOldCertificateModelWithPrivateKeyInPkcs8())
         );
     }