Skip to content

Commit fadd5db

Browse files
Merge pull request #147 from Ashutosh-Jarvis/feature/unsupported-field-fix
[PLUGIN-1952] Add column details in logging in case of invalid data
2 parents b69eb43 + d0ae86f commit fadd5db

3 files changed

Lines changed: 131 additions & 17 deletions

File tree

src/main/java/io/cdap/plugin/servicenow/connector/ServiceNowRecordConverter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ public static void convertToValue(String fieldName, Schema fieldSchema, Map<Stri
121121
recordBuilder.set(fieldName, fieldValue);
122122
return;
123123
case DOUBLE:
124-
recordBuilder.set(fieldName, convertToDoubleValue(fieldValue));
124+
recordBuilder.set(fieldName, convertToDoubleValue(fieldValue, fieldName));
125125
return;
126126
case INT:
127-
recordBuilder.set(fieldName, convertToIntegerValue(fieldValue));
127+
recordBuilder.set(fieldName, convertToIntegerValue(fieldValue, fieldName));
128128
return;
129129
case BOOLEAN:
130-
recordBuilder.set(fieldName, convertToBooleanValue(fieldValue));
130+
recordBuilder.set(fieldName, convertToBooleanValue(fieldValue, fieldName));
131131
return;
132132
case ARRAY:
133133
recordBuilder.set(fieldName, legacyMapping.equals(Boolean.FALSE) ? convertToList(fieldValue) : fieldValue);
@@ -150,33 +150,33 @@ public static List<String> convertToList(String fieldValue) {
150150
}
151151

152152
@VisibleForTesting
153-
public static Double convertToDoubleValue(String fieldValue) {
153+
public static Double convertToDoubleValue(String fieldValue, String fieldName) {
154154
try {
155155
return NumberFormat.getNumberInstance(Locale.US).parse(fieldValue).doubleValue();
156156
} catch (ParseException exception) {
157157
throw new UnexpectedFormatException(
158-
String.format("Field with value '%s' is not in valid format.", fieldValue), exception);
158+
String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue), exception);
159159
}
160160
}
161161

162162
@VisibleForTesting
163-
public static Integer convertToIntegerValue(String fieldValue) {
163+
public static Integer convertToIntegerValue(String fieldValue, String fieldName) {
164164
try {
165165
return NumberFormat.getNumberInstance(java.util.Locale.US).parse(fieldValue).intValue();
166166
} catch (ParseException exception) {
167167
throw new UnexpectedFormatException(
168-
String.format("Field with value '%s' is not in valid format.", fieldValue), exception);
168+
String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue), exception);
169169
}
170170
}
171171

172172
@VisibleForTesting
173-
public static Boolean convertToBooleanValue(String fieldValue) {
173+
public static Boolean convertToBooleanValue(String fieldValue, String fieldName) {
174174
if (fieldValue.equalsIgnoreCase(Boolean.TRUE.toString()) ||
175175
fieldValue.equalsIgnoreCase(Boolean.FALSE.toString())) {
176176
return Boolean.parseBoolean(fieldValue);
177177
}
178178
throw new UnexpectedFormatException(
179-
String.format("Field with value '%s' is not in valid format.", fieldValue));
179+
String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue));
180180

181181
}
182182

