Search in sources :

Example 21 with Envelope

use of io.cucumber.messages.types.Envelope in project cucumber-jvm by cucumber.

the class MessageFormatterTest method test.

@Test
void test() {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    MessageFormatter formatter = new MessageFormatter(bytes);
    EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
    formatter.setEventPublisher(bus);
    TestRunStarted testRunStarted = new TestRunStarted();
    testRunStarted.setTimestamp(new Timestamp(10L, 0L));
    Envelope testRunStartedEnvelope = new Envelope();
    testRunStartedEnvelope.setTestRunStarted(testRunStarted);
    bus.send(testRunStartedEnvelope);
    TestRunFinished testRunFinished = new TestRunFinished();
    testRunFinished.setTimestamp(new Timestamp(15L, 0L));
    Envelope testRunFinishedEnvelope = new Envelope();
    testRunFinishedEnvelope.setTestRunFinished(testRunFinished);
    bus.send(testRunFinishedEnvelope);
    String ndjson = new String(bytes.toByteArray(), UTF_8);
    assertThat(ndjson, containsString("" + "{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}\n" + "{\"testRunFinished\":{\"timestamp\":{\"seconds\":15,\"nanos\":0}}}\n"));
}
Also used : TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) TestRunStarted(io.cucumber.messages.types.TestRunStarted) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) EventBus(io.cucumber.core.eventbus.EventBus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UUID(java.util.UUID) Envelope(io.cucumber.messages.types.Envelope) TestRunFinished(io.cucumber.messages.types.TestRunFinished) Timestamp(io.cucumber.messages.types.Timestamp) Test(org.junit.jupiter.api.Test)

Example 22 with Envelope

use of io.cucumber.messages.types.Envelope in project cucumber-jvm by cucumber.

the class GherkinMessagesFeatureParser method parse.

@Override
public Optional<Feature> parse(URI path, String source, Supplier<UUID> idGenerator) {
    List<Envelope> sources = singletonList(makeSourceEnvelope(source, path.toString()));
    List<Envelope> envelopes = Gherkin.fromSources(sources, true, true, true, () -> idGenerator.get().toString()).collect(toList());
    GherkinDocument gherkinDocument = envelopes.stream().map(Envelope::getGherkinDocument).filter(Objects::nonNull).findFirst().orElse(null);
    if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
        List<String> errors = envelopes.stream().map(Envelope::getParseError).filter(Objects::nonNull).map(ParseError::getMessage).collect(toList());
        if (!errors.isEmpty()) {
            throw new FeatureParserException("Failed to parse resource at: " + path + "\n" + String.join("\n", errors));
        }
        return Optional.empty();
    }
    CucumberQuery cucumberQuery = new CucumberQuery();
    cucumberQuery.update(gherkinDocument);
    GherkinDialectProvider dialectProvider = new GherkinDialectProvider();
    io.cucumber.messages.types.Feature feature = gherkinDocument.getFeature();
    String language = feature.getLanguage();
    GherkinDialect dialect = dialectProvider.getDialect(language, null);
    List<io.cucumber.messages.types.Pickle> pickleMessages = envelopes.stream().map(Envelope::getPickle).filter(Objects::nonNull).collect(toList());
    List<Pickle> pickles = pickleMessages.stream().map(pickle -> new GherkinMessagesPickle(pickle, path, dialect, cucumberQuery)).collect(toList());
    GherkinMessagesFeature messagesFeature = new GherkinMessagesFeature(feature, path, source, pickles, envelopes);
    return Optional.of(messagesFeature);
}
Also used : Pickle(io.cucumber.core.gherkin.Pickle) FeatureParser(io.cucumber.core.gherkin.FeatureParser) GherkinDialect(io.cucumber.gherkin.GherkinDialect) Envelope(io.cucumber.messages.types.Envelope) Gherkin(io.cucumber.gherkin.Gherkin) UUID(java.util.UUID) Gherkin.makeSourceEnvelope(io.cucumber.gherkin.Gherkin.makeSourceEnvelope) Supplier(java.util.function.Supplier) Collections.singletonList(java.util.Collections.singletonList) Objects(java.util.Objects) GherkinDocument(io.cucumber.messages.types.GherkinDocument) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Feature(io.cucumber.core.gherkin.Feature) FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) Optional(java.util.Optional) ParseError(io.cucumber.messages.types.ParseError) URI(java.net.URI) GherkinDialectProvider(io.cucumber.gherkin.GherkinDialectProvider) Pickle(io.cucumber.core.gherkin.Pickle) FeatureParserException(io.cucumber.core.gherkin.FeatureParserException) GherkinDialectProvider(io.cucumber.gherkin.GherkinDialectProvider) GherkinDialect(io.cucumber.gherkin.GherkinDialect) GherkinDocument(io.cucumber.messages.types.GherkinDocument) Envelope(io.cucumber.messages.types.Envelope) Gherkin.makeSourceEnvelope(io.cucumber.gherkin.Gherkin.makeSourceEnvelope) Objects(java.util.Objects)

Aggregations

Envelope (io.cucumber.messages.types.Envelope)22 UUID (java.util.UUID)8 Test (org.junit.jupiter.api.Test)8 EventBus (io.cucumber.core.eventbus.EventBus)5 Feature (io.cucumber.core.gherkin.Feature)5 ArrayList (java.util.ArrayList)5 TimeServiceEventBus (io.cucumber.core.runtime.TimeServiceEventBus)4 StepDefinition (io.cucumber.core.backend.StepDefinition)3 TestRunFinished (io.cucumber.messages.types.TestRunFinished)3 TestRunStarted (io.cucumber.messages.types.TestRunStarted)3 Timestamp (io.cucumber.messages.types.Timestamp)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 URI (java.net.URI)3 List (java.util.List)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 HookDefinition (io.cucumber.core.backend.HookDefinition)2 ScenarioScoped (io.cucumber.core.backend.ScenarioScoped)2 TestCaseState (io.cucumber.core.backend.TestCaseState)2 TestFeatureParser (io.cucumber.core.feature.TestFeatureParser)2 EmbedEvent (io.cucumber.plugin.event.EmbedEvent)2