Skip to content

Commit

Permalink
HSEARCH-4574 Allow asserting the root projection in the stub backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed May 31, 2023
1 parent 15f3f47 commit 5bdfcba
Show file tree
Hide file tree
Showing 27 changed files with 562 additions and 98 deletions.
Expand Up @@ -17,6 +17,8 @@
import java.lang.invoke.MethodHandles;
import java.util.Collections;

import org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep;
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory;
import org.hibernate.search.util.impl.integrationtest.mapper.pojo.standalone.StandalonePojoMappingSetupHelper;
import org.hibernate.search.mapper.pojo.standalone.mapping.SearchMapping;
import org.hibernate.search.mapper.pojo.standalone.session.SearchSession;
Expand Down Expand Up @@ -58,6 +60,10 @@ public class CustomConstructorMappingAnnotationBaseIT {
@Rule
public StaticCounters counters = new StaticCounters();

protected final ProjectionFinalStep<?> dummyProjectionForEnclosingClassInstance(SearchProjectionFactory<?, ?> f) {
return f.constant( null );
}

/**
* Basic test checking that a simple constructor mapping will be applied as expected.
*/
Expand Down Expand Up @@ -89,6 +95,15 @@ public MyProjection(String text) {
try ( SearchSession session = mapping.createSession() ) {
backendMock.expectSearchProjection(
INDEX_NAME,
b -> {
SearchProjectionFactory<?, ?> f = mapping.scope( IndexedEntity.class ).projection();
b.projection( f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class )
)
.asList() );
},
StubSearchWorkBehavior.of(
2,
Collections.singletonList( "hit1Text" ),
Expand Down
Expand Up @@ -9,7 +9,10 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import java.util.function.Function;

import org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep;
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory;
import org.hibernate.search.mapper.pojo.standalone.mapping.SearchMapping;
import org.hibernate.search.mapper.pojo.standalone.session.SearchSession;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
Expand All @@ -24,7 +27,11 @@ public abstract class AbstractProjectionConstructorIT {
@Rule
public BackendMock backendMock = new BackendMock();

protected final <P> void testSuccessfulRootProjection(SearchMapping mapping, Class<?> indexedType, Class<P> projectionType,
protected final ProjectionFinalStep<?> dummyProjectionForEnclosingClassInstance(SearchProjectionFactory<?, ?> f) {
return f.constant( null );
}

protected final <P> void testSuccessfulRootProjectionExecutionOnly(SearchMapping mapping, Class<?> indexedType, Class<P> projectionType,
List<?> rawProjectionResults, List<P> expectedProjectionResults) {
try ( SearchSession session = mapping.createSession() ) {
backendMock.expectSearchProjection(
Expand All @@ -44,4 +51,28 @@ protected final <P> void testSuccessfulRootProjection(SearchMapping mapping, Cla
}
backendMock.verifyExpectationsMet();
}

protected final <P> void testSuccessfulRootProjection(SearchMapping mapping, Class<?> indexedType, Class<P> projectionType,
List<?> rawProjectionResults,
Function<SearchProjectionFactory<?, ?>, ProjectionFinalStep<?>> expectedProjection,
List<P> expectedProjectionResults) {
try ( SearchSession session = mapping.createSession() ) {
backendMock.expectSearchProjection(
INDEX_NAME,
b -> b.projection( expectedProjection.apply( mapping.scope( indexedType ).projection() ) ),
StubSearchWorkBehavior.of(
rawProjectionResults.size(),
rawProjectionResults
)
);

assertThat( session.search( indexedType )
.select( projectionType )
.where( f -> f.matchAll() )
.fetchAllHits() )
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf( expectedProjectionResults );
}
backendMock.verifyExpectationsMet();
}
}
Expand Up @@ -65,7 +65,7 @@ public MyProjection(String text, Integer integer) {
.withAnnotatedTypes( MyProjection.class )
.setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjection.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down Expand Up @@ -150,7 +150,7 @@ public MyProjection(String text, Integer integer, String somethingElse) {
.withAnnotatedTypes( MyProjection.class )
.setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjection.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down Expand Up @@ -226,7 +226,7 @@ public IndexedEntity(String text, Integer integer) {
backendMock.expectAnySchema( INDEX_NAME );
SearchMapping mapping = setupHelper.start().setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, IndexedEntity.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down Expand Up @@ -271,7 +271,7 @@ public MyProjection(String text, Integer integer) {
.withAnnotatedTypes( MyProjection.class )
.setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjection.class,
Arrays.asList(
Arrays.asList(),
Expand Down Expand Up @@ -424,7 +424,7 @@ public MyProjectionSubclass(String text, Integer integer, String somethingElse)
MyNonProjection.class.getName() );
}

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjectionSubclass.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down Expand Up @@ -484,7 +484,7 @@ public MyNonProjectionSubclass(String text, Integer integer, String somethingEls
.withAnnotatedTypes( MyProjection.class, MyNonProjectionSubclass.class )
.setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjection.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down Expand Up @@ -553,7 +553,7 @@ public MyProjectionSubclass(String text, Integer integer, String somethingElse)
.withAnnotatedTypes( MyProjection.class, MyProjectionSubclass.class )
.setup( IndexedEntity.class );

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjection.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand All @@ -567,7 +567,7 @@ public MyProjectionSubclass(String text, Integer integer, String somethingElse)
)
);

testSuccessfulRootProjection(
testSuccessfulRootProjectionExecutionOnly(
mapping, IndexedEntity.class, MyProjectionSubclass.class,
Arrays.asList(
Arrays.asList( "result1", 1 ),
Expand Down
Expand Up @@ -68,6 +68,13 @@ public MyProjection(String text, Integer integer) {
Arrays.asList( "result1", 11 ),
Arrays.asList( "result2", 21 )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.field( "integer", Integer.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 11 ),
new MyProjection( "result2", 21 )
Expand Down Expand Up @@ -109,6 +116,13 @@ public MyProjection(List<String> text, List<Integer> integer) {
Arrays.asList( Collections.emptyList(), Collections.emptyList() ),
Arrays.asList( Arrays.asList( "result4_1" ), Arrays.asList( 41 ) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ).multi(),
f.field( "integer", Integer.class ).multi()
)
.asList(),
Arrays.asList(
new MyProjection( Arrays.asList( "result1_1", "result1_2" ), Arrays.asList( 11, 12 ) ),
new MyProjection( Arrays.asList( "result2_1" ), Arrays.asList( 21 ) ),
Expand Down Expand Up @@ -152,6 +166,13 @@ public MyProjection(Collection<String> text, Collection<Integer> integer) {
Arrays.asList( Collections.emptyList(), Collections.emptyList() ),
Arrays.asList( Arrays.asList( "result4_1" ), Arrays.asList( 41 ) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ).multi(),
f.field( "integer", Integer.class ).multi()
)
.asList(),
Arrays.asList(
new MyProjection( Arrays.asList( "result1_1", "result1_2" ), Arrays.asList( 11, 12 ) ),
new MyProjection( Arrays.asList( "result2_1" ), Arrays.asList( 21 ) ),
Expand Down Expand Up @@ -195,6 +216,13 @@ public MyProjection(Iterable<String> text, Iterable<Integer> integer) {
Arrays.asList( Collections.emptyList(), Collections.emptyList() ),
Arrays.asList( Arrays.asList( "result4_1" ), Arrays.asList( 41 ) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ).multi(),
f.field( "integer", Integer.class ).multi()
)
.asList(),
Arrays.asList(
new MyProjection( Arrays.asList( "result1_1", "result1_2" ), Arrays.asList( 11, 12 ) ),
new MyProjection( Arrays.asList( "result2_1" ), Arrays.asList( 21 ) ),
Expand Down Expand Up @@ -290,6 +318,19 @@ public MyProjection(String text, MyInnerProjection contained) {
Arrays.asList( "result2", Arrays.asList( null, null ) ),
Arrays.asList( "result3", null )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.object( "contained" )
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "contained.text", String.class ),
f.field( "contained.integer", Integer.class )
)
.asList()
)
.asList(),
Arrays.asList(
new MyProjection( "result1", new MyInnerProjection( "result1_1", 11 ) ),
new MyProjection( "result2", new MyInnerProjection( null, null ) ),
Expand Down Expand Up @@ -352,6 +393,19 @@ public MyProjection(String text, MyInnerProjection contained) {
Arrays.asList( "result2", Arrays.asList( null, null ) ),
Arrays.asList( "result3", null )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.object( "contained" )
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "contained.text", String.class ),
f.field( "contained.integer", Integer.class )
)
.asList()
)
.asList(),
Arrays.asList(
new MyProjection( "result1", new MyInnerProjection( "result1_1", 11 ) ),
new MyProjection( "result2", new MyInnerProjection( null, null ) ),
Expand Down Expand Up @@ -418,6 +472,20 @@ public MyProjection(String text, List<MyInnerProjection> contained) {
Arrays.asList( "result4_1", 41 )
) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.object( "contained" )
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "contained.text", String.class ),
f.field( "contained.integer", Integer.class )
)
.asList()
.multi()
)
.asList(),
Arrays.asList(
new MyProjection( "result1", Arrays.asList(
new MyInnerProjection( "result1_1", 11 ),
Expand Down Expand Up @@ -492,6 +560,20 @@ public MyProjection(String text, Collection<MyInnerProjection> contained) {
Arrays.asList( "result4_1", 41 )
) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.object( "contained" )
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "contained.text", String.class ),
f.field( "contained.integer", Integer.class )
)
.asList()
.multi()
)
.asList(),
Arrays.asList(
new MyProjection( "result1", Arrays.asList(
new MyInnerProjection( "result1_1", 11 ),
Expand Down Expand Up @@ -566,6 +648,20 @@ public MyProjection(String text, Iterable<MyInnerProjection> contained) {
Arrays.asList( "result4_1", 41 )
) )
),
f -> f.composite()
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "text", String.class ),
f.object( "contained" )
.from(
dummyProjectionForEnclosingClassInstance( f ),
f.field( "contained.text", String.class ),
f.field( "contained.integer", Integer.class )
)
.asList()
.multi()
)
.asList(),
Arrays.asList(
new MyProjection( "result1", Arrays.asList(
new MyInnerProjection( "result1_1", 11 ),
Expand Down
Expand Up @@ -63,6 +63,12 @@ record MyProjection(String text, Integer integer) { }
Arrays.asList( "result2", 2 ),
Arrays.asList( "result3", 3 )
),
f -> f.composite()
.from(
f.field( "text", String.class ),
f.field( "integer", Integer.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 1 ),
new MyProjection( "result2", 2 ),
Expand Down Expand Up @@ -103,6 +109,12 @@ public MyProjection(String text, Integer integer, String somethingElse) {
Arrays.asList( "result2", 2 ),
Arrays.asList( "result3", 3 )
),
f -> f.composite()
.from(
f.field( "text", String.class ),
f.field( "integer", Integer.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 1 ),
new MyProjection( "result2", 2 ),
Expand Down
Expand Up @@ -51,6 +51,12 @@ record MyProjection(String text, Integer integer) { }
Arrays.asList( "result2", 2 ),
Arrays.asList( "result3", 3 )
),
f -> f.composite()
.from(
f.field( "text", String.class ),
f.field( "integer", Integer.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 1 ),
new MyProjection( "result2", 2 ),
Expand Down Expand Up @@ -92,6 +98,12 @@ public MyProjection(String text, Integer integer, String somethingElse) {
Arrays.asList( "result2", 2 ),
Arrays.asList( "result3", 3 )
),
f -> f.composite()
.from(
f.field( "text", String.class ),
f.field( "integer", Integer.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 1 ),
new MyProjection( "result2", 2 ),
Expand Down Expand Up @@ -134,6 +146,13 @@ public MyProjection(String text, Integer integer, String otherText) {
Arrays.asList( "result2", 2, "otherText2" ),
Arrays.asList( "result3", 3, "otherText3" )
),
f -> f.composite()
.from(
f.field( "text", String.class ),
f.field( "integer", Integer.class ),
f.field( "otherText", String.class )
)
.asList(),
Arrays.asList(
new MyProjection( "result1", 1, "otherText1" ),
new MyProjection( "result2", 2, "otherText2" ),
Expand Down

0 comments on commit 5bdfcba

Please sign in to comment.