Skip to content

Commit

Permalink
HSEARCH-2219 Change the API of TestElasticsearchClient to make adding…
Browse files Browse the repository at this point in the history
… new methods easier

Each time we had to have three versions of each method due to the fact
the index name and mapping name could be provided either as a class or
as a string. Now this part of the job is independent.
  • Loading branch information
yrodiere authored and Sanne committed Dec 19, 2016
1 parent bedb514 commit 32565f5
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 167 deletions.
Expand Up @@ -144,8 +144,9 @@ public void testProjectionOnUnindexedClassBridgeField() {
@Test
public void testProjectionOnUnknownBridgeField() throws Exception {
// Add an additional field to the ES mapping, unknown to Hibernate Search
elasticsearchClient.putMapping( "golfplayer", GolfPlayer.class, "{'properties': {'fieldNotInMapping': {'type':'integer'}}}" );
elasticsearchClient.index( "golfplayer", GolfPlayer.class, "9999", "{'id':9999,'fieldNotInMapping':42}" );
elasticsearchClient.index( "golfplayer" ).mapping( GolfPlayer.class )
.put( "{'properties': {'fieldNotInMapping': {'type':'integer'}}}" )
.index( "9999", "{'id':9999,'fieldNotInMapping':42}" );

Session s = openSession();
FullTextSession session = Search.getFullTextSession( s );
Expand Down
Expand Up @@ -67,7 +67,7 @@ public void indexMissing() throws Exception {
+ " No point running this test.",
createsIndex( strategy ) );

elasticSearchClient.ensureIndexDoesNotExist( SimpleEntity.class );
elasticSearchClient.index( SimpleEntity.class ).ensureDoesNotExist();

thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH400050" );
Expand All @@ -82,17 +82,17 @@ public void invalidIndexStatus_creatingIndex() throws Exception {
createsIndex( strategy ) );

// Make sure automatically created indexes will never be green
elasticSearchClient.createTemplate(
"yellow_index_because_not_enough_nodes_for_so_many_replicas",
"*",
/*
* The exact number of replicas we ask for doesn't matter much,
* since we're testing with only 1 node (the cluster can't replicate shards)
*/
JsonBuilder.object().addProperty( "number_of_replicas", 5 ).build()
elasticSearchClient.template( "yellow_index_because_not_enough_nodes_for_so_many_replicas" )
.create(
"*",
/*
* The exact number of replicas we ask for doesn't matter much,
* since we're testing with only 1 node (the cluster can't replicate shards)
*/
JsonBuilder.object().addProperty( "number_of_replicas", 5 ).build()
);

elasticSearchClient.ensureIndexDoesNotExist( SimpleEntity.class );
elasticSearchClient.index( SimpleEntity.class ).ensureDoesNotExist();

thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH400024" );
Expand All @@ -104,17 +104,17 @@ public void invalidIndexStatus_creatingIndex() throws Exception {
@Test
public void invalidIndexStatus_usingPreexistingIndex() throws Exception {
// Make sure automatically created indexes will never be green
elasticSearchClient.createTemplate(
"yellow_index_because_not_enough_nodes_for_so_many_replicas",
"*",
/*
* The exact number of replicas we ask for doesn't matter much,
* since we're testing with only 1 node (the cluster can't replicate shards)
*/
JsonBuilder.object().addProperty( "number_of_replicas", 5 ).build()
elasticSearchClient.template( "yellow_index_because_not_enough_nodes_for_so_many_replicas" )
.create(
"*",
/*
* The exact number of replicas we ask for doesn't matter much,
* since we're testing with only 1 node (the cluster can't replicate shards)
*/
JsonBuilder.object().addProperty( "number_of_replicas", 5 ).build()
);

elasticSearchClient.deleteAndCreateIndex( SimpleEntity.class );
elasticSearchClient.index( SimpleEntity.class ).deleteAndCreate();

thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH400024" );
Expand Down
Expand Up @@ -19,25 +19,24 @@
import org.hibernate.search.Search;
import org.hibernate.search.elasticsearch.ElasticsearchProjectionConstants;
import org.hibernate.search.elasticsearch.ElasticsearchQueries;
import org.hibernate.search.elasticsearch.client.impl.JestClient;
import org.hibernate.search.elasticsearch.testutil.JsonHelper;
import org.hibernate.search.elasticsearch.testutil.TestElasticsearchClient;
import org.hibernate.search.elasticsearch.util.impl.ElasticsearchDateHelper;
import org.hibernate.search.engine.service.spi.ServiceManager;
import org.hibernate.search.engine.service.spi.ServiceReference;
import org.hibernate.search.query.engine.spi.QueryDescriptor;
import org.hibernate.search.test.SearchTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import io.searchbox.client.JestResult;
import io.searchbox.indices.mapping.GetMapping;

/**
* @author Gunnar Morling
*/
public class ElasticsearchIndexMappingIT extends SearchTestBase {

@Rule
public TestElasticsearchClient elasticsearchClient = new TestElasticsearchClient();

@Before
public void setupTestData() {
Session s = openSession();
Expand Down Expand Up @@ -123,24 +122,6 @@ public void deleteTestData() {
s.close();
}

private String getMapping(String indexName, String typeName) {
ServiceManager serviceManager = getExtendedSearchIntegrator().getServiceManager();
try (ServiceReference<JestClient> clientReference = serviceManager.requestReference( JestClient.class ) ) {
JestClient client = clientReference.get();
JestResult result = client.executeRequest(
new GetMapping.Builder()
.addIndex( indexName )
.addType( typeName )
.build()
);

return result.getJsonObject()
.get( indexName ).getAsJsonObject()
.get( "mappings" ).getAsJsonObject()
.get( typeName ).toString();
}
}

@Test
public void testMapping() throws Exception {
Session s = openSession();
Expand Down Expand Up @@ -247,7 +228,7 @@ public void testMapping() throws Exception {
"}" +
"}" +
"}",
getMapping( "golfplayer", GolfPlayer.class.getName() )
elasticsearchClient.index( "golfplayer" ).mapping( GolfPlayer.class.getName() ).get()
);

// Check we send correctly formatted data when indexing
Expand Down
Expand Up @@ -59,8 +59,9 @@ protected void init(Class<?>... annotatedClasses) {

@Test
public void nothingToDo() throws Exception {
elasticSearchClient.deleteAndCreateIndex( SimpleDateEntity.class );
elasticSearchClient.putMapping( SimpleDateEntity.class,
elasticSearchClient.index( SimpleDateEntity.class )
.deleteAndCreate()
.mapping( SimpleDateEntity.class ).put(
"{"
+ "'dynamic': 'strict',"
+ "'properties': {"
Expand All @@ -81,9 +82,9 @@ public void nothingToDo() throws Exception {
+ "}"
+ "}"
);
elasticSearchClient.deleteAndCreateIndex( SimpleBooleanEntity.class );
elasticSearchClient.putMapping(
SimpleBooleanEntity.class,
elasticSearchClient.index( SimpleBooleanEntity.class )
.deleteAndCreate()
.mapping( SimpleBooleanEntity.class ).put(
"{"
+ "'dynamic': 'strict',"
+ "'properties': {"
Expand Down Expand Up @@ -126,7 +127,7 @@ public void nothingToDo() throws Exception {
+ "}"
+ "}"
+ "}",
elasticSearchClient.getMapping( SimpleDateEntity.class )
elasticSearchClient.mapping( SimpleDateEntity.class ).get()
);
assertJsonEquals(
"{"
Expand All @@ -145,13 +146,13 @@ public void nothingToDo() throws Exception {
+ "}"
+ "}"
+ "}",
elasticSearchClient.getMapping( SimpleBooleanEntity.class )
elasticSearchClient.mapping( SimpleBooleanEntity.class ).get()
);
}

@Test
public void mapping_missing() throws Exception {
elasticSearchClient.deleteAndCreateIndex( SimpleBooleanEntity.class );
elasticSearchClient.index( SimpleBooleanEntity.class ).deleteAndCreate();

init( SimpleBooleanEntity.class );

Expand All @@ -169,15 +170,15 @@ public void mapping_missing() throws Exception {
+ "}"
+ "}"
+ "}",
elasticSearchClient.getMapping( SimpleBooleanEntity.class )
elasticSearchClient.mapping( SimpleBooleanEntity.class ).get()
);
}

@Test
public void rootMapping_attribute_missing() throws Exception {
elasticSearchClient.deleteAndCreateIndex( SimpleBooleanEntity.class );
elasticSearchClient.putMapping(
SimpleBooleanEntity.class,
elasticSearchClient.index( SimpleBooleanEntity.class )
.deleteAndCreate()
.mapping(SimpleBooleanEntity.class).put(
"{"
+ "'properties': {"
+ "'id': {"
Expand Down Expand Up @@ -216,15 +217,15 @@ public void rootMapping_attribute_missing() throws Exception {
+ "}"
+ "}"
+ "}",
elasticSearchClient.getMapping( SimpleBooleanEntity.class )
elasticSearchClient.mapping( SimpleBooleanEntity.class ).get()
);
}

@Test
public void property_missing() throws Exception {
elasticSearchClient.deleteAndCreateIndex( SimpleDateEntity.class );
elasticSearchClient.putMapping(
SimpleDateEntity.class,
elasticSearchClient.index( SimpleDateEntity.class )
.deleteAndCreate()
.mapping( SimpleDateEntity.class ).put(
"{"
+ "'dynamic': 'strict',"
+ "'properties': {"
Expand Down Expand Up @@ -262,15 +263,15 @@ public void property_missing() throws Exception {
+ "}"
+ "}"
+ "}",
elasticSearchClient.getMapping( SimpleDateEntity.class )
elasticSearchClient.mapping( SimpleDateEntity.class ).get()
);
}

@Test
public void property_attribute_invalid() throws Exception {
elasticSearchClient.deleteAndCreateIndex( SimpleDateEntity.class );
elasticSearchClient.putMapping(
SimpleDateEntity.class,
elasticSearchClient.index( SimpleDateEntity.class )
.deleteAndCreate()
.mapping( SimpleDateEntity.class ).put(
"{"
+ "'dynamic': 'strict',"
+ "'properties': {"
Expand Down
Expand Up @@ -106,7 +106,7 @@ private void testDetectConflictDuringSchemaGeneration(Class<?> entityClass) {
.build()
);

elasticSearchClient.registerForCleanup( entityClass );
elasticSearchClient.index( entityClass ).registerForCleanup();
init( strategy, entityClass );
}

Expand All @@ -128,7 +128,7 @@ private void testDetectConflictDuringIndexing(Class<?> entityClass) throws Excep
+ " and thus prevent indexing. No point running this test.",
generatesSchema( strategy ) );

elasticSearchClient.deleteAndCreateIndex( entityClass );
elasticSearchClient.index( entityClass ).deleteAndCreate();
init( strategy, entityClass );

thrown.expect(
Expand Down Expand Up @@ -167,7 +167,7 @@ public void detectIndexedEmbeddedPrefixBypass_schemaGeneration() {
.build()
);

elasticSearchClient.registerForCleanup( entityClass );
elasticSearchClient.index( entityClass ).registerForCleanup();
init( strategy, entityClass );
}

Expand All @@ -181,7 +181,7 @@ public void detectIndexedEmbeddedPrefixBypass_indexing() throws Exception {

Class<?> entityClass = BypassingIndexedEmbeddedPrefixEntity.class;

elasticSearchClient.deleteAndCreateIndex( entityClass );
elasticSearchClient.index( entityClass ).deleteAndCreate();
init( strategy, entityClass );

thrown.expect(
Expand Down

0 comments on commit 32565f5

Please sign in to comment.