From 06f7465cebf967f6c1ffd9970b05158c00aa5ff1 Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Thu, 13 Feb 2020 14:55:21 +0100 Subject: Improve exception flow Issue-ID: AAF-995 Signed-off-by: Bartosz Gardziejewski Change-Id: I4d690fbe27364bc50b97ab42e64e610566d93ca0 --- .../certservice/api/CertificationServiceTest.java | 42 +++++++++++----------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'certService/src/test/java/org/onap/aaf/certservice/api') 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 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 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); } } -- cgit 1.2.3-korg