|
| 1 | +package io.cucumber.compatibility; |
| 2 | + |
| 3 | +import io.cucumber.compatibility.matchers.AComparableMessage; |
| 4 | +import io.cucumber.core.options.RuntimeOptionsBuilder; |
| 5 | +import io.cucumber.core.plugin.MessageFormatter; |
| 6 | +import io.cucumber.core.runtime.Runtime; |
| 7 | +import io.cucumber.core.runtime.TimeServiceEventBus; |
| 8 | +import io.cucumber.messages.Messages; |
| 9 | +import io.cucumber.messages.NdjsonToMessageIterable; |
| 10 | +import io.cucumber.messages.internal.com.google.protobuf.GeneratedMessageV3; |
| 11 | +import org.hamcrest.Matcher; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.MethodSource; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileOutputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.time.Clock; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.UUID; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import static org.hamcrest.CoreMatchers.is; |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder; |
| 32 | +import static org.hamcrest.collection.IsMapContaining.hasEntry; |
| 33 | + |
| 34 | +public class CompatibilityTest { |
| 35 | + |
| 36 | + @ParameterizedTest |
| 37 | + @MethodSource("io.cucumber.compatibility.TestCase#testCases") |
| 38 | + void produces_expected_output_for(TestCase testCase) throws IOException { |
| 39 | + File parentDir = new File("target/messages/" + testCase.getId()); |
| 40 | + parentDir.mkdirs(); |
| 41 | + File output = new File(parentDir, "out.ndjson"); |
| 42 | + |
| 43 | + try { |
| 44 | + Runtime.builder() |
| 45 | + .withRuntimeOptions(new RuntimeOptionsBuilder() |
| 46 | + .addGlue(testCase.getGlue()) |
| 47 | + .addFeature(testCase.getFeature()) |
| 48 | + .build()) |
| 49 | + .withAdditionalPlugins(new MessageFormatter(new FileOutputStream(output))) |
| 50 | + .withEventBus(new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID)) |
| 51 | + .build() |
| 52 | + .run(); |
| 53 | + } catch (Exception ignored) { |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + List<Messages.Envelope> expected = readAllMessages(testCase.getExpectedFile()); |
| 58 | + List<Messages.Envelope> actual = readAllMessages(output.toPath()); |
| 59 | + |
| 60 | + Map<String, List<GeneratedMessageV3>> expectedEnvelopes = openEnvelopes(expected); |
| 61 | + Map<String, List<GeneratedMessageV3>> actualEnvelopes = openEnvelopes(actual); |
| 62 | + |
| 63 | + expectedEnvelopes.forEach((messageType, expectedMessages) -> |
| 64 | + assertThat( |
| 65 | + actualEnvelopes, |
| 66 | + hasEntry(is(messageType), containsInRelativeOrder(aComparableMessage(expectedMessages))) |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private static List<Messages.Envelope> readAllMessages(Path output) throws IOException { |
| 72 | + List<Messages.Envelope> expectedEnvelopes = new ArrayList<>(); |
| 73 | + InputStream input = Files.newInputStream(output); |
| 74 | + new NdjsonToMessageIterable(input) |
| 75 | + .forEach(expectedEnvelopes::add); |
| 76 | + return expectedEnvelopes; |
| 77 | + } |
| 78 | + |
| 79 | + @SuppressWarnings("unchecked") |
| 80 | + private static <T> Map<String, List<T>> openEnvelopes(List<? extends GeneratedMessageV3> actual) { |
| 81 | + Map<String, List<T>> map = new LinkedHashMap<>(); |
| 82 | + actual.forEach(envelope -> envelope.getAllFields() |
| 83 | + .forEach((fieldDescriptor, value) -> { |
| 84 | + String jsonName = fieldDescriptor.getJsonName(); |
| 85 | + map.putIfAbsent(jsonName, new ArrayList<>()); |
| 86 | + map.get(jsonName).add((T) value); |
| 87 | + })); |
| 88 | + return map; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + private static List<Matcher<? super GeneratedMessageV3>> aComparableMessage(List<GeneratedMessageV3> expectedMessages) { |
| 93 | + return expectedMessages.stream() |
| 94 | + .map(AComparableMessage::new) |
| 95 | + .collect(Collectors.toList()); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
0 commit comments