forked from lordofthejars/nosql-unit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elasticsearch6): add support for Elasticsearch 6.x.x
Closes lordofthejars#191
- Loading branch information
Showing
29 changed files
with
2,099 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 | ||
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
25 changes: 25 additions & 0 deletions
25
...va/com/lordofthejars/nosqlunit/elasticsearch6/DefaultElasticsearchComparisonStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ava/com/lordofthejars/nosqlunit/elasticsearch6/DefaultElasticsearchInsertionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
...rch6/src/main/java/com/lordofthejars/nosqlunit/elasticsearch6/ElasticsearchAssertion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...main/java/com/lordofthejars/nosqlunit/elasticsearch6/ElasticsearchComparisonStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
48 changes: 48 additions & 0 deletions
48
.../src/main/java/com/lordofthejars/nosqlunit/elasticsearch6/ElasticsearchConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...main/java/com/lordofthejars/nosqlunit/elasticsearch6/ElasticsearchConnectionCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
6 changes: 6 additions & 0 deletions
6
.../main/java/com/lordofthejars/nosqlunit/elasticsearch6/ElasticsearchInsertionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
Oops, something went wrong.