diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java index 5057ff2..b110584 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java @@ -17,7 +17,6 @@ package com.consol.citrus.db.driver; import com.consol.citrus.db.driver.data.Row; -import org.apache.commons.beanutils.ConvertUtils; import org.apache.http.client.HttpClient; import java.io.InputStream; @@ -74,42 +73,42 @@ public boolean wasNull() throws SQLException { @Override public String getString(final int parameterIndex) throws SQLException { - return (String) getData(parameterIndex, String.class); + return (String) getDataRow().getValue(parameterIndex-1, String.class); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return (boolean) getData(parameterIndex, boolean.class); + return (boolean) getDataRow().getValue(parameterIndex-1, boolean.class); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return (byte) getData(parameterIndex, byte.class); + return (byte) getDataRow().getValue(parameterIndex-1, byte.class); } @Override public short getShort(final int parameterIndex) throws SQLException { - return (short) getData(parameterIndex, short.class); + return (short) getDataRow().getValue(parameterIndex-1, short.class); } @Override public int getInt(final int parameterIndex) throws SQLException { - return (int) getData(parameterIndex, int.class); + return (int) getDataRow().getValue(parameterIndex-1, int.class); } @Override public long getLong(final int parameterIndex) throws SQLException { - return (long) getData(parameterIndex, long.class); + return (long) getDataRow().getValue(parameterIndex-1, long.class); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return (float) getData(parameterIndex, float.class); + return (float) getDataRow().getValue(parameterIndex-1, float.class); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return (double) getData(parameterIndex, double.class); + return (double) getDataRow().getValue(parameterIndex-1, double.class); } @Override @@ -119,32 +118,32 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - return (byte[]) getData(parameterIndex, byte[].class); + return (byte[]) getDataRow().getValue(parameterIndex-1, byte[].class); } @Override public Date getDate(final int parameterIndex) throws SQLException { - return (Date) getData(parameterIndex, Date.class); + return (Date) getDataRow().getValue(parameterIndex-1, Date.class); } @Override public Time getTime(final int parameterIndex) throws SQLException { - return (Time) getData(parameterIndex, Time.class); + return (Time) getDataRow().getValue(parameterIndex-1, Time.class); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - return (Timestamp) getData(parameterIndex, Timestamp.class); + return (Timestamp) getDataRow().getValue(parameterIndex-1, Timestamp.class); } @Override public Object getObject(final int parameterIndex) throws SQLException { - return getData(parameterIndex, Object.class); + return getDataRow().getValue(parameterIndex-1, Object.class); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return (BigDecimal) getData(parameterIndex, BigDecimal.class); + return (BigDecimal) getDataRow().getValue(parameterIndex-1, BigDecimal.class); } @Override @@ -339,72 +338,72 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return (String) getData(parameterName, String.class); + return (String) getDataRow().getValue(parameterName, String.class); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return (boolean) getData(parameterName, boolean.class); + return (boolean) getDataRow().getValue(parameterName, boolean.class); } @Override public byte getByte(final String parameterName) throws SQLException { - return (byte) getData(parameterName, byte.class); + return (byte) getDataRow().getValue(parameterName, byte.class); } @Override public short getShort(final String parameterName) throws SQLException { - return (short) getData(parameterName, short.class); + return (short) getDataRow().getValue(parameterName, short.class); } @Override public int getInt(final String parameterName) throws SQLException { - return (int) getData(parameterName, int.class); + return (int) getDataRow().getValue(parameterName, int.class); } @Override public long getLong(final String parameterName) throws SQLException { - return (long) getData(parameterName, long.class); + return (long) getDataRow().getValue(parameterName, long.class); } @Override public float getFloat(final String parameterName) throws SQLException { - return (float) getData(parameterName, float.class); + return (float) getDataRow().getValue(parameterName, float.class); } @Override public double getDouble(final String parameterName) throws SQLException { - return (double) getData(parameterName, double.class); + return (double) getDataRow().getValue(parameterName, double.class); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return (byte[]) getData(parameterName, byte[].class); + return (byte[]) getDataRow().getValue(parameterName, byte[].class); } @Override public Date getDate(final String parameterName) throws SQLException { - return (Date) getData(parameterName, Date.class); + return (Date) getDataRow().getValue(parameterName, Date.class); } @Override public Time getTime(final String parameterName) throws SQLException { - return (Time) getData(parameterName, Time.class); + return (Time) getDataRow().getValue(parameterName, Time.class); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - return (Timestamp) getData(parameterName, Timestamp.class); + return (Timestamp) getDataRow().getValue(parameterName, Timestamp.class); } @Override public Object getObject(final String parameterName) throws SQLException { - return getData(parameterName, Object.class); + return getDataRow().getValue(parameterName, Object.class); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return (BigDecimal) getData(parameterName, BigDecimal.class); + return (BigDecimal) getDataRow().getValue(parameterName, BigDecimal.class); } @Override @@ -660,20 +659,6 @@ private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } - private Object getData(final int parameterIndex, final Class clazz) throws SQLException { - final Object value = getDataRow().getValue(parameterIndex - 1); - return convertData(value, clazz); - } - - private Object getData(final String parameterName, final Class clazz) throws SQLException { - final Object value = getDataRow().getValue(parameterName); - return convertData(value, clazz); - } - - private Object convertData(final Object value, final Class clazz) { - return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz); - } - private Row getDataRow() throws SQLException { if(Objects.isNull(resultRow)){ resultRow = dataSet.getNextRow(); diff --git a/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java b/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java index c0c1e3c..79b02bd 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java @@ -16,6 +16,8 @@ package com.consol.citrus.db.driver.data; +import org.apache.commons.beanutils.ConvertUtils; + import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; @@ -23,9 +25,6 @@ import java.util.Objects; import java.util.SortedMap; -/** - * @author Christoph Deppisch - */ public class Row { /** Row values with column name as key */ @@ -35,36 +34,60 @@ public class Row { /** * Gets set of column names available in this row. - * @return + * @return A list of column values */ public List getColumns() { - return Arrays.asList(values.keySet().toArray(new String[values.size()])); + return Arrays.asList(values.keySet().toArray(new String[0])); } /** * Gets the row value identified by its column name. - * @param columnName - * @return + * @param columnName The name to get the value for + * @return The value of that column */ public Object getValue(final String columnName) { lastValue = values.get(columnName); return lastValue; } + /** + * Gets the row value identified by its column name converted into the given type + * @param parameterName The name to get the value for + * @param clazz The class to convert the value to + * @param Generic parameter to ensure correct conversion + * @return The converted object + */ + public Object getValue(final String parameterName, final Class clazz) { + final Object value = getValue(parameterName); + return convertData(value, clazz); + } + /** * Gets the row value identified by its column index. - * @param columnIndex - * @return + * @param columnIndex The column index to get the value from + * @return The object of that index */ public Object getValue(final int columnIndex) { lastValue = values.values().toArray()[columnIndex]; return lastValue; } + /** + * Gets the row value identified by its column index converted into the given type + * @param columnIndex The index to get the value from + * @param clazz The class to convert the value to + * @param Generic parameter to ensure correct conversion + * @return The converted object + */ + public Object getValue(final int columnIndex, final Class clazz){ + final Object value = getValue(columnIndex); + return convertData(value, clazz); + } + /** * Gets the values. * - * @return + * @return The values of the row identified by column manes */ public Map getValues() { return values; @@ -73,12 +96,16 @@ public Map getValues() { /** * Sets the values. * - * @param values + * @param values The values to set in the Row */ public void setValues(final SortedMap values) { this.values = values; } + private Object convertData(final Object value, final Class clazz) { + return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz); + } + @Override public boolean equals(final Object o) { if (this == o) return true; @@ -94,7 +121,7 @@ public int hashCode() { /** * Gets the lastValue. - * @return + * @return The last value as object */ public Object getLastValue() { return lastValue; diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java index 5f10797..8417fd0 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -5,11 +5,7 @@ import com.consol.citrus.db.driver.dataset.DataSetBuilder; import com.jparams.verifier.tostring.ToStringVerifier; import nl.jqno.equalsverifier.EqualsVerifier; -import org.apache.commons.beanutils.ConvertUtils; import org.apache.http.client.HttpClient; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.testng.PowerMockTestCase; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -26,27 +22,30 @@ import java.util.SortedMap; import java.util.TreeMap; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.spy; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; -@PrepareForTest(ConvertUtils.class) -public class JdbcCallableStatementTest extends PowerMockTestCase { +public class JdbcCallableStatementTest{ private HttpClient httpClient; private String serverUrl = "localhost"; private JdbcConnection jdbcConnection; - private final int TEST_VALUE_INDEX = 2; + private final int TEST_VALUE_INDEX_JDBC = 2; + //Because in JDBC, arrays start at 1 + private final int TEST_VALUE_INDEX_INTERNAL = TEST_VALUE_INDEX_JDBC -1; private final String TEST_VALUE_NAME = "col2"; private final JdbcCallableStatement callableStatement = generateCallableStatement(); + private Row rowSpy; + @BeforeMethod public void setup(){ httpClient = mock(HttpClient.class); jdbcConnection = mock(JdbcConnection.class); - PowerMockito.spy(ConvertUtils.class); } @Test @@ -437,7 +436,7 @@ void testGetTwoValuesByIndex() throws SQLException { //WHEN final String firstOutParameter = callableStatement.getString(1); - final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX); + final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(firstOutParameter, "dummyValue"); @@ -452,11 +451,11 @@ void testGetStringByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); //WHEN - final String string = callableStatement.getString(TEST_VALUE_INDEX); + final String string = callableStatement.getString(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(string, expectedString); - verifyConversion(expectedString, String.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); } @Test @@ -467,11 +466,11 @@ void testGetBooleanByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); //WHEN - final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX); + final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX_JDBC); //THEN assertTrue(aBoolean); - verifyConversion(expectedBoolean, boolean.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, boolean.class); } @Test @@ -482,11 +481,11 @@ void testGetByteByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); //WHEN - final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX); + final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aByte, expectedByte); - verifyConversion(expectedByte, byte.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte.class); } @Test @@ -497,11 +496,11 @@ void testGetBytesByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); //WHEN - final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX); + final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aByte, expectedBytes); - verifyConversion(expectedBytes, byte[].class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); } @Test @@ -512,11 +511,11 @@ void testGetShortByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); //WHEN - final short aShort = callableStatement.getShort(TEST_VALUE_INDEX); + final short aShort = callableStatement.getShort(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aShort, expectedShort); - verifyConversion(expectedShort, short.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, short.class); } @Test @@ -527,11 +526,11 @@ void testGetIntByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); //WHEN - final int anInt = callableStatement.getInt(TEST_VALUE_INDEX); + final int anInt = callableStatement.getInt(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(anInt, expectedInt); - verifyConversion(expectedInt, int.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, int.class); } @Test @@ -542,11 +541,11 @@ void testGetLongByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); //WHEN - final long aLong = callableStatement.getLong(TEST_VALUE_INDEX); + final long aLong = callableStatement.getLong(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aLong, expectedLong); - verifyConversion(expectedLong, long.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, long.class); } @Test @@ -557,11 +556,11 @@ void testGetFloatByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); //WHEN - final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX); + final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aFloat, expectedFloat); - verifyConversion(expectedFloat, float.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, float.class); } @Test @@ -572,11 +571,11 @@ void testGetDoubleByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); //WHEN - final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX); + final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aDouble, expectedDouble); - verifyConversion(expectedDouble, double.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, double.class); } @Test @@ -587,11 +586,11 @@ void testGetBigDecimalByIndexWithScale() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX, 2); + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX_JDBC, 2); //THEN assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); - verifyConversion(expectedBigDecimal, BigDecimal.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, BigDecimal.class); } @Test @@ -606,7 +605,7 @@ void testGetStringByName() throws SQLException { //THEN assertEquals(string, expectedString); - verifyConversion(expectedString, String.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); } @Test @@ -621,7 +620,7 @@ void testGetBooleanByName() throws SQLException { //THEN assertTrue(aBoolean); - verifyConversion(expectedBoolean, boolean.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, boolean.class); } @Test @@ -636,7 +635,7 @@ void testGetByteByName() throws SQLException { //THEN assertEquals(aByte, expectedByte); - verifyConversion(expectedByte, byte.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte.class); } @Test @@ -651,7 +650,7 @@ void testGetShortByName() throws SQLException { //THEN assertEquals(aShort, expectedShort); - verifyConversion(expectedShort, short.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, short.class); } @Test @@ -666,7 +665,7 @@ void testGetIntByName() throws SQLException { //THEN assertEquals(anInt, expectedInt); - verifyConversion(expectedInt, int.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, int.class); } @Test @@ -681,7 +680,7 @@ void testGetLongByName() throws SQLException { //THEN assertEquals(aLong, expectedLong); - verifyConversion(expectedLong, long.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, long.class); } @Test @@ -696,7 +695,7 @@ void testGetFloatByName() throws SQLException { //THEN assertEquals(aFloat, expectedFloat); - verifyConversion(expectedFloat, float.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, float.class); } @Test @@ -711,7 +710,7 @@ void testGetDoubleByName() throws SQLException { //THEN assertEquals(aFloat, expectedDouble); - verifyConversion(expectedDouble, double.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, double.class); } @Test @@ -726,7 +725,7 @@ void testGetBytesByName() throws SQLException { //THEN assertEquals(bytes, expectedBytes); - verifyConversion(expectedBytes, byte[].class); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); } @Test @@ -741,7 +740,7 @@ void testGetObjectByName() throws SQLException { //THEN assertEquals(object, expectedObject); - verifyConversion(expectedObject, Object.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Object.class); } @Test @@ -752,11 +751,11 @@ void testGetBigDecimalByName() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME ); + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME); //THEN assertEquals(aBigDecimal, expectedBigDecimal); - verifyConversion(aBigDecimal, BigDecimal.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, BigDecimal.class); } @Test @@ -807,11 +806,11 @@ void testGetDateByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); //WHEN - final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX); + final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aDate, expectedDate); - verifyConversion(expectedDate, Date.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Date.class); } @Test @@ -822,11 +821,11 @@ void testGetTimeByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); //WHEN - final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX); + final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aTime, expectedTime); - verifyConversion(expectedTime, Time.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Time.class); } @Test @@ -837,11 +836,11 @@ void testGetTimestampByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); //WHEN - final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX); + final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aTimestamp, expectedTimestamp); - verifyConversion(expectedTimestamp, Timestamp.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Timestamp.class); } @Test @@ -852,11 +851,11 @@ void testGetObjectStampByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); //WHEN - final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX); + final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(anObject, expectedObject); - verifyConversion(expectedObject, Object.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Object.class); } @Test @@ -871,7 +870,7 @@ void testGetDateByName() throws SQLException { //THEN assertEquals(aDate, expectedDate); - verifyConversion(expectedDate, Date.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Date.class); } @Test @@ -886,7 +885,7 @@ void testGetTimeByName() throws SQLException { //THEN assertEquals(aTime, expectedTime); - verifyConversion(expectedTime, Time.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Time.class); } @Test @@ -901,7 +900,7 @@ void testGetTimestampByName() throws SQLException { //THEN assertEquals(aTimestamp, expectedTimestamp); - verifyConversion(expectedTimestamp, Timestamp.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Timestamp.class); } @Test @@ -935,13 +934,8 @@ private DataSet generateTestDataSet(final Object testValue) throws SQLException testData.put("col1", "dummyValue"); testData.put("col2", testValue); - final Row row = new Row(); - row.setValues(testData); - return new DataSetBuilder().add(row).build(); - } - - private void verifyConversion(final Object expectedValue, final Class clazz) { - PowerMockito.verifyStatic(ConvertUtils.class); - ConvertUtils.convert(eq(expectedValue), eq(clazz)); + rowSpy = spy(new Row()); + rowSpy.setValues(testData); + return new DataSetBuilder().add(rowSpy).build(); } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java b/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java new file mode 100644 index 0000000..f036d34 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java @@ -0,0 +1,136 @@ +package com.consol.citrus.db.driver.data; + +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.commons.beanutils.ConvertUtils; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.mockito.ArgumentMatchers.eq; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static org.testng.Assert.assertEquals; + +@PrepareForTest(ConvertUtils.class) +public class RowTest extends PowerMockTestCase { + + private Row row ; + private final String COLUMN_1 = "col1"; + private final String COLUMN_2 = "col2"; + private final String COLUMN_3 = "col3"; + + private final Object VALUE_1 = 42; + private final Object VALUE_2 = "84"; + + @BeforeMethod + public void setUp(){ + spy(ConvertUtils.class); + row = generateRow(); + } + + @Test + public void testGetColumns(){ + + //WHEN + final List columns = row.getColumns(); + + //THEN + assertEquals(columns.get(0), COLUMN_1); + assertEquals(columns.get(1), COLUMN_2); + assertEquals(columns.get(2), COLUMN_3); + } + + @Test + public void testGetValueByColumnName(){ + + //WHEN + final Object value = row.getValue(COLUMN_1); + + //THEN + assertEquals(value, VALUE_1); + + } + + @Test + public void testGetValueByColumnNameWithConversion(){ + + //WHEN + final Object value = row.getValue(COLUMN_2, int.class); + + //THEN + assertEquals(value, Integer.valueOf((String) VALUE_2)); + verifyConversion(VALUE_2, int.class); + + } + + @Test + public void testGetValueByColumnIndex(){ + + //WHEN + final Object value = row.getValue(1); + + //THEN + assertEquals(value, VALUE_2); + + } + + @Test + public void testGetValueByColumnIndexWithConversion(){ + + //WHEN + final Object value = row.getValue(1, double.class); + + //THEN + assertEquals(value, Double.valueOf((String) VALUE_2)); + verifyConversion(VALUE_2, double.class); + + } + + @Test + public void getGetValues(){ + + //WHEN + final Map values = row.getValues(); + + //THEN + assertEquals(values, generateRowValues()); + + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(Row.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(Row.class); + } + + private Row generateRow() { + final SortedMap values = generateRowValues(); + final Row row = new Row(); + row.setValues(values); + return row; + } + + private SortedMap generateRowValues() { + final SortedMap values = new TreeMap<>(); + values.put(COLUMN_1, VALUE_1); + values.put(COLUMN_2, VALUE_2); + values.put(COLUMN_3, null); + return values; + } + + private void verifyConversion(final Object toBeConverted, final Class clazz) { + verifyStatic(ConvertUtils.class); + ConvertUtils.convert(eq(toBeConverted), eq(clazz)); + } +} \ No newline at end of file