use of com.datastax.oss.driver.api.core.ProtocolVersion in project java-driver by datastax.
the class DataTypeIT method readValue.
private <K> void readValue(Statement<?> select, DataType dataType, K value, K expectedPrimitiveValue) {
TypeCodec<Object> codec = SESSION_RULE.session().getContext().getCodecRegistry().codecFor(dataType);
ResultSet result = SESSION_RULE.session().execute(select);
String columnName = columnNameFor(dataType);
List<Row> rows = result.all();
assertThat(rows).hasSize(1);
Row row = rows.iterator().next();
K expectedValue = expectedPrimitiveValue != null ? expectedPrimitiveValue : value;
switch(dataType.getProtocolCode()) {
case ProtocolConstants.DataType.ASCII:
case ProtocolConstants.DataType.VARCHAR:
assertThat(row.getString(columnName)).isEqualTo(expectedValue);
assertThat(row.getString(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BIGINT:
assertThat(row.getLong(columnName)).isEqualTo(expectedValue);
assertThat(row.getLong(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BLOB:
assertThat(row.getByteBuffer(columnName)).isEqualTo(expectedValue);
assertThat(row.getByteBuffer(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BOOLEAN:
assertThat(row.getBoolean(columnName)).isEqualTo(expectedValue);
assertThat(row.getBoolean(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DECIMAL:
assertThat(row.getBigDecimal(columnName)).isEqualTo(expectedValue);
assertThat(row.getBigDecimal(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DOUBLE:
assertThat(row.getDouble(columnName)).isEqualTo(expectedValue);
assertThat(row.getDouble(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.FLOAT:
assertThat(row.getFloat(columnName)).isEqualTo(expectedValue);
assertThat(row.getFloat(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.INET:
assertThat(row.getInetAddress(columnName)).isEqualTo(expectedValue);
assertThat(row.getInetAddress(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TINYINT:
assertThat(row.getByte(columnName)).isEqualTo(expectedValue);
assertThat(row.getByte(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.SMALLINT:
assertThat(row.getShort(columnName)).isEqualTo(expectedValue);
assertThat(row.getShort(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.INT:
assertThat(row.getInt(columnName)).isEqualTo(expectedValue);
assertThat(row.getInt(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DURATION:
assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIMESTAMP:
assertThat(row.getInstant(columnName)).isEqualTo(expectedValue);
assertThat(row.getInstant(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DATE:
assertThat(row.getLocalDate(columnName)).isEqualTo(expectedValue);
assertThat(row.getLocalDate(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIME:
assertThat(row.getLocalTime(columnName)).isEqualTo(expectedValue);
assertThat(row.getLocalTime(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIMEUUID:
case ProtocolConstants.DataType.UUID:
assertThat(row.getUuid(columnName)).isEqualTo(expectedValue);
assertThat(row.getUuid(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.VARINT:
assertThat(row.getBigInteger(columnName)).isEqualTo(expectedValue);
assertThat(row.getBigInteger(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.CUSTOM:
if (((CustomType) dataType).getClassName().equals("org.apache.cassandra.db.marshal.DurationType")) {
assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
break;
}
// fall through
case ProtocolConstants.DataType.LIST:
case ProtocolConstants.DataType.MAP:
case ProtocolConstants.DataType.SET:
assertThat(row.get(columnName, codec)).isEqualTo(expectedValue);
assertThat(row.get(0, codec)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TUPLE:
TupleValue returnedValue = row.getTupleValue(columnName);
TupleValue exValue = (TupleValue) expectedValue;
assertThat(returnedValue.getType()).isEqualTo(exValue.getType());
assertThat(row.getTupleValue(columnName)).isEqualTo(expectedValue);
assertThat(row.getTupleValue(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.UDT:
UdtValue returnedUdtValue = row.getUdtValue(columnName);
UdtValue exUdtValue = (UdtValue) expectedValue;
assertThat(returnedUdtValue.getType()).isEqualTo(exUdtValue.getType());
assertThat(row.getUdtValue(columnName)).isEqualTo(expectedValue);
assertThat(row.getUdtValue(0)).isEqualTo(expectedValue);
break;
default:
fail("Unhandled DataType " + dataType);
}
if (value == null) {
assertThat(row.isNull(columnName)).isTrue();
assertThat(row.isNull(0)).isTrue();
}
// Decode directly using the codec
ProtocolVersion protocolVersion = SESSION_RULE.session().getContext().getProtocolVersion();
assertThat(codec.decode(row.getBytesUnsafe(columnName), protocolVersion)).isEqualTo(value);
assertThat(codec.decode(row.getBytesUnsafe(0), protocolVersion)).isEqualTo(value);
}
Aggregations