elassandra-unit-parent

Test framework to develop with Cassandra

License

License

Categories

Categories

Data
GroupId

GroupId

com.strapdata.elassandraunit
ArtifactId

ArtifactId

elassandra-unit-parent
Last Version

Last Version

3.7.1.4
Release Date

Release Date

Type

Type

pom
Description

Description

elassandra-unit-parent
Test framework to develop with Cassandra
Project URL

Project URL

https://github.com/strapdata/elassandra-unit
Source Code Management

Source Code Management

https://github.com/strapdata/elassandra-unit.git

Download elassandra-unit-parent

How to add to project

<!-- https://jarcasting.com/artifacts/com.strapdata.elassandraunit/elassandra-unit-parent/ -->
<dependency>
    <groupId>com.strapdata.elassandraunit</groupId>
    <artifactId>elassandra-unit-parent</artifactId>
    <version>3.7.1.4</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.strapdata.elassandraunit/elassandra-unit-parent/
implementation 'com.strapdata.elassandraunit:elassandra-unit-parent:3.7.1.4'
// https://jarcasting.com/artifacts/com.strapdata.elassandraunit/elassandra-unit-parent/
implementation ("com.strapdata.elassandraunit:elassandra-unit-parent:3.7.1.4")
'com.strapdata.elassandraunit:elassandra-unit-parent:pom:3.7.1.4'
<dependency org="com.strapdata.elassandraunit" name="elassandra-unit-parent" rev="3.7.1.4">
  <artifact name="elassandra-unit-parent" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.strapdata.elassandraunit', module='elassandra-unit-parent', version='3.7.1.4')
)
libraryDependencies += "com.strapdata.elassandraunit" % "elassandra-unit-parent" % "3.7.1.4"
[com.strapdata.elassandraunit/elassandra-unit-parent "3.7.1.4"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • cassandra-unit
  • cassandra-unit-shaded
  • cassandra-unit-spring

git statu# ElassandraUnit

ElassandraUnit is a fork form CassandraUnit modified for Elassandra.

ElassandraUnit helps you writing isolated JUnit tests in a Test Driven Development style with an embedded Elassandra instance.

Elassandra Unit Figure

  • Start an embedded Elassandra (including both Cassandra and Elasticsearch).
  • Create structure (keyspace and Column Families) and load data from an XML, JSON or YAML DataSet.
  • Execute a CQL script.
  • Query Cassandra through the Cassandra driver
  • Query Elasticsearch through the Elasticsearch REST API.

Quick start

Add the following dependencies:

    <dependency>
        <groupId>com.strapdata.elassandraunit</groupId>
        <artifactId>elassandra-unit-spring</artifactId>
        <version>${elassandra-unit.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.strapdata.elassandraunit</groupId>
                <artifactId>elassandra-unit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.strapdata.elassandraunit</groupId>
        <artifactId>elassandra-unit</artifactId>
        <classifier>shaded</classifier>
        <exclusions>
            <exclusion>
                <artifactId>httpclient</artifactId>
                <groupId>org.apache.httpcomponents</groupId>
            </exclusion>
            <exclusion>
                <artifactId>httpcore</artifactId>
                <groupId>org.apache.httpcomponents</groupId>
            </exclusion>
        </exclusions>
        <version>${elassandra-unit.version}</version>
        <scope>test</scope>
    </dependency>

Add the Elasticsearch REST high level client with the same version as the one embedded in elassandra.

    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-high-level-client</artifactId>
      <version>6.2.3</version>
    </dependency>

To handle Elasticsearch search requests over CQL, set the system property cassandra.custom_query_handler_class to org.elassandra.index.ElasticQueryHandler:

    ...
    <build>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <systemPropertyVariables>
                    <cassandra.custom_query_handler_class>org.elassandra.index.ElasticQueryHandler</cassandra.custom_query_handler_class>
                </systemPropertyVariables>
            </configuration>
            </plugin>
        </plugins>
    </build>

Create a JUnit test class :

public class SimpleTest {

    static final String KEYSPACE = "ks";

    @ClassRule
    public static CassandraCQLUnit cassandraCQLUnit =
        new CassandraCQLUnit(new SimpleCQLDataSet(
            "CREATE TABLE users (email text PRIMARY KEY, firstname text, lastname text, es_query text, es_options text);", KEYSPACE));

    private static Mapper<User> userMapper;
    private static RestHighLevelClient client;

    @Before
    public void setup() throws IOException {
        userMapper = new MappingManager(cassandraCQLUnit.session).mapper(User.class);
        client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        // create an elasticsearch index on table users
        CreateIndexRequest request = new CreateIndexRequest("users");
        request.mapping("users", XContentFactory.jsonBuilder()
            .startObject()
                .startObject("users")
                    .field("discover", ".*")
                .endObject()
            .endObject());
        request.settings(Settings.builder()
            .put("keyspace", KEYSPACE)                          // map index users to our keyspace.
            .put("index.synchronous_refresh",true)// synchronous elasticsearch refresh
            .build());
        CreateIndexResponse createIndexResponse = client.indices().create(request);
    }

    @Test
    public void testMapper() throws Exception {
        User user1 = new User().withEmail("[email protected]").withFirstname("Bob").withLastname("Smith");
        User user2 = new User().withEmail("[email protected]").withFirstname("Alice").withLastname("Smith");
        User user3 = new User().withEmail("[email protected]").withFirstname("Paul").withLastname("Dupont");
        userMapper.save(user1);
        userMapper.save(user2);
        userMapper.save(user3);

        User user = userMapper.get("[email protected]");
        assertThat(user, is(user1));

        // Elasticsearch search through the REST API
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.termQuery("lastname", "Smith"));
        SearchRequest searchRequest = new SearchRequest().indices("users").source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest);
        assertThat(searchResponse.getHits().totalHits, is(2L));

        // Elasticsearch search through CQL
        String esQuery = new SearchSourceBuilder().query(QueryBuilders.termQuery("lastname", "Smith")).toString(ToXContent.EMPTY_PARAMS);
        ResultSet results = cassandraCQLUnit.session.execute(
            "SELECT * FROM users WHERE es_options = ? AND es_query = ? ALLOW FILTERING", "indices=users", esQuery);
        Result<User> users = userMapper.map(results);
        assertThat(users.all().size(), is(2));
    }
}

Resources

Support

License

This project is licensed under LGPL V3.0

Acknowledgments

  • CassandraUnit is developped by Jérémy Sevellec
  • Elasticsearch and Kibana are trademarks of Elasticsearch BV, registered in the U.S. and in other countries.
  • Apache Cassandra, Apache Lucene, Apache, Lucene and Cassandra are trademarks of the Apache Software Foundation.
  • Elassandra is a trademark of Strapdata SAS, registered in the U.S. and in other countries.
com.strapdata.elassandraunit

Strapdata

Strapdata simplify your data stack with Elassandra = Elasticsearch + Cassandra

Versions

Version
3.7.1.4
3.7.1.3
3.7.1.2
3.7.1.1
3.7.1.0
3.5.0.5
3.5.0.4
3.5.0.3
3.5.0.2
3.5.0.1