[CMPV2] Fix NPE & enhance error messages
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / oom / certservice / api / advice / CertificationExceptionAdviceTest.java
index 081a01a..9e6a6ce 100644 (file)
@@ -3,6 +3,7 @@
  * PROJECT
  * ================================================================================
  * Copyright (C) 2020 Nokia. All rights reserved.
+ * Copyright (C) 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.
 
 package org.onap.oom.certservice.api.advice;
 
-import com.google.gson.Gson;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.onap.oom.certservice.certification.exception.Cmpv2ClientAdapterException;
@@ -29,12 +33,10 @@ import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
 import org.onap.oom.certservice.certification.exception.ErrorResponseModel;
 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
+import org.onap.oom.certservice.cmpv2client.exceptions.CmpServerException;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
 class CertificationExceptionAdviceTest {
 
     private CertificationExceptionAdvice certificationExceptionAdvice;
@@ -98,7 +100,7 @@ class CertificationExceptionAdviceTest {
 
         // Then
         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
-        assertEquals(expectedMessage, response.getBody().getErrorMessage());
+        assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
     }
 
     @Test
@@ -112,7 +114,7 @@ class CertificationExceptionAdviceTest {
 
         // Then
         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
-        assertEquals(expectedMessage, response.getBody().getErrorMessage());
+        assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
     }
 
     @Test
@@ -131,4 +133,17 @@ class CertificationExceptionAdviceTest {
         assertEquals(expectedMessage, exception.getMessage());
     }
 
+    @Test
+    void shouldReturnResponseEntityWithCmpErrorMessage() {
+        // Given
+        String expectedMessage = "CMPv2 server returned following error: EJBCA fault";
+        CmpServerException exception = new CmpServerException("EJBCA fault");
+
+        // When
+        ResponseEntity<ErrorResponseModel> response = certificationExceptionAdvice.handle(exception);
+
+        // Then
+        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
+        assertTrue(response.getBody().getErrorMessage().startsWith(expectedMessage));
+    }
 }