Skip to content

Commit c5e121f

Browse files
committed
fix: use of config in tests
Some of the existing tests create a JsonRecordBuilder and start using it without configuring it. This is not valid and would not happen in real use as configure() is a required step in the Connect lifecycle. As I want to introduce new config for the record builder, I need to correct this mistake first. Signed-off-by: Dale Lane <[email protected]>
1 parent 034f16c commit c5e121f

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/integration/java/com/ibm/eventstreams/connect/mqsource/AbstractJMSContextIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.dockerjava.api.model.HostConfig;
2020
import com.github.dockerjava.api.model.PortBinding;
2121
import com.github.dockerjava.api.model.Ports;
22+
import com.ibm.eventstreams.connect.mqsource.builders.DefaultRecordBuilder;
2223
import com.ibm.eventstreams.connect.mqsource.utils.MQTestUtil;
2324
import com.ibm.mq.jms.MQConnectionFactory;
2425
import com.ibm.msg.client.wmq.WMQConstants;
@@ -102,6 +103,7 @@ public static Map<String, String> getDefaultConnectorProperties() {
102103
props.put("mq.channel.name", CHANNEL_NAME);
103104
props.put("mq.queue", DEFAULT_SOURCE_QUEUE);
104105
props.put("mq.user.authentication.mqcsp", "false");
106+
props.put("mq.record.builder", DefaultRecordBuilder.class.getCanonicalName());
105107
props.put("topic", "mytopic");
106108
return props;
107109
}

