summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap
diff options
context:
space:
mode:
authoremartin <ephraim.martin@est.tech>2019-02-08 18:22:27 +0000
committeremartin <ephraim.martin@est.tech>2019-02-08 18:22:27 +0000
commita8e48cfb480f325f0d359428672cc7f5cc20d13e (patch)
tree8f7fa492013d748585ef112cf44caada1c7cbb95 /src/test/java/org/onap
parent302b392825129871132a6354db7014771dfd70a4 (diff)
Improve ONAP logging compliance
Change-Id: I677977f592407d17c7cbd35b034785803c35d327 Issue-ID: DCAEGEN2-1166 Signed-off-by: emartin <ephraim.martin@est.tech>
Diffstat (limited to 'src/test/java/org/onap')
-rw-r--r--src/test/java/org/onap/dcaegen2/pmmapper/config/ConfigHandlerTests.java11
-rw-r--r--src/test/java/org/onap/dcaegen2/pmmapper/config/util/RequestSenderTests.java57
-rw-r--r--src/test/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriberTest.java24
3 files changed, 66 insertions, 26 deletions
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<ILoggingEvent> 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<ILoggingEvent> 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<ILoggingEvent> 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();
}
+
}