Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move IndexCreatingLoadStrategyFactory out of SearchesTest
- Loading branch information
Jochen Schalanda
committed
Jun 30, 2015
1 parent
a33856b
commit c0744f4
Showing
4 changed files
with
91 additions
and
80 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
61 changes: 61 additions & 0 deletions
61
...2-server/src/test/java/org/graylog2/indexer/nosqlunit/IndexCreatingDatabaseOperation.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,61 @@ | ||
package org.graylog2.indexer.nosqlunit; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.lordofthejars.nosqlunit.core.DatabaseOperation; | ||
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.client.IndicesAdminClient; | ||
import org.graylog2.configuration.ElasticsearchConfiguration; | ||
import org.graylog2.indexer.IndexMapping; | ||
import org.graylog2.indexer.indices.Indices; | ||
|
||
import java.io.InputStream; | ||
import java.util.Set; | ||
|
||
public class IndexCreatingDatabaseOperation implements DatabaseOperation<Client> { | ||
private final DatabaseOperation<Client> databaseOperation; | ||
private final Client client; | ||
private final Set<String> indexes; | ||
|
||
public IndexCreatingDatabaseOperation(DatabaseOperation<Client> databaseOperation, Set<String> indexes) { | ||
this.databaseOperation = databaseOperation; | ||
this.client = databaseOperation.connectionManager(); | ||
this.indexes = ImmutableSet.copyOf(indexes); | ||
} | ||
|
||
@Override | ||
public void insert(InputStream dataScript) { | ||
final IndicesAdminClient indicesAdminClient = client.admin().indices(); | ||
for (String index : indexes) { | ||
IndicesExistsResponse indicesExistsResponse = indicesAdminClient.prepareExists(index) | ||
.execute() | ||
.actionGet(); | ||
|
||
if (indicesExistsResponse.isExists()) { | ||
client.admin().indices().prepareDelete(index).execute().actionGet(); | ||
} | ||
|
||
Indices indices = new Indices(client, new ElasticsearchConfiguration(), new IndexMapping(client)); | ||
if (!indices.create(index)) { | ||
throw new IllegalStateException("Couldn't create index " + index); | ||
} | ||
} | ||
|
||
databaseOperation.insert(dataScript); | ||
} | ||
|
||
@Override | ||
public void deleteAll() { | ||
databaseOperation.deleteAll(); | ||
} | ||
|
||
@Override | ||
public boolean databaseIs(InputStream expectedData) { | ||
return databaseOperation.databaseIs(expectedData); | ||
} | ||
|
||
@Override | ||
public Client connectionManager() { | ||
return databaseOperation.connectionManager(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...server/src/test/java/org/graylog2/indexer/nosqlunit/IndexCreatingLoadStrategyFactory.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,27 @@ | ||
package org.graylog2.indexer.nosqlunit; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.lordofthejars.nosqlunit.core.DatabaseOperation; | ||
import com.lordofthejars.nosqlunit.core.LoadStrategyEnum; | ||
import com.lordofthejars.nosqlunit.core.LoadStrategyFactory; | ||
import com.lordofthejars.nosqlunit.core.LoadStrategyOperation; | ||
import com.lordofthejars.nosqlunit.core.ReflectionLoadStrategyFactory; | ||
|
||
import java.util.Set; | ||
|
||
public class IndexCreatingLoadStrategyFactory implements LoadStrategyFactory { | ||
private final LoadStrategyFactory loadStrategyFactory; | ||
private final Set<String> indexNames; | ||
|
||
public IndexCreatingLoadStrategyFactory(Set<String> indexNames) { | ||
this.loadStrategyFactory = new ReflectionLoadStrategyFactory(); | ||
this.indexNames = ImmutableSet.copyOf(indexNames); | ||
} | ||
|
||
@Override | ||
public LoadStrategyOperation getLoadStrategyInstance(LoadStrategyEnum loadStrategyEnum, DatabaseOperation databaseOperation) { | ||
return loadStrategyFactory.getLoadStrategyInstance( | ||
loadStrategyEnum, | ||
new IndexCreatingDatabaseOperation(databaseOperation, indexNames)); | ||
} | ||
} |
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