Search in sources :

Example 1 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class BaseDocumentationTest method sources.

@TestFactory
public Stream<DynamicTest> sources() {
    final File parentDirectory = new File(outputDirectory, "sources");
    final String templateName = "rst/source.rst.ftl";
    return this.pluginTemplate.getSourceConnectors().stream().map(connectorTemplate -> connectorRstTest(connectorTemplate, templateName, parentDirectory));
}
Also used : File(java.io.File) TestFactory(org.junit.jupiter.api.TestFactory)

Example 2 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class BaseDocumentationTest method schema.

@TestFactory
public Stream<DynamicTest> schema() throws IOException {
    final File parentDirectory = new File(outputDirectory, "schemas");
    if (!parentDirectory.exists()) {
        parentDirectory.mkdirs();
    }
    List<Schema> schemas = schemas();
    if (null != schemas && !schemas.isEmpty()) {
        final File schemaRstPath = new File(outputDirectory, "schemas.rst");
        final String schemaRst = "=======\n" + "Schemas\n" + "=======\n" + "\n" + ".. toctree::\n" + "    :maxdepth: 0\n" + "    :caption: Schemas:\n" + "    :glob:\n" + "\n" + "    schemas/*";
        Files.write(schemaRst, schemaRstPath, Charsets.UTF_8);
    }
    final String templateName = "rst/schema.rst.ftl";
    return this.schemas().stream().filter(schema -> !Strings.isNullOrEmpty(schema.name())).map(schema -> dynamicTest(String.format("%s.%s", schema.type(), schema.name()), () -> {
        StringBuilder filenameBuilder = new StringBuilder().append(schema.type().toString().toLowerCase());
        if (!Strings.isNullOrEmpty(schema.name())) {
            filenameBuilder.append('.').append(schema.name());
        }
        filenameBuilder.append(".rst");
        File outputFile = new File(parentDirectory, filenameBuilder.toString());
        Template template = configuration.getTemplate(templateName);
        log.info("Writing {}", outputFile);
        try (Writer writer = Files.newWriter(outputFile, Charsets.UTF_8)) {
            Map<String, Object> variables = ImmutableMap.of("input", TemplateSchema.of(schema), "helper", new RstTemplateHelper());
            template.process(variables, writer);
        }
    }));
}
Also used : Graphviz(guru.nidi.graphviz.engine.Graphviz) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BeansWrapper(freemarker.ext.beans.BeansWrapper) TestFactory(org.junit.jupiter.api.TestFactory) Factory.node(guru.nidi.graphviz.model.Factory.node) LoggerFactory(org.slf4j.LoggerFactory) ConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.ConnectorTemplate) Reflections(org.reflections.Reflections) Schema(org.apache.kafka.connect.data.Schema) Transformation(org.apache.kafka.connect.transforms.Transformation) SourceConnector(org.apache.kafka.connect.source.SourceConnector) BeforeAll(org.junit.jupiter.api.BeforeAll) Factory.to(guru.nidi.graphviz.model.Factory.to) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Map(java.util.Map) Graph(guru.nidi.graphviz.model.Graph) ConfigDef(org.apache.kafka.common.config.ConfigDef) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) ClasspathHelper(org.reflections.util.ClasspathHelper) Test(org.junit.jupiter.api.Test) List(java.util.List) Factory.graph(guru.nidi.graphviz.model.Factory.graph) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Writer(java.io.Writer) MarkdownTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.markdown.MarkdownTemplateHelper) TemplateSchema(com.github.jcustenborder.kafka.connect.utils.templates.TemplateSchema) TemplateException(freemarker.template.TemplateException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) Format(guru.nidi.graphviz.engine.Format) Files(com.google.common.io.Files) Template(freemarker.template.Template) Shape(guru.nidi.graphviz.attribute.Shape) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) TransformationTemplate(com.github.jcustenborder.kafka.connect.utils.templates.TransformationTemplate) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) RankDir(guru.nidi.graphviz.attribute.RankDir) StringWriter(java.io.StringWriter) IOException(java.io.IOException) File(java.io.File) Label(guru.nidi.graphviz.model.Label) Configuration(freemarker.template.Configuration) PluginTemplate(com.github.jcustenborder.kafka.connect.utils.templates.PluginTemplate) SourceConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.SourceConnectorTemplate) DynamicTest(org.junit.jupiter.api.DynamicTest) RstTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.rst.RstTemplateHelper) Comparator(java.util.Comparator) RstTemplateHelper(com.github.jcustenborder.kafka.connect.utils.templates.rst.RstTemplateHelper) Schema(org.apache.kafka.connect.data.Schema) TemplateSchema(com.github.jcustenborder.kafka.connect.utils.templates.TemplateSchema) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Writer(java.io.Writer) StringWriter(java.io.StringWriter) ConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.ConnectorTemplate) Template(freemarker.template.Template) TransformationTemplate(com.github.jcustenborder.kafka.connect.utils.templates.TransformationTemplate) PluginTemplate(com.github.jcustenborder.kafka.connect.utils.templates.PluginTemplate) SourceConnectorTemplate(com.github.jcustenborder.kafka.connect.utils.templates.SourceConnectorTemplate) TestFactory(org.junit.jupiter.api.TestFactory)

