Search in sources :

Example 1 with SearchSession

use of org.hibernate.search.mapper.javabean.session.SearchSession in project hibernate-search by hibernate.

the class SearchTimeoutIT method failAfter.

@Test
public void failAfter() {
    try (SearchSession session = mapping.createSession()) {
        SearchQuery<EntityReference> query = session.search(IndexedEntity.class).selectEntityReference().where(f -> f.matchAll()).failAfter(5L, TimeUnit.SECONDS).toQuery();
        SearchException timeoutException = new SearchException("Timed out");
        backendMock.expectSearchReferences(Collections.singletonList(INDEX_NAME), // timeout is supposed to be set on the backend
        b -> b.failAfter(5L, TimeUnit.SECONDS), StubSearchWorkBehavior.failing(() -> timeoutException));
        // Just check that the exception is propagated
        assertThatThrownBy(() -> query.fetchAll()).isSameAs(timeoutException);
    }
}
Also used : EntityReference(org.hibernate.search.mapper.javabean.common.EntityReference) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) SearchException(org.hibernate.search.util.common.SearchException) Test(org.junit.Test)

Example 2 with SearchSession

use of org.hibernate.search.mapper.javabean.session.SearchSession in project hibernate-search by hibernate.

the class PojoIndexingPlanBaseIT method nullEntity_state.

/**
 * Test the state inside indexing plans when the entity is null and must be loaded.
 */
@Test
public void nullEntity_state() {
    List<Integer> idsToLoad = new ArrayList<>();
    List<IndexedEntity> loadedEntities = new ArrayList<>();
    try (SearchSession session = mapping.createSession()) {
        IndexedEntity entity;
        BackendMock.DocumentWorkCallListContext expectations = backendMock.expectWorks(IndexedEntity.INDEX);
        // add then add
        entity = new IndexedEntity(1);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.add("1", b -> b.field("value", "val1"));
        // add then delete
        entity = new IndexedEntity(2);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        // No work expected
        // add then update
        entity = new IndexedEntity(3);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.add("3", b -> b.field("value", "val3"));
        // add then update then delete
        entity = new IndexedEntity(4);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        // No work expected
        // update then update
        entity = new IndexedEntity(5);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.addOrUpdate("5", b -> b.field("value", "val5"));
        // update then delete
        entity = new IndexedEntity(6);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        expectations.delete("6");
        // update then delete then add
        entity = new IndexedEntity(7);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.addOrUpdate("7", b -> b.field("value", "val7"));
        // delete then delete
        entity = new IndexedEntity(8);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        expectations.delete("8");
        // delete then add
        entity = new IndexedEntity(9);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.addOrUpdate("9", b -> b.field("value", "val9"));
        // delete then add then update
        entity = new IndexedEntity(10);
        session.indexingPlan().delete(IndexedEntity.class, entity.id, null);
        session.indexingPlan().add(IndexedEntity.class, entity.id, null);
        session.indexingPlan().addOrUpdate(IndexedEntity.class, entity.id, null);
        idsToLoad.add(entity.id);
        loadedEntities.add(entity);
        expectations.addOrUpdate("10", b -> b.field("value", "val10"));
        when(loaderMock.load(idsToLoad, null)).thenReturn(loadedEntities);
    }
    verify(loaderMock).load(any(), any());
}
Also used : BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) ArrayList(java.util.ArrayList) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test)

Example 3 with SearchSession

use of org.hibernate.search.mapper.javabean.session.SearchSession in project hibernate-search by hibernate.

the class AbstractPojoIndexerDeleteNullEntityIT method runtimeException.

