Search in sources :

Example 1 with CassandraRequirement

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

the class PerRequestKeyspaceIT method should_execute_simple_statement_with_keyspace.

@Test
@CassandraRequirement(min = "4.0")
public void should_execute_simple_statement_with_keyspace() {
    CqlSession session = sessionRule.session();
    session.execute(SimpleStatement.newInstance("INSERT INTO foo (k, cc, v) VALUES (?, ?, ?)", nameRule.getMethodName(), 1, 1).setKeyspace(sessionRule.keyspace()));
    Row row = session.execute(SimpleStatement.newInstance("SELECT v FROM foo WHERE k = ? AND cc = 1", nameRule.getMethodName()).setKeyspace(sessionRule.keyspace())).one();
    assertThat(row.getInt(0)).isEqualTo(1);
}
Also used : Row(com.datastax.oss.driver.api.core.cql.Row) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 2 with CassandraRequirement

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

the class ExecutionInfoWarningsIT method should_expose_warnings_on_execution_info.

@Test
@CassandraRequirement(min = "2.2")
public void should_expose_warnings_on_execution_info() {
    // the default batch size warn threshold is 5 * 1024 bytes, but after CASSANDRA-10876 there must
    // be multiple mutations in a batch to trigger this warning so the batch includes 2 different
    // inserts.
    final String query = String.format("BEGIN UNLOGGED BATCH\n" + "INSERT INTO test (k, v) VALUES (1, '%s')\n" + "INSERT INTO test (k, v) VALUES (2, '%s')\n" + "APPLY BATCH", Strings.repeat("1", 2 * 1024), Strings.repeat("1", 3 * 1024));
    Statement<?> st = SimpleStatement.builder(query).build();
    ResultSet result = sessionRule.session().execute(st);
    ExecutionInfo executionInfo = result.getExecutionInfo();
    assertThat(executionInfo).isNotNull();
    List<String> warnings = executionInfo.getWarnings();
    assertThat(warnings).isNotEmpty();
    // verify the log was generated
    verify(appender, timeout(500).atLeast(1)).doAppend(loggingEventCaptor.capture());
    List<String> logMessages = loggingEventCaptor.getAllValues().stream().map(ILoggingEvent::getFormattedMessage).collect(Collectors.toList());
    assertThat(logMessages).anySatisfy(logMessage -> assertThat(logMessage).startsWith("Query '").contains(query.substring(0, RequestLogger.DEFAULT_REQUEST_LOGGER_MAX_QUERY_LENGTH)).contains("' generated server side warning(s): ").contains("Batch").contains("for").contains(String.format("%s.test", sessionRule.keyspace().asCql(true))).contains("is of size").containsPattern("exceeding specified .*threshold"));
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 3 with CassandraRequirement

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

the class ExecutionInfoWarningsIT method should_execute_query_and_log_server_side_warnings.

@Test
@CassandraRequirement(min = "3.0")
public void should_execute_query_and_log_server_side_warnings() {
    final String query = "SELECT count(*) FROM test;";
    Statement<?> st = SimpleStatement.builder(query).build();
    ResultSet result = sessionRule.session().execute(st);
    ExecutionInfo executionInfo = result.getExecutionInfo();
    assertThat(executionInfo).isNotNull();
    List<String> warnings = executionInfo.getWarnings();
    assertThat(warnings).isNotEmpty();
    String warning = warnings.get(0);
    assertThat(warning).isEqualTo("Aggregation query used without partition key");
    // verify the log was generated
    verify(appender, timeout(500).times(1)).doAppend(loggingEventCaptor.capture());
    assertThat(loggingEventCaptor.getValue().getMessage()).isNotNull();
    String logMessage = loggingEventCaptor.getValue().getFormattedMessage();
    assertThat(logMessage).startsWith("Query '[0 values] " + query + "' generated server side warning(s): Aggregation query used without partition key");
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 4 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_pages.

@Test
@CassandraRequirement(min = "4.0")
public void should_update_metadata_when_schema_changed_across_pages() {
    // Given
    CqlSession session = sessionRule.session();
    PreparedStatement ps = session.prepare("SELECT * FROM prepared_statement_test");
    ByteBuffer idBefore = ps.getResultMetadataId();
    assertThat(ps.getResultSetDefinitions()).hasSize(3);
    CompletionStage<AsyncResultSet> future = session.executeAsync(ps.bind());
    AsyncResultSet rows = CompletableFutures.getUninterruptibly(future);
    assertThat(rows.getColumnDefinitions()).hasSize(3);
    assertThat(rows.getColumnDefinitions().contains("d")).isFalse();
    // Consume the first page
    for (Row row : rows.currentPage()) {
        try {
            row.getInt("d");
            TestCase.fail("expected an error");
        } catch (IllegalArgumentException e) {
        /*expected*/
        }
    }
    // When
    session.execute(SimpleStatement.builder("ALTER TABLE prepared_statement_test ADD d int").setExecutionProfile(sessionRule.slowProfile()).build());
    // Then
    // this should trigger a background fetch of the second page, and therefore update the
    // definitions
    rows = CompletableFutures.getUninterruptibly(rows.fetchNextPage());
    for (Row row : rows.currentPage()) {
        assertThat(row.isNull("d")).isTrue();
    }
    assertThat(rows.getColumnDefinitions()).hasSize(4);
    assertThat(rows.getColumnDefinitions().get("d").getType()).isEqualTo(DataTypes.INT);
    // Should have updated the prepared statement too
    ByteBuffer idAfter = ps.getResultMetadataId();
    assertThat(Bytes.toHexString(idAfter)).isNotEqualTo(Bytes.toHexString(idBefore));
    assertThat(ps.getResultSetDefinitions()).hasSize(4);
    assertThat(ps.getResultSetDefinitions().get("d").getType()).isEqualTo(DataTypes.INT);
}
Also used : AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 5 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_sessions.

@Test
@CassandraRequirement(min = "4.0")
public void should_update_metadata_when_schema_changed_across_sessions() {
    // Given
    CqlSession session1 = sessionRule.session();
    CqlSession session2 = SessionUtils.newSession(ccmRule, sessionRule.keyspace());
    PreparedStatement ps1 = session1.prepare("SELECT * FROM prepared_statement_test WHERE a = ?");
    PreparedStatement ps2 = session2.prepare("SELECT * FROM prepared_statement_test WHERE a = ?");
    ByteBuffer id1a = ps1.getResultMetadataId();
    ByteBuffer id2a = ps2.getResultMetadataId();
    ResultSet rows1 = session1.execute(ps1.bind(1));
    ResultSet rows2 = session2.execute(ps2.bind(1));
    assertThat(rows1.getColumnDefinitions()).hasSize(3);
    assertThat(rows1.getColumnDefinitions().contains("d")).isFalse();
    assertThat(rows2.getColumnDefinitions()).hasSize(3);
    assertThat(rows2.getColumnDefinitions().contains("d")).isFalse();
    // When
    session1.execute("ALTER TABLE prepared_statement_test ADD d int");
    rows1 = session1.execute(ps1.bind(1));
    rows2 = session2.execute(ps2.bind(1));
    ByteBuffer id1b = ps1.getResultMetadataId();
    ByteBuffer id2b = ps2.getResultMetadataId();
    // Then
    assertThat(Bytes.toHexString(id1b)).isNotEqualTo(Bytes.toHexString(id1a));
    assertThat(Bytes.toHexString(id2b)).isNotEqualTo(Bytes.toHexString(id2a));
    assertThat(ps1.getResultSetDefinitions()).hasSize(4);
    assertThat(ps1.getResultSetDefinitions().contains("d")).isTrue();
    assertThat(ps2.getResultSetDefinitions()).hasSize(4);
    assertThat(ps2.getResultSetDefinitions().contains("d")).isTrue();
    assertThat(rows1.getColumnDefinitions()).hasSize(4);
    assertThat(rows1.getColumnDefinitions().contains("d")).isTrue();
    assertThat(rows2.getColumnDefinitions()).hasSize(4);
    assertThat(rows2.getColumnDefinitions().contains("d")).isTrue();
    session2.close();
}
Also used : 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) Test(org.junit.Test) 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