Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / utils / LoggingUtilsTest.java
1 package org.onap.vid.utils;
2
3 import com.fasterxml.jackson.core.JsonLocation;
4 import com.fasterxml.jackson.core.JsonParseException;
5 import com.fasterxml.jackson.databind.JsonMappingException;
6 import org.onap.vid.exceptions.GenericUncheckedException;
7 import org.testng.annotations.DataProvider;
8 import org.testng.annotations.Test;
9 import sun.security.provider.certpath.SunCertPathBuilderException;
10 import sun.security.validator.ValidatorException;
11
12 import javax.crypto.BadPaddingException;
13 import javax.net.ssl.SSLHandshakeException;
14 import javax.ws.rs.ProcessingException;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.security.UnrecoverableKeyException;
18 import java.security.cert.CertificateException;
19
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.onap.vid.testUtils.RegExMatcher.matchesRegEx;
22
23 public class LoggingUtilsTest {
24
25     @DataProvider
26     public static Object[][] exceptions() {
27         Exception e0 = new CertificateException("No X509TrustManager implementation available");
28         Exception noTrustMngrImplException = new SSLHandshakeException(e0.toString());
29         noTrustMngrImplException.initCause(e0);
30
31         Exception e1 = new BadPaddingException("Given final block not properly padded");
32         Exception incorrectPasswordException = new IOException("keystore password was incorrect",
33                 new UnrecoverableKeyException("failed to decrypt safe contents entry: " + e1));
34         String incorrectPasswordExceptionDescription = "" +
35                 "java.io.IOException: keystore password was incorrect: " +
36                 "java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: " +
37                 "javax.crypto.BadPaddingException: Given final block not properly padded";
38
39         Exception e2 = new SunCertPathBuilderException("unable to find valid certification path to requested target");
40         Exception noValidCert = new ProcessingException(new ValidatorException("PKIX path building failed: " + e2.toString(), e2));
41         String noValidCertDescription = "" +
42                 "javax.ws.rs.ProcessingException: " +
43                 "sun.security.validator.ValidatorException: PKIX path building failed: " +
44                 "sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target";
45
46         RuntimeException codehausParseException = new RuntimeException(new JsonParseException("Unexpected character ('<' (code 60)):" +
47                 " expected a valid value (number, String, array, object, 'true', 'false' or 'null')",
48                 new JsonLocation("<html>i'm an error</html>", 25, 1, 1)));
49         String codehausParseDescription = "" +
50                 "com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)):" +
51                 " expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n" +
52                 " at [Source: (String)\"<html>i'm an error</html>\"; line: 1, column: 1]";
53
54         RuntimeException fasterxmlMappingException = new RuntimeException(new JsonMappingException("Can not deserialize instance of java.lang.String out of START_ARRAY token",
55                 new com.fasterxml.jackson.core.JsonLocation("{ example json }", 15, 1, 20)));
56         String fasterxmlMappingDescription = "" +
57                 "com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token\n" +
58                 " at [Source: (String)\"{ example json }\"; line: 1, column: 20]";
59
60         return new Object[][]{
61                 {"javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available",
62                         noTrustMngrImplException},
63                 {"java.lang.StringIndexOutOfBoundsException: String index out of range: 4",
64                         new StringIndexOutOfBoundsException(4)},
65                 {"java.io.FileNotFoundException: vid/WEB-INF/cert/aai-client-cert.p12",
66                         new FileNotFoundException("vid/WEB-INF/cert/aai-client-cert.p12")},
67                 {"NullPointerException at LoggingUtilsTest.java:[0-9]+",
68                         new NullPointerException("null")},
69                 {incorrectPasswordExceptionDescription,
70                         incorrectPasswordException},
71                 {incorrectPasswordExceptionDescription,
72                         new GenericUncheckedException(incorrectPasswordException)},
73                 {"javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_expired",
74                         new ProcessingException(new SSLHandshakeException("Received fatal alert: certificate_expired"))},
75                 {noValidCertDescription,
76                         noValidCert},
77                 {escapeBrackets(codehausParseDescription),
78                         codehausParseException},
79                 {escapeBrackets(fasterxmlMappingDescription),
80                         fasterxmlMappingException},
81                 {"org.onap.vid.exceptions.GenericUncheckedException: top message: org.onap.vid.exceptions.GenericUncheckedException: root message",
82                         new GenericUncheckedException("top message", new IOException("sandwich message", new GenericUncheckedException("root message")))},
83                 {"org.onap.vid.exceptions.GenericUncheckedException: basa",
84                         new GenericUncheckedException("basa")}
85         };
86
87     }
88
89     @Test(dataProvider = "exceptions")
90     public void testExceptionToDescription(String expectedDescription, Exception exceptionToDescribe) {
91         String expectedButDotsEscaped = expectedDescription.replace(".", "\\.");
92
93         assertThat(Logging.exceptionToDescription(exceptionToDescribe), matchesRegEx(expectedButDotsEscaped));
94     }
95
96     private static String escapeBrackets(String in) {
97         return in.replaceAll("[\\(\\[\\{\\)]", "\\\\$0");
98     }
99 }