1 package org.onap.vid.utils;
3 import com.fasterxml.jackson.databind.JsonMappingException;
4 import org.codehaus.jackson.JsonLocation;
5 import org.codehaus.jackson.JsonParseException;
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;
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;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.onap.vid.testUtils.RegExMatcher.matchesRegEx;
23 public class LoggingUtilsTest {
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);
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";
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";
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 "org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)):" +
51 " expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n" +
52 " at [Source: <html>i'm an error</html>; line: 1, column: 1]";
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: { example json }; line: 1, column: 20]";
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,
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")}
89 @Test(dataProvider = "exceptions")
90 public void testExceptionToDescription(String expectedDescription, Exception exceptionToDescribe) {
91 String expectedButDotsEscaped = expectedDescription.replace(".", "\\.");
93 assertThat(Logging.exceptionToDescription(exceptionToDescribe), matchesRegEx(expectedButDotsEscaped));
96 private static String escapeBrackets(String in) {
97 return in.replaceAll("[\\(\\[\\{\\)]", "\\\\$0");