Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public static void convertToValue(String fieldName, Schema fieldSchema, Map<Stri
recordBuilder.set(fieldName, fieldValue);
return;
case DOUBLE:
recordBuilder.set(fieldName, convertToDoubleValue(fieldValue));
recordBuilder.set(fieldName, convertToDoubleValue(fieldValue, fieldName));
return;
case INT:
recordBuilder.set(fieldName, convertToIntegerValue(fieldValue));
recordBuilder.set(fieldName, convertToIntegerValue(fieldValue, fieldName));
return;
case BOOLEAN:
recordBuilder.set(fieldName, convertToBooleanValue(fieldValue));
recordBuilder.set(fieldName, convertToBooleanValue(fieldValue, fieldName));
return;
case ARRAY:
recordBuilder.set(fieldName, legacyMapping.equals(Boolean.FALSE) ? convertToList(fieldValue) : fieldValue);
Expand All @@ -150,33 +150,33 @@ public static List<String> 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));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,79 @@ public void testConvertToValueInvalidFieldType() {

@Test
public void testConvertToDoubleValue() throws ParseException {
Assert.assertEquals(42.0, ServiceNowRecordConverter.convertToDoubleValue("42"), 0.0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write a test case to compare error message

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading