Skip to content

Commit

Permalink
HSEARCH-4132 Test outbox table indexing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Mar 8, 2021
1 parent 2a7048a commit 0dd0d07
Showing 1 changed file with 24 additions and 36 deletions.
Expand Up @@ -10,22 +10,22 @@
import static org.hibernate.search.mapper.orm.outbox.impl.OutboxAdditionalJaxbMappingProducer.ENTITY_ID_PROPERTY_NAME;
import static org.hibernate.search.mapper.orm.outbox.impl.OutboxAdditionalJaxbMappingProducer.ENTITY_NAME_PROPERTY_NAME;
import static org.hibernate.search.mapper.orm.outbox.impl.OutboxAdditionalJaxbMappingProducer.OUTBOX_ENTITY_NAME;
import static org.hibernate.search.mapper.orm.outbox.impl.OutboxAdditionalJaxbMappingProducer.ROUTING_KEY_PROPERTY_NAME;
import static org.hibernate.search.mapper.orm.outbox.impl.OutboxAdditionalJaxbMappingProducer.ROUTE_PROPERTY_NAME;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.metamodel.EntityType;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.hibernate.search.engine.backend.analysis.AnalyzerNames;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer;
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext;
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep;
import org.hibernate.search.util.common.serializzation.spi.SerializationUtils;
import org.hibernate.search.mapper.pojo.route.DocumentRoutesDescriptor;
import org.hibernate.search.util.common.serialization.spi.SerializationUtils;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils;
Expand All @@ -34,7 +34,7 @@
import org.junit.Rule;
import org.junit.Test;

public class OutboxSyntheticEntityMappingIT {
public class OutboxTableAutomaticIndexingStrategyIT {

private static final String INDEX_NAME = "IndexedEntity";

Expand Down Expand Up @@ -63,54 +63,42 @@ public void setup() {
}
)
.withProperty( HibernateOrmMapperSettings.FILL_OUTBOX_TABLE, true )
.withProperty(
"hibernate.search.automatic_indexing.strategy",
"org.hibernate.search.mapper.orm.outbox.impl.OutboxTableAutomaticIndexingStrategy"
)
.setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}

@Test
public void index_workingAsUsual() {
public void test() {
OrmUtils.withinTransaction( sessionFactory, session -> {
IndexedEntity indexedPojo = new IndexedEntity( 1, "Using some text here" );
session.save( indexedPojo );

backendMock.expectWorks( IndexedEntity.class.getSimpleName() ).add( "1", b -> b
.field( "text", "Using some text here" )
).createdThenExecuted();
} );
}

@Test
public void saveAndLoadOutboxSyntheticEntity() {
EntityType<?> entityType = sessionFactory.getMetamodel().getEntities().iterator().next();
String entityName = entityType.getName();
AtomicInteger id = new AtomicInteger();

OrmUtils.withinTransaction( sessionFactory, session -> {
HashMap<String, Object> entityData = new HashMap<>();
entityData.put( ENTITY_NAME_PROPERTY_NAME, entityName );
entityData.put( ENTITY_ID_PROPERTY_NAME, "739" );
entityData.put( ROUTING_KEY_PROPERTY_NAME, SerializationUtils.serialize( "fake-routing-key" ) );

session.save( OUTBOX_ENTITY_NAME, entityData );
Query<Map> query = session.createQuery( "select e from " + OUTBOX_ENTITY_NAME + " e", Map.class );

@SuppressWarnings("unchecked") // this field is defined as integer
Integer generatedId = (Integer) entityData.get( "id" );
List<Map> list = query.list();
assertThat( list ).hasSize( 1 );

id.set( generatedId );
} );
Map<String, Object> load = list.get( 0 );
assertThat( load ).containsEntry( ENTITY_NAME_PROPERTY_NAME, "IndexedEntity" );
assertThat( load ).containsEntry( ENTITY_ID_PROPERTY_NAME, "1" );

OrmUtils.withinTransaction( sessionFactory, session -> {
@SuppressWarnings("unchecked") // synthetic entities are loaded as map
Map<String, Object> load = (Map<String, Object>) session.load( OUTBOX_ENTITY_NAME, id.get() );
assertThat( load ).isNotNull();
byte[] serializedRoutingKeys = (byte[]) load.get( ROUTE_PROPERTY_NAME );
DocumentRoutesDescriptor routesDescriptor = SerializationUtils.deserialize(
DocumentRoutesDescriptor.class, serializedRoutingKeys );

assertThat( load ).containsEntry( ENTITY_NAME_PROPERTY_NAME, entityName );
assertThat( load ).containsEntry( ENTITY_ID_PROPERTY_NAME, "739" );
assertThat( load ).containsEntry( ROUTING_KEY_PROPERTY_NAME, SerializationUtils.serialize( "fake-routing-key" ) );
assertThat( routesDescriptor ).isNotNull();
assertThat( routesDescriptor.currentRoute().routingKey() ).isNull();
assertThat( routesDescriptor.previousRoutes() ).isEmpty();
} );
}

@Entity
@Entity(name = "IndexedEntity")
public static class IndexedEntity {

@Id
Expand Down

0 comments on commit 0dd0d07

Please sign in to comment.