diff options
author | 2020-02-13 14:55:21 +0100 | |
---|---|---|
committer | 2020-02-13 14:55:21 +0100 | |
commit | 06f7465cebf967f6c1ffd9970b05158c00aa5ff1 (patch) | |
tree | ad950726b6da96f9af5ba898e7038d5984a87fee /certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java | |
parent | 459a31e973f55fd97831a9a7cae0dce664259a4e (diff) |
Improve exception flow
Issue-ID: AAF-995
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I4d690fbe27364bc50b97ab42e64e610566d93ca0
Diffstat (limited to 'certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java')
-rw-r--r-- | certService/src/test/java/org/onap/aaf/certservice/api/CertificationServiceTest.java | 42 |
1 files changed, 20 insertions, 22 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); } } |