-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from lensesio/debug/smt-print-properties-speci…
…fic-error SMT should print properties on specific error to assist debugging
- Loading branch information
Showing
6 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
target/ | ||
.idea/ | ||
kafka-connect-smt.iml |
45 changes: 45 additions & 0 deletions
45
src/main/java/io/lenses/connect/smt/header/PropsFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. You may obtain a | ||
* copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable | ||
* law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | ||
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | ||
* for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package io.lenses.connect.smt.header; | ||
|
||
import org.apache.kafka.connect.transforms.util.SimpleConfig; | ||
|
||
/** | ||
* This class is responsible for formatting properties from a SimpleConfig object. | ||
* It converts the properties into a string representation in a json-like format. | ||
*/ | ||
public class PropsFormatter { | ||
|
||
private final SimpleConfig simpleConfig; | ||
|
||
/** | ||
* Constructs a new PropsFormatter with the given SimpleConfig. | ||
* | ||
* @param simpleConfig the SimpleConfig object containing the properties to be formatted | ||
*/ | ||
public PropsFormatter(SimpleConfig simpleConfig) { | ||
this.simpleConfig = simpleConfig; | ||
} | ||
|
||
/** | ||
* Formats the properties from the SimpleConfig object into a string. | ||
* The properties are represented as key-value pairs in the format: "key: "value"". | ||
* All properties are enclosed in curly braces. | ||
* | ||
* @return a string representation of the properties | ||
*/ | ||
public String apply() { | ||
StringBuilder sb = new StringBuilder("{"); | ||
simpleConfig.originalsStrings().forEach((k, v) -> sb.append(k).append(": \"").append(v).append("\", ")); | ||
sb.delete(sb.length() - 2, sb.length()); | ||
return sb.append("}").toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/io/lenses/connect/smt/header/PropsFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. You may obtain a | ||
* copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable | ||
* law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | ||
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | ||
* for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package io.lenses.connect.smt.header; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.transforms.util.SimpleConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PropsFormatterTest { | ||
|
||
@Test | ||
void singleEntry() { | ||
Map<String, Object> props = Map.of("something", "else"); | ||
PropsFormatter writer = new PropsFormatter(new SimpleConfig(new ConfigDef(), props)); | ||
assertEquals("{something: \"else\"}", writer.apply()); | ||
} | ||
|
||
@Test | ||
void multipleEntries() { | ||
Map<String, Object> props = Map.of("first", "item", "something", "else"); | ||
PropsFormatter writer = new PropsFormatter(new SimpleConfig(new ConfigDef(), props)); | ||
assertEquals("{first: \"item\", something: \"else\"}", writer.apply()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/io/lenses/connect/smt/header/UtilsTimestampTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. You may obtain a | ||
* copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable | ||
* law or agreed to in writing, software distributed under the License is distributed on an "AS IS" | ||
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License | ||
* for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package io.lenses.connect.smt.header; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.connect.errors.DataException; | ||
import org.apache.kafka.connect.transforms.util.SimpleConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.ZoneId; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class UtilsTimestampTest { | ||
|
||
public static final String TIMESTAMP = "2024-08-16T04:30:00.232Z"; | ||
public static final String PRECISION = "milliseconds"; | ||
|
||
@Test | ||
void convertToTimestampShouldWritePropsOnFailure() { | ||
PropsFormatter propsFormatter = new PropsFormatter(new SimpleConfig(new ConfigDef(), Map.of("some", "props", "for", "2" ) )); | ||
DataException dataException = assertThrows(DataException.class, () -> Utils.convertToTimestamp( | ||
TIMESTAMP, | ||
PRECISION, | ||
Optional.empty(), | ||
ZoneId.of("UTC"), | ||
Optional.of(propsFormatter) | ||
)); | ||
assertEquals("Expected a long, but found 2024-08-16T04:30:00.232Z. Props: {some: \"props\", for: \"2\"}",dataException.getMessage()); | ||
} | ||
|
||
@Test | ||
void convertToTimestampShouldNotFailWhenNoPropsFormatter() { | ||
DataException dataException = assertThrows(DataException.class, () -> Utils.convertToTimestamp( | ||
TIMESTAMP, | ||
PRECISION, | ||
Optional.empty(), | ||
ZoneId.of("UTC") | ||
)); | ||
assertEquals("Expected a long, but found 2024-08-16T04:30:00.232Z. Props: (No props formatter)",dataException.getMessage()); | ||
} | ||
|
||
} |