From a8e48cfb480f325f0d359428672cc7f5cc20d13e Mon Sep 17 00:00:00 2001 From: emartin Date: Fri, 8 Feb 2019 18:22:27 +0000 Subject: Improve ONAP logging compliance Change-Id: I677977f592407d17c7cbd35b034785803c35d327 Issue-ID: DCAEGEN2-1166 Signed-off-by: emartin --- .../pmmapper/config/ConfigHandlerTests.java | 11 ++++- .../pmmapper/config/util/RequestSenderTests.java | 57 +++++++++++++--------- .../datarouter/DataRouterSubscriberTest.java | 24 ++++++++- src/test/java/utils/LoggingUtils.java | 41 ++++++++++++++++ 4 files changed, 107 insertions(+), 26 deletions(-) create mode 100644 src/test/java/utils/LoggingUtils.java (limited to 'src/test') diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java b/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java index 0c3fb84..b4a2870 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java @@ -19,7 +19,6 @@ */ package org.onap.dcaegen2.pmmapper.config; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.when; @@ -46,6 +45,10 @@ import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; import com.google.gson.Gson; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import utils.LoggingUtils; + @ExtendWith(MockitoExtension.class) public class ConfigHandlerTests { private static String cbsConfig; @@ -86,6 +89,7 @@ public class ConfigHandlerTests { @Test public void getMapperConfig_success() throws Exception { + ListAppender logAppender = LoggingUtils.getLogListAppender(ConfigHandler.class); when(sender.send(anyString())).then(invocation -> { String url = (String) invocation.getArguments()[0]; return url.equals(consulURL) ? cbsConfig : validMapperConfig; @@ -95,6 +99,11 @@ public class ConfigHandlerTests { MapperConfig expectedConfig = gson.fromJson(validMapperConfig, MapperConfig.class); assertEquals(expectedConfig, actualConfig); + assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY"); + assertTrue(logAppender.list.get(1).getMessage().contains("Received ConfigBinding Service parameters")); + assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT"); + assertTrue(logAppender.list.get(4).getMessage().contains("Received pm-mapper configuration from ConfigBinding Service")); + logAppender.stop(); } @Test diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java b/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java index 470f146..770ae43 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java @@ -19,13 +19,14 @@ */ package org.onap.dcaegen2.pmmapper.config.util; +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 static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; import java.io.IOException; -import java.net.HttpURLConnection; import java.net.URL; import java.net.UnknownHostException; @@ -33,15 +34,19 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Matchers; import org.mockserver.client.server.MockServerClient; import org.mockserver.integration.ClientAndServer; +import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpStatusCode; import org.mockserver.verify.VerificationTimes; import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; +import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import utils.LoggingUtils; @RunWith(PowerMockRunner.class) @PrepareForTest(RequestSender.class) @@ -60,23 +65,27 @@ public class RequestSenderTests { mockServer.stop(); } - @BeforeClass - public static void setEnvironmentVariable() { - System.setProperty("CONSUL_HOST", "my_consult_host"); - System.setProperty("CONFIG_BINDING_SERVICE", "config-binding-service"); - System.setProperty("HOSTNAME", "hostname"); - } - @Test public void send_success() throws Exception { - - client.when(request()) - .respond(response().withStatusCode(HttpStatusCode.OK_200.code())); - - new RequestSender().send("http://127.0.0.1:1080/once"); - - client.verify(request(), VerificationTimes.exactly(1)); - client.clear(request()); + String url = "http://127.0.0.1:1080/once"; + String uuidRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"; + ListAppender logAppender = LoggingUtils.getLogListAppender(RequestSender.class); + HttpRequest req = HttpRequest.request(); + + client.when(req + .withHeader(ONAPLogConstants.Headers.REQUEST_ID, uuidRegex) + .withHeader(ONAPLogConstants.Headers.INVOCATION_ID, uuidRegex)) + .respond(response() + .withStatusCode(HttpStatusCode.OK_200.code()) + .withBody("ResponseBody")); + String result = new RequestSender().send(url); + + client.verify(req, VerificationTimes.atLeast(1)); + assertEquals(result, "ResponseBody"); + assertTrue(logAppender.list.get(1).getMessage().contains("Sending")); + assertTrue(logAppender.list.get(2).getMessage().contains("Received")); + logAppender.stop(); + client.clear(req); } @Test @@ -95,17 +104,17 @@ public class RequestSenderTests { } @Test - public void host_unknown() throws IOException { + public void host_unknown() throws Exception { PowerMockito.mockStatic(Thread.class); - URL url = PowerMockito.mock(URL.class); - PowerMockito.when(url.openConnection()) - .thenThrow(UnknownHostException.class); + String unknownHostUrl = "http://unknown-host:1080/host-is-unknown"; + PowerMockito.whenNew(URL.class).withArguments(unknownHostUrl) + .thenThrow(UnknownHostException.class); assertThrows(Exception.class, () -> { - new RequestSender().send("http://127.0.0.1:1080/host-is-unknown"); + new RequestSender().send(unknownHostUrl); }); - client.verify(request(), VerificationTimes.exactly(5)); + client.verify(request(), VerificationTimes.exactly(0)); client.clear(request()); } @@ -113,4 +122,4 @@ public class RequestSenderTests { return new MockServerClient("127.0.0.1", 1080); } -} +} \ No newline at end of file diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java index 645d1be..3239e93 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java @@ -29,14 +29,21 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; import io.undertow.io.Receiver; import io.undertow.io.Sender; import io.undertow.server.HttpServerExchange; import io.undertow.util.StatusCodes; +import utils.LoggingUtils; + import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; @@ -53,10 +60,11 @@ import org.onap.dcaegen2.services.pmmapper.exceptions.TooManyTriesException; import org.onap.dcaegen2.services.pmmapper.model.BusControllerConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; +import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; - +import org.slf4j.LoggerFactory; @RunWith(PowerMockRunner.class) @PrepareForTest(DataRouterSubscriber.class) @@ -139,6 +147,9 @@ public class DataRouterSubscriberTest { @Test public void testRequestInboundLimitedStateServiceUnavailable() throws Exception { HttpServerExchange httpServerExchange = mock(HttpServerExchange.class); + HttpServerExchangeAdapter adapterMock = PowerMockito.mock(HttpServerExchangeAdapter.class); + PowerMockito.whenNew(HttpServerExchangeAdapter.class).withAnyArguments().thenReturn(adapterMock); + Sender responseSender = mock(Sender.class); when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange); when(httpServerExchange.getResponseSender()).thenReturn(responseSender); @@ -150,6 +161,9 @@ public class DataRouterSubscriberTest { @Test public void testRequestInboundLimitedStateServiceNoEmission() throws Exception { HttpServerExchange httpServerExchange = mock(HttpServerExchange.class); + HttpServerExchangeAdapter adapterMock = PowerMockito.mock(HttpServerExchangeAdapter.class); + PowerMockito.whenNew(HttpServerExchangeAdapter.class).withAnyArguments().thenReturn(adapterMock); + Sender responseSender = mock(Sender.class); when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange); when(httpServerExchange.getResponseSender()).thenReturn(responseSender); @@ -197,6 +211,7 @@ public class DataRouterSubscriberTest { @Test public void testRequestInboundSuccess() throws Exception { + ListAppender logAppender = LoggingUtils.getLogListAppender(DataRouterSubscriber.class); HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS); Receiver receiver = mock(Receiver.class); when(httpServerExchange.getRequestReceiver()).thenReturn(receiver); @@ -222,5 +237,12 @@ public class DataRouterSubscriberTest { .receive(new Event(httpServerExchange, testString, new GsonBuilder().create() .fromJson(metadata, EventMetadata.class))); + + assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY"); + assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID")); + assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID")); + assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT"); + logAppender.stop(); } + } diff --git a/src/test/java/utils/LoggingUtils.java b/src/test/java/utils/LoggingUtils.java new file mode 100644 index 0000000..053bd0c --- /dev/null +++ b/src/test/java/utils/LoggingUtils.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package utils; + +import org.slf4j.LoggerFactory; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +public class LoggingUtils { + + /** + * Returns a ListAppender that contains all logging events. Call this method at the very beginning of the test + * */ + public static ListAppender getLogListAppender(Class c) { + Logger logger = (Logger) LoggerFactory.getLogger(c); + ListAppender listAppender = new ListAppender<>(); + listAppender.start(); + logger.addAppender(listAppender); + + return listAppender; + } +} -- cgit 1.2.3-korg