Skip to content

Commit

Permalink
Test cases (#38)
Browse files Browse the repository at this point in the history
* add rlp_03 and rlo_06 based tests

* fix test RlpLogbackAppenderTest

* remove throw from testDefaultSyslogMessage() signature

* fix assertions in RlpLogbackAppenderTest
  • Loading branch information
kortemik authored Nov 26, 2024
1 parent c5b5b10 commit 37853f9
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 137 deletions.
34 changes: 11 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,28 @@
<artifactId>rlo_14</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0-RC1</version>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0-RC1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>com.teragrep</groupId>
<artifactId>rlp_03</artifactId>
<version>9.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0-RC1</version>
<groupId>com.teragrep</groupId>
<artifactId>rlo_06</artifactId>
<version>9.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -150,7 +138,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.5.2</version>
<configuration>
<parallel>all</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -42,7 +43,7 @@ public void testgetDefaultSyslogMessage() {
"",
"");
System.out.println(sm.toRfc5424SyslogMessage());
assertNotNull(sm);
Assertions.assertNotNull(sm);

}

Expand Down
231 changes: 121 additions & 110 deletions src/test/java/com/teragrep/jla_01/RlpLogbackAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<RelpConnectionInstance> rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) {
rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender);
RlpLogbackAppender<ILoggingEvent> adapter = new RlpLogbackAppender<ILoggingEvent>();
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<byte[]> 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<ILoggingEvent> 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<RelpConnectionInstance> rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) {
rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender);
RlpLogbackAppender<ILoggingEvent> adapter = new RlpLogbackAppender<ILoggingEvent>();
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<RelpConnectionInstance> rlpClazz = Mockito.mockStatic(RelpConnectionInstance.class)) {
rlpClazz.when(RelpConnectionInstance::getRelpConnection).thenReturn(sender);
RlpLogbackAppender<ILoggingEvent> adapter = new RlpLogbackAppender<ILoggingEvent>();
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<byte[]> 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<ILoggingEvent> 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");
}
*/
}
4 changes: 2 additions & 2 deletions src/test/java/com/teragrep/jla_01/TestILoggingEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit 37853f9

Please sign in to comment.