Skip to content

Commit

Permalink
HSEARCH-4132 Test outbox synthetic entity mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever authored and yrodiere committed Mar 8, 2021
1 parent 01ecd96 commit f7371db
Showing 1 changed file with 136 additions and 0 deletions.
@@ -0,0 +1,136 @@
/*
* 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.integrationtest.mapper.orm.outbox;

import static org.assertj.core.api.Assertions.assertThat;
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 java.util.HashMap;
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.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.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;

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

public class OutboxSyntheticEntityMappingIT {

private static final String INDEX_NAME = "IndexedEntity";

@Rule
public BackendMock backendMock = new BackendMock();

@Rule
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );

private SessionFactory sessionFactory;

@Before
public void setup() {
backendMock.expectSchema(
INDEX_NAME, b -> b.field( "text", String.class, f -> f.analyzerName( AnalyzerNames.DEFAULT ) ) );

sessionFactory = ormSetupHelper.start()
.withProperty(
HibernateOrmMapperSettings.MAPPING_CONFIGURER,
(HibernateOrmSearchMappingConfigurer) context -> {
ProgrammaticMappingConfigurationContext mapping = context.programmaticMapping();
TypeMappingStep indexedEntityMapping = mapping.type( IndexedEntity.class );
indexedEntityMapping.indexed().index( INDEX_NAME );
indexedEntityMapping.property( "id" ).documentId();
indexedEntityMapping.property( "text" ).fullTextField();
}
)
.withProperty( HibernateOrmMapperSettings.FILL_OUTBOX_TABLE, true )
.setup( IndexedEntity.class );
backendMock.verifyExpectationsMet();
}

@Test
public void index_workingAsUsual() {
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 );

@SuppressWarnings("unchecked") // this field is defined as integer
Integer generatedId = (Integer) entityData.get( "id" );

id.set( generatedId );
} );

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();

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" ) );
} );
}

@Entity
public static class IndexedEntity {

@Id
private Integer id;
private String text;

private IndexedEntity() {
}

public IndexedEntity(Integer id, String text) {
this.id = id;
this.text = text;
}

public Integer getId() {
return id;
}

public String getText() {
return text;
}
}
}

0 comments on commit f7371db

Please sign in to comment.