Skip to content

Commit

Permalink
Verify each FileOpener/HttpOpener test "process" individually. (#517)
Browse files Browse the repository at this point in the history
Instead of only the last one in the case of HttpOpener.
  • Loading branch information
blackwinter committed Jan 12, 2024
1 parent 83e5d9e commit 3beb8c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,14 @@ private void shouldPerformRequest(final String input, final HttpOpener.Method me

WireMock.stubFor(stub);

TestHelpers.assertReader(receiver, responseBody, () -> {
TestHelpers.assertReader(receiver, () -> {
opener.process(String.format(input, baseUrl));

// use the opener a second time in a workflow:
opener.process(String.format(input, baseUrl));

opener.closeStream();

return 2;
});
}, responseBody, responseBody);

WireMock.verify(request);
}
Expand Down
29 changes: 12 additions & 17 deletions metafacture-io/src/test/java/org/metafacture/io/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

import java.io.File;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.IntSupplier;

public final class TestHelpers {

public static void assertFile(final ObjectReceiver<Reader> receiver, final String expected, final File file, final Consumer<FileOpener> consumer) {
assertReader(receiver, expected, () -> {
assertReader(receiver, () -> {
final FileOpener opener = new FileOpener();
if (consumer != null) {
consumer.accept(opener);
Expand All @@ -39,25 +41,18 @@ public static void assertFile(final ObjectReceiver<Reader> receiver, final Strin
opener.setReceiver(receiver);
opener.process(file.getAbsolutePath());
opener.closeStream();

return 1;
});
}, expected);
}

public static void assertReader(final ObjectReceiver<Reader> receiver, final String expected, final IntSupplier supplier) {
final StringBuilder sb = new StringBuilder();

Mockito.doAnswer(i -> {
sb.delete(0, sb.length());
sb.append(ResourceUtil.readAll(i.getArgument(0)));

return null;
}).when(receiver).process(Mockito.any(Reader.class));
public static void assertReader(final ObjectReceiver<Reader> receiver, final Runnable runnable, final String... expected) {
final List<String> actual = new ArrayList<>();
Mockito.doAnswer(i -> actual.add(ResourceUtil.readAll(i.getArgument(0)))).when(receiver).process(Mockito.any(Reader.class));

final int times = supplier.getAsInt();
runnable.run();

Mockito.verify(receiver, Mockito.times(times)).process(Mockito.any(Reader.class));
Assert.assertEquals(expected, sb.toString());
Mockito.verify(receiver, Mockito.times(expected.length)).process(Mockito.any(Reader.class));
Arrays.stream(expected).forEach(i -> Assert.assertEquals(i, actual.remove(0)));
Assert.assertEquals(0, actual.size());
}

}

0 comments on commit 3beb8c4

Please sign in to comment.