[OOM-CERT-SERVICE] Add logic for KUR/CR detection
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / CertificationControllerTest.java
index abd950e..f1d5baa 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * ============LICENSE_START=======================================================
- * PROJECT
+ * Cert Service
  * ================================================================================
- * 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.
@@ -32,12 +32,15 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.oom.certservice.certification.exception.CertificateDecryptionException;
+import org.onap.oom.certservice.certification.exception.StringToCertificateConversionException;
+import org.onap.oom.certservice.certification.model.CertificateUpdateModel;
 import org.onap.oom.certservice.certification.CertificationModelFactory;
-import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
 import org.onap.oom.certservice.certification.exception.Cmpv2ServerNotFoundException;
 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
 import org.onap.oom.certservice.certification.exception.DecryptionException;
 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
+import org.onap.oom.certservice.certification.model.CertificateUpdateModel.CertificateUpdateModelBuilder;
 import org.onap.oom.certservice.certification.model.CertificationModel;
 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
 import org.springframework.http.HttpStatus;
@@ -52,6 +55,15 @@ class CertificationControllerTest {
     private static final String TEST_WRONG_ENCODED_CSR = "wrongEncodedCSR";
     private static final String TEST_WRONG_ENCODED_PK = "wrongEncodedPK";
     private static final String TEST_WRONG_CA_NAME = "wrongTestCa";
+    private static final String TEST_ENCODED_OLD_PK = "encodedOldPK";
+    private static final String TEST_ENCODED_OLD_CERT = "encodedOldCert";
+    private static final CertificateUpdateModel TEST_CERTIFICATE_UPDATE_MODEL = new CertificateUpdateModelBuilder()
+        .setEncodedCsr(TEST_ENCODED_CSR)
+        .setEncodedPrivateKey(TEST_ENCODED_PK)
+        .setEncodedOldCert(TEST_ENCODED_OLD_CERT)
+        .setEncodedOldPrivateKey(TEST_ENCODED_OLD_PK)
+        .setCaName(TEST_CA_NAME)
+        .build();
 
     private CertificationController certificationController;
 
@@ -65,7 +77,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldReturnDataAboutCsrBaseOnEncodedParameters()
-            throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
+            throws DecryptionException, CmpClientException {
         // Given
         CertificationModel testCertificationModel = new CertificationModel(
                 Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
@@ -87,7 +99,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails()
-            throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
+            throws DecryptionException, CmpClientException {
         // Given
         String expectedMessage = "Incorrect CSR, decryption failed";
         when(certificationModelFactory.createCertificationModel(TEST_WRONG_ENCODED_CSR, TEST_ENCODED_PK, TEST_CA_NAME))
@@ -107,7 +119,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails()
-            throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
+            throws DecryptionException, CmpClientException {
         // Given
         String expectedMessage = "Incorrect PEM, decryption failed";
         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_WRONG_ENCODED_PK, TEST_CA_NAME))
@@ -127,7 +139,7 @@ class CertificationControllerTest {
 
     @Test
     void shouldThrowCmpv2ServerNotFoundWhenGivenWrongCaName()
-            throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
+            throws DecryptionException, CmpClientException {
         // Given
         String expectedMessage = "No server found for given CA name";
         when(certificationModelFactory.createCertificationModel(TEST_ENCODED_CSR, TEST_ENCODED_PK, TEST_WRONG_CA_NAME))
@@ -144,4 +156,46 @@ class CertificationControllerTest {
         // Then
         assertEquals(expectedMessage, actualMessage);
     }
+
+    @Test
+    void shouldUpdateEndpointReturnDataAboutCsrBaseOnEncodedParameters()
+        throws DecryptionException, CertificateDecryptionException {
+        // Given
+        CertificationModel testCertificationModel = new CertificationModel(
+                Arrays.asList("ENTITY_CERT", "INTERMEDIATE_CERT"),
+                Arrays.asList("CA_CERT", "EXTRA_CA_CERT")
+        );
+        when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL)).thenReturn(testCertificationModel);
+
+        // When
+        ResponseEntity<CertificationModel> responseCertificationModel =
+                certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
+                        TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK);
+
+        // Then
+        assertEquals(HttpStatus.OK, responseCertificationModel.getStatusCode());
+        assertThat(responseCertificationModel.getBody()).isEqualToComparingFieldByField(testCertificationModel);
+    }
+
+    @Test
+    void shouldThrowCertificateDecryptionExceptionWhenCreatingPemModelFails()
+        throws DecryptionException, CertificateDecryptionException {
+        // Given
+        String expectedMessage = "Incorrect certificate, decryption failed";
+        when(certificationModelFactory.createCertificationModel(TEST_CERTIFICATE_UPDATE_MODEL))
+            .thenThrow(new CertificateDecryptionException(expectedMessage));
+
+        // When
+        Exception exception = assertThrows(
+            CertificateDecryptionException.class, () ->
+                certificationController.updateCertificate(TEST_CA_NAME, TEST_ENCODED_CSR,
+                    TEST_ENCODED_PK, TEST_ENCODED_OLD_CERT, TEST_ENCODED_OLD_PK)
+        );
+
+        String actualMessage = exception.getMessage();
+
+        // Then
+        assertEquals(expectedMessage, actualMessage);
+    }
+
 }