-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: finish off implementation ElasticsearchIntegrationTestExtension (…
…#41)
- Loading branch information
John Plaisted
authored
Nov 12, 2020
1 parent
57b8103
commit 3ad0855
Showing
3 changed files
with
219 additions
and
11 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
50 changes: 50 additions & 0 deletions
50
...rch-dao-integ-testing/src/main/java/com/linkedin/metadata/testing/SearchIndexFactory.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,50 @@ | ||
package com.linkedin.metadata.testing; | ||
|
||
import com.linkedin.data.template.RecordTemplate; | ||
import java.util.concurrent.ExecutionException; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
|
||
|
||
/** | ||
* Factory to create {@link SearchIndex} instances for testing. | ||
*/ | ||
final class SearchIndexFactory { | ||
private final ElasticsearchConnection _connection; | ||
|
||
SearchIndexFactory(@Nonnull ElasticsearchConnection connection) { | ||
_connection = connection; | ||
} | ||
|
||
/** | ||
* Creates a search index to read / write the given document type for testing. | ||
* | ||
* <p>This will create an index on the Elasticsearch instance with a unique name. | ||
* | ||
* @param documentClass the document type | ||
* @param name the name to use for the index | ||
*/ | ||
public <DOCUMENT extends RecordTemplate> SearchIndex<DOCUMENT> createIndex(@Nonnull Class<DOCUMENT> documentClass, | ||
@Nonnull String name, @Nullable String settingsJson, @Nullable String mappingsJson) { | ||
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(name); | ||
|
||
if (settingsJson != null) { | ||
createIndexRequest.settings(settingsJson, XContentType.JSON); | ||
} | ||
|
||
if (mappingsJson != null) { | ||
// TODO | ||
createIndexRequest.mapping("doc", mappingsJson, XContentType.JSON); | ||
} | ||
|
||
try { | ||
_connection.getTransportClient().admin().indices().create(createIndexRequest).get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return new SearchIndex<>(documentClass, _connection, name); | ||
} | ||
} |
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