src/test/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReaderTest.java

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,79 @@ public void testConvertToValueInvalidFieldType() {
101101

102102
@Test
103103
public void testConvertToDoubleValue() throws ParseException {
104-
Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42"), 0.0);
104+
Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42", "code"), 0.0);
105+
}
106+
107+
@Test
108+
public void testConvertToDoubleValue_ThrowsExceptionForBadData() {
109+
String badValue = "not_a_number";
110+
String testFieldName = "code";
111+
112+
try {
113+
// 1. Attempt the conversion with bad data
114+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
115+
116+
// 2. If the line above DOES NOT throw an error, fail the test immediately
117+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
118+
119+
} catch (UnexpectedFormatException exception) {
120+
// 3. Catch the exception and assert the message matches perfectly
121+
String expectedMessage = "Field 'code' with value 'not_a_number' is not in valid format.";
122+
Assert.assertEquals(expectedMessage, exception.getMessage());
123+
}
105124
}
106125

107126
@Test
108127
public void testConvertToIntegerValue() throws ParseException {
109-
Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42").intValue());
128+
Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42", "code").intValue());
129+
}
130+
131+
@Test
132+
public void testConvertToIntegerValue_ThrowsExceptionForBadData() {
133+
String badValue = "not_an_integer";
134+
String testFieldName = "code";
135+
136+
try {
137+
// Attempt the conversion with bad data
138+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
139+
140+
// If the line above DOES NOT throw an error, fail the test immediately
141+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
142+
143+
} catch (UnexpectedFormatException exception) {
144+
// Catch the exception and assert the message matches perfectly
145+
String expectedMessage = "Field 'code' with value 'not_an_integer' is not in valid format.";
146+
Assert.assertEquals(expectedMessage, exception.getMessage());
147+
}
110148
}
111149

112150
@Test
113151
public void testConvertToBooleanValue() {
114-
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true"));
152+
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true", "accept"));
115153
}
116154

117155
@Test(expected = UnexpectedFormatException.class)
118156
public void testConvertToBooleanValueForInvalidFieldValue() {
119-
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1"));
157+
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1", "accept"));
158+
}
159+
160+
@Test
161+
public void testConvertToBooleanValue_ThrowsExceptionForBadData() {
162+
String badValue = "not_a_boolean";
163+
String testFieldName = "accept";
164+
165+
try {
166+
// Attempt the conversion with bad data
167+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
168+
169+
// If the line above DOES NOT throw an error, fail the test immediately
170+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
171+
172+
} catch (UnexpectedFormatException exception) {
173+
// Catch the exception and assert the message matches perfectly
174+
String expectedMessage = "Field 'accept' with value 'not_a_boolean' is not in valid format.";
175+
Assert.assertEquals(expectedMessage, exception.getMessage());
176+
}
120177
}
121178

122179
@Test

src/test/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReaderTest.java

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,23 +263,80 @@ public void testConvertToValue_WithArrayFieldType_ParseAsArray() {
263263

264264
@Test
265265
public void testConvertToDoubleValue() throws ParseException {
266-
Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42"),
266+
Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42", "code"),
267267
0.0);
268268
}
269269

270+
@Test
271+
public void testConvertToDoubleValue_ThrowsExceptionForBadData() {
272+
String badValue = "not_a_number";
273+
String testFieldName = "code";
274+
275+
try {
276+
// 1. Attempt the conversion with bad data
277+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
278+
279+
// 2. If the line above DOES NOT throw an error, fail the test immediately
280+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
281+
282+
} catch (UnexpectedFormatException exception) {
283+
// 3. Catch the exception and assert the message matches perfectly
284+
String expectedMessage = "Field 'code' with value 'not_a_number' is not in valid format.";
285+
Assert.assertEquals(expectedMessage, exception.getMessage());
286+
}
287+
}
288+
270289
@Test
271290
public void testConvertToIntegerValue() throws ParseException {
272-
Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42").intValue());
291+
Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42", "code").intValue());
292+
}
293+
294+
@Test
295+
public void testConvertToIntegerValue_ThrowsExceptionForBadData() {
296+
String badValue = "not_an_integer";
297+
String testFieldName = "code";
298+
299+
try {
300+
// Attempt the conversion with bad data
301+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
302+
303+
// If the line above DOES NOT throw an error, fail the test immediately
304+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
305+
306+
} catch (UnexpectedFormatException exception) {
307+
// Catch the exception and assert the message matches perfectly
308+
String expectedMessage = "Field 'code' with value 'not_an_integer' is not in valid format.";
309+
Assert.assertEquals(expectedMessage, exception.getMessage());
310+
}
273311
}
274312

275313
@Test
276314
public void testConvertToBooleanValue() {
277-
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true"));
315+
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true", "accept"));
278316
}
279317

280318
@Test(expected = UnexpectedFormatException.class)
281319
public void testConvertToBooleanValueForInvalidFieldValue() {
282-
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1"));
320+
Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1", "accept"));
321+
}
322+
323+
@Test
324+
public void testConvertToBooleanValue_ThrowsExceptionForBadData() {
325+
String badValue = "not_a_boolean";
326+
String testFieldName = "accept";
327+
328+
try {
329+
// Attempt the conversion with bad data
330+
ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName);
331+
332+
// If the line above DOES NOT throw an error, fail the test immediately
333+
Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not.");
334+
335+
} catch (UnexpectedFormatException exception) {
336+
// Catch the exception and assert the message matches perfectly
337+
String expectedMessage = "Field 'accept' with value 'not_a_boolean' is not in valid format.";
338+
Assert.assertEquals(expectedMessage, exception.getMessage());
339+
}
283340
}
284341

285342
@Test

0 commit comments

Comments
 (0)