Search in sources :

Example 1 with TestCaseNode

use of io.confluent.ksql.test.model.TestCaseNode in project ksql by confluentinc.

the class TestExecutorUtilTest method loadTestCase.

private static TestCase loadTestCase(final String testName) {
    try {
        final QttTestFile qttTestFile = TestJsonMapper.INSTANCE.get().readValue(TEST_FILE, QttTestFile.class);
        final TestCaseNode testCaseNode = qttTestFile.tests.stream().filter(tcn -> tcn.name().equals(testName)).findFirst().orElseThrow(() -> new AssertionError("Invalid test: no test case named " + testName));
        return TestCaseBuilder.buildTests(testCaseNode, TEST_FILE.toPath(), name -> mock(TestLocation.class)).get(0);
    } catch (final Exception e) {
        throw new AssertionError("Invalid test: failed to load test " + testName, e);
    }
}
Also used : QttTestFile(io.confluent.ksql.test.model.QttTestFile) CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) PlannedStatement(io.confluent.ksql.test.tools.TestExecutorUtil.PlannedStatement) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) ServiceContext(io.confluent.ksql.services.ServiceContext) RunWith(org.junit.runner.RunWith) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ConfiguredKsqlPlan(io.confluent.ksql.planner.plan.ConfiguredKsqlPlan) After(org.junit.After) StubKafkaService(io.confluent.ksql.test.tools.stubs.StubKafkaService) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) Before(org.junit.Before) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) QttTestFile(io.confluent.ksql.test.model.QttTestFile) Iterator(java.util.Iterator) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) TestLocation(io.confluent.ksql.test.model.TestLocation) Test(org.junit.Test) KsqlConfig(io.confluent.ksql.util.KsqlConfig) File(java.io.File) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) List(java.util.List) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException)

Example 2 with TestCaseNode

use of io.confluent.ksql.test.model.TestCaseNode in project ksql by confluentinc.

the class TestCasePlanLoader method buildStatementsInTestCase.

private static TestCasePlan buildStatementsInTestCase(final TestCase testCase, final KsqlVersion version, final long timestamp, final Map<String, String> configs, final String simpleTestName, final boolean validateResults) {
    final TestInfoGatherer testInfo = executeTestCaseAndGatherInfo(testCase, validateResults);
    final List<TopicNode> allTopicNodes = getTopicsFromTestCase(testCase, configs);
    final TestCaseNode testCodeNode = new TestCaseNode(simpleTestName, Optional.empty(), ImmutableList.of(), testCase.getInputRecords().stream().map(RecordNode::from).collect(Collectors.toList()), testCase.getOutputRecords().stream().map(RecordNode::from).collect(Collectors.toList()), allTopicNodes, testCase.statements(), testCase.properties(), null, testCase.getPostConditions().asNode(testInfo.getTopics(), testInfo.getSources()), true);
    final TestCaseSpecNode spec = new TestCaseSpecNode(version.getVersion().toString(), timestamp, testCase.getOriginalFileName().toString(), testInfo.getSchemas(), testCodeNode);
    final TestCasePlanNode plan = new TestCasePlanNode(testInfo.getPlans(), configs);
    return new TestCasePlan(// not used
    new PathLocation(Paths.get("").toAbsolutePath()), spec, plan, testInfo.getTopologyDescription());
}
Also used : PathLocation(io.confluent.ksql.test.model.PathLocation) PostTopicNode(io.confluent.ksql.test.model.PostConditionsNode.PostTopicNode) TopicNode(io.confluent.ksql.test.model.TopicNode) TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) RecordNode(io.confluent.ksql.test.model.RecordNode)

Example 3 with TestCaseNode

use of io.confluent.ksql.test.model.TestCaseNode in project ksql by confluentinc.

the class KsqlTestingTool method runWithTripleFiles.