Example 3 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class JsonNodeTest method parseJsonNode.

@TestFactory
public Stream<DynamicTest> parseJsonNode() {
    List<TestCase> tests = new ArrayList<>();
    of(tests, Boolean.class, Schema.BOOLEAN_SCHEMA, Boolean.TRUE);
    of(tests, Boolean.class, Schema.BOOLEAN_SCHEMA, Boolean.FALSE);
    List<Float> floats = new ArrayList<>();
    floats.add(Float.MAX_VALUE);
    floats.add(Float.MIN_VALUE);
    for (int i = 0; i < 30; i++) {
        floats.add(this.random.nextFloat());
    }
    of(tests, Float.class, Schema.FLOAT32_SCHEMA, floats);
    List<Double> doubles = new ArrayList<>();
    doubles.add(Double.MAX_VALUE);
    doubles.add(Double.MIN_VALUE);
    for (int i = 0; i < 30; i++) {
        doubles.add(this.random.nextDouble());
    }
    of(tests, Double.class, Schema.FLOAT64_SCHEMA, doubles);
    List<Byte> bytes = new ArrayList<>();
    bytes.add(Byte.MAX_VALUE);
    bytes.add(Byte.MIN_VALUE);
    byte[] buffer = new byte[30];
    this.random.nextBytes(buffer);
    for (Byte b : buffer) {
        bytes.add(b);
    }
    of(tests, Byte.class, Schema.INT8_SCHEMA, bytes);
    List<Short> shorts = new ArrayList<>();
    shorts.add(Short.MAX_VALUE);
    shorts.add(Short.MIN_VALUE);
    for (int i = 0; i < 30; i++) {
        shorts.add((short) this.random.nextInt(Short.MAX_VALUE));
    }
    of(tests, Short.class, Schema.INT16_SCHEMA, shorts);
    List<Integer> ints = new ArrayList<>();
    ints.add(Integer.MAX_VALUE);
    ints.add(Integer.MIN_VALUE);
    for (int i = 0; i < 30; i++) {
        ints.add(this.random.nextInt());
    }
    of(tests, Integer.class, Schema.INT32_SCHEMA, ints);
    List<Long> longs = new ArrayList<>();
    longs.add(Long.MAX_VALUE);
    longs.add(Long.MIN_VALUE);
    for (int i = 0; i < 30; i++) {
        longs.add(this.random.nextLong());
    }
    of(tests, Long.class, Schema.INT64_SCHEMA, longs);
    // String
    of(tests, String.class, Schema.STRING_SCHEMA, "");
    of(tests, String.class, Schema.STRING_SCHEMA, "This is a string");
    // Decimal
    for (int SCALE = 3; SCALE < 30; SCALE++) {
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("12345").setScale(SCALE));
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("0").setScale(SCALE));
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("-12345.001").setScale(SCALE));
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("12345").setScale(SCALE).toString(), new BigDecimal("12345").setScale(SCALE));
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("0").setScale(SCALE).toString(), new BigDecimal("0").setScale(SCALE));
        of(tests, BigDecimal.class, Decimal.schema(SCALE), new BigDecimal("-12345.001").setScale(SCALE).toString(), new BigDecimal("-12345.001").setScale(SCALE));
    }
    // Timestamp
    java.util.Date expectedDate = new java.util.Date(1494855736000L);
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
    of(tests, java.util.Date.class, Timestamp.SCHEMA, expectedDate);
    of(tests, java.util.Date.class, Timestamp.SCHEMA, inputFormat.format(expectedDate), expectedDate);
    of(tests, java.util.Date.class, Timestamp.SCHEMA, expectedDate.getTime(), expectedDate);
    // Date
    expectedDate = new java.util.Date(994204800000L);
    inputFormat = new SimpleDateFormat("yyyy-MM-dd");
    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    of(tests, java.util.Date.class, Date.SCHEMA, expectedDate);
    of(tests, java.util.Date.class, Date.SCHEMA, inputFormat.format(expectedDate), expectedDate);
    of(tests, java.util.Date.class, Date.SCHEMA, expectedDate.getTime(), expectedDate);
    // Time
    expectedDate = new java.util.Date(65336000L);
    inputFormat = new SimpleDateFormat("HH:mm:ss");
    of(tests, java.util.Date.class, Time.SCHEMA, new java.util.Date(65336000L));
    of(tests, java.util.Date.class, Time.SCHEMA, inputFormat.format(expectedDate), expectedDate);
    of(tests, java.util.Date.class, Time.SCHEMA, expectedDate.getTime(), expectedDate);
    return tests.stream().map(testCase -> dynamicTest(testCase.toString(), () -> {
        parseJsonNode(testCase);
    }));
}
Also used : ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Date(org.apache.kafka.connect.data.Date) SimpleDateFormat(java.text.SimpleDateFormat) TestFactory(org.junit.jupiter.api.TestFactory)

