package biz.paluch.logging.gelf.log4j2; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; import com.google.code.tempusfugit.temporal.Condition; import com.google.code.tempusfugit.temporal.Duration; import com.google.code.tempusfugit.temporal.Timeout; import com.google.code.tempusfugit.temporal.WaitFor; import biz.paluch.logging.gelf.GelfTestSender; import biz.paluch.logging.gelf.netty.NettyLocalServer; import io.netty.channel.socket.nio.NioDatagramChannel; /** * @author Mark Paluch */ public class GelfLogAppenderEmptyFacilityNettyUdpIntegrationTests { public static final String LOG_MESSAGE = "foo bar test log message"; public static final String EXPECTED_LOG_MESSAGE = LOG_MESSAGE; private static LoggerContext loggerContext; private static NettyLocalServer server = new NettyLocalServer(NioDatagramChannel.class); @BeforeAll public static void setupClass() throws Exception { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2/log4j2-empty-facility-netty-udp.xml"); loggerContext = (LoggerContext) LogManager.getContext(false); loggerContext.reconfigure(); server.run(); } @AfterAll public static void afterClass() throws Exception { System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); loggerContext.reconfigure(); server.close(); } @BeforeEach public void before() throws Exception { GelfTestSender.getMessages().clear(); ThreadContext.clearAll(); server.clear(); } @Test public void testSimpleInfo() throws Exception { Logger logger = loggerContext.getLogger(getClass().getName()); logger.info(LOG_MESSAGE); waitForGelf(); List jsonValues = server.getJsonValues(); assertThat(jsonValues).hasSize(1); Map<String, Object> jsonValue = (Map<String, Object>) jsonValues.get(0); assertThat(jsonValue.get("facility")).isNull(); } @Test public void testEmptyMessage() throws Exception { Logger logger = loggerContext.getLogger(getClass().getName()); logger.info(""); assertThrows(TimeoutException.class, new Executable() { @Override public void execute() throws Throwable { waitForGelf(); } }); } private void waitForGelf() throws InterruptedException, TimeoutException { WaitFor.waitOrTimeout(new Condition() { @Override public boolean isSatisfied() { return !server.getJsonValues().isEmpty(); } }, Timeout.timeout(Duration.seconds(2))); } @Test public void testVeryLargeMessage() throws Exception { Logger logger = loggerContext.getLogger(getClass().getName()); StringBuilder builder = new StringBuilder(); for (int i = 0; i < 20000; i++) { int charId = (int) (Math.random() * Character.MAX_CODE_POINT); builder.append(charId); } logger.info(builder.toString()); waitForGelf(); List jsonValues = server.getJsonValues(); assertThat(jsonValues).hasSize(1); Map<String, Object> jsonValue = (Map<String, Object>) jsonValues.get(0); String shortMessage = builder.substring(0, 249); assertThat(jsonValue.get("full_message")).isEqualTo(builder.toString()); assertThat(jsonValue.get("short_message")).isEqualTo(shortMessage); } }