static void runWithTripleFiles(final String statementFile, final String inputFile, final String outputFile, final Optional<String> extensionDir) throws Exception {
    final InputRecordsNode inputRecordNodes;
    final OutputRecordsNode outRecordNodes;
    try {
        inputRecordNodes = (inputFile == null) ? null : OBJECT_MAPPER.readValue(new File(inputFile), InputRecordsNode.class);
    } catch (final Exception inputException) {
        throw new Exception("File name: " + inputFile + " Message: " + inputException.getMessage());
    }
    try {
        outRecordNodes = OBJECT_MAPPER.readValue(new File(outputFile), OutputRecordsNode.class);
    } catch (final Exception outputException) {
        throw new Exception("File name: " + outputFile + " Message: " + outputException.getMessage());
    }
    final Path stmtsPath = Paths.get(statementFile);
    final List<String> statements = getSqlStatements(stmtsPath);
    final TestCaseNode testCaseNode = new TestCaseNode("KSQL_Test", Optional.empty(), null, (inputFile == null) ? null : inputRecordNodes.getInputRecords(), outRecordNodes.getOutputRecords(), Collections.emptyList(), statements, null, null, null, true);
    final PathLocation location = new PathLocation(stmtsPath.toAbsolutePath());
    final TestCase testCase = TestCaseBuilder.buildTests(testCaseNode, location.getTestPath(), name -> location).get(0);
    executeTestCase(testCase, TestExecutor.create(true, extensionDir));
}
Also used : Path(java.nio.file.Path) TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KsqlParser(io.confluent.ksql.parser.KsqlParser) IOException(java.io.IOException) OutputRecordsNode(io.confluent.ksql.test.model.OutputRecordsNode) Collectors(java.util.stream.Collectors) File(java.io.File) PathLocation(io.confluent.ksql.test.model.PathLocation) TestOptions(io.confluent.ksql.test.tools.command.TestOptions) List(java.util.List) InputRecordsNode(io.confluent.ksql.test.model.InputRecordsNode) Paths(java.nio.file.Paths) DefaultKsqlParser(io.confluent.ksql.parser.DefaultKsqlParser) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Path(java.nio.file.Path) Collections(java.util.Collections) PathLocation(io.confluent.ksql.test.model.PathLocation) TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) OutputRecordsNode(io.confluent.ksql.test.model.OutputRecordsNode) InputRecordsNode(io.confluent.ksql.test.model.InputRecordsNode) File(java.io.File) IOException(java.io.IOException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 4 with TestCaseNode

use of io.confluent.ksql.test.model.TestCaseNode in project ksql by confluentinc.

the class TestCaseBuilder method createTest.

private static TestCase createTest(final TestCaseNode test, final Path originalFileName, final TestLocation location, final Optional<String> explicitFormat) {
    final String testName = TestCaseBuilderUtil.buildTestName(originalFileName, test.name(), explicitFormat);
    try {
        final VersionBounds versionBounds = test.versionBounds().build();
        final List<String> statements = TestCaseBuilderUtil.buildStatements(test.statements(), explicitFormat);
        final Optional<Matcher<Throwable>> ee = test.expectedException().map(ExpectedExceptionNode::build);
        final Map<String, Topic> topics = TestCaseBuilderUtil.getAllTopics(statements, test.topics().stream().map(TopicNode::build).collect(Collectors.toList()), test.outputs().stream().map(RecordNode::build).collect(Collectors.toList()), test.inputs().stream().map(RecordNode::build).collect(Collectors.toList()), new InternalFunctionRegistry(), new KsqlConfig(test.properties())).stream().collect(Collectors.toMap(t -> t.getName().toLowerCase(), t -> t));
        final List<Record> inputRecords = test.inputs().stream().map(r -> {
            final String topicName = r.topicName().toLowerCase();
            // parse columns which value type cannot be detected without a schema
            if (topics.containsKey(topicName)) {
                return r.build(topics.get(topicName).getKeySchema(), topics.get(topicName).getValueSchema(), topics.get(topicName).getKeyFeatures(), topics.get(topicName).getValueFeatures());
            }
            return r.build();
        }).collect(Collectors.toList());
        final List<Record> outputRecords = test.outputs().stream().map(RecordNode::build).collect(Collectors.toList());
        final PostConditions post = test.postConditions().map(PostConditionsNode::build).orElse(PostConditions.NONE);
        return new TestCase(location, originalFileName, testName, versionBounds, test.properties(), topics.values(), inputRecords, outputRecords, statements, ee, post);
    } catch (final Exception e) {
        throw new AssertionError(testName + ": Invalid test. " + e.getMessage(), e);
    }
}
Also used : TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry) TestLocation(io.confluent.ksql.test.model.TestLocation) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) ExpectedExceptionNode(io.confluent.ksql.test.model.ExpectedExceptionNode) TopicNode(io.confluent.ksql.test.model.TopicNode) PostConditionsNode(io.confluent.ksql.test.model.PostConditionsNode) PostConditions(io.confluent.ksql.test.tools.conditions.PostConditions) Map(java.util.Map) Matcher(org.hamcrest.Matcher) Optional(java.util.Optional) RecordNode(io.confluent.ksql.test.model.RecordNode) Path(java.nio.file.Path) TopicNode(io.confluent.ksql.test.model.TopicNode) Matcher(org.hamcrest.Matcher) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ExpectedExceptionNode(io.confluent.ksql.test.model.ExpectedExceptionNode) RecordNode(io.confluent.ksql.test.model.RecordNode) PostConditions(io.confluent.ksql.test.tools.conditions.PostConditions) InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry)

