Skip to content

Commit

Permalink
Added unit tests for Protobuf and JSonSchema classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hifly81 committed Sep 26, 2024
1 parent 5316926 commit 8715209
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ JR Source Connector can be configured with:
- _**template**_: A valid JR existing template name. For a list of available templates see: https://jrnd.io/docs/#listing-existing-templates
- _**embedded_template**_: Location of a file containing a valid custom JR template. This property will take precedence over _template_. File must exist on Kafka Connect Worker nodes.
- _**topic**_: target topic
- _**frequency**_: Repeat the creation of a random object every X milliseconds.
- _**frequency**_: Repeat the creation of a random object every 'frequency' milliseconds.
- _**objects**_: Number of objects to create at every run. Default is 1.
- _**key_field_name**_: Name for key field, for example 'ID'. This is an _OPTIONAL_ config, if not set, objects will be created without a key. Value for key will be calculated using JR function _key_, https://jrnd.io/docs/functions/#key
- _**key_value_interval_max**_: Maximum interval value for key value, for example 150 (0 to key_value_interval_max). Default is 100.
Expand All @@ -75,7 +75,7 @@ JR Source Connector can be configured with:
- _**value.converter.schema.registry.url**_: Only if _value.converter_ is set to _io.confluent.connect.avro.AvroConverter_, _io.confluent.connect.json.JsonSchemaConverter_ or _io.confluent.connect.protobuf.ProtobufConverter_. URL for Confluent Schema Registry.

> [!NOTE]
> At the moment for keys the supported format is _String_.
> At the moment for keys (_key.converter_) the supported format is _org.apache.kafka.connect.storage.StringConverter_.
For values there is also support for _Confluent Schema Registry_ with _Avro, Json and Protobuf schemas_.

## Examples
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package io.jrnd.kafka.connect.format.jsonschema;

import io.jrnd.kafka.connect.connector.format.jsonschema.JsonSchemaHelper;
import org.apache.kafka.connect.data.Schema;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.*;

public class JsonSchemaHelperTest {

@Test
public void testCreateJsonSchemaFromJson_simpleTypes() throws Exception {
// Test a simple JSON with various data types
String jsonDocument = """
{
"name": "John Doe",
"age": 30,
"isEmployee": true,
"salary": 55000.50
}
""";

Schema schema = JsonSchemaHelper.createJsonSchemaFromJson(jsonDocument);

assertNotNull(schema);
assertEquals(Schema.Type.STRUCT, schema.type());
assertEquals(4, schema.fields().size());

assertEquals(Schema.Type.STRING, schema.field("name").schema().type());
assertEquals(Schema.Type.FLOAT64, schema.field("salary").schema().type());
assertEquals(Schema.Type.BOOLEAN, schema.field("isEmployee").schema().type());
assertEquals(Schema.Type.FLOAT64, schema.field("age").schema().type());
}

@Test
public void testCreateJsonSchemaFromJson_nestedObject() throws Exception {

String jsonDocument = """
{
"name": "Jane Doe",
"address": {
"street": "Main St",
"city": "Metropolis",
"zipcode": 12345
}
}
""";

Schema schema = JsonSchemaHelper.createJsonSchemaFromJson(jsonDocument);

assertNotNull(schema);
assertEquals(Schema.Type.STRUCT, schema.type());

assertEquals(Schema.Type.STRING, schema.field("name").schema().type());

Schema addressSchema = schema.field("address").schema();
assertNotNull(addressSchema);
assertEquals(Schema.Type.STRUCT, addressSchema.type());

assertEquals(Schema.Type.STRING, addressSchema.field("street").schema().type());
assertEquals(Schema.Type.STRING, addressSchema.field("city").schema().type());
assertEquals(Schema.Type.FLOAT64, addressSchema.field("zipcode").schema().type());
}

@Test
public void testCreateJsonSchemaFromJson_array() throws Exception {

String jsonDocument = """
{
"name": "Jane Doe",
"skills": ["Java", "Kafka", "Docker"]
}
""";

Schema schema = JsonSchemaHelper.createJsonSchemaFromJson(jsonDocument);

assertNotNull(schema);
assertEquals(Schema.Type.STRUCT, schema.type());

Schema skillsSchema = schema.field("skills").schema();
assertNotNull(skillsSchema);
assertEquals(Schema.Type.ARRAY, skillsSchema.type());

Schema elementType = skillsSchema.valueSchema();
assertEquals(Schema.Type.STRING, elementType.type());
}

@Test
public void testCreateJsonSchemaFromJson_invalidJson() {

String jsonDocument = """
{
"name": "John Doe",
"age":
}
""";

assertThrows(IOException.class, () -> {
JsonSchemaHelper.createJsonSchemaFromJson(jsonDocument);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package io.jrnd.kafka.connect.format.protobuf;

import io.jrnd.kafka.connect.connector.format.protobuf.ProtobufHelper;
import org.apache.kafka.connect.data.Schema;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ProtobufHelperTest {

@Test
public void testCreateProtobufSchemaFromJson_simpleFields() throws Exception {
String jsonString = "{ \"name\": \"John\", \"age\": 30, \"isEmployee\": true }";
String messageName = "Person";

Schema schema = ProtobufHelper.createProtobufSchemaFromJson(messageName, jsonString);

assertNotNull(schema);
assertEquals("Person", schema.name());
assertEquals(3, schema.fields().size());

assertEquals(Schema.Type.STRING, schema.field("name").schema().type());
assertEquals(Schema.Type.INT32, schema.field("age").schema().type());
assertEquals(Schema.Type.BOOLEAN, schema.field("isEmployee").schema().type());
}

@Test
public void testCreateProtobufSchemaFromJson_invalidJson() {

String invalidJsonString = "{ \"name\": \"John\", \"age\": }";
String messageName = "Person";

assertThrows(Exception.class, () -> {
ProtobufHelper.createProtobufSchemaFromJson(messageName, invalidJsonString);
});
}

}

0 comments on commit 8715209

Please sign in to comment.