Search in sources :

Example 1 with JsonNode

use of io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode in project cucumber-jvm by cucumber.

the class AComparableMessage method aComparableValue.

@SuppressWarnings("unchecked")
private static Matcher<?> aComparableValue(Object value, int depth) {
    if (value instanceof ObjectNode) {
        JsonNode message = (JsonNode) value;
        return new AComparableMessage(message, depth);
    }
    if (value instanceof ArrayNode) {
        ArrayNode values = (ArrayNode) value;
        Spliterator<JsonNode> spliterator = spliteratorUnknownSize(values.iterator(), 0);
        List<Matcher<? super Object>> allComparableValues = stream(spliterator, false).map(o -> aComparableValue(o, depth)).map(o -> (Matcher<? super Object>) o).collect(Collectors.toList());
        return contains(allComparableValues);
    }
    if (value instanceof TextNode || value instanceof NumericNode || value instanceof BooleanNode) {
        return CoreMatchers.is(value);
    }
    throw new IllegalArgumentException("Unsupported type " + value.getClass() + ": " + value);
}
Also used : Spliterators.spliteratorUnknownSize(java.util.Spliterators.spliteratorUnknownSize) CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers(org.hamcrest.CoreMatchers) CoreMatchers.anyOf(org.hamcrest.CoreMatchers.anyOf) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) CoreMatchers.not(org.hamcrest.CoreMatchers.not) JsonNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) LinkedHashMap(java.util.LinkedHashMap) NumericNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.NumericNode) Map(java.util.Map) TextNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.TextNode) IsMapContaining.hasEntry(org.hamcrest.collection.IsMapContaining.hasEntry) Description(org.hamcrest.Description) IsMapContaining.hasKey(org.hamcrest.collection.IsMapContaining.hasKey) ObjectNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ObjectNode) IsIterableContainingInRelativeOrder.containsInRelativeOrder(org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder) ArrayNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ArrayNode) BooleanNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.BooleanNode) Collectors(java.util.stream.Collectors) List(java.util.List) StreamSupport.stream(java.util.stream.StreamSupport.stream) Matcher(org.hamcrest.Matcher) Spliterator(java.util.Spliterator) ObjectNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ObjectNode) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher) Matcher(org.hamcrest.Matcher) JsonNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode) TextNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.TextNode) NumericNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.NumericNode) BooleanNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.BooleanNode) ArrayNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with JsonNode

use of io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode in project cucumber-jvm by cucumber.

the class CompatibilityTest method readAllMessages.

private static List<JsonNode> readAllMessages(Path output) throws IOException {
    List<JsonNode> expectedEnvelopes = new ArrayList<>();
    ObjectMapper mapper = new ObjectMapper().enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Files.readAllLines(output).forEach(s -> {
        try {
            expectedEnvelopes.add(mapper.readTree(s));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    });
    return expectedEnvelopes;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(io.cucumber.messages.internal.com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(io.cucumber.messages.internal.com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with JsonNode

use of io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode in project cucumber-jvm by cucumber.

the class AComparableMessage method asMapOfJsonNameToField.

private static Map<String, Object> asMapOfJsonNameToField(JsonNode envelope) {
    Map<String, Object> map = new LinkedHashMap<>();
    envelope.fieldNames().forEachRemaining(jsonField -> {
        JsonNode value = envelope.get(jsonField);
        map.put(jsonField, value);
    });
    return map;
}
Also used : JsonNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with JsonNode

use of io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode in project cucumber-jvm by cucumber.

the class CompatibilityTest method produces_expected_output_for.

@ParameterizedTest
@MethodSource("io.cucumber.compatibility.TestCase#testCases")
void produces_expected_output_for(TestCase testCase) throws IOException {
    Path parentDir = Files.createDirectories(Paths.get("target", "messages", testCase.getId()));
    Path outputNdjson = parentDir.resolve("out.ndjson");
    Path outputHtml = parentDir.resolve("out.html");
    Path outputJson = parentDir.resolve("out.json");
    try {
        Runtime.builder().withRuntimeOptions(new RuntimeOptionsBuilder().addGlue(testCase.getGlue()).addFeature(testCase.getFeature()).build()).withAdditionalPlugins(new MessageFormatter(newOutputStream(outputNdjson)), new HtmlFormatter(newOutputStream(outputHtml)), new JsonFormatter(newOutputStream(outputJson))).build().run();
    } catch (Exception ignored) {
    }
    List<JsonNode> expected = readAllMessages(testCase.getExpectedFile());
    List<JsonNode> actual = readAllMessages(outputNdjson);
    Map<String, List<JsonNode>> expectedEnvelopes = openEnvelopes(expected);
    Map<String, List<JsonNode>> actualEnvelopes = openEnvelopes(actual);
    // exception: Java step definitions are not in a predictable order
    // because Class#getMethods() does not return a predictable order.
    sortStepDefinitions(expectedEnvelopes);
    sortStepDefinitions(actualEnvelopes);
    // unknown-parameter-types
    if ("unknown-parameter-type".equals(testCase.getId())) {
        expectedEnvelopes.remove("testCase");
        expectedEnvelopes.remove("testCaseStarted");
        expectedEnvelopes.remove("testStepStarted");
        expectedEnvelopes.remove("testStepFinished");
        expectedEnvelopes.remove("testCaseFinished");
    }
    expectedEnvelopes.forEach((messageType, expectedMessages) -> assertThat(actualEnvelopes, hasEntry(is(messageType), containsInRelativeOrder(aComparableMessage(expectedMessages)))));
}
Also used : Path(java.nio.file.Path) JsonFormatter(io.cucumber.core.plugin.JsonFormatter) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) JsonNode(io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) MessageFormatter(io.cucumber.core.plugin.MessageFormatter) HtmlFormatter(io.cucumber.core.plugin.HtmlFormatter) JsonProcessingException(io.cucumber.messages.internal.com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

JsonNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)3 JsonProcessingException (io.cucumber.messages.internal.com.fasterxml.jackson.core.JsonProcessingException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 RuntimeOptionsBuilder (io.cucumber.core.options.RuntimeOptionsBuilder)1 HtmlFormatter (io.cucumber.core.plugin.HtmlFormatter)1 JsonFormatter (io.cucumber.core.plugin.JsonFormatter)1 MessageFormatter (io.cucumber.core.plugin.MessageFormatter)1 ObjectMapper (io.cucumber.messages.internal.com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ArrayNode)1 BooleanNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.BooleanNode)1 NumericNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.NumericNode)1 ObjectNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (io.cucumber.messages.internal.com.fasterxml.jackson.databind.node.TextNode)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Spliterator (java.util.Spliterator)1 Spliterators.spliteratorUnknownSize (java.util.Spliterators.spliteratorUnknownSize)1