Example 5 with TestCaseNode

use of io.confluent.ksql.test.model.TestCaseNode in project ksql by confluentinc.

the class TestCaseBuilder method buildTests.

public static List<TestCase> buildTests(final TestCaseNode test, final Path originalFileName, final Function<String, TestLocation> testLocator) {
    if (!test.isEnabled()) {
        return ImmutableList.of();
    }
    try {
        final Stream<Optional<String>> formats = test.formats().isEmpty() ? Stream.of(Optional.empty()) : test.formats().stream().map(Optional::of);
        final TestLocation location = testLocator.apply(test.name());
        return formats.map(format -> createTest(test, originalFileName, location, format)).collect(Collectors.toList());
    } catch (final Exception e) {
        throw new AssertionError("Invalid test '" + test.name() + "': " + e.getMessage(), e);
    }
}
Also used : TestCaseNode(io.confluent.ksql.test.model.TestCaseNode) InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry) TestLocation(io.confluent.ksql.test.model.TestLocation) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) ExpectedExceptionNode(io.confluent.ksql.test.model.ExpectedExceptionNode) TopicNode(io.confluent.ksql.test.model.TopicNode) PostConditionsNode(io.confluent.ksql.test.model.PostConditionsNode) PostConditions(io.confluent.ksql.test.tools.conditions.PostConditions) Map(java.util.Map) Matcher(org.hamcrest.Matcher) Optional(java.util.Optional) RecordNode(io.confluent.ksql.test.model.RecordNode) Path(java.nio.file.Path) Optional(java.util.Optional) TestLocation(io.confluent.ksql.test.model.TestLocation)

Aggregations

TestCaseNode (io.confluent.ksql.test.model.TestCaseNode)5 List (java.util.List)4 Optional (java.util.Optional)4 RecordNode (io.confluent.ksql.test.model.RecordNode)3 TestLocation (io.confluent.ksql.test.model.TestLocation)3 TopicNode (io.confluent.ksql.test.model.TopicNode)3 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 Path (java.nio.file.Path)3 Collectors (java.util.stream.Collectors)3 ImmutableList (com.google.common.collect.ImmutableList)2 InternalFunctionRegistry (io.confluent.ksql.function.InternalFunctionRegistry)2 ExpectedExceptionNode (io.confluent.ksql.test.model.ExpectedExceptionNode)2 PathLocation (io.confluent.ksql.test.model.PathLocation)2 PostConditionsNode (io.confluent.ksql.test.model.PostConditionsNode)2 PostConditions (io.confluent.ksql.test.tools.conditions.PostConditions)2 File (java.io.File)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 Matcher (org.hamcrest.Matcher)2