aboutsummaryrefslogtreecommitdiffstats
path: root/certService/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'certService/src/test')
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java42
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java24
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/PemObjectFactoryTest.java18
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/TestUtils.java6
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/exception/CertificationExceptionControllerTest.java74
-rw-r--r--certService/src/test/java/org/onap/aaf/certservice/certification/model/CsrModelTest.java40
6 files changed, 157 insertions, 47 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java
index 8ee88db5..0bb99d9f 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java
@@ -30,7 +30,7 @@ import org.onap.aaf.certservice.certification.CsrModelFactory;
import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64;
import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
import org.onap.aaf.certservice.certification.exceptions.DecryptionException;
-import org.onap.aaf.certservice.certification.exceptions.PemDecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import org.onap.aaf.certservice.certification.model.CertificationModel;
import org.onap.aaf.certservice.certification.model.CsrModel;
import org.springframework.http.HttpStatus;
@@ -41,7 +41,7 @@ import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -93,43 +93,41 @@ class CertificationServiceTest {
}
@Test
- void shouldReturnBadRequestWhenCreatingCsrModelFails() throws DecryptionException {
+ void shouldThrowCsrDecryptionExceptionWhenCreatingCsrModelFails() throws DecryptionException {
// given
+ String expectedMessage = "Incorrect CSR, decryption failed";
when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class)))
- .thenThrow(new CsrDecryptionException("CSR creation fail",new IOException()));
+ .thenThrow(new CsrDecryptionException(expectedMessage,new IOException()));
// when
- ResponseEntity<String> testResponse =
- certificationService.signCertificate("TestCa", "encryptedCSR", "encryptedPK");
+ Exception exception = assertThrows(
+ CsrDecryptionException.class, () -> certificationService.
+ signCertificate("TestCa", "encryptedCSR", "encryptedPK")
+ );
- String expectedMessage = "Wrong certificate signing request (CSR) format";
+ String actualMessage = exception.getMessage();
// then
- assertEquals(HttpStatus.BAD_REQUEST, testResponse.getStatusCode());
- assertTrue(
- testResponse.toString().contains(expectedMessage)
- );
-
+ assertEquals(expectedMessage, actualMessage);
}
@Test
- void shouldReturnBadRequestWhenCreatingPemModelFails() throws DecryptionException {
+ void shouldThrowPemDecryptionExceptionWhenCreatingPemModelFails() throws DecryptionException {
// given
+ String expectedMessage = "Incorrect PEM, decryption failed";
when(csrModelFactory.createCsrModel(any(StringBase64.class), any(StringBase64.class)))
- .thenThrow(new PemDecryptionException("PEM creation fail",new IOException()));
+ .thenThrow(new KeyDecryptionException(expectedMessage,new IOException()));
// when
- ResponseEntity<String> testResponse =
- certificationService.signCertificate("TestCa", "encryptedCSR", "encryptedPK");
+ Exception exception = assertThrows(
+ KeyDecryptionException.class, () -> certificationService.
+ signCertificate("TestCa", "encryptedCSR", "encryptedPK")
+ );
- String expectedMessage = "Wrong key (PK) format";
+ String actualMessage = exception.getMessage();
// then
- assertEquals(HttpStatus.BAD_REQUEST, testResponse.getStatusCode());
- assertTrue(
- testResponse.toString().contains(expectedMessage)
- );
-
+ assertEquals(expectedMessage, actualMessage);
}
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
index 065c7a0e..77594ed7 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/CsrModelFactoryTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import org.onap.aaf.certservice.certification.CsrModelFactory.StringBase64;
import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
import org.onap.aaf.certservice.certification.exceptions.DecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import org.onap.aaf.certservice.certification.model.CsrModel;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -33,6 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.onap.aaf.certservice.certification.TestData.TEST_CSR;
import static org.onap.aaf.certservice.certification.TestData.TEST_PK;
import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_CSR;
+import static org.onap.aaf.certservice.certification.TestData.TEST_WRONG_PEM;
class CsrModelFactoryTest {
@@ -66,9 +68,8 @@ class CsrModelFactoryTest {
);
}
-
@Test
- void shouldThrowCsrDecryptionExceptionWhenCsrAreIncorrect() {
+ void shouldThrowCsrDecryptionExceptionWhenCsrIsIncorrect() {
// given
String encoderPK = new String(Base64.encode(TEST_PK.getBytes()));
String wrongCsr = new String(Base64.encode(TEST_WRONG_CSR.getBytes()));
@@ -86,4 +87,23 @@ class CsrModelFactoryTest {
assertTrue(actualMessage.contains(expectedMessage));
}
+ @Test
+ void shouldThrowKeyDecryptionExceptionWhenKeyIsIncorrect() {
+ // given
+ String encoderPK = new String(Base64.encode(TEST_WRONG_PEM.getBytes()));
+ String wrongCsr = new String(Base64.encode(TEST_CSR.getBytes()));
+
+ // when
+ Exception exception = assertThrows(
+ KeyDecryptionException.class, () -> csrModelFactory
+ .createCsrModel(new StringBase64(wrongCsr), new StringBase64(encoderPK))
+ );
+
+ String expectedMessage = "Incorrect Key, decryption failed";
+ String actualMessage = exception.getMessage();
+
+ // then
+ assertTrue(actualMessage.contains(expectedMessage));
+ }
+
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/PemObjectFactoryTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/PemObjectFactoryTest.java
index 479c375b..0b70475c 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/certification/PemObjectFactoryTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/PemObjectFactoryTest.java
@@ -23,8 +23,8 @@ package org.onap.aaf.certservice.certification;
import org.bouncycastle.util.io.pem.PemObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
-import org.onap.aaf.certservice.certification.exceptions.PemDecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.DecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -45,9 +45,11 @@ class PemObjectFactoryTest {
}
@Test
- void shouldTransformStringInToPemObjectAndBackToString() throws PemDecryptionException {
+ void shouldTransformStringInToPemObjectAndBackToString() throws DecryptionException {
// when
- PemObject pemObject = pemObjectFactory.createPemObject(TEST_PEM);
+ PemObject pemObject = pemObjectFactory.createPemObject(TEST_PEM).orElseThrow(
+ () -> new DecryptionException("Pem decryption failed")
+ );
String parsedPemObject = pemObjectToString(pemObject);
// then
@@ -56,12 +58,16 @@ class PemObjectFactoryTest {
@Test
void shouldThrowExceptionWhenParsingPemFailed() {
+ // given
+ String expectedMessage = "Unable to create PEM";
+
// when
Exception exception = assertThrows(
- PemDecryptionException.class, () -> pemObjectFactory.createPemObject(TEST_WRONG_PEM)
+ DecryptionException.class, () -> pemObjectFactory.createPemObject(TEST_WRONG_PEM).orElseThrow(
+ () -> new DecryptionException(expectedMessage)
+ )
);
- String expectedMessage = "Unable to create PEM";
String actualMessage = exception.getMessage();
// then
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/TestUtils.java b/certService/src/test/java/org/onap/aaf/certservice/certification/TestUtils.java
index c2824c80..39554417 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/certification/TestUtils.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/TestUtils.java
@@ -22,7 +22,7 @@ package org.onap.aaf.certservice.certification;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
-import org.onap.aaf.certservice.certification.exceptions.PemDecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import java.io.IOException;
import java.io.StringWriter;
@@ -33,7 +33,7 @@ public final class TestUtils {
private TestUtils() {
}
- public static String pemObjectToString(PemObject pemObject) throws PemDecryptionException {
+ public static String pemObjectToString(PemObject pemObject) throws KeyDecryptionException {
try (StringWriter output = new StringWriter()) {
PemWriter pemWriter = new PemWriter(output);
pemWriter.writeObject(pemObject);
@@ -41,7 +41,7 @@ public final class TestUtils {
return output.getBuffer().toString();
} catch (IOException e) {
- throw new PemDecryptionException("Writing PAM Object to string failed", e);
+ throw new KeyDecryptionException("Writing PAM Object to string failed", e);
}
}
}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/exception/CertificationExceptionControllerTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/exception/CertificationExceptionControllerTest.java
new file mode 100644
index 00000000..58e59f45
--- /dev/null
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/exception/CertificationExceptionControllerTest.java
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aaf.certservice.certification.exception;
+
+import com.google.gson.Gson;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.aaf.certservice.certification.exceptions.CertificationExceptionController;
+import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
+import org.onap.aaf.certservice.certification.model.ErrorResponseModel;
+import org.springframework.http.ResponseEntity;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class CertificationExceptionControllerTest {
+
+ private CertificationExceptionController certificationExceptionController;
+
+ @BeforeEach
+ void setUp() {
+ certificationExceptionController =
+ new CertificationExceptionController();
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenCsrDecryptionException() {
+ // given
+ String expectedMessage = "Wrong certificate signing request (CSR) format";
+ CsrDecryptionException csrDecryptionException = new CsrDecryptionException("test csr exception");
+
+ // when
+ ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
+
+ ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
+
+ // then
+ assertEquals(expectedMessage, response.getErrorMessage());
+ }
+
+ @Test
+ void shouldReturnResponseEntityWithAppropriateErrorMessageWhenGivenKeyDecryptionException() {
+ // given
+ String expectedMessage = "Wrong key (PK) format";
+ KeyDecryptionException csrDecryptionException = new KeyDecryptionException("test pk exception");
+
+ // when
+ ResponseEntity<String> responseEntity = certificationExceptionController.handle(csrDecryptionException);
+
+ ErrorResponseModel response = new Gson().fromJson(responseEntity.getBody(), ErrorResponseModel.class);
+
+ // then
+ assertEquals(expectedMessage, response.getErrorMessage());
+ }
+
+}
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/model/CsrModelTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/model/CsrModelTest.java
index 9d748150..7df785d2 100644
--- a/certService/src/test/java/org/onap/aaf/certservice/certification/model/CsrModelTest.java
+++ b/certService/src/test/java/org/onap/aaf/certservice/certification/model/CsrModelTest.java
@@ -24,10 +24,11 @@ import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.util.io.pem.PemObject;
import org.junit.jupiter.api.Test;
+import org.onap.aaf.certservice.certification.PKCS10CertificationRequestFactory;
import org.onap.aaf.certservice.certification.PemObjectFactory;
import org.onap.aaf.certservice.certification.exceptions.CsrDecryptionException;
import org.onap.aaf.certservice.certification.exceptions.DecryptionException;
-import org.onap.aaf.certservice.certification.exceptions.PemDecryptionException;
+import org.onap.aaf.certservice.certification.exceptions.KeyDecryptionException;
import java.io.IOException;
@@ -44,7 +45,10 @@ import static org.onap.aaf.certservice.certification.TestUtils.pemObjectToString
class CsrModelTest {
-
+ private final PKCS10CertificationRequestFactory certificationRequestFactory
+ = new PKCS10CertificationRequestFactory();
+ private final PemObjectFactory pemObjectFactory
+ = new PemObjectFactory();
@Test
void shouldByConstructedAndReturnProperFields() throws DecryptionException, IOException {
// given
@@ -70,7 +74,7 @@ class CsrModelTest {
}
@Test
- void shouldThrowExceptionWhenKeyIsNotCorrect() throws PemDecryptionException ,IOException {
+ void shouldThrowExceptionWhenPublicKeyIsNotCorrect() throws KeyDecryptionException, IOException {
// given
PemObjectFactory pemObjectFactory = new PemObjectFactory();
PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
@@ -79,7 +83,9 @@ class CsrModelTest {
.thenReturn(wrongKryInfo);
when(wrongKryInfo.getEncoded())
.thenThrow(new IOException());
- PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK);
+ PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
+ () -> new KeyDecryptionException("Private key decoding fail")
+ );
CsrModel csrModel = new CsrModel(testCsr, testPrivateKey);
// when
@@ -95,20 +101,26 @@ class CsrModelTest {
assertTrue(actualMessage.contains(expectedMessage));
}
- private CsrModel generateTestCsrModel() throws PemDecryptionException, IOException {
- PemObjectFactory pemObjectFactory = new PemObjectFactory();
- PKCS10CertificationRequest testCsr = new PKCS10CertificationRequest(
- pemObjectFactory.createPemObject(TEST_CSR).getContent()
+ private CsrModel generateTestCsrModel() throws DecryptionException {
+ PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
+ () -> new DecryptionException("Incorrect Private Key, decryption failed")
);
- PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK);
+ PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
return new CsrModel(testCsr, testPrivateKey);
}
- private PemObject generateTestPublicKey() throws PemDecryptionException, IOException {
- PemObjectFactory pemObjectFactory = new PemObjectFactory();
- PKCS10CertificationRequest testCsr = new PKCS10CertificationRequest(
- pemObjectFactory.createPemObject(TEST_CSR).getContent()
- );
+ private PemObject generateTestPublicKey() throws DecryptionException, IOException {
+ PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
return new PemObject("PUBLIC KEY", testCsr.getSubjectPublicKeyInfo().getEncoded());
}
+
+ private PKCS10CertificationRequest generateTestCertificationRequest() throws DecryptionException {
+ return pemObjectFactory.createPemObject(TEST_CSR)
+ .flatMap(
+ certificationRequestFactory::createKCS10CertificationRequest
+ ).orElseThrow(
+ () -> new DecryptionException("Incorrect CSR, decryption failed")
+ );
+ }
+
}