src/integration/java/com/ibm/eventstreams/connect/mqsource/builders/JsonRecordBuilderIT.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void buildFromJmsTextMessage() throws Exception {
6565

6666
// use the builder to convert it to a Kafka record
6767
final JsonRecordBuilder builder = new JsonRecordBuilder();
68+
builder.configure(getDefaultConnectorProperties());
6869
final SourceRecord record = builder.toSourceRecord(getJmsContext(), topic, isJMS, message);
6970

7071
// verify the Kafka record
@@ -82,6 +83,7 @@ public void buildFromJmsBytesMessage() throws Exception {
8283

8384
// use the builder to convert it to a Kafka record
8485
final JsonRecordBuilder builder = new JsonRecordBuilder();
86+
builder.configure(getDefaultConnectorProperties());
8587
final SourceRecord record = builder.toSourceRecord(getJmsContext(), topic, isJMS, message);
8688

8789
// verify the Kafka record
@@ -100,6 +102,7 @@ public void buildFromJmsMapMessage() throws Exception {
100102

101103
// use the builder to convert it to a Kafka record
102104
final JsonRecordBuilder builder = new JsonRecordBuilder();
105+
builder.configure(getDefaultConnectorProperties());
103106
final RecordBuilderException exc = assertThrows(RecordBuilderException.class, () -> {
104107
builder.toSourceRecord(getJmsContext(), topic, isJMS, message);
105108
});
@@ -115,6 +118,7 @@ public void buildFromJmsTestJsonError() throws Exception {
115118

116119
// use the builder to convert it to a Kafka record
117120
final JsonRecordBuilder builder = new JsonRecordBuilder();
121+
builder.configure(getDefaultConnectorProperties());
118122
final DataException exec = assertThrows(DataException.class, () -> builder.toSourceRecord(getJmsContext(), topic, isJMS, message));
119123
assertEquals("Converting byte[] to Kafka Connect data failed due to serialization error: ", exec.getMessage());
120124
}
@@ -143,7 +147,7 @@ public void buildFromJmsTestErrorToleranceNone() throws Exception {
143147

144148
// use the builder to convert it to a Kafka record
145149
final JsonRecordBuilder builder = new JsonRecordBuilder();
146-
final HashMap<String, String> config = new HashMap<String, String>();
150+
final Map<String, String> config = getDefaultConnectorProperties();
147151
config.put("errors.tolerance", "none");
148152
config.put("mq.message.body.jms", "true");
149153
config.put("mq.record.builder", "com.ibm.eventstreams.connect.mqsource.builders.JsonRecordBuilder");
@@ -187,7 +191,7 @@ public void testToSourceRecord_JsonRecordBuilder_JsonMessage() throws Exception
187191
assertThat(sourceRecord).isNotNull();
188192
assertThat(sourceRecord.value()).isInstanceOf(Map.class);
189193
assertNull(sourceRecord.valueSchema()); // JSON with no schema
190-
194+
191195
// Verify JSON data
192196
@SuppressWarnings("unchecked")
193197
Map<String, Object> value = (Map<String, Object>) sourceRecord.value();

src/main/java/com/ibm/eventstreams/connect/mqsource/builders/RecordBuilderFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public static RecordBuilder getRecordBuilder(final Map<String, String> props) {
3232
);
3333
}
3434

35-
protected static RecordBuilder getRecordBuilder(final String builderClass, final Map<String, String> props) {
35+
private static RecordBuilder getRecordBuilder(final String builderClass, final Map<String, String> props) {
3636

3737
final RecordBuilder builder;
3838

3939
try {
4040
final Class<? extends RecordBuilder> c = Class.forName(builderClass).asSubclass(RecordBuilder.class);
4141
builder = c.newInstance();
4242
builder.configure(props);
43-
} catch (ClassNotFoundException | ClassCastException | IllegalAccessException | InstantiationException | NullPointerException exc) {
43+
} catch (ClassNotFoundException | ClassCastException | IllegalAccessException | InstantiationException exc) {
4444
log.error("Could not instantiate message builder {}", builderClass);
4545
throw new RecordBuilderException("Could not instantiate message builder", exc);
4646
}

src/test/java/com/ibm/eventstreams/connect/mqsource/builders/RecordBuilderFactoryTest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,42 @@
1818
import org.assertj.core.api.Assertions;
1919
import org.junit.Test;
2020

21+
import com.ibm.eventstreams.connect.mqsource.MQSourceConnector;
22+
2123
import java.util.HashMap;
2224
import java.util.Map;
2325

2426
public class RecordBuilderFactoryTest {
2527

26-
final Map<String, String> emptyProps = new HashMap<>();
28+
final Map<String, String> placeholderProps = Map.of(
29+
"mq.queue.manager", "placeholder",
30+
"mq.queue", "placeholder",
31+
"topic", "placeholder"
32+
);
2733

2834
@Test
2935
public void testGetRecordBuilder_ForJsonRecordBuilder() {
30-
RecordBuilder recordBuilder = RecordBuilderFactory.getRecordBuilder("com.ibm.eventstreams.connect.mqsource.builders.JsonRecordBuilder", emptyProps);
36+
Map<String, String> props = new HashMap<>(placeholderProps);
37+
props.put(MQSourceConnector.CONFIG_NAME_MQ_RECORD_BUILDER, "com.ibm.eventstreams.connect.mqsource.builders.JsonRecordBuilder");
38+
39+
RecordBuilder recordBuilder = RecordBuilderFactory.getRecordBuilder(props);
3140
Assertions.assertThat(recordBuilder).isInstanceOf(JsonRecordBuilder.class);
3241
}
3342

3443
@Test
3544
public void testGetRecordBuilder_ForDefaultRecordBuilder() {
36-
RecordBuilder recordBuilder = RecordBuilderFactory.getRecordBuilder("com.ibm.eventstreams.connect.mqsource.builders.DefaultRecordBuilder", emptyProps);
45+
Map<String, String> props = new HashMap<>(placeholderProps);
46+
props.put(MQSourceConnector.CONFIG_NAME_MQ_RECORD_BUILDER, "com.ibm.eventstreams.connect.mqsource.builders.DefaultRecordBuilder");
47+
48+
RecordBuilder recordBuilder = RecordBuilderFactory.getRecordBuilder(props);
3749
Assertions.assertThat(recordBuilder).isInstanceOf(DefaultRecordBuilder.class);
3850
}
3951

4052
@Test(expected = RecordBuilderException.class)
4153
public void testGetRecordBuilder_JunkClass() {
42-
RecordBuilderFactory.getRecordBuilder("casjsajhasdhusdo;iasd", emptyProps);
43-
}
54+
Map<String, String> props = new HashMap<>(placeholderProps);
55+
props.put(MQSourceConnector.CONFIG_NAME_MQ_RECORD_BUILDER, "casjsajhasdhusdo;iasd");
4456

45-
@Test(expected = RecordBuilderException.class)
46-
public void testGetRecordBuilder_NullProps() {
47-
RecordBuilderFactory.getRecordBuilder("casjsajhasdhusdo;iasd", null);
57+
RecordBuilderFactory.getRecordBuilder(props);
4858
}
4959
}

0 commit comments

Comments
 (0)