@Test
public void runtimeException() {
    CompletableFuture<?> futureFromBackend = new CompletableFuture<>();
    RuntimeException exception = new RuntimeException();
    try (SearchSession session = createSession()) {
        SearchIndexer indexer = session.indexer();
        expectOperation(futureFromBackend, 1, null, "1");
        CompletionStage<?> returnedFuture = scenario().execute(indexer, 1);
        backendMock.verifyExpectationsMet();
        assertThatFuture(returnedFuture).isPending();
        futureFromBackend.completeExceptionally(exception);
        assertThatFuture(returnedFuture).isFailed(exception);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) SearchIndexer(org.hibernate.search.mapper.javabean.work.SearchIndexer) Test(org.junit.Test)

Example 4 with SearchSession

use of org.hibernate.search.mapper.javabean.session.SearchSession in project hibernate-search by hibernate.

the class AbstractPojoIndexerOperationBaseIT method runtimeException.

@Test
public void runtimeException() {
    CompletableFuture<?> futureFromBackend = new CompletableFuture<>();
    RuntimeException exception = new RuntimeException();
    try (SearchSession session = createSession()) {
        SearchIndexer indexer = session.indexer();
        expectOperation(futureFromBackend, 1, null, "1");
        CompletionStage<?> returnedFuture = scenario().execute(indexer, null, IndexedEntity.of(1));
        backendMock.verifyExpectationsMet();
        assertThatFuture(returnedFuture).isPending();
        futureFromBackend.completeExceptionally(exception);
        assertThatFuture(returnedFuture).isFailed(exception);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) SearchIndexer(org.hibernate.search.mapper.javabean.work.SearchIndexer) Test(org.junit.Test)

Example 5 with SearchSession

use of org.hibernate.search.mapper.javabean.session.SearchSession in project hibernate-search by hibernate.

the class AbstractPojoIndexerOperationBaseIT method previouslyIndexedWithMultipleRoutes.

@Test
@TestForIssue(jiraKey = "HSEARCH-3108")
public void previouslyIndexedWithMultipleRoutes() {
    assumeImplicitRoutingEnabled();
    CompletableFuture<?> futureFromBackend = new CompletableFuture<>();
    try (SearchSession session = createSession()) {
        SearchIndexer indexer = session.indexer();
        MyRoutingBridge.previousValues = Arrays.asList("1", "foo", "3");
        expectOperation(futureFromBackend, worksBefore -> {
            if (!isAdd()) {
                // For operations other than add, expect a delete for every previous route distinct from the current one.
                backendMock.expectWorks(IndexedEntity.INDEX, commitStrategy, refreshStrategy).delete(b -> addWorkInfo(b, tenantId, "1", MyRoutingBridge.toRoutingKey(tenantId, 1, "foo")));
                backendMock.expectWorks(IndexedEntity.INDEX, commitStrategy, refreshStrategy).delete(b -> addWorkInfo(b, tenantId, "1", MyRoutingBridge.toRoutingKey(tenantId, 1, "3")));
            }
        }, // And only then, expect the actual operation.
        1, null, "1");
        CompletionStage<?> returnedFuture = scenario().execute(indexer, null, IndexedEntity.of(1));
        backendMock.verifyExpectationsMet();
        assertThatFuture(returnedFuture).isPending();
        futureFromBackend.complete(null);
        assertThatFuture(returnedFuture).isSuccessful();
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) SearchIndexer(org.hibernate.search.mapper.javabean.work.SearchIndexer) Test(org.junit.Test) TestForIssue(org.hibernate.search.util.impl.test.annotation.TestForIssue)

Aggregations

SearchSession (org.hibernate.search.mapper.javabean.session.SearchSession)180 Test (org.junit.Test)177 SearchMapping (org.hibernate.search.mapper.javabean.mapping.SearchMapping)92 BackendMock (org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock)85 MethodHandles (java.lang.invoke.MethodHandles)77 JavaBeanMappingSetupHelper (org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper)77 TestForIssue (org.hibernate.search.util.impl.test.annotation.TestForIssue)77 Rule (org.junit.Rule)77 SearchException (org.hibernate.search.util.common.SearchException)75 Indexed (org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed)73 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)62 DocumentId (org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId)61 Collections (java.util.Collections)42 FailureReportUtils (org.hibernate.search.util.impl.integrationtest.common.reporting.FailureReportUtils)41 GenericField (org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField)40 CompletableFuture (java.util.concurrent.CompletableFuture)36 List (java.util.List)33 SearchIndexingPlan (org.hibernate.search.mapper.javabean.work.SearchIndexingPlan)33 TypeMappingStep (org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep)29 Before (org.junit.Before)29