Example 4 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class StringParserTest method parseString.

@TestFactory
Stream<DynamicTest> parseString() {
    List<TestCase> tests = new ArrayList<>();
    of(tests, Schema.FLOAT64_SCHEMA, new Double(Double.MAX_VALUE).toString(), new Double(Double.MAX_VALUE));
    of(tests, Schema.FLOAT64_SCHEMA, new Double(Double.MIN_VALUE).toString(), new Double(Double.MIN_VALUE));
    of(tests, Schema.INT8_SCHEMA, new Byte(Byte.MAX_VALUE).toString(), new Byte(Byte.MAX_VALUE));
    of(tests, Schema.INT8_SCHEMA, new Byte(Byte.MIN_VALUE).toString(), new Byte(Byte.MIN_VALUE));
    of(tests, Schema.INT16_SCHEMA, new Short(Short.MAX_VALUE).toString(), new Short(Short.MAX_VALUE));
    of(tests, Schema.INT16_SCHEMA, new Short(Short.MIN_VALUE).toString(), new Short(Short.MIN_VALUE));
    of(tests, Schema.INT32_SCHEMA, new Integer(Integer.MAX_VALUE).toString(), new Integer(Integer.MAX_VALUE));
    of(tests, Schema.INT32_SCHEMA, new Integer(Integer.MIN_VALUE).toString(), new Integer(Integer.MIN_VALUE));
    of(tests, Schema.INT64_SCHEMA, new Long(Long.MAX_VALUE).toString(), new Long(Long.MAX_VALUE));
    of(tests, Schema.INT64_SCHEMA, new Long(Long.MIN_VALUE).toString(), new Long(Long.MIN_VALUE));
    of(tests, Schema.STRING_SCHEMA, "", "");
    of(tests, Schema.STRING_SCHEMA, "mirror", "mirror");
    for (int SCALE = 3; SCALE < 30; SCALE++) {
        Schema schema = Decimal.schema(SCALE);
        of(tests, schema, "12345", new BigDecimal("12345").setScale(SCALE));
        of(tests, schema, "0", new BigDecimal("0").setScale(SCALE));
        of(tests, schema, "-12345.001", new BigDecimal("-12345.001").setScale(SCALE));
    }
    return tests.stream().map(testCase -> dynamicTest(testCase.toString(), () -> {
        final Object actual = parser.parseString(testCase.schema, testCase.input);
        assertEquals(testCase.expected, actual);
    }));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) TestFactory(org.junit.jupiter.api.TestFactory)

