blob: 6187cad009697e94d2a833bb29553ca2c3aeb042 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21
22
23package org.onap.osam.utils;
24
25import com.fasterxml.jackson.core.JsonLocation;
26import com.fasterxml.jackson.core.JsonParseException;
27import com.fasterxml.jackson.databind.JsonMappingException;
28import org.onap.osam.exceptions.GenericUncheckedException;
29import org.testng.annotations.DataProvider;
30import org.testng.annotations.Test;
31import sun.security.provider.certpath.SunCertPathBuilderException;
32import sun.security.validator.ValidatorException;
33
34import javax.crypto.BadPaddingException;
35import javax.net.ssl.SSLHandshakeException;
36import javax.ws.rs.ProcessingException;
37import java.io.FileNotFoundException;
38import java.io.IOException;
39import java.security.UnrecoverableKeyException;
40import java.security.cert.CertificateException;
41
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.onap.osam.testUtils.RegExMatcher.matchesRegEx;
44
45public class LoggingUtilsTest {
46
47 @DataProvider
48 public static Object[][] exceptions() {
49 Exception e0 = new CertificateException("No X509TrustManager implementation available");
50 Exception noTrustMngrImplException = new SSLHandshakeException(e0.toString());
51 noTrustMngrImplException.initCause(e0);
52
53 Exception e1 = new BadPaddingException("Given final block not properly padded");
54 Exception incorrectPasswordException = new IOException("keystore password was incorrect",
55 new UnrecoverableKeyException("failed to decrypt safe contents entry: " + e1));
56 String incorrectPasswordExceptionDescription = "" +
57 "java.io.IOException: keystore password was incorrect: " +
58 "java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: " +
59 "javax.crypto.BadPaddingException: Given final block not properly padded";
60
61 Exception e2 = new SunCertPathBuilderException("unable to find valid certification path to requested target");
62 Exception noValidCert = new ProcessingException(new ValidatorException("PKIX path building failed: " + e2.toString(), e2));
63 String noValidCertDescription = "" +
64 "javax.ws.rs.ProcessingException: " +
65 "sun.security.validator.ValidatorException: PKIX path building failed: " +
66 "sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target";
67
68 RuntimeException fasterxmlMappingException = new RuntimeException(new JsonMappingException("Can not deserialize instance of java.lang.String out of START_ARRAY token",
69 new com.fasterxml.jackson.core.JsonLocation("{ example json }", 15, 1, 20)));
70 String fasterxmlMappingDescription = "" +
71 "com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token\n" +
72 " at [Source: { example json }; line: 1, column: 20]";
73
74 return new Object[][]{
75 {"javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available",
76 noTrustMngrImplException},
77 {"java.lang.StringIndexOutOfBoundsException: String index out of range: 4",
78 new StringIndexOutOfBoundsException(4)},
79 {"java.io.FileNotFoundException: osam/WEB-INF/cert/aai-client-cert.p12",
80 new FileNotFoundException("osam/WEB-INF/cert/aai-client-cert.p12")},
81 {"NullPointerException at LoggingUtilsTest.java:[0-9]+",
82 new NullPointerException("null")},
83 {incorrectPasswordExceptionDescription,
84 incorrectPasswordException},
85 {incorrectPasswordExceptionDescription,
86 new GenericUncheckedException(incorrectPasswordException)},
87 {"javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_expired",
88 new ProcessingException(new SSLHandshakeException("Received fatal alert: certificate_expired"))},
89 {noValidCertDescription,
90 noValidCert},
91 {escapeBrackets(fasterxmlMappingDescription),
92 fasterxmlMappingException},
93 {"org.onap.osam.exceptions.GenericUncheckedException: top message: org.onap.osam.exceptions.GenericUncheckedException: root message",
94 new GenericUncheckedException("top message", new IOException("sandwich message", new GenericUncheckedException("root message")))},
95 {"org.onap.osam.exceptions.GenericUncheckedException: basa",
96 new GenericUncheckedException("basa")}
97 };
98
99 }
100
101 @Test(dataProvider = "exceptions")
102 public void testExceptionToDescription(String expectedDescription, Exception exceptionToDescribe) {
103 String expectedButDotsEscaped = expectedDescription.replace(".", "\\.");
104
105 assertThat(Logging.exceptionToDescription(exceptionToDescribe), matchesRegEx(expectedButDotsEscaped));
106 }
107
108 private static String escapeBrackets(String in) {
109 return in.replaceAll("[\\(\\[\\{\\)]", "\\\\$0");
110 }
111}