Search in sources :

Example 26 with CassandraRequirement

use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.

the class SchemaIT method should_exclude_virtual_keyspaces_from_token_map.

@CassandraRequirement(min = "4.0", description = "virtual tables introduced in 4.0")
@Test
public void should_exclude_virtual_keyspaces_from_token_map() {
    skipIfDse60();
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, Arrays.asList("system_views", "system_virtual_schema", sessionRule.keyspace().asInternal())).build();
    try (CqlSession session = SessionUtils.newSession(ccmRule, loader)) {
        Metadata metadata = session.getMetadata();
        Map<CqlIdentifier, KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
        assertThat(keyspaces).containsKey(CqlIdentifier.fromCql("system_views")).containsKey(CqlIdentifier.fromCql("system_virtual_schema"));
        TokenMap tokenMap = metadata.getTokenMap().orElseThrow(AssertionError::new);
        // value does not matter
        ByteBuffer partitionKey = Bytes.fromHexString("0x00");
        assertThat(tokenMap.getReplicas("system_views", partitionKey)).isEmpty();
        assertThat(tokenMap.getReplicas("system_virtual_schema", partitionKey)).isEmpty();
        // Check that a non-virtual keyspace is present
        assertThat(tokenMap.getReplicas(sessionRule.keyspace(), partitionKey)).isNotEmpty();
    }
}
Also used : Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 27 with CassandraRequirement

use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.

the class PreparedStatementIT method should_update_metadata_when_schema_changed_across_executions.

@Test
@CassandraRequirement(min = "4.0")
public void should_update_metadata_when_schema_changed_across_executions() {
    // Given
    CqlSession session = sessionRule.session();
    PreparedStatement ps = session.prepare("SELECT * FROM prepared_statement_test WHERE a = ?");
    ByteBuffer idBefore = ps.getResultMetadataId();
    // When
    session.execute(SimpleStatement.builder("ALTER TABLE prepared_statement_test ADD d int").setExecutionProfile(sessionRule.slowProfile()).build());
    BoundStatement bs = ps.bind(1);
    ResultSet rows = session.execute(bs);
    // Then
    ByteBuffer idAfter = ps.getResultMetadataId();
    assertThat(Bytes.toHexString(idAfter)).isNotEqualTo(Bytes.toHexString(idBefore));
    for (ColumnDefinitions columnDefinitions : ImmutableList.of(ps.getResultSetDefinitions(), bs.getPreparedStatement().getResultSetDefinitions(), rows.getColumnDefinitions())) {
        assertThat(columnDefinitions).hasSize(4);
        assertThat(columnDefinitions.get("d").getType()).isEqualTo(DataTypes.INT);
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ByteBuffer(java.nio.ByteBuffer) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 28 with CassandraRequirement

use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.

the class PreparedStatementIT method should_fail_to_reprepare_if_query_becomes_invalid.

@Test
@CassandraRequirement(min = "4.0")
public void should_fail_to_reprepare_if_query_becomes_invalid() {
    // Given
    CqlSession session = sessionRule.session();
    session.execute("ALTER TABLE prepared_statement_test ADD d int");
    PreparedStatement ps = session.prepare("SELECT a, b, c, d FROM prepared_statement_test WHERE a = ?");
    session.execute("ALTER TABLE prepared_statement_test DROP d");
    // When
    Throwable t = catchThrowable(() -> session.execute(ps.bind()));
    // Then
    assertThat(t).isInstanceOf(InvalidQueryException.class).hasMessageContaining("Undefined column name d");
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 29 with CassandraRequirement

use of com.datastax.oss.driver.api.testinfra.CassandraRequirement in project java-driver by datastax.

the class BaseCcmRule method apply.

@Override
public Statement apply(Statement base, Description description) {
    // If test is annotated with CassandraRequirement or DseRequirement, ensure configured CCM
    // cluster meets those requirements.
    CassandraRequirement cassandraRequirement = description.getAnnotation(CassandraRequirement.class);
    if (cassandraRequirement != null) {
        // if the configured cassandra cassandraRequirement exceeds the one being used skip this test.
        if (!cassandraRequirement.min().isEmpty()) {
            Version minVersion = Version.parse(cassandraRequirement.min());
            if (minVersion.compareTo(ccmBridge.getCassandraVersion()) > 0) {
                return buildErrorStatement(minVersion, cassandraRequirement.description(), false, false);
            }
        }
        if (!cassandraRequirement.max().isEmpty()) {
            // if the test version exceeds the maximum configured one, fail out.
            Version maxVersion = Version.parse(cassandraRequirement.max());
            if (maxVersion.compareTo(ccmBridge.getCassandraVersion()) <= 0) {
                return buildErrorStatement(maxVersion, cassandraRequirement.description(), true, false);
            }
        }
    }
    DseRequirement dseRequirement = description.getAnnotation(DseRequirement.class);
    if (dseRequirement != null) {
        Optional<Version> dseVersionOption = ccmBridge.getDseVersion();
        if (!dseVersionOption.isPresent()) {
            return new Statement() {

                @Override
                public void evaluate() {
                    throw new AssumptionViolatedException("Test Requires DSE but C* is configured.");
                }
            };
        } else {
            Version dseVersion = dseVersionOption.get();
            if (!dseRequirement.min().isEmpty()) {
                Version minVersion = Version.parse(dseRequirement.min());
                if (minVersion.compareTo(dseVersion) > 0) {
                    return buildErrorStatement(minVersion, dseRequirement.description(), false, true);
                }
            }
            if (!dseRequirement.max().isEmpty()) {
                Version maxVersion = Version.parse(dseRequirement.max());
                if (maxVersion.compareTo(dseVersion) <= 0) {
                    return buildErrorStatement(maxVersion, dseRequirement.description(), true, true);
                }
            }
        }
    }
    return super.apply(base, description);
}
Also used : DseRequirement(com.datastax.oss.driver.api.testinfra.DseRequirement) DefaultProtocolVersion(com.datastax.oss.driver.api.core.DefaultProtocolVersion) Version(com.datastax.oss.driver.api.core.Version) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) AssumptionViolatedException(org.junit.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Aggregations

CassandraRequirement (com.datastax.oss.driver.api.testinfra.CassandraRequirement)29 Test (org.junit.Test)27 CqlSession (com.datastax.oss.driver.api.core.CqlSession)21 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)8 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)7 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)6 Row (com.datastax.oss.driver.api.core.cql.Row)6 ByteBuffer (java.nio.ByteBuffer)5 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)3 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)3 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)3 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)2 UnsupportedProtocolVersionException (com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException)2 Version (com.datastax.oss.driver.api.core.Version)2 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)2 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)2 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)2 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)2 DseRequirement (com.datastax.oss.driver.api.testinfra.DseRequirement)2