Example 5 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class StructSerializationModuleTest method roundtrip.

@TestFactory
public Stream<DynamicTest> roundtrip() {
    Schema innerSchema = SchemaBuilder.struct().name("InnerSchema").optional().field("latitude", Schema.FLOAT32_SCHEMA).field("longitude", Schema.FLOAT32_SCHEMA).build();
    Map<String, Struct> cityMap = ImmutableMap.of("Austin", new Struct(innerSchema).put("latitude", 30.2672F).put("longitude", 97.7431F), "San Francisco", new Struct(innerSchema).put("latitude", 37.7749F).put("longitude", 122.4194F));
    List<Struct> cityList = ImmutableList.copyOf(cityMap.values());
    List<Struct> testCases = Arrays.asList(new Struct(SchemaBuilder.struct().name("Testing").field("firstName", Schema.STRING_SCHEMA).build()).put("firstName", "Example"), new Struct(SchemaBuilder.struct().name("Nullable").field("firstName", Schema.OPTIONAL_STRING_SCHEMA).build()).put("firstName", null), new Struct(SchemaBuilder.struct().name("NestedOptionalStructWithValue").field("firstName", Schema.OPTIONAL_STRING_SCHEMA).field("inner", innerSchema).build()).put("inner", new Struct(innerSchema).put("latitude", 30.2672F).put("longitude", 97.7431F)), new Struct(SchemaBuilder.struct().name("NestedOptionalStructWithoutValue").field("firstName", Schema.OPTIONAL_STRING_SCHEMA).field("inner", innerSchema).build()).put("inner", null), new Struct(SchemaBuilder.struct().name("NestedMapStruct").field("firstName", Schema.OPTIONAL_STRING_SCHEMA).field("inner", SchemaBuilder.map(Schema.STRING_SCHEMA, innerSchema)).build()).put("inner", cityMap), new Struct(SchemaBuilder.struct().name("NestedArrayStruct").field("firstName", Schema.OPTIONAL_STRING_SCHEMA).field("inner", SchemaBuilder.array(innerSchema)).build()).put("inner", cityList));
    return testCases.stream().map(expected -> dynamicTest(expected.schema().name(), () -> {
        String input = ObjectMapperFactory.INSTANCE.writeValueAsString(expected);
        log.trace(input);
        Struct actual = ObjectMapperFactory.INSTANCE.readValue(input, Struct.class);
        assertStruct(expected, actual);
    }));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) AssertStruct.assertStruct(com.github.jcustenborder.kafka.connect.utils.AssertStruct.assertStruct) Struct(org.apache.kafka.connect.data.Struct) TestFactory(org.junit.jupiter.api.TestFactory)

Aggregations

TestFactory (org.junit.jupiter.api.TestFactory)19 ArrayList (java.util.ArrayList)9 DynamicTest (org.junit.jupiter.api.DynamicTest)8 File (java.io.File)5 List (java.util.List)5 Arrays (java.util.Arrays)4 Stream (java.util.stream.Stream)4 Schema (org.apache.kafka.connect.data.Schema)4 BigDecimal (java.math.BigDecimal)3 Collection (java.util.Collection)3 Function (java.util.function.Function)3 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)3 ThrowingConsumer (org.junit.jupiter.api.function.ThrowingConsumer)3 Map (java.util.Map)2 Struct (org.apache.kafka.connect.data.Struct)2 Test (org.junit.jupiter.api.Test)2 Employee (com.baeldung.helpers.Employee)1 EmployeeDao (com.baeldung.helpers.EmployeeDao)1 ChangeKey (com.github.jcustenborder.kafka.connect.cdc.ChangeKey)1 AssertStruct.assertStruct (com.github.jcustenborder.kafka.connect.utils.AssertStruct.assertStruct)1