Skip to content

Commit d48070a

Browse files
committed
feat(elasticsearch6): add support for Elasticsearch 6.x.x
Closes lordofthejars#191
1 parent 950c5ca commit d48070a

29 files changed

+2099
-45
lines changed

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: java
2+
3+
services:
4+
- docker
5+
6+
before_install:
7+
- docker --version
8+
- docker-compose --version
9+
- docker-compose up -d
10+
- docker-compose run wait
11+
- docker-compose run mvn -B -Pintregration-tests clean install
12+
13+
script:
14+
- mvn -B -Pintegration-tests clean install
15+
16+

docker-compose.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
version: '2.2'
2+
services:
3+
nosqlunit-elasticsearch6.elasticsearch:
4+
image: docker.elastic.co/elasticsearch/elasticsearch:6.7.1
5+
aliases:
6+
- elasticsearch6
7+
environment:
8+
- discovery.type=single-node
9+
#- cluster.name=docker-cluster
10+
- bootstrap.memory_lock=true
11+
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
12+
ulimits:
13+
memlock:
14+
soft: -1
15+
hard: -1
16+
#volumes:
17+
# - esdata1:/usr/share/elasticsearch/data
18+
ports:
19+
- 9200:9200
20+
- 9300:9300
21+
networks:
22+
- nosqlunit-net
23+
nosqlunit.maven:
24+
image: maven:3.6.0-jdk-8-alpine
25+
aliases:
26+
- maven
27+
stop_signal: SIGKILL
28+
stdin_open: true
29+
tty: true
30+
working_dir: $PWD
31+
volumes:
32+
- $PWD:/usr/src/nosqlunit
33+
- /var/run/docker.sock:/var/run/docker.sock
34+
# Maven cache (optional)
35+
- ~/.m2:/root/.m2
36+
networks:
37+
- nosqlunit-net
38+
#command: mvn -B -Pintegration-tests clean install
39+
nosqlunit.wait:
40+
image: waisbrot/wait
41+
aliases:
42+
- wait
43+
depends_on:
44+
- nosqlunit-elasticsearch6.maven
45+
- nosqlunit-elasticsearch6.elasticsearch
46+
environment:
47+
- TARGETS=elasticsearch6:9200
48+
networks:
49+
- nosqlunit-net
50+
networks:
51+
nosqlunit-net:

nosqlunit-demo/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<plugin>
1515
<groupId>org.apache.maven.plugins</groupId>
1616
<artifactId>maven-deploy-plugin</artifactId>
17+
<version>2.8.1</version>
1718
<configuration>
1819
<skip>true</skip>
1920
</configuration>

