com.strapdata.elassandraunit:elassandra-unit

Test framework to develop with Elassandra

License

License

Categories

Categories

Data
GroupId

GroupId

com.strapdata.elassandraunit
ArtifactId

ArtifactId

elassandra-unit
Last Version

Last Version

3.7.1.4
Release Date

Release Date

Type

Type

jar
Description

Description

Test framework to develop with Elassandra

Download elassandra-unit

How to add to project

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

Dependencies

compile (11)

Group / Artifact Type Version
junit : junit jar 4.12
com.strapdata.elasticsearch : elasticsearch jar 6.8.4.4
com.strapdata.elasticsearch.plugin » transport-netty4-client jar 6.8.4.4
com.google.guava : guava jar 21.0
org.apache.commons : commons-lang3 jar 3.4
org.apache.thrift : libthrift jar 0.9.2
net.java.dev.jna : jna jar 4.1.0
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
com.datastax.cassandra : cassandra-driver-core Optional jar 3.7.1
org.slf4j : slf4j-api jar 1.7.12

test (5)

Group / Artifact Type Version
org.mockito : mockito-core jar 1.10.19
commons-io : commons-io jar 2.4
org.slf4j : slf4j-log4j12 jar 1.7.12
org.slf4j : jcl-over-slf4j jar 1.7.12
log4j : log4j jar 1.2.17

Project Modules

There are no modules declared in this project.

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