Skip to content

Commit

Permalink
use AssertJ
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Oct 8, 2023
1 parent 56309c2 commit 5eca9d6
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 495 deletions.
12 changes: 6 additions & 6 deletions src/test/java/de/siegmar/logbackgelf/AddressResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

package de.siegmar.logbackgelf;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.jupiter.api.Test;

public class AddressResolverTest {
class AddressResolverTest {

@Test
public void test() throws UnknownHostException {
void test() throws UnknownHostException {
final AddressResolver resolver =
new AddressResolver("foo", new AtomicInteger(Integer.MAX_VALUE)) {
@Override
Expand All @@ -42,9 +42,9 @@ protected InetAddress[] lookup() throws UnknownHostException {
}
};

assertEquals("8.8.8.8", resolver.resolve().getHostAddress());
assertEquals("127.0.0.1", resolver.resolve().getHostAddress());
assertEquals("8.8.8.8", resolver.resolve().getHostAddress());
assertThat(resolver.resolve().getHostAddress()).isEqualTo("8.8.8.8");
assertThat(resolver.resolve().getHostAddress()).isEqualTo("127.0.0.1");
assertThat(resolver.resolve().getHostAddress()).isEqualTo("8.8.8.8");
}

}
24 changes: 8 additions & 16 deletions src/test/java/de/siegmar/logbackgelf/CustomGelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@

import static de.siegmar.logbackgelf.GelfEncoderTest.basicValidation;
import static de.siegmar.logbackgelf.GelfEncoderTest.simpleLoggingEvent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggingEvent;
import de.siegmar.logbackgelf.custom.CustomGelfEncoder;

public class CustomGelfEncoderTest {
class CustomGelfEncoderTest {

private static final String LOGGER_NAME = GelfEncoderTest.class.getCanonicalName();
private static final String THREAD_NAME = "thread name";
Expand All @@ -47,13 +43,13 @@ public class CustomGelfEncoderTest {
private final CustomGelfEncoder encoder = new CustomGelfEncoder();

@BeforeEach
public void before() {
void before() {
encoder.setContext(new LoggerContext());
encoder.setOriginHost("localhost");
}

@Test
public void custom() throws IOException {
void custom() {
encoder.start();

final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Expand All @@ -64,15 +60,11 @@ public void custom() throws IOException {
event.setThreadName(THREAD_NAME);
final String logMsg = encodeToStr(event);

final ObjectMapper om = new ObjectMapper();
final JsonNode jsonNode = om.readTree(logMsg);
basicValidation(jsonNode);
basicValidation(logMsg);

assertEquals("message 1\n", jsonNode.get("full_message").textValue());
assertEquals(
"ad4ab384b5b7dca879dc1b65132db321a67239f13c2cc0cd9867c8e607c7ce08",
jsonNode.get("_sha256").textValue(),
"Log line: " + logMsg
assertThatJson(logMsg).and(
j -> j.node("full_message").isEqualTo("message 1\\n"),
j -> j.node("_sha256").isEqualTo("ad4ab384b5b7dca879dc1b65132db321a67239f13c2cc0cd9867c8e607c7ce08")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

package de.siegmar.logbackgelf;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
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.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.security.KeyStore;
Expand All @@ -32,7 +30,6 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.LocalDate;
import java.util.Arrays;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
Expand All @@ -41,7 +38,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CustomX509TrustManagerTest {
class CustomX509TrustManagerTest {

private static final String HOSTNAME = "graylog.foo.bar";

Expand All @@ -50,28 +47,26 @@ public class CustomX509TrustManagerTest {
private X509Util.CertBuilder c;

@BeforeEach
public void before() throws Exception {
void before() throws Exception {
c = new X509Util.CertBuilder();
}

@SuppressWarnings("checkstyle:illegalcatch")
@Test
public void selfSigned() {
void selfSigned() {
// The default trust manager will reject a self-signed certificate
try {
validate(c.build(null, HOSTNAME));
fail();
} catch (Exception e) {
assertTrue(e.getMessage().contains("unable to find valid certification path"));
}
assertThatThrownBy(() -> validate(c.build(null, HOSTNAME)))
.hasRootCauseMessage("unable to find valid certification path to requested target");

// Using the NoopX509TrustManager the same certificate will be allowed
tm = new CustomX509TrustManager(new NoopX509TrustManager(), HOSTNAME);
assertDoesNotThrow(() -> validate(c.build(null, HOSTNAME)));

assertThatCode(() -> validate(c.build(null, HOSTNAME)))
.doesNotThrowAnyException();
}

@Test
public void validCert() throws Exception {
void validCert() throws Exception {
tm = new CustomX509TrustManager(new NoopX509TrustManager(), HOSTNAME);

validate(c.build(null, HOSTNAME));
Expand All @@ -81,23 +76,24 @@ public void validCert() throws Exception {
}

@Test
public void nameMismatch() {
void nameMismatch() {
tm = new CustomX509TrustManager(new NoopX509TrustManager(), HOSTNAME);

final String wrongName = "graylog2.foo.bar";

CertificateException e = assertThrows(CertificateException.class,
() -> validate(c.build(null, wrongName)));
assertEquals("Server certificate mismatch. Tried to verify graylog.foo.bar "
+ "against subject alternative names: [graylog2.foo.bar]", e.getMessage());
assertThatThrownBy(() -> validate(c.build(null, wrongName)))
.isInstanceOf(CertificateException.class)
.hasMessage("Server certificate mismatch. Tried to verify graylog.foo.bar "
+ "against subject alternative names: [%s]", wrongName);

e = assertThrows(CertificateException.class, () -> validate(c.build(wrongName)));
assertEquals("Server certificate mismatch. Tried to verify graylog.foo.bar "
+ "against common name: graylog2.foo.bar", e.getMessage());
assertThatThrownBy(() -> validate(c.build(wrongName)))
.isInstanceOf(CertificateException.class)
.hasMessage("Server certificate mismatch. Tried to verify graylog.foo.bar "
+ "against common name: %s", wrongName);
}

@Test
public void caSigned() throws Exception {
void caSigned() throws Exception {
final X509Util.CABuilder caBuilder = new X509Util.CABuilder();
final X509Certificate cert = prepareCaSigned(caBuilder)
.build(HOSTNAME);
Expand All @@ -107,13 +103,15 @@ public void caSigned() throws Exception {

@Test
void clientValidation() {
assertThrows(UnsupportedOperationException.class,
() -> tm.checkClientTrusted(new X509Certificate[] {}, "RSA"));
assertThatThrownBy(() -> tm.checkClientTrusted(new X509Certificate[]{}, "RSA"))
.isInstanceOf(UnsupportedOperationException.class);
}

@Test
void acceptedIssuers() {
assertTrue(Arrays.equals(defaultTrustManager(null).getAcceptedIssuers(), tm.getAcceptedIssuers()));
assertThat(defaultTrustManager(null)).satisfies(
t -> assertThat(t.getAcceptedIssuers()).containsExactly(tm.getAcceptedIssuers())
);
}

private X509Util.CertBuilder prepareCaSigned(final X509Util.CABuilder caBuilder)
Expand All @@ -131,15 +129,15 @@ private X509Util.CertBuilder prepareCaSigned(final X509Util.CABuilder caBuilder)
}

@Test
public void expired() throws Exception {
void expired() throws Exception {
final X509Util.CABuilder caBuilder = new X509Util.CABuilder();
final X509Certificate cert = prepareCaSigned(caBuilder)
.validFrom(LocalDate.now().minusYears(2))
.validTo(LocalDate.now().minusYears(1))
.build(HOSTNAME);

assertThrows(CertificateException.class,
() -> validate(cert, caBuilder.getCaCertificate()));
assertThatThrownBy(() -> validate(cert, caBuilder.getCaCertificate()))
.isInstanceOf(CertificateException.class);
}

private void validate(final X509Certificate... certificates) throws CertificateException {
Expand Down
Loading

0 comments on commit 5eca9d6

Please sign in to comment.