All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.goodforgod.testcontainers.extensions.cassandra.CassandraConnection Maven / Gradle / Ivy

There is a newer version: 0.12.1
Show newest version
package io.goodforgod.testcontainers.extensions.cassandra;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import java.util.List;
import java.util.Optional;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.CassandraContainer;

/**
 * Describes active Cassandra connection of currently running {@link CassandraContainer}
 */
public interface CassandraConnection {

    @FunctionalInterface
    interface RowMapper {

        /**
         * @param row to map
         * @return mapped value from row
         * @throws CassandraConnectionException if cql data manipulation error occurred
         * @throws E                            if mapping error occurred
         */
        R apply(@NotNull Row row) throws E;
    }

    /**
     * Cassandra connection parameters
     */
    interface Params {

        @NotNull
        String host();

        int port();

        @NotNull
        String datacenter();

        String username();

        String password();
    }

    /**
     * @return connection parameters to container
     */
    @NotNull
    Params params();

    /**
     * @return connection parameters inside docker network, can be useful when one container require
     *             params to connect to Cassandra Database container inside docker network
     */
    @NotNull
    Optional paramsInNetwork();

    /**
     * NOTE: DO NOT CLOSE CONNECTION
     *
     * @return new Cassandra connection
     */
    @NotNull
    CqlSession get();

    /**
     * @param cql to execute
     */
    void execute(@NotNull @Language("CQL") String cql);

    /**
     * @param pathToResource path to CQL code to execute
     */
    void executeFromResources(@NotNull String pathToResource);

    /**
     * @param cql       to execute and retrieve results from
     * @param extractor map executed CQL query applied to each {@link ResultSet} after
     *                  {@link ResultSet#all()} is invoked
     * @param        mapped type
     * @param        error type
     * @return mapped entity if {@link ResultSet#all()} was true or {@link Optional#empty()} otherwise
     * @throws E of error
     */
     Optional queryOne(@NotNull @Language("CQL") String cql,
                                                  @NotNull CassandraConnection.RowMapper extractor)
            throws E;

    /**
     * @param cql       to execute and retrieve results from
     * @param extractor map executed CQL query applied to each {@link ResultSet} after
     *                  {@link ResultSet#all()} is invoked
     * @param        mapped type
     * @param        error type
     * @return mapped list of entities if {@link ResultSet#all()} found any or {@link Optional#empty()}
     *             otherwise
     * @throws E of error
     */
     List queryMany(@NotNull @Language("CQL") String cql,
                                               @NotNull CassandraConnection.RowMapper extractor)
            throws E;

    /**
     * @param table example: mykeyspace.mytable
     * @return SELECT COUNT(*) from specified table
     */
    long count(@NotNull String table);

    /**
     * Asserts that SELECT COUNT(*) in specified table counts 0 rows
     *
     * @param table example: mykeyspace.mytable
     */
    void assertCountsNone(@NotNull String table);

    /**
     * Asserts that SELECT COUNT(*) in specified table counts at least minimal number expectedAtLeast
     * rows
     *
     * @param table           example: mykeyspace.mytable
     * @param expectedAtLeast at least minimal number of rows expected
     */
    void assertCountsAtLeast(long expectedAtLeast, @NotNull String table);

    /**
     * Asserts that SELECT COUNT(*) in specified table counts exact number expected rows
     *
     * @param table    example: mykeyspace.mytable
     * @param expected exact number of rows expected
     */
    void assertCountsEquals(long expected, @NotNull String table);

    /**
     * Asserts that executed CQL results in 0 rows
     *
     * @param cql to execute
     */
    void assertQueriesNone(@NotNull @Language("CQL") String cql);

    /**
     * Asserts that executed CQL results in at least minimal number of expectedAtLeast rows
     *
     * @param cql             to execute
     * @param expectedAtLeast at least minimal number of rows expected
     */
    void assertQueriesAtLeast(int expectedAtLeast, @NotNull @Language("CQL") String cql);

    /**
     * Asserts that executed CQL results in exact number of expected rows
     *
     * @param cql      to execute
     * @param expected exact number of rows expected
     */
    void assertQueriesEquals(int expected, @NotNull @Language("CQL") String cql);

    /**
     * @param cql to execute
     * @return true if executed CQL results in 0 rows
     */
    boolean checkQueriesNone(@NotNull @Language("CQL") String cql);

    /**
     * @param cql             to execute
     * @param expectedAtLeast at least minimal number of rows expected
     * @return true if executed CQL results in at least minimal number expectedAtLeast rows
     */
    boolean checkQueriesAtLeast(int expectedAtLeast, @NotNull @Language("CQL") String cql);

    /**
     * @param cql      to execute
     * @param expected exact number of rows expected
     * @return true if executed CQL results in exact number of expected rows
     */
    boolean checkQueriesEquals(int expected, @NotNull @Language("CQL") String cql);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy