From daed5d0aea1635597238969a3057a264701a558e Mon Sep 17 00:00:00 2001 From: Ties de Kock Date: Mon, 28 Aug 2023 09:02:12 +0200 Subject: [PATCH] Use more idiomatic api in tests --- .../ripe/rpki/rsyncit/rrdp/RrdpFetcher.java | 4 ++- .../rpki/rsyncit/rrdp/RrdpFetcherTest.java | 31 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcher.java b/src/main/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcher.java index 89409ae..bc1c721 100644 --- a/src/main/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcher.java +++ b/src/main/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcher.java @@ -1,5 +1,6 @@ package net.ripe.rpki.rsyncit.rrdp; +import com.google.common.annotations.VisibleForTesting; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import net.ripe.rpki.commons.crypto.cms.RpkiSignedObject; @@ -335,7 +336,8 @@ private static void checkResult(String objectUri, ValidationResult result) { * This MAY help for the corner case of objects having second-accuracy timestamps * and the timestatmp in seconds being the same for multiple objects. */ - private Instant incorporateHashInTimestamp(Instant t, byte[] hash) { + @VisibleForTesting + public static Instant incorporateHashInTimestamp(Instant t, byte[] hash) { final BigInteger ms = new BigInteger(hash).mod(BigInteger.valueOf(1000_000_000L)); return t.truncatedTo(ChronoUnit.SECONDS).plusNanos(ms.longValue()); } diff --git a/src/test/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcherTest.java b/src/test/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcherTest.java index dcdec39..9d54f4b 100644 --- a/src/test/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcherTest.java +++ b/src/test/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcherTest.java @@ -15,6 +15,7 @@ import java.time.Instant; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertThrows; class RrdpFetcherTest { @@ -43,8 +44,9 @@ public void testNormalFlow() throws NotificationStructureException, XPathExpress @Test public void testEmptyNotificationXml() { - NotificationStructureException exception = assertThrows(NotificationStructureException.class, () -> tryFetch("", null)); - assertThat(exception.getMessage()).isEqualTo("Empty notification file."); + assertThatThrownBy(() -> tryFetch("", null)) + .isInstanceOf(NotificationStructureException.class) + .hasMessage("Empty notification file."); } @Test @@ -57,8 +59,9 @@ public void testNotificationXmlWithoutSnapshotTag() { final String notificationXml = """ """; - NotificationStructureException exception = assertThrows(NotificationStructureException.class, () -> tryFetch(notificationXml, null)); - assertThat(exception.getMessage()).isEqualTo("No snapshot tag in the notification file."); + assertThatThrownBy(() -> tryFetch(notificationXml, null)) + .isInstanceOf(NotificationStructureException.class) + .hasMessage("No snapshot tag in the notification file."); } @Test @@ -68,8 +71,9 @@ public void testNotificationXmlTwoSnapshotTags() { """; - NotificationStructureException exception = assertThrows(NotificationStructureException.class, () -> tryFetch(notificationXml, null)); - assertThat(exception.getMessage()).isEqualTo("More than one snapshot tag in the notification file."); + assertThatThrownBy(() -> tryFetch(notificationXml, null)) + .isInstanceOf(NotificationStructureException.class) + .hasMessage("More than one snapshot tag in the notification file."); } @Test @@ -87,9 +91,10 @@ public void testSnapshotWrongHash() throws NotificationStructureException, XPath """; - SnapshotStructureException exception = assertThrows(SnapshotStructureException.class, () -> tryFetch(notificationXml, snapshotXml)); - assertThat(exception.getMessage()).isEqualTo( - "Structure of snapshot at https://rrdp.paas.rpki.ripe.net/1c33ba5d-4e16-448d-9a22-b12599ef1cba/29861/5d1d7670842dd277/snapshot.xml " + + assertThatThrownBy(() -> tryFetch(notificationXml, snapshotXml)) + .isInstanceOf(SnapshotStructureException.class) + .hasMessage( + "Structure of snapshot at https://rrdp.paas.rpki.ripe.net/1c33ba5d-4e16-448d-9a22-b12599ef1cba/29861/5d1d7670842dd277/snapshot.xml " + "did not match expected structure: with len(content) = 400 had " + "sha256(content) = 25ffe0eb76860c269e6abdd16ec4eb991008e66de32173b6d3411ab3f6dcf058, " + "expected 770c21936e8129499d4f08698b0f08eadf3610a6624004a179e216d568ac04f5"); @@ -102,8 +107,9 @@ public void testEmptySnapshot() { """; - SnapshotStructureException exception = assertThrows(SnapshotStructureException.class, () -> tryFetch(notificationXml, "")); - assertThat(exception.getMessage()).isEqualTo("Structure of snapshot at https:/host/snapshot.xml did not match expected structure: Empty snapshot"); + assertThatThrownBy(() -> tryFetch(notificationXml, "")) + .isInstanceOf(SnapshotStructureException.class) + .hasMessage("Structure of snapshot at https:/host/snapshot.xml did not match expected structure: Empty snapshot"); } @Test @@ -120,7 +126,7 @@ public void testBrokenSnapshot() { """, Sha256.asString(snapshotXml)); - SAXParseException exception = assertThrows(SAXParseException.class, () -> tryFetch(notificationXml, snapshotXml)); + assertThrows(SAXParseException.class, () -> tryFetch(notificationXml, snapshotXml)); } private RrdpFetcher.FetchResult tryFetch(String notificationXml, String snapshotXml) throws NotificationStructureException, XPathExpressionException, IOException, ParserConfigurationException, SAXException { @@ -128,5 +134,4 @@ private RrdpFetcher.FetchResult tryFetch(String notificationXml, String snapshot return fetcher.processNotificationXml(notificationXml.getBytes(StandardCharsets.UTF_8), url -> new RrdpFetcher.Downloaded(snapshotXml.getBytes(StandardCharsets.UTF_8), Instant.now())); } - } \ No newline at end of file