/** * ============LICENSE_START======================================================= * org.onap.logging * ================================================================================ * Copyright © 2018 Amdocs * 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.logging.ref.slf4j; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.UUID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsSame.sameInstance; import static org.hamcrest.core.StringContains.containsString; import static org.hamcrest.core.StringEndsWith.endsWith; import static org.hamcrest.core.Every.everyItem; import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.hamcrest.core.IsNull.notNullValue; import static org.hamcrest.core.IsNull.nullValue; import static org.hamcrest.core.StringStartsWith.startsWith; import static org.hamcrest.beans.HasProperty.hasProperty; import static org.hamcrest.number.IsCloseTo.closeTo; import static org.hamcrest.number.OrderingComparison.greaterThan; import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; import static org.hamcrest.number.OrderingComparison.lessThan; import static org.hamcrest.number.OrderingComparison.lessThanOrEqualTo; import static org.hamcrest.collection.IsArray.array; import static org.hamcrest.collection.IsArrayContaining.hasItemInArray; import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder; import static org.hamcrest.collection.IsIn.isIn; import static org.hamcrest.collection.IsIn.isOneOf; import static org.hamcrest.collection.IsMapContaining.hasKey; import static org.hamcrest.collection.IsMapContaining.hasEntry; import static org.hamcrest.collection.IsMapContaining.hasValue; import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace; import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase; import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; import static org.hamcrest.text.IsEmptyString.isEmptyString; import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder; import static org.hamcrest.xml.HasXPath.hasXPath; import org.testng.Assert; import org.testng.annotations.Test; import javax.xml.bind.DatatypeConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import org.springframework.mock.web.MockHttpServletRequest; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNull.notNullValue; import static org.hamcrest.core.StringContains.containsString; import static org.hamcrest.number.OrderingComparison.greaterThan; /** * Smoketest output, though the embedded configuration isn't necessarily * canonical. * *
There are more comprehensive tests in the logging-slf4j-demo * project.
*/ public class ONAPLogAdapterOutputTest { /** Temporary directory into which logfiles are written. */ private static File sDir; @BeforeSuite public static void setUp() throws Exception { sDir = Files.createTempDirectory(ONAPLogAdapterOutputTest.class.getName()).toFile(); System.getProperties().setProperty("TESTNG_SLF4J_OUTPUT_DIRECTORY", sDir.getAbsolutePath()); LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Starting."); } @AfterSuite public static void tearDown() throws Exception { LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class).info("Ending."); Thread.sleep(1000L); if (sDir != null) { System.err.println("Should be deleting [" + sDir.getAbsolutePath() + "]..."); } } @Test public void testOutput() throws Exception { assertThat(sDir, notNullValue()); assertThat(sDir.isDirectory(), is(true)); final String uuid = UUID.randomUUID().toString(); final String errorcode = UUID.randomUUID().toString(); final Logger logger = LoggerFactory.getLogger(ONAPLogAdapterOutputTest.class); try { MDC.put("uuid", uuid); final ONAPLogAdapter adapter = new ONAPLogAdapter(logger); final ONAPLogAdapter.HttpServletRequestAdapter http = new ONAPLogAdapter.HttpServletRequestAdapter(new MockHttpServletRequest()); adapter.entering(http); adapter.unwrap().warn("a_warning"); try { throw new Exception("errorcode=" + errorcode); } catch (final Exception e) { adapter.unwrap().error("an_error", e); } Thread.sleep(1000L); } finally { MDC.clear(); } final List