Skip to content

Commit

Permalink
Fix checkstyle, Apply code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismail Simsek committed Jan 9, 2025
1 parent da505c0 commit acac80a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class JsonToMapTransform<R extends ConnectRecord<R>> implements Transform

public static final String JSON_LEVEL = "json.root";

private static final ObjectReader mapper = new ObjectMapper().reader();
private static final ObjectReader MAPPER = new ObjectMapper().reader();

private boolean startAtRoot = false;

Expand Down Expand Up @@ -77,7 +77,7 @@ private R process(R record) {
JsonNode obj;

try {
obj = mapper.readTree(json);
obj = MAPPER.readTree(json);
} catch (Exception e) {
throw new JsonToMapException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public static ArrayEncoding parse(String value) {
if (value == null) {
return null;
}
value = value.trim();
for (ArrayEncoding option : ArrayEncoding.values()) {
if (option.getValue().equalsIgnoreCase(value)) {
if (option.getValue().equalsIgnoreCase(value.trim())) {
return option;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package org.debezium.connector.mongodb.transforms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.kafka.connect.data.Field;
import org.apache.kafka.connect.data.Schema;
import org.apache.kafka.connect.data.SchemaBuilder;
Expand Down Expand Up @@ -156,7 +156,7 @@ public void convertFieldValue(
if (keyvalueforStruct.getValue().asArray().isEmpty()) {
switch (arrayEncoding) {
case ARRAY:
colValue = new ArrayList<>();
colValue = Lists.newArrayList();
break;
case DOCUMENT:
final Schema fieldSchema = schema.field(key).schema();
Expand All @@ -168,7 +168,7 @@ public void convertFieldValue(
case ARRAY:
BsonType valueType = keyvalueforStruct.getValue().asArray().get(0).getBsonType();
List<BsonValue> arrValues = keyvalueforStruct.getValue().asArray().getValues();
ArrayList<Object> list = new ArrayList<>();
List<Object> list = Lists.newArrayList();

arrValues.forEach(
arrValue -> {
Expand All @@ -184,7 +184,7 @@ public void convertFieldValue(
break;
case DOCUMENT:
final BsonArray array = keyvalueforStruct.getValue().asArray();
final Map<String, BsonValue> convertedArray = new HashMap<>();
final Map<String, BsonValue> convertedArray = Maps.newHashMap();
final Schema arraySchema = schema.field(key).schema();
final Struct arrayStruct = new Struct(arraySchema);
for (int i = 0; i < array.size(); i++) {
Expand All @@ -209,8 +209,10 @@ public void convertFieldValue(
struct.put(key, keyvalueforStruct.getValue().isNull() ? null : colValue);
}

// TODO FIX Cyclomatic Complexity is 30 (max allowed is 12). [CyclomaticComplexity]
@SuppressWarnings("checkstyle:CyclomaticComplexity")
private void convertFieldValue(
Schema valueSchema, BsonType valueType, BsonValue arrValue, ArrayList<Object> list) {
Schema valueSchema, BsonType valueType, BsonValue arrValue, List<Object> list) {
if (arrValue.getBsonType() == BsonType.STRING && valueType == BsonType.STRING) {
String temp = arrValue.asString().getValue();
list.add(temp);
Expand Down Expand Up @@ -251,7 +253,7 @@ private void convertFieldValue(
}
list.add(struct1);
} else if (arrValue.getBsonType() == BsonType.ARRAY && valueType == BsonType.ARRAY) {
ArrayList<Object> subList = new ArrayList<>();
List<Object> subList = Lists.newArrayList();
final Schema subValueSchema;
if (Arrays.asList(BsonType.ARRAY, BsonType.DOCUMENT)
.contains(arrValue.asArray().get(0).getBsonType())) {
Expand All @@ -266,8 +268,8 @@ private void convertFieldValue(
}
}

protected String arrayElementStructName(int i) {
return "_" + i;
protected String arrayElementStructName(int index) {
return "_" + index;
}

public void addFieldSchema(Entry<String, BsonValue> keyValuesforSchema, SchemaBuilder builder) {
Expand Down Expand Up @@ -370,7 +372,7 @@ public void addFieldSchema(Entry<String, BsonValue> keyValuesforSchema, SchemaBu
final BsonArray array = keyValuesforSchema.getValue().asArray();
final SchemaBuilder arrayStructBuilder =
SchemaBuilder.struct().name(builder.name() + "." + key).optional();
final Map<String, BsonValue> convertedArray = new HashMap<>();
final Map<String, BsonValue> convertedArray = Maps.newHashMap();
for (int i = 0; i < array.size(); i++) {
convertedArray.put(arrayElementStructName(i), array.get(i));
}
Expand Down Expand Up @@ -409,12 +411,12 @@ private Schema subSchema(SchemaBuilder builder, String key, BsonType valueType,
case DOCUMENT:
final SchemaBuilder documentSchemaBuilder =
SchemaBuilder.struct().name(builder.name() + "." + key).optional();
final Map<String, BsonType> union = new HashMap<>();
final Map<String, BsonType> union = Maps.newHashMap();
if (value.isArray()) {
value
.asArray()
.forEach(f -> subSchema(documentSchemaBuilder, union, f.asDocument(), true));
if (documentSchemaBuilder.fields().size() == 0) {
if (documentSchemaBuilder.fields().isEmpty()) {
value
.asArray()
.forEach(f -> subSchema(documentSchemaBuilder, union, f.asDocument(), false));
Expand Down Expand Up @@ -443,9 +445,9 @@ private void subSchema(
final String key = arrayDoc.getKey();
if (emptyChecker
&& ((arrayDoc.getValue() instanceof BsonDocument
&& ((BsonDocument) arrayDoc.getValue()).size() == 0)
&& ((BsonDocument) arrayDoc.getValue()).isEmpty())
|| (arrayDoc.getValue() instanceof BsonArray
&& ((BsonArray) arrayDoc.getValue()).size() == 0))) {
&& ((BsonArray) arrayDoc.getValue()).isEmpty()))) {
continue;
}
final BsonType prevType = union.putIfAbsent(key, arrayDoc.getValue().getBsonType());
Expand All @@ -459,7 +461,7 @@ private void subSchema(

private void testType(SchemaBuilder builder, String key, BsonValue value, BsonType valueType) {
if (valueType == BsonType.DOCUMENT) {
final Map<String, BsonType> union = new HashMap<>();
final Map<String, BsonType> union = Maps.newHashMap();
for (BsonValue element : value.asArray()) {
final BsonDocument arrayDocs = element.asDocument();
for (Entry<String, BsonValue> arrayDoc : arrayDocs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
package org.apache.iceberg.connect.transforms;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -85,7 +84,7 @@ public void shouldThrowExceptionNonJsonObjects() {
offset,
timestamp,
TimestampType.CREATE_TIME);
assertThrows(JsonToMapException.class, () -> smt.apply(record));
assertThatThrownBy(() -> smt.apply(record)).isInstanceOf(JsonToMapException.class);
}
}

Expand All @@ -104,7 +103,7 @@ public void shouldThrowExceptionInvalidJson() {
offset,
timestamp,
TimestampType.CREATE_TIME);
assertThrows(JsonToMapException.class, () -> smt.apply(record));
assertThatThrownBy(() -> smt.apply(record)).isInstanceOf(JsonToMapException.class);
}
}

Expand Down Expand Up @@ -132,7 +131,7 @@ public void singleValueOnRootNode() {
"payload",
SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA).optional().build())
.build();
assertInstanceOf(Struct.class, result.value());
assertThat(result.value()).isInstanceOf(Struct.class);
Struct resultStruct = (Struct) result.value();

Map<String, String> expectedValue = Maps.newHashMap();
Expand All @@ -159,7 +158,7 @@ public void structOnRootNode() {
timestamp,
TimestampType.CREATE_TIME);
SinkRecord result = smt.apply(record);
assertInstanceOf(Struct.class, result.value());
assertThat(result.value()).isInstanceOf(Struct.class);
Struct resultStruct = (Struct) result.value();
assertThat(resultStruct.schema().fields().size()).isEqualTo(19);
assertThat(resultStruct.get("string")).isEqualTo("string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
package org.apache.iceberg.connect.transforms;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -95,23 +93,23 @@ public void primitiveBasedOnSchemaHappyPath() {
.isEqualTo(3.0);
assertThat(JsonToMapUtils.extractValue(doubleNode, Schema.Type.FLOAT64, "")).isEqualTo(0.3);
byte[] byteResult = (byte[]) JsonToMapUtils.extractValue(bytesNode, Schema.Type.BYTES, "");
assertArrayEquals(byteResult, Base64.getDecoder().decode("SGVsbG8="));
assertThat(byteResult).isEqualTo(Base64.getDecoder().decode("SGVsbG8="));
}

@Test
@DisplayName("extractValue converts complex nodes to strings if schema is string")
public void exactStringsFromComplexNodes() {
JsonNode arrayObjects = objNode.get("array_objects");
assertInstanceOf(ArrayNode.class, arrayObjects);
assertThat(arrayObjects).isInstanceOf(ArrayNode.class);

JsonNode nestedObjNode = objNode.get("nested_obj");
assertInstanceOf(ObjectNode.class, nestedObjNode);
assertThat(nestedObjNode).isInstanceOf(ObjectNode.class);

JsonNode arrayDifferentTypes = objNode.get("array_different_types");
assertInstanceOf(ArrayNode.class, arrayDifferentTypes);
assertThat(arrayDifferentTypes).isInstanceOf(ArrayNode.class);

JsonNode bigInt = objNode.get("bigInt");
assertInstanceOf(BigIntegerNode.class, bigInt);
assertThat(bigInt).isInstanceOf(BigIntegerNode.class);

assertThat(JsonToMapUtils.extractValue(arrayObjects, Schema.Type.STRING, ""))
.isEqualTo("[{\"key\":1}]");
Expand All @@ -124,9 +122,9 @@ public void exactStringsFromComplexNodes() {
@Test
@DisplayName("extractSimpleValue throws for non-primitive schema types")
public void primitiveBasedOnSchemaThrows() {
assertThrows(
RuntimeException.class,
() -> JsonToMapUtils.extractValue(objNode.get("string"), Schema.Type.STRUCT, ""));
assertThatThrownBy(
() -> JsonToMapUtils.extractValue(objNode.get("string"), Schema.Type.STRUCT, ""))
.isInstanceOf(RuntimeException.class);
}

@Test
Expand Down Expand Up @@ -158,7 +156,7 @@ public void schemaFromNodeNullOnNullNodes() {
@DisplayName("schemaFromNode returns null for empty ObjectNodes")
public void schemaFromNodeNullEmptyObjectNodes() {
JsonNode node = objNode.get("empty_obj");
assertInstanceOf(ObjectNode.class, node);
assertThat(node).isInstanceOf(ObjectNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node)).isNull();
}

Expand All @@ -167,7 +165,7 @@ public void schemaFromNodeNullEmptyObjectNodes() {
"schemaFromNode returns bytes with logical name decimal with scale 0 for BigInteger nodes")
public void schemaFromNodeStringForBigInteger() {
JsonNode node = objNode.get("bigInt");
assertInstanceOf(BigIntegerNode.class, node);
assertThat(node).isInstanceOf(BigIntegerNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node)).isEqualTo(JsonToMapUtils.decimalSchema(0));
}

Expand All @@ -176,8 +174,8 @@ public void schemaFromNodeStringForBigInteger() {
public void schemaFromNodePrimitiveSchemasFromPrimitiveNodes() {
JsonNode intNode = objNode.get("int");
JsonNode doubleNode = objNode.get("double");
assertInstanceOf(IntNode.class, intNode);
assertInstanceOf(DoubleNode.class, doubleNode);
assertThat(intNode).isInstanceOf(IntNode.class);
assertThat(doubleNode).isInstanceOf(DoubleNode.class);
assertThat(JsonToMapUtils.schemaFromNode(intNode)).isEqualTo(Schema.OPTIONAL_INT32_SCHEMA);
assertThat(JsonToMapUtils.schemaFromNode(doubleNode)).isEqualTo(Schema.OPTIONAL_FLOAT64_SCHEMA);
}
Expand All @@ -186,7 +184,7 @@ public void schemaFromNodePrimitiveSchemasFromPrimitiveNodes() {
@DisplayName("schemaFromNode returns Map<String, String> for ObjectNodes")
public void schmefromNodeObjectNodesAsMaps() {
JsonNode node = objNode.get("nested_obj");
assertInstanceOf(ObjectNode.class, node);
assertThat(node).isInstanceOf(ObjectNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node))
.isEqualTo(
SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA).optional().build());
Expand All @@ -196,7 +194,7 @@ public void schmefromNodeObjectNodesAsMaps() {
@DisplayName("schemaFromNode returns Array String schema for ArrayNodes with ObjectNode elements")
public void schemaFromNodeArrayStringFromArrayObjects() {
JsonNode arrayObjects = objNode.get("array_objects");
assertInstanceOf(ArrayNode.class, arrayObjects);
assertThat(arrayObjects).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(arrayObjects))
.isEqualTo(JsonToMapUtils.ARRAY_MAP_OPTIONAL_STRING);
}
Expand All @@ -205,7 +203,7 @@ public void schemaFromNodeArrayStringFromArrayObjects() {
@DisplayName("schemaFromNode returns Array String schema for ArrayNodes with inconsistent types")
public void schemaFromNodeArrayStringFromInconsistentArrayNodes() {
JsonNode inconsistent = objNode.get("array_different_types");
assertInstanceOf(ArrayNode.class, inconsistent);
assertThat(inconsistent).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(inconsistent))
.isEqualTo(SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build());
}
Expand All @@ -215,7 +213,7 @@ public void schemaFromNodeArrayStringFromInconsistentArrayNodes() {
"schemaFromNode returns Array[Array[String]] for ArrayNodes of ArrayNodes with inconsistent types")
public void schemaFromNodeArraysArrays() {
JsonNode node = objNode.get("array_array_inconsistent");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);

Schema expected =
SchemaBuilder.array(SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).optional().build())
Expand All @@ -230,7 +228,7 @@ public void schemaFromNodeArraysArrays() {
"schemaFromNode returns Array[Array[Map<String, String>]] for ArrayNodes of ArrayNodes of objects")
public void schemaFromNodeArrayArrayObjects() {
JsonNode node = objNode.get("array_array_objects");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
Schema expected =
SchemaBuilder.array(JsonToMapUtils.ARRAY_MAP_OPTIONAL_STRING).optional().build();
Schema result = JsonToMapUtils.schemaFromNode(node);
Expand All @@ -241,7 +239,7 @@ public void schemaFromNodeArrayArrayObjects() {
@DisplayName("schemaFromNode returns Array[Array[Int]] for ArrayNodes of ArrayNodes of IntNode")
public void schemaFromNodeArrayArrayOfArrayArrayInt() {
JsonNode node = objNode.get("array_array_int");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
Schema expected =
SchemaBuilder.array(SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).optional().build())
.optional()
Expand All @@ -254,15 +252,15 @@ public void schemaFromNodeArrayArrayOfArrayArrayInt() {
@DisplayName("schemaFromNode returns null for empty ArrayNodes")
public void schemaFromNodeNullFromEmptyArray() {
JsonNode node = objNode.get("empty_arr");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node)).isNull();
}

@Test
@DisplayName("schemaFromNode returns null for empty Array of Array nodes")
public void schemaFromNodeEmptyArrayOfEmptyArrays() {
JsonNode node = objNode.get("empty_arr_arr");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node)).isNull();
}

Expand All @@ -271,7 +269,7 @@ public void schemaFromNodeEmptyArrayOfEmptyArrays() {
"schemaFromNode returns optional map of optional <string, string> for array of empty object")
public void schemaFromNodeNullArrayEmptyObject() {
JsonNode node = objNode.get("array_empty_object");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node))
.isEqualTo(JsonToMapUtils.ARRAY_MAP_OPTIONAL_STRING);
}
Expand All @@ -281,7 +279,7 @@ public void schemaFromNodeNullArrayEmptyObject() {
"schemaFromNode returns Array[Map<String, String>] for array of objects if one object is empty")
public void schemaFromNodeMixedObjectsOneEmpty() {
JsonNode node = objNode.get("nested_object_contains_empty");
assertInstanceOf(ArrayNode.class, node);
assertThat(node).isInstanceOf(ArrayNode.class);
assertThat(JsonToMapUtils.schemaFromNode(node))
.isEqualTo(JsonToMapUtils.ARRAY_MAP_OPTIONAL_STRING);
}
Expand Down Expand Up @@ -329,9 +327,9 @@ public void addToStruct() {
assertThat(result.get("empty_string")).isEqualTo("");

// assert empty fields don't show up on the struct
assertThrows(RuntimeException.class, () -> result.get("null"));
assertThrows(RuntimeException.class, () -> result.get("empty_obj"));
assertThrows(RuntimeException.class, () -> result.get("empty_arr"));
assertThrows(RuntimeException.class, () -> result.get("empty_arr_arr"));
assertThatThrownBy(() -> result.get("null")).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> result.get("empty_obj")).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> result.get("empty_arr")).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> result.get("empty_arr_arr")).isInstanceOf(RuntimeException.class);
}
}
Loading

0 comments on commit acac80a

Please sign in to comment.