Skip to content

Commit

Permalink
HSEARCH-2393 Fixed IndexingActionInterceptorTest for Elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Nov 3, 2016
1 parent 78f29cf commit f4c43cd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
1 change: 0 additions & 1 deletion elasticsearch/pom.xml
Expand Up @@ -234,7 +234,6 @@

<!-- HSEARCH-2393 Remove depencies to Lucene in tests for features common to Lucene and Elasticsearch -->
<exclude>**/ProgrammaticMappingTest.java</exclude><!-- Analyzers -->
<exclude>**/IndexingActionInterceptorTest.java</exclude><!-- Queries on the object's class -->
<exclude>**/ToStringTest.java</exclude><!-- Relies on the lucene query's toString(), should be the HSQuery's getQueryString? -->
<exclude>**/DynamicBoostingTest.java</exclude><!-- Projection constant __HSearch_Explanation -->
<exclude>**/IndexAndQueryNullTest.java</exclude><!-- Projection constant __HSearch_Document -->
Expand Down
14 changes: 14 additions & 0 deletions orm/src/test/java/org/hibernate/search/test/interceptor/Blog.java
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.search.test.interceptor;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
Expand All @@ -29,6 +31,18 @@ public void setId(Integer id) {
this.id = id;
}

@Override
public boolean equals(Object obj) {
return getClass().equals( obj.getClass() )
&& id != null
&& Objects.equals( id, ((Blog) obj).id );
}

@Override
public int hashCode() {
return getClass().hashCode();
}

private Integer id;

@Field
Expand Down
Expand Up @@ -12,14 +12,12 @@

