Skip to content

Add smoke tests for null messages #4314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class JavaUtilLoggingWithExceptionServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger("smoketestapp");

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
logger.log(Level.SEVERE, "This is an exception!", new Exception("Fake Exception"));
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
logger.log(Level.SEVERE, "This is an exception!", e);
}

private static boolean testNullMessage(HttpServletRequest request) {
String testNullMessage = request.getParameter("test-null-message");
return "true".equalsIgnoreCase(testNullMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ void test() throws Exception {
@Test
@TargetUri("/testWithException")
void testWithException() throws Exception {
testWithException(false);
}

@Test
@TargetUri("/testWithException?test-null-message=true")
void testWithExceptionWithNullMessage() throws Exception {
testWithException(true);
}

private void testWithException(boolean testNullMessage) throws Exception {
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);

Envelope rdEnvelope = rdList.get(0);
Expand All @@ -97,7 +107,11 @@ void testWithException() throws Exception {
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData();

assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.Exception");
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Fake Exception");
if (testNullMessage) {
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.Exception");
} else {
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Fake Exception");
}
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public class Log4j1WithExceptionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
MDC.put("MDC key", "MDC value");
logger.error("This is an exception!", new Exception("Fake Exception"));
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
logger.error("This is an exception!", e);
MDC.remove("MDC key");
}

private static boolean testNullMessage(HttpServletRequest request) {
String testNullMessage = request.getParameter("test-null-message");
return "true".equalsIgnoreCase(testNullMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ private void logDisableMessage() {
@Test
@TargetUri("/testWithException")
void testWithException() throws Exception {
testWithException(false);
}

@Test
@TargetUri("/testWithException?test-null-message=true")
void testWithExceptionWithNullMessage() throws Exception {
testWithException(true);
}

private void testWithException(boolean testNullMessage) throws Exception {
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);

Envelope rdEnvelope = rdList.get(0);
Expand All @@ -119,7 +129,11 @@ void testWithException() throws Exception {
ExceptionDetails ex = details.get(0);

assertThat(ex.getTypeName()).isEqualTo("java.lang.Exception");
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
if (testNullMessage) {
assertThat(ex.getMessage()).isEqualTo("java.lang.Exception");
} else {
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
}
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public class Log4j2WithExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
ThreadContext.put("MDC key", "MDC value");
logger.error("This is an exception!", new Exception("Fake Exception"));
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
logger.error("This is an exception!", e);
ThreadContext.remove("MDC key");
}

private static boolean testNullMessage(HttpServletRequest request) {
String testNullMessage = request.getParameter("test-null-message");
return "true".equalsIgnoreCase(testNullMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ void test() throws Exception {
@Test
@TargetUri("/testWithException")
void testWithException() throws Exception {
testWithException(false);
}

@Test
@TargetUri("/testWithException?test-null-message=true")
void testWithExceptionWithNullMessage() throws Exception {
testWithException(true);
}

private void testWithException(boolean testNullMessage) throws Exception {
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);

Envelope rdEnvelope = rdList.get(0);
Expand All @@ -130,7 +140,11 @@ void testWithException() throws Exception {
ExceptionDetails ex = details.get(0);

assertThat(ex.getTypeName()).isEqualTo("java.lang.Exception");
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
if (testNullMessage) {
assertThat(ex.getMessage()).isEqualTo("java.lang.Exception");
} else {
assertThat(ex.getMessage()).isEqualTo("Fake Exception");
}
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties()).containsEntry("Logger Message", "This is an exception!");
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public class LogbackWithExceptionServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
MDC.put("MDC key", "MDC value");
logger.error("This is an exception!", new Exception("Fake Exception"));
Exception e = testNullMessage(request) ? new Exception() : new Exception("Fake Exception");
logger.error("This is an exception!", e);
MDC.remove("MDC key");
}

private static boolean testNullMessage(HttpServletRequest request) {
String testNullMessage = request.getParameter("test-null-message");
return "true".equalsIgnoreCase(testNullMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void testWithException() {
"ClassName",
"com.microsoft.applicationinsights.smoketestapp.LogbackWithExceptionServlet")
.hasProperty("MethodName", "doGet")
.hasProperty("LineNumber", "21")
.hasProperty("LineNumber", "22")
.hasPropertiesSize(9)));
} else {
testing.waitAndAssertTrace(
Expand Down