Skip to content

Commit

Permalink
test(common): restructure tests for invalid Base64 encoded strings
Browse files Browse the repository at this point in the history
- Use a parameterized test with format.parse(), not the lower level API call, making it more aligned to the pre-existing tests.
- Add a parsing test with a string that contains UTF-8 chars to ensure encoding and decoding those works and don't provoke failures.
  • Loading branch information
poikilotherm committed Oct 18, 2024
1 parent 8d1545d commit e9ef711
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.gdcc.xoai.services.impl;

import static org.junit.jupiter.api.Assertions.*;
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 io.gdcc.xoai.exceptions.BadResumptionTokenException;
import io.gdcc.xoai.model.oaipmh.Granularity;
Expand All @@ -9,7 +11,6 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -69,21 +70,22 @@ void failingParse(String token) {

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t\t\t", "offset::1|", "until::2022-05-13T10:00:00Z"})
@ValueSource(
strings = {
" ",
"\t\t\t",
"offset::1|",
"until::2022-05-13T10:00:00Z",
"offset::1|set::Ätherische Öle"
})
void validParse(String token) {
String encoded = SimpleResumptionTokenFormat.base64Encode(token);
assertDoesNotThrow(() -> format.parse(encoded));
}

@Test
void validBase64Decoding() {
assertDoesNotThrow(() -> SimpleResumptionTokenFormat.base64Decode("b2Zmc2V0OjoxMDA="));
}

@Test
void invalidBase64Decoding() {
assertThrows(
BadResumptionTokenException.class,
() -> SimpleResumptionTokenFormat.base64Decode("b2Zmc2V0OjoMDA="));
@ParameterizedTest
@ValueSource(strings = {"b2Zmc2V0OjoMDA=", "VG========"})
void invalidBase64Parse(String sut) {
assertThrows(BadResumptionTokenException.class, () -> format.parse(sut));
}
}

0 comments on commit e9ef711

Please sign in to comment.