import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Query;
import org.hibernate.Transaction;
import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.engine.ProjectionConstants;
import org.hibernate.search.test.SearchTestBase;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -64,17 +62,18 @@ public void tearDown() throws Exception {
public void testBlogAndArticleAreNotIndexedInDraftStatus() throws Exception {
Transaction tx = fullTextSession.beginTransaction();

assertThat( getBlogEntriesFor( Blog.class ) ).as( "Blog is explicit intercepted" ).hasSize( 0 );
assertThat( getBlogEntriesFor( Article.class ) ).as( "Article is inherently intercepted" ).hasSize( 0 );
List<Blog> blogEntries = getBlogEntries();
assertThat( blogEntries ).as( "Blog is explicitly intercepted" ).excludes( blog );
assertThat( blogEntries ).as( "Article is inherently intercepted" ).excludes( article );

tx.commit();
}

@Test
public void testTotalArticleIsIndexedInDraftStatus() throws Exception {
Transaction tx = fullTextSession.beginTransaction();
assertThat( getBlogEntriesFor( TotalArticle.class ) ).as( "TotalArticle is explicit not intercepted" )
.hasSize( 1 );
List<Blog> blogEntries = getBlogEntries();
assertThat( blogEntries ).as( "TotalArticle is explicitly not intercepted" ).contains( totalArticle );
tx.commit();
}

Expand All @@ -84,10 +83,10 @@ public void testBlogAndArticleAreIndexedInPublishedStatus() throws Exception {
setAllBlogEntriesToStatus( BlogStatus.PUBLISHED );
Transaction tx = fullTextSession.beginTransaction();

assertThat( getBlogEntriesFor( Blog.class ) ).hasSize( 1 );
assertThat( getBlogEntriesFor( Article.class ) ).as( "Article is inherently intercepted" ).hasSize( 1 );
assertThat( getBlogEntriesFor( TotalArticle.class ) ).as( "TotalArticle is explicit not intercepted" )
.hasSize( 1 );
List<Blog> blogEntries = getBlogEntries();
assertThat( blogEntries ).contains( blog );
assertThat( blogEntries ).as( "Article is inherently intercepted" ).contains( article );
assertThat( blogEntries ).as( "TotalArticle is explicitly not intercepted" ).contains( totalArticle );

tx.commit();
}
Expand All @@ -98,8 +97,9 @@ public void testBlogAndArticleAreNotIndexedInRemovedStatus() throws Exception {
setAllBlogEntriesToStatus( BlogStatus.REMOVED );
Transaction tx = fullTextSession.beginTransaction();

assertThat( getBlogEntriesFor( Blog.class ) ).hasSize( 0 );
assertThat( getBlogEntriesFor( Article.class ) ).as( "Article is inherently intercepted" ).hasSize( 0 );
List<Blog> blogEntries = getBlogEntries();
assertThat( blogEntries ).excludes( blog );
assertThat( blogEntries ).as( "Article is inherently intercepted" ).excludes( article );

tx.commit();
}
Expand All @@ -109,8 +109,8 @@ public void testTotalArticleIsIndexedInRemovedStatus() throws Exception {
setAllBlogEntriesToStatus( BlogStatus.REMOVED );
Transaction tx = fullTextSession.beginTransaction();

assertThat( getBlogEntriesFor( TotalArticle.class ) ).as( "TotalArticle is explicit not intercepted" )
.hasSize( 1 );
List<Blog> blogEntries = getBlogEntries();
assertThat( blogEntries ).as( "TotalArticle is explicitly not intercepted" ).contains( totalArticle );

tx.commit();
}
Expand All @@ -120,9 +120,9 @@ public void testTotalArticleIsIndexedInRemovedStatus() throws Exception {
public void testInterceptorWithMassIndexer() throws Exception {
setAllBlogEntriesToStatus( BlogStatus.PUBLISHED );

List<Blog> allEntries = fullTextSession.createFullTextQuery( new MatchAllDocsQuery() ).list();
assertEquals( "Wrong total number of entries", 3, allEntries.size() );
for ( Blog blog : allEntries ) {
List<Blog> blogEntries = getBlogEntries();
assertEquals( "Wrong total number of entries", 3, blogEntries.size() );
for ( Blog blog : blogEntries ) {
assertTrue( blog.getStatus().equals( BlogStatus.PUBLISHED ) );
}

Expand All @@ -134,8 +134,8 @@ public void testInterceptorWithMassIndexer() throws Exception {

tx.commit();

allEntries = fullTextSession.createFullTextQuery( new MatchAllDocsQuery() ).list();
assertEquals( "Wrong total number of entries. Index should be empty after purge.", 0, allEntries.size() );
blogEntries = fullTextSession.createFullTextQuery( new MatchAllDocsQuery() ).list();
assertEquals( "Wrong total number of entries. Index should be empty after purge.", 0, blogEntries.size() );

tx = fullTextSession.beginTransaction();
fullTextSession.createIndexer()
Expand All @@ -146,8 +146,8 @@ public void testInterceptorWithMassIndexer() throws Exception {
.startAndWait();
tx.commit();

allEntries = fullTextSession.createFullTextQuery( new MatchAllDocsQuery() ).list();
assertEquals( "Wrong total number of entries.", 3, allEntries.size() );
blogEntries = getBlogEntries();
assertEquals( "Wrong total number of entries.", 3, blogEntries.size() );
}

private void createPersistAndIndexTestData() {
Expand Down Expand Up @@ -190,9 +190,10 @@ private void setAllBlogEntriesToStatus(BlogStatus status) {
fullTextSession.clear();
}

private List getBlogEntriesFor(Class<?> blogType) {
TermQuery query = new TermQuery( new Term( ProjectionConstants.OBJECT_CLASS, blogType.getName() ) );
return fullTextSession.createFullTextQuery( query ).list();
@SuppressWarnings("unchecked")
private List<Blog> getBlogEntries() {
Query query = new MatchAllDocsQuery();
return fullTextSession.createFullTextQuery( query, Blog.class ).list();
}

@Override
Expand Down

0 comments on commit f4c43cd

Please sign in to comment.