Skip to content

Commit

Permalink
Use more idiomatic api in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ties committed Aug 28, 2023
1 parent 8bf1904 commit daed5d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcher.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
31 changes: 18 additions & 13 deletions src/test/java/net/ripe/rpki/rsyncit/rrdp/RrdpFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -57,8 +59,9 @@ public void testNotificationXmlWithoutSnapshotTag() {
final String notificationXml = """
<notification xmlns="http://www.ripe.net/rpki/rrdp" version="1" session_id="1c33ba5d-4e16-448d-9a22-b12599ef1cba" serial="29861">
</notification>""";
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
Expand All @@ -68,8 +71,9 @@ public void testNotificationXmlTwoSnapshotTags() {
<snapshot uri="https://host/snapshot1.xml" hash="b6dc8a81fea493c5c7a4314a3a9c5996c96cf94417983c8a92462aaf13d6cac8"/>
<snapshot uri="https://host/snapshot2.xml" hash="b6dc8a81fea493c5c7a4314a3a9c5996c735234417983c8a92462aaf13d6cac8"/>
</notification>""";
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
Expand All @@ -87,9 +91,10 @@ public void testSnapshotWrongHash() throws NotificationStructureException, XPath
<snapshot uri="https://rrdp.paas.rpki.ripe.net/1c33ba5d-4e16-448d-9a22-b12599ef1cba/29861/5d1d7670842dd277/snapshot.xml" hash="770c21936e8129499d4f08698b0f08eadf3610a6624004a179e216d568ac04f5"/>
</notification>""";

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");
Expand All @@ -102,8 +107,9 @@ public void testEmptySnapshot() {
<snapshot uri="https:/host/snapshot.xml" hash="770c21936e8129499d4f08698b0f08eadf3610a6624004a179e216d568ac04f5"/>
</notification>""";

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
Expand All @@ -120,13 +126,12 @@ public void testBrokenSnapshot() {
</notification>
""", 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 {
var fetcher = new RrdpFetcher(TestDefaults.defaultConfig(), TestDefaults.defaultWebClient(), new State(), new RRDPFetcherMetrics(new SimpleMeterRegistry()));
return fetcher.processNotificationXml(notificationXml.getBytes(StandardCharsets.UTF_8),
url -> new RrdpFetcher.Downloaded(snapshotXml.getBytes(StandardCharsets.UTF_8), Instant.now()));
}

}

0 comments on commit daed5d0

Please sign in to comment.