diff --git a/pom.xml b/pom.xml index a017828..16b0032 100644 --- a/pom.xml +++ b/pom.xml @@ -69,40 +69,28 @@ rlo_14 1.0.1 - - org.mockito - mockito-core - 3.6.0 - test - - - org.mockito - mockito-inline - 3.6.0 - test - org.junit.jupiter junit-jupiter-engine - 5.4.0-RC1 + 5.11.3 test - org.junit.platform - junit-platform-launcher - 1.4.0-RC1 + org.junit.jupiter + junit-jupiter-api + 5.11.3 test - junit - junit - 4.13.1 + com.teragrep + rlp_03 + 9.0.0 test - org.junit.jupiter - junit-jupiter-api - 5.4.0-RC1 + com.teragrep + rlo_06 + 9.0.1 test @@ -150,7 +138,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.5.2 all true diff --git a/src/test/java/com/teragrep/jla_01/LoggingEventConverterTest.java b/src/test/java/com/teragrep/jla_01/LoggingEventConverterTest.java index 9bcdc5f..52dc1df 100644 --- a/src/test/java/com/teragrep/jla_01/LoggingEventConverterTest.java +++ b/src/test/java/com/teragrep/jla_01/LoggingEventConverterTest.java @@ -17,7 +17,6 @@ Reliable Event Logging Protocol (RELP) Logback plugin package com.teragrep.jla_01; -import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.LocalDate; @@ -26,6 +25,8 @@ Reliable Event Logging Protocol (RELP) Logback plugin import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; + +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import com.teragrep.rlo_14.SyslogMessage; import ch.qos.logback.classic.spi.ILoggingEvent; @@ -42,7 +43,7 @@ public void testgetDefaultSyslogMessage() { "", ""); System.out.println(sm.toRfc5424SyslogMessage()); - assertNotNull(sm); + Assertions.assertNotNull(sm); } diff --git a/src/test/java/com/teragrep/jla_01/RlpLogbackAppenderTest.java b/src/test/java/com/teragrep/jla_01/RlpLogbackAppenderTest.java index 082ebf6..2b88c85 100644 --- a/src/test/java/com/teragrep/jla_01/RlpLogbackAppenderTest.java +++ b/src/test/java/com/teragrep/jla_01/RlpLogbackAppenderTest.java @@ -17,132 +17,143 @@ Reliable Event Logging Protocol (RELP) Logback plugin package com.teragrep.jla_01; -import static org.mockito.ArgumentMatchers.any; -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 java.io.IOException; -import java.util.concurrent.TimeoutException; - -import org.junit.Ignore; +import java.io.ByteArrayInputStream; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + +import ch.qos.logback.classic.LoggerContext; +import com.teragrep.jla_01.server.TestServer; +import com.teragrep.jla_01.server.TestServerFactory; +import com.teragrep.rlo_06.RFC5424Frame; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; -import org.mockito.Mockito; -import org.slf4j.Logger; +import org.junit.jupiter.api.TestInstance; import org.slf4j.LoggerFactory; -import com.teragrep.jla_01.RlpLogbackAppender; - import ch.qos.logback.classic.encoder.PatternLayoutEncoder; import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.Layout; -import com.teragrep.rlp_01.RelpConnection; -import com.teragrep.rlp_01.RelpWindow; +@TestInstance(TestInstance.Lifecycle.PER_CLASS) public class RlpLogbackAppenderTest { - @Test - @Ignore - public void testRelpOnLocalHost() { - Logger logger = LoggerFactory.getLogger("relp"); - logger.info("relp"); - } - - /* @Test() - public void testDefaultSyslogMessage() throws IllegalStateException, IOException { - RelpConnection sender = mock(RelpConnection.class); - try (MockedStatic rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) { - rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender); - RlpLogbackAppender adapter = new RlpLogbackAppender(); - adapter.setAppName("appName"); - adapter.start(); - - TestILoggingEvent eventObject = new TestILoggingEvent(); - - PatternLayoutEncoder encoder = mock(PatternLayoutEncoder.class); - Layout layout = mock(Layout.class); - when(encoder.getLayout()).thenReturn(layout); - when(layout.doLayout(any())).thenReturn("message"); - adapter.setEncoder(encoder); - - RelpWindow relpWindow = mock(RelpWindow.class); - when(sender.begin()).thenReturn(relpWindow); - - adapter.append(eventObject); - - verify(relpWindow, times(1)).insert(any()); - try { - verify(sender).commit(relpWindow); - } catch (TimeoutException e) { - e.printStackTrace(); - } + public void testDefaultSyslogMessage() { + TestServerFactory serverFactory = new TestServerFactory(); + + final int serverPort = 22601; + + final ConcurrentLinkedDeque messageList = new ConcurrentLinkedDeque<>(); + AtomicLong openCount = new AtomicLong(); + AtomicLong closeCount = new AtomicLong(); + + Assertions.assertDoesNotThrow(() -> { + try (TestServer server = serverFactory.create(serverPort, messageList, openCount, closeCount)) { + server.run(); + + ILoggingEvent eventObject = new TestILoggingEvent(); + + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + + + PatternLayoutEncoder encoder = new PatternLayoutEncoder(); + encoder.setPattern("%-5level %logger{36} - %msg%n"); + encoder.setContext(loggerContext); + encoder.start(); + + + RlpLogbackAppender relpAppender = new RlpLogbackAppender<>(); + relpAppender.setEncoder(encoder); + relpAppender.setAppName("appName"); + relpAppender.setHostname("localhost"); + relpAppender.setRelpPort(serverPort); + relpAppender.start(); + + relpAppender.append(eventObject); + relpAppender.stop(); + } + + } + ); + + Assertions.assertEquals(1, messageList.size(), "messageList size not expected"); + + + for (byte[] message : messageList) { + RFC5424Frame rfc5424Frame = new RFC5424Frame(); + rfc5424Frame.load(new ByteArrayInputStream(message)); + + AtomicBoolean hasNext = new AtomicBoolean(); + Assertions.assertDoesNotThrow(() -> hasNext.set(rfc5424Frame.next())); + Assertions.assertTrue(hasNext.get()); + + Assertions.assertEquals("localhost", rfc5424Frame.hostname.toString()); + Assertions.assertEquals("appName", rfc5424Frame.appName.toString()); + Assertions.assertEquals("DEBUG logger - none\n", rfc5424Frame.msg.toString()); } + + Assertions.assertTrue (openCount.get() >= 1, "openCount not expected"); + Assertions.assertEquals(1, closeCount.get(), "closeCount not expected"); } - @Test() + + @Test public void testDefaultSyslogMessageWithSDElement() { - RelpConnection sender = mock(RelpConnection.class); - try (MockedStatic rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) { - rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender); - RlpLogbackAppender adapter = new RlpLogbackAppender(); - adapter.setAppName("appName"); - adapter.setEnableEventId48577(Boolean.TRUE); - adapter.start(); - - TestILoggingEvent eventObject = new TestILoggingEvent(); - - PatternLayoutEncoder encoder = mock(PatternLayoutEncoder.class); - Layout layout = mock(Layout.class); - when(encoder.getLayout()).thenReturn(layout); - when(layout.doLayout(any())).thenReturn("message"); - adapter.setEncoder(encoder); - - RelpWindow relpWindow = mock(RelpWindow.class); - when(sender.begin()).thenReturn(relpWindow); - - adapter.append(eventObject); - verify(relpWindow, times(1)).insert(any()); - try { - verify(sender).commit(relpWindow); - } catch (TimeoutException e) { - e.printStackTrace(); - } - - } catch (IllegalStateException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + TestServerFactory serverFactory = new TestServerFactory(); - } + final int serverPort = 22602; - @Test() - public void testStopSyncAdapter() { - RelpConnection sender = mock(RelpConnection.class); - try (MockedStatic rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) { - rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender); - RlpLogbackAppender adapter = new RlpLogbackAppender(); - adapter.setAppName("appName"); - adapter.setEnableEventId48577(Boolean.TRUE); - adapter.start(); - - RelpWindow relpWindow = mock(RelpWindow.class); - when(sender.begin()).thenReturn(relpWindow); - adapter.stop(); - verify(relpWindow, times(0)).insert(any()); - try { - verify(sender).disconnect(); - } catch (TimeoutException e) { - e.printStackTrace(); - } - - } catch (IllegalStateException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + final ConcurrentLinkedDeque messageList = new ConcurrentLinkedDeque<>(); + AtomicLong openCount = new AtomicLong(); + AtomicLong closeCount = new AtomicLong(); + + Assertions.assertDoesNotThrow(() -> { + try (TestServer server = serverFactory.create(serverPort, messageList, openCount, closeCount)) { + server.run(); + + + TestILoggingEvent eventObject = new TestILoggingEvent(); + + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + + PatternLayoutEncoder encoder = new PatternLayoutEncoder(); + encoder.setPattern("%-5level %logger{36} - %msg%n"); + encoder.setContext(loggerContext); + encoder.start(); + + RlpLogbackAppender adapter = new RlpLogbackAppender<>(); + adapter.setEncoder(encoder); + adapter.setHostname("host1"); + adapter.setAppName("appName"); + adapter.setRelpPort(serverPort); + adapter.setEnableEventId48577(Boolean.TRUE); + adapter.start(); + + adapter.append(eventObject); + + adapter.stop(); + } + } + ); + Assertions.assertEquals(1, messageList.size(), "messageList size not expected"); + + + for (byte[] message : messageList) { + RFC5424Frame rfc5424Frame = new RFC5424Frame(); + rfc5424Frame.load(new ByteArrayInputStream(message)); + + AtomicBoolean frameNext = new AtomicBoolean(); + Assertions.assertDoesNotThrow( () -> {frameNext.set(rfc5424Frame.next());}); + Assertions.assertTrue(frameNext.get()); + + Assertions.assertEquals("host1", rfc5424Frame.hostname.toString()); + Assertions.assertEquals("appName", rfc5424Frame.appName.toString()); + Assertions.assertEquals("DEBUG logger - none\n", rfc5424Frame.msg.toString()); } + + + Assertions.assertTrue(openCount.get() >= 1, "openCount not expected"); + Assertions.assertEquals(1, closeCount.get(), "closeCount not expected"); } - */ } diff --git a/src/test/java/com/teragrep/jla_01/TestILoggingEvent.java b/src/test/java/com/teragrep/jla_01/TestILoggingEvent.java index 1bef717..dc1fb9e 100644 --- a/src/test/java/com/teragrep/jla_01/TestILoggingEvent.java +++ b/src/test/java/com/teragrep/jla_01/TestILoggingEvent.java @@ -51,13 +51,13 @@ public String getMessage() { @Override public Object[] getArgumentArray() { // TODO Auto-generated method stub - return null; + return new Object[]{}; } @Override public String getFormattedMessage() { // TODO Auto-generated method stub - return null; + return "none"; } @Override diff --git a/src/test/java/com/teragrep/jla_01/server/RelpEventCloseCounting.java b/src/test/java/com/teragrep/jla_01/server/RelpEventCloseCounting.java new file mode 100644 index 0000000..e46bfe2 --- /dev/null +++ b/src/test/java/com/teragrep/jla_01/server/RelpEventCloseCounting.java @@ -0,0 +1,39 @@ +/* + Reliable Event Logging Protocol (RELP) Logback plugin + Copyright (C) 2021 Suomen Kanuuna Oy + + 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. + */ +package com.teragrep.jla_01.server; + +import com.teragrep.rlp_03.frame.delegate.FrameContext; +import com.teragrep.rlp_03.frame.delegate.event.RelpEvent; +import com.teragrep.rlp_03.frame.delegate.event.RelpEventClose; + +import java.util.concurrent.atomic.AtomicLong; + +class RelpEventCloseCounting extends RelpEvent { + private final AtomicLong closeCount; + private final RelpEventClose relpEventClose; + + RelpEventCloseCounting(AtomicLong closeCount) { + this.closeCount = closeCount; + this.relpEventClose = new RelpEventClose(); + } + + @Override + public void accept(FrameContext frameContext) { + relpEventClose.accept(frameContext); + closeCount.incrementAndGet(); + } +} diff --git a/src/test/java/com/teragrep/jla_01/server/RelpEventOpenCounting.java b/src/test/java/com/teragrep/jla_01/server/RelpEventOpenCounting.java new file mode 100644 index 0000000..f2321fb --- /dev/null +++ b/src/test/java/com/teragrep/jla_01/server/RelpEventOpenCounting.java @@ -0,0 +1,39 @@ +/* + Reliable Event Logging Protocol (RELP) Logback plugin + Copyright (C) 2021 Suomen Kanuuna Oy + + 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. + */ +package com.teragrep.jla_01.server; + +import com.teragrep.rlp_03.frame.delegate.FrameContext; +import com.teragrep.rlp_03.frame.delegate.event.RelpEvent; +import com.teragrep.rlp_03.frame.delegate.event.RelpEventOpen; + +import java.util.concurrent.atomic.AtomicLong; + +class RelpEventOpenCounting extends RelpEvent { + private final AtomicLong openCount; + private final RelpEventOpen eventOpen; + + RelpEventOpenCounting(AtomicLong openCount) { + this.openCount = openCount; + this.eventOpen = new RelpEventOpen(); + } + + @Override + public void accept(FrameContext frameContext) { + eventOpen.accept(frameContext); + openCount.incrementAndGet(); + } +} diff --git a/src/test/java/com/teragrep/jla_01/server/TestServer.java b/src/test/java/com/teragrep/jla_01/server/TestServer.java new file mode 100644 index 0000000..0104554 --- /dev/null +++ b/src/test/java/com/teragrep/jla_01/server/TestServer.java @@ -0,0 +1,50 @@ +/* + Reliable Event Logging Protocol (RELP) Logback plugin + Copyright (C) 2021 Suomen Kanuuna Oy + + 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. + */ +package com.teragrep.jla_01.server; + +import com.teragrep.net_01.eventloop.EventLoop; +import com.teragrep.net_01.server.Server; +import org.junit.jupiter.api.Assertions; + +import java.util.concurrent.ExecutorService; + +public class TestServer implements Runnable, AutoCloseable { + private final EventLoop eventLoop; + private final Thread eventLoopThread; + private final ExecutorService executorService; + private final Server server; + + public TestServer(EventLoop eventLoop, Thread eventLoopThread, ExecutorService executorService, Server server) { + this.eventLoop = eventLoop; + this.eventLoopThread = eventLoopThread; + this.executorService = executorService; + this.server = server; + } + + @Override + public void close() throws Exception { + eventLoop.stop(); + executorService.shutdown(); + eventLoopThread.join(); + server.close(); // closes port + } + + @Override + public void run() { + eventLoopThread.start(); + } +} diff --git a/src/test/java/com/teragrep/jla_01/server/TestServerFactory.java b/src/test/java/com/teragrep/jla_01/server/TestServerFactory.java new file mode 100644 index 0000000..364f673 --- /dev/null +++ b/src/test/java/com/teragrep/jla_01/server/TestServerFactory.java @@ -0,0 +1,70 @@ +/* + Reliable Event Logging Protocol (RELP) Logback plugin + Copyright (C) 2021 Suomen Kanuuna Oy + + 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. + */ +package com.teragrep.jla_01.server; + +import com.teragrep.net_01.channel.socket.PlainFactory; +import com.teragrep.net_01.eventloop.EventLoop; +import com.teragrep.net_01.eventloop.EventLoopFactory; +import com.teragrep.net_01.server.Server; +import com.teragrep.net_01.server.ServerFactory; +import com.teragrep.rlp_03.frame.FrameDelegationClockFactory; +import com.teragrep.rlp_03.frame.delegate.EventDelegate; +import com.teragrep.rlp_03.frame.delegate.FrameDelegate; +import com.teragrep.rlp_03.frame.delegate.event.RelpEvent; +import com.teragrep.rlp_03.frame.delegate.event.RelpEventSyslog; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Supplier; + +public class TestServerFactory { + + public TestServer create(int port, ConcurrentLinkedDeque messageList, AtomicLong connectionOpenCount, AtomicLong connectionCleanCloseCount) throws IOException { + EventLoopFactory eventLoopFactory = new EventLoopFactory(); + + EventLoop eventLoop = eventLoopFactory.create(); + Thread eventLoopThread = new Thread(eventLoop); + ExecutorService executorService = Executors.newSingleThreadExecutor(); + + Supplier frameDelegateSupplier = () -> { + Map relpCommandConsumerMap = new HashMap<>(); + + relpCommandConsumerMap.put("close", new RelpEventCloseCounting(connectionCleanCloseCount)); + + relpCommandConsumerMap.put("open", new RelpEventOpenCounting(connectionOpenCount)); + + relpCommandConsumerMap.put("syslog", new RelpEventSyslog((frame) -> messageList.add(frame.relpFrame().payload().toBytes()))); + + return new EventDelegate(relpCommandConsumerMap); + }; + + ServerFactory serverFactory = new ServerFactory( + eventLoop, + executorService, + new PlainFactory(), + new FrameDelegationClockFactory(frameDelegateSupplier) + ); + Server server = serverFactory.create(port); + + return new TestServer(eventLoop, eventLoopThread, executorService, server); + } +} diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..4c6ca15 --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,13 @@ + + + + + + %-5level [%logger{0}] - %msg%n + + + + + + +