Skip to content

Commit

Permalink
feat(elasticsearch6): add support for Elasticsearch 6.x.x
Browse files Browse the repository at this point in the history
  • Loading branch information
tinesoft committed Apr 16, 2019
1 parent 950c5ca commit 983f4ec
Show file tree
Hide file tree
Showing 29 changed files with 2,101 additions and 45 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: java

services:
- docker

before_install:
- docker --version
- docker-compose --version
- docker-compose up -d
- docker-compose run wait
- docker-compose run mvn -B -Pintregration-tests clean install

script:
- mvn -B -Pintegration-tests clean install


53 changes: 53 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: '2.2'
services:
nosqlunit-elasticsearch6.elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.7.1
environment:
- discovery.type=single-node
#- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
#volumes:
# - esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
networks:
nosqlunit-net:
aliases:
- elasticsearch6
nosqlunit.maven:
image: maven:3.6.0-jdk-8-alpine
aliases:
- maven
stop_signal: SIGKILL
stdin_open: true
tty: true
working_dir: $PWD
volumes:
- $PWD:/usr/src/nosqlunit
- /var/run/docker.sock:/var/run/docker.sock
# Maven cache (optional)
- ~/.m2:/root/.m2
networks:
nosqlunit-net:
aliases:
- maven
#command: mvn -B -Pintegration-tests clean install
nosqlunit.wait:
image: waisbrot/wait
depends_on:
- nosqlunit-elasticsearch6.maven
- nosqlunit-elasticsearch6.elasticsearch
environment:
- TARGETS=elasticsearch6:9200
networks:
nosqlunit-net:
aliases:
- wait
networks:
nosqlunit-net:
1 change: 1 addition & 0 deletions nosqlunit-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<skip>true</skip>
</configuration>
Expand Down
38 changes: 38 additions & 0 deletions nosqlunit-elasticsearch6/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lordofthejars</groupId>
<artifactId>nosqlunit</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>nosqlunit-elasticsearch6</artifactId>

<dependencies>
<dependency>
<artifactId>nosqlunit-core</artifactId>
<groupId>com.lordofthejars</groupId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch6.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import com.lordofthejars.nosqlunit.core.NoSqlAssertionError;
import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;

