Skip to content

Commit

Permalink
HSEARCH-2624 Add test for multi-tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
mincong-h authored and Sanne committed Oct 25, 2017
1 parent 3389e14 commit d80089b
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 12 deletions.
23 changes: 19 additions & 4 deletions jsr352/core/pom.xml
Expand Up @@ -8,15 +8,15 @@
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-jsr352-parent</artifactId>
<version>5.9.0-SNAPSHOT</version>
</parent>

<artifactId>hibernate-search-jsr352-core</artifactId>

<name>Hibernate Search JSR-352</name>
<description>Core of the Hibernate Search JSR-352 integration</description>

Expand All @@ -39,11 +39,26 @@
</dependency>

<!-- test -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand All @@ -69,7 +84,7 @@
<artifactId>byteman-bmunit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.ibm.jbatch</groupId>
<artifactId>com.ibm.jbatch-runtime</artifactId>
Expand All @@ -79,7 +94,7 @@
<!--
JBatch requires a database in order to work, and it seems it uses SQL that won't work with H2.
Anyway, it uses an embedded Derby instance by default, so we just put the Derby driver in the classpath
so it won't complain.
so it won't complain.
-->
<dependency>
<groupId>org.apache.derby</groupId>
Expand Down
@@ -0,0 +1,121 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.jsr352.massindexing;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;
import javax.persistence.EntityManagerFactory;

import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.jsr352.massindexing.test.entity.Company;
import org.hibernate.search.jsr352.test.util.JobFactory;
import org.hibernate.search.jsr352.test.util.JobTestUtil;
import org.hibernate.search.test.SearchTestBase;

import org.junit.Before;
import org.junit.Test;

import org.fest.util.Collections;

import static org.fest.assertions.Assertions.assertThat;
import static org.hibernate.search.jsr352.test.util.JobTestUtil.findIndexedResultsInTenant;

/**
* @author Mincong Huang
*/
public class MassIndexingJobWithMultiTenancyIT extends SearchTestBase {

private static final String TARGET_TENANT_ID = "targetTenant";

private static final String UNUSED_TENANT_ID = "unusedTenant";

private static final int JOB_TIMEOUT_MS = 10_000;

private JobOperator jobOperator = JobFactory.getJobOperator();

private final List<Company> companies = Arrays.asList(
new Company( "Google" ),
new Company( "Red Hat" ),
new Company( "Microsoft" )
);

@Override
@Before
public void setUp() throws Exception {
super.setUp();
persist( TARGET_TENANT_ID, companies );
purgeAll( TARGET_TENANT_ID, Company.class );
}

private <T> void persist(String tenantId, List<T> entities) {
try (Session session = openSessionWithTenantId( tenantId )) {
session.getTransaction().begin();
entities.forEach( session::persist );
session.getTransaction().commit();
}
}

private void purgeAll(String tenantId, Class<?> entityType) throws IOException {
FullTextSession session = Search.getFullTextSession( openSessionWithTenantId( tenantId ) );
session.purgeAll( entityType );
session.flushToIndexes();
session.close();
}

@Test
public void shouldHandleTenantIds() throws Exception {
long executionId = jobOperator.start(
MassIndexingJob.NAME,
MassIndexingJob.parameters()
.forEntity( Company.class )
.tenantId( TARGET_TENANT_ID )
.build()
);

JobExecution jobExecution = jobOperator.getJobExecution( executionId );
JobTestUtil.waitForTermination( jobOperator, jobExecution, JOB_TIMEOUT_MS );
assertThat( jobExecution.getBatchStatus() ).isEqualTo( BatchStatus.COMPLETED );

EntityManagerFactory emf = getSessionFactory().unwrap( EntityManagerFactory.class );

assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Google", TARGET_TENANT_ID ) ).hasSize( 1 );
assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Red Hat", TARGET_TENANT_ID ) ).hasSize( 1 );
assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Microsoft", TARGET_TENANT_ID ) ).hasSize( 1 );

assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Google", UNUSED_TENANT_ID ) ).isEmpty();
assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Red Hat", UNUSED_TENANT_ID ) ).isEmpty();
assertThat( findIndexedResultsInTenant( emf, Company.class, "name", "Microsoft", UNUSED_TENANT_ID ) ).isEmpty();
}

private Session openSessionWithTenantId(String tenantId) {
return getSessionFactory().withOptions().tenantIdentifier( tenantId ).openSession();
}

@Override
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Company.class };
}

@Override
public Set<String> multiTenantIds() {
return Collections.set( TARGET_TENANT_ID, UNUSED_TENANT_ID );
}

@Override
public void configure(Map<String, Object> cfg) {
cfg.put( "hibernate.search.indexing_strategy", "manual" );
}

}
Expand Up @@ -10,11 +10,12 @@
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.jsr352.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;

Expand Down Expand Up @@ -55,15 +56,27 @@ public static JobExecution waitForTermination(JobOperator jobOperator, JobExecut
}

public static <T> List<T> findIndexedResults(EntityManagerFactory emf, Class<T> clazz, String key, String value) {
EntityManager em = emf.createEntityManager();
FullTextEntityManager ftem = Search.getFullTextEntityManager( em );
Query luceneQuery = ftem.getSearchFactory().buildQueryBuilder()
SessionFactory sessionFactory = emf.unwrap( SessionFactory.class );
try (Session session = sessionFactory.openSession()) {
return find( session, clazz, key, value );
}
}

public static <T> List<T> findIndexedResultsInTenant(EntityManagerFactory emf, Class<T> clazz, String key, String value, String tenantId) {
SessionFactory sessionFactory = emf.unwrap( SessionFactory.class );
try (Session session = sessionFactory.withOptions().tenantIdentifier( tenantId ).openSession()) {
return find( session, clazz, key, value );
}
}

private static <T> List<T> find(Session session, Class<T> clazz, String key, String value) {
FullTextSession fts = Search.getFullTextSession( session );
Query luceneQuery = fts.getSearchFactory().buildQueryBuilder()
.forEntity( clazz ).get()
.keyword().onField( key ).matching( value )
.createQuery();
@SuppressWarnings("unchecked")
List<T> result = ftem.createFullTextQuery( luceneQuery ).getResultList();
em.close();
List<T> result = fts.createFullTextQuery( luceneQuery ).getResultList();
return result;
}

Expand Down
7 changes: 7 additions & 0 deletions jsr352/pom.xml
Expand Up @@ -72,6 +72,13 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down

0 comments on commit d80089b

Please sign in to comment.