nosqlunit-elasticsearch6/pom.xml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>com.lordofthejars</groupId>
5+
<artifactId>nosqlunit</artifactId>
6+
<version>1.0.1-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>nosqlunit-elasticsearch6</artifactId>
9+
10+
<dependencies>
11+
<dependency>
12+
<artifactId>nosqlunit-core</artifactId>
13+
<groupId>com.lordofthejars</groupId>
14+
<version>${project.version}</version>
15+
</dependency>
16+
17+
<dependency>
18+
<groupId>org.elasticsearch.client</groupId>
19+
<artifactId>transport</artifactId>
20+
<version>${elasticsearch6.version}</version>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>2.4</version>
30+
<configuration>
31+
<source>1.8</source>
32+
<target>1.8</target>
33+
</configuration>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import com.lordofthejars.nosqlunit.core.NoSqlAssertionError;
4+
import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;
5+
6+
import org.elasticsearch.client.Client;
7+
8+
import java.io.InputStream;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class DefaultElasticsearchComparisonStrategy implements ElasticsearchComparisonStrategy {
13+
@Override
14+
public boolean compare(ElasticsearchConnectionCallback connection, InputStream dataset) throws NoSqlAssertionError,
15+
Throwable {
16+
final Client nodeClient = connection.nodeClient();
17+
final List<Map<String, Object>> documents = DataReader.getDocuments(dataset);
18+
ElasticsearchAssertion.strictAssertEquals(documents, nodeClient);
19+
return true;
20+
}
21+
22+
@Override
23+
public void setIgnoreProperties(String[] ignoreProperties) {
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import java.io.InputStream;
4+
5+
import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;
6+
7+
public class DefaultElasticsearchInsertionStrategy implements ElasticsearchInsertionStrategy {
8+
@Override
9+
public void insert(ElasticsearchConnectionCallback connection, InputStream dataset) throws Throwable {
10+
DataReader dataReader = new DataReader(connection.nodeClient());
11+
dataReader.read(dataset);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.LinkedHashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.codehaus.jackson.map.ObjectMapper;
11+
import org.elasticsearch.action.get.GetRequestBuilder;
12+
import org.elasticsearch.action.get.GetResponse;
13+
import org.elasticsearch.action.search.SearchResponse;
14+
import org.elasticsearch.client.Client;
15+
import org.elasticsearch.search.builder.SearchSourceBuilder;
16+
17+
import com.lordofthejars.nosqlunit.core.FailureHandler;
18+
import com.lordofthejars.nosqlunit.elasticsearch6.parser.DataReader;
19+
import com.lordofthejars.nosqlunit.util.DeepEquals;
20+
21+
public class ElasticsearchAssertion {
22+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
23+
24+
private ElasticsearchAssertion() {
25+
super();
26+
}
27+
28+
public static void strictAssertEquals(final List<Map<String, Object>> expectedDocuments, final Client client) {
29+
30+
checkNumberOfDocuments(expectedDocuments, client);
31+
32+
for (Map<String, Object> document : expectedDocuments) {
33+
final Object object = document.get(DataReader.DOCUMENT_ELEMENT);
34+
35+
if (object instanceof List) {
36+
@SuppressWarnings("unchecked")
37+
final List<Map<String, Object>> properties = (List<Map<String, Object>>) object;
38+
39+
final List<GetRequestBuilder> indexes = new ArrayList<>();
40+
Map<String, Object> expectedDataOfDocument = new HashMap<>();
41+
42+
for (Map<String, Object> property : properties) {
43+
44+
if (property.containsKey(DataReader.INDEX_ELEMENT)) {
45+
indexes.add(prepareGetIndex(property.get(DataReader.INDEX_ELEMENT), client));
46+
} else {
47+
if (property.containsKey(DataReader.DATA_ELEMENT)) {
48+
expectedDataOfDocument = dataOfDocument(property.get(DataReader.DATA_ELEMENT));
49+
}
50+
}
51+
52+
}
53+
54+
checkIndicesWithDocument(indexes, expectedDataOfDocument);
55+
56+
} else {
57+
throw new IllegalArgumentException("Array of Indexes and Data are required.");
58+
}
59+
}
60+
}
61+
62+
private static void checkIndicesWithDocument(final List<GetRequestBuilder> indexes,
63+
final Map<String, Object> expectedDataOfDocument) {
64+
for (GetRequestBuilder getRequestBuilder : indexes) {
65+
66+
GetResponse dataOfDocumentResponse = getRequestBuilder.execute().actionGet();
67+
68+
checkExistenceOfDocument(getRequestBuilder, dataOfDocumentResponse);
69+
checkDocumentEquality(expectedDataOfDocument, getRequestBuilder, dataOfDocumentResponse);
70+
71+
}
72+
}
73+
74+
private static void checkDocumentEquality(final Map<String, Object> expectedDataOfDocument,
75+
final GetRequestBuilder getRequestBuilder, final GetResponse dataOfDocumentResponse) {
76+
final Map<String, Object> dataOfDocument = new LinkedHashMap<>(dataOfDocumentResponse.getSource());
77+
78+
if (!DeepEquals.deepEquals(dataOfDocument, expectedDataOfDocument)) {
79+
try {
80+
throw FailureHandler.createFailure(
81+
"Expected document for index: %s - type: %s - id: %s is %s, but %s was found.",
82+
getRequestBuilder.request().index(), getRequestBuilder.request().type(),
83+
getRequestBuilder.request().id(), OBJECT_MAPPER.writeValueAsString(expectedDataOfDocument),
84+
OBJECT_MAPPER.writeValueAsString(dataOfDocument));
85+
} catch (IOException e) {
86+
throw new IllegalArgumentException(e);
87+
}
88+
}
89+
}
90+
91+
private static void checkExistenceOfDocument(final GetRequestBuilder getRequestBuilder,
92+
final GetResponse dataOfDocumentResponse) {
93+
if (!dataOfDocumentResponse.isExists()) {
94+
throw FailureHandler.createFailure(
95+
"Document with index: %s - type: %s - id: %s has not returned any document.",
96+
getRequestBuilder.request().index(), getRequestBuilder.request().type(),
97+
getRequestBuilder.request().id());
98+
}
99+
}
100+
101+
private static void checkNumberOfDocuments(final List<Map<String, Object>> expectedDocuments, final Client client) {
102+
int expectedNumberOfElements = expectedDocuments.size();
103+
104+
long numberOfInsertedDocuments = numberOfInsertedDocuments(client);
105+
106+
if (expectedNumberOfElements != numberOfInsertedDocuments) {
107+
throw FailureHandler.createFailure("Expected number of documents are %s but %s has been found.",
108+
expectedNumberOfElements, numberOfInsertedDocuments);
109+
}
110+
}
111+
112+
private static GetRequestBuilder prepareGetIndex(final Object object, final Client client) {
113+
@SuppressWarnings("unchecked")
114+
Map<String, String> indexInformation = (Map<String, String>) object;
115+
116+
GetRequestBuilder prepareGet = client.prepareGet();
117+
118+
if (indexInformation.containsKey(DataReader.INDEX_NAME_ELEMENT)) {
119+
prepareGet.setIndex(indexInformation.get(DataReader.INDEX_NAME_ELEMENT));
120+
}
121+
122+
if (indexInformation.containsKey(DataReader.INDEX_TYPE_ELEMENT)) {
123+
prepareGet.setType(indexInformation.get(DataReader.INDEX_TYPE_ELEMENT));
124+
}
125+
126+
if (indexInformation.containsKey(DataReader.INDEX_ID_ELEMENT)) {
127+
prepareGet.setId(indexInformation.get(DataReader.INDEX_ID_ELEMENT));
128+
}
129+
130+
return prepareGet;
131+
}
132+
133+
@SuppressWarnings("unchecked")
134+
private static Map<String, Object> dataOfDocument(final Object object) {
135+
return (Map<String, Object>) object;
136+
}
137+
138+
private static long numberOfInsertedDocuments(final Client client) {
139+
SearchResponse response = client.prepareSearch().setSource(new SearchSourceBuilder().size(0)).get();
140+
return response.getHits().totalHits;
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import com.lordofthejars.nosqlunit.core.ComparisonStrategy;
4+
5+
public interface ElasticsearchComparisonStrategy extends ComparisonStrategy<ElasticsearchConnectionCallback> {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import com.lordofthejars.nosqlunit.core.AbstractJsr330Configuration;
4+
import org.elasticsearch.client.Client;
5+
import org.elasticsearch.common.settings.Settings;
6+
7+
public class ElasticsearchConfiguration extends AbstractJsr330Configuration {
8+
private static final String LOCALHOST = "localhost";
9+
private static final int DEFAULT_PORT = 9300;
10+
11+
private String host = LOCALHOST;
12+
private int port = DEFAULT_PORT;
13+
private Settings settings = null;
14+
15+
private Client client;
16+
17+
public void setClient(Client client) {
18+
this.client = client;
19+
}
20+
21+
public Client getClient() {
22+
return client;
23+
}
24+
25+
public void setPort(int port) {
26+
this.port = port;
27+
}
28+
29+
public int getPort() {
30+
return port;
31+
}
32+
33+
public void setSettings(Settings settings) {
34+
this.settings = settings;
35+
}
36+
37+
public Settings getSettings() {
38+
return settings;
39+
}
40+
41+
public void setHost(String host) {
42+
this.host = host;
43+
}
44+
45+
public String getHost() {
46+
return host;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import org.elasticsearch.client.Client;
4+
5+
public interface ElasticsearchConnectionCallback {
6+
Client nodeClient();
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.lordofthejars.nosqlunit.elasticsearch6;
2+
3+
import com.lordofthejars.nosqlunit.core.InsertionStrategy;
4+
5+
public interface ElasticsearchInsertionStrategy extends InsertionStrategy<ElasticsearchConnectionCallback> {
6+
}

0 commit comments

Comments
 (0)