diff --git a/src/main/java/io/cdap/plugin/servicenow/connector/ServiceNowRecordConverter.java b/src/main/java/io/cdap/plugin/servicenow/connector/ServiceNowRecordConverter.java index 4d0323b9..09290419 100644 --- a/src/main/java/io/cdap/plugin/servicenow/connector/ServiceNowRecordConverter.java +++ b/src/main/java/io/cdap/plugin/servicenow/connector/ServiceNowRecordConverter.java @@ -121,13 +121,13 @@ public static void convertToValue(String fieldName, Schema fieldSchema, Map convertToList(String fieldValue) { } @VisibleForTesting - public static Double convertToDoubleValue(String fieldValue) { + public static Double convertToDoubleValue(String fieldValue, String fieldName) { try { return NumberFormat.getNumberInstance(Locale.US).parse(fieldValue).doubleValue(); } catch (ParseException exception) { throw new UnexpectedFormatException( - String.format("Field with value '%s' is not in valid format.", fieldValue), exception); + String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue), exception); } } @VisibleForTesting - public static Integer convertToIntegerValue(String fieldValue) { + public static Integer convertToIntegerValue(String fieldValue, String fieldName) { try { return NumberFormat.getNumberInstance(java.util.Locale.US).parse(fieldValue).intValue(); } catch (ParseException exception) { throw new UnexpectedFormatException( - String.format("Field with value '%s' is not in valid format.", fieldValue), exception); + String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue), exception); } } @VisibleForTesting - public static Boolean convertToBooleanValue(String fieldValue) { + public static Boolean convertToBooleanValue(String fieldValue, String fieldName) { if (fieldValue.equalsIgnoreCase(Boolean.TRUE.toString()) || fieldValue.equalsIgnoreCase(Boolean.FALSE.toString())) { return Boolean.parseBoolean(fieldValue); } throw new UnexpectedFormatException( - String.format("Field with value '%s' is not in valid format.", fieldValue)); + String.format("Field '%s' with value '%s' is not in valid format.", fieldName, fieldValue)); } diff --git a/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReaderTest.java b/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReaderTest.java index f3e722d7..3b5d1aae 100644 --- a/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReaderTest.java +++ b/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowMultiRecordReaderTest.java @@ -101,22 +101,79 @@ public void testConvertToValueInvalidFieldType() { @Test public void testConvertToDoubleValue() throws ParseException { - Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42"), 0.0); + Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42", "code"), 0.0); + } + + @Test + public void testConvertToDoubleValue_ThrowsExceptionForBadData() { + String badValue = "not_a_number"; + String testFieldName = "code"; + + try { + // 1. Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // 2. If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // 3. Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'code' with value 'not_a_number' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } } @Test public void testConvertToIntegerValue() throws ParseException { - Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42").intValue()); + Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42", "code").intValue()); + } + + @Test + public void testConvertToIntegerValue_ThrowsExceptionForBadData() { + String badValue = "not_an_integer"; + String testFieldName = "code"; + + try { + // Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'code' with value 'not_an_integer' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } } @Test public void testConvertToBooleanValue() { - Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true")); + Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true", "accept")); } @Test(expected = UnexpectedFormatException.class) public void testConvertToBooleanValueForInvalidFieldValue() { - Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1")); + Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1", "accept")); + } + + @Test + public void testConvertToBooleanValue_ThrowsExceptionForBadData() { + String badValue = "not_a_boolean"; + String testFieldName = "accept"; + + try { + // Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'accept' with value 'not_a_boolean' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } } @Test diff --git a/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReaderTest.java b/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReaderTest.java index 2c6a63c5..0be20add 100644 --- a/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReaderTest.java +++ b/src/test/java/io/cdap/plugin/servicenow/source/ServiceNowRecordReaderTest.java @@ -263,23 +263,80 @@ public void testConvertToValue_WithArrayFieldType_ParseAsArray() { @Test public void testConvertToDoubleValue() throws ParseException { - Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42"), + Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42", "code"), 0.0); } + @Test + public void testConvertToDoubleValue_ThrowsExceptionForBadData() { + String badValue = "not_a_number"; + String testFieldName = "code"; + + try { + // 1. Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // 2. If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // 3. Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'code' with value 'not_a_number' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } + } + @Test public void testConvertToIntegerValue() throws ParseException { - Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42").intValue()); + Assert.assertEquals(42, ServiceNowRecordConverter.convertToIntegerValue("42", "code").intValue()); + } + + @Test + public void testConvertToIntegerValue_ThrowsExceptionForBadData() { + String badValue = "not_an_integer"; + String testFieldName = "code"; + + try { + // Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'code' with value 'not_an_integer' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } } @Test public void testConvertToBooleanValue() { - Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true")); + Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("true", "accept")); } @Test(expected = UnexpectedFormatException.class) public void testConvertToBooleanValueForInvalidFieldValue() { - Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1")); + Assert.assertTrue(ServiceNowRecordConverter.convertToBooleanValue("1", "accept")); + } + + @Test + public void testConvertToBooleanValue_ThrowsExceptionForBadData() { + String badValue = "not_a_boolean"; + String testFieldName = "accept"; + + try { + // Attempt the conversion with bad data + ServiceNowRecordConverter.convertToDoubleValue(badValue, testFieldName); + + // If the line above DOES NOT throw an error, fail the test immediately + Assert.fail("Expected an UnexpectedFormatException to be thrown, but it was not."); + + } catch (UnexpectedFormatException exception) { + // Catch the exception and assert the message matches perfectly + String expectedMessage = "Field 'accept' with value 'not_a_boolean' is not in valid format."; + Assert.assertEquals(expectedMessage, exception.getMessage()); + } } @Test