import org.elasticsearch.client.Client;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class DefaultElasticsearchComparisonStrategy implements ElasticsearchComparisonStrategy {
@Override
public boolean compare(ElasticsearchConnectionCallback connection, InputStream dataset) throws NoSqlAssertionError,
Throwable {
final Client nodeClient = connection.nodeClient();
final List<Map<String, Object>> documents = DataReader.getDocuments(dataset);
ElasticsearchAssertion.strictAssertEquals(documents, nodeClient);
return true;
}

@Override
public void setIgnoreProperties(String[] ignoreProperties) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import java.io.InputStream;

import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;

public class DefaultElasticsearchInsertionStrategy implements ElasticsearchInsertionStrategy {
@Override
public void insert(ElasticsearchConnectionCallback connection, InputStream dataset) throws Throwable {
DataReader dataReader = new DataReader(connection.nodeClient());
dataReader.read(dataset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import com.lordofthejars.nosqlunit.core.FailureHandler;
import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;
import com.lordofthejars.nosqlunit.util.DeepEquals;

public class ElasticsearchAssertion {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private ElasticsearchAssertion() {
super();
}

public static void strictAssertEquals(final List<Map<String, Object>> expectedDocuments, final Client client) {

checkNumberOfDocuments(expectedDocuments, client);

for (Map<String, Object> document : expectedDocuments) {
final Object object = document.get(DataReader.DOCUMENT_ELEMENT);

if (object instanceof List) {
@SuppressWarnings("unchecked")
final List<Map<String, Object>> properties = (List<Map<String, Object>>) object;

final List<GetRequestBuilder> indexes = new ArrayList<>();
Map<String, Object> expectedDataOfDocument = new HashMap<>();

for (Map<String, Object> property : properties) {

if (property.containsKey(DataReader.INDEX_ELEMENT)) {
indexes.add(prepareGetIndex(property.get(DataReader.INDEX_ELEMENT), client));
} else {
if (property.containsKey(DataReader.DATA_ELEMENT)) {
expectedDataOfDocument = dataOfDocument(property.get(DataReader.DATA_ELEMENT));
}
}

}

checkIndicesWithDocument(indexes, expectedDataOfDocument);

} else {
throw new IllegalArgumentException("Array of Indexes and Data are required.");
}
}
}

private static void checkIndicesWithDocument(final List<GetRequestBuilder> indexes,
final Map<String, Object> expectedDataOfDocument) {
for (GetRequestBuilder getRequestBuilder : indexes) {

GetResponse dataOfDocumentResponse = getRequestBuilder.execute().actionGet();

checkExistenceOfDocument(getRequestBuilder, dataOfDocumentResponse);
checkDocumentEquality(expectedDataOfDocument, getRequestBuilder, dataOfDocumentResponse);

}
}

private static void checkDocumentEquality(final Map<String, Object> expectedDataOfDocument,
final GetRequestBuilder getRequestBuilder, final GetResponse dataOfDocumentResponse) {
final Map<String, Object> dataOfDocument = new LinkedHashMap<>(dataOfDocumentResponse.getSource());

if (!DeepEquals.deepEquals(dataOfDocument, expectedDataOfDocument)) {
try {
throw FailureHandler.createFailure(
"Expected document for index: %s - type: %s - id: %s is %s, but %s was found.",
getRequestBuilder.request().index(), getRequestBuilder.request().type(),
getRequestBuilder.request().id(), OBJECT_MAPPER.writeValueAsString(expectedDataOfDocument),
OBJECT_MAPPER.writeValueAsString(dataOfDocument));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}

private static void checkExistenceOfDocument(final GetRequestBuilder getRequestBuilder,
final GetResponse dataOfDocumentResponse) {
if (!dataOfDocumentResponse.isExists()) {
throw FailureHandler.createFailure(
"Document with index: %s - type: %s - id: %s has not returned any document.",
getRequestBuilder.request().index(), getRequestBuilder.request().type(),
getRequestBuilder.request().id());
}
}

private static void checkNumberOfDocuments(final List<Map<String, Object>> expectedDocuments, final Client client) {
int expectedNumberOfElements = expectedDocuments.size();

long numberOfInsertedDocuments = numberOfInsertedDocuments(client);

if (expectedNumberOfElements != numberOfInsertedDocuments) {
throw FailureHandler.createFailure("Expected number of documents are %s but %s has been found.",
expectedNumberOfElements, numberOfInsertedDocuments);
}
}

private static GetRequestBuilder prepareGetIndex(final Object object, final Client client) {
@SuppressWarnings("unchecked")
Map<String, String> indexInformation = (Map<String, String>) object;

GetRequestBuilder prepareGet = client.prepareGet();

if (indexInformation.containsKey(DataReader.INDEX_NAME_ELEMENT)) {
prepareGet.setIndex(indexInformation.get(DataReader.INDEX_NAME_ELEMENT));
}

if (indexInformation.containsKey(DataReader.INDEX_TYPE_ELEMENT)) {
prepareGet.setType(indexInformation.get(DataReader.INDEX_TYPE_ELEMENT));
}

if (indexInformation.containsKey(DataReader.INDEX_ID_ELEMENT)) {
prepareGet.setId(indexInformation.get(DataReader.INDEX_ID_ELEMENT));
}

return prepareGet;
}

@SuppressWarnings("unchecked")
private static Map<String, Object> dataOfDocument(final Object object) {
return (Map<String, Object>) object;
}

private static long numberOfInsertedDocuments(final Client client) {
SearchResponse response = client.prepareSearch().setSource(new SearchSourceBuilder().size(0)).get();
return response.getHits().totalHits;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import com.lordofthejars.nosqlunit.core.ComparisonStrategy;

public interface ElasticsearchComparisonStrategy extends ComparisonStrategy<ElasticsearchConnectionCallback> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import com.lordofthejars.nosqlunit.core.AbstractJsr330Configuration;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;

public class ElasticsearchConfiguration extends AbstractJsr330Configuration {
private static final String LOCALHOST = "localhost";
private static final int DEFAULT_PORT = 9300;

private String host = LOCALHOST;
private int port = DEFAULT_PORT;
private Settings settings = null;

private Client client;

public void setClient(Client client) {
this.client = client;
}

public Client getClient() {
return client;
}

public void setPort(int port) {
this.port = port;
}

public int getPort() {
return port;
}

public void setSettings(Settings settings) {
this.settings = settings;
}

public Settings getSettings() {
return settings;
}

public void setHost(String host) {
this.host = host;
}

public String getHost() {
return host;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import org.elasticsearch.client.Client;

public interface ElasticsearchConnectionCallback {
Client nodeClient();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.lordofthejars.nosqlunit.elasticsearch6;

import com.lordofthejars.nosqlunit.core.InsertionStrategy;

public interface ElasticsearchInsertionStrategy extends InsertionStrategy<ElasticsearchConnectionCallback> {
}
Loading

0 comments on commit 983f4ec